| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#ifndef DMLC_LOGGING_H_ |
| 9 |
#define DMLC_LOGGING_H_ |
| 10 |
#include <cstdio> |
| 11 |
#include <cstdlib> |
| 12 |
#include <memory> |
| 13 |
#include <stdexcept> |
| 14 |
#include <string> |
| 15 |
#include <vector> |
| 16 |
|
| 17 |
#include "./base.h" |
| 18 |
|
| 19 |
#if DMLC_LOG_STACK_TRACE |
| 20 |
#include <sstream> |
| 21 |
|
| 22 |
#include <cxxabi.h> |
| 23 |
#include DMLC_EXECINFO_H |
| 24 |
#endif |
| 25 |
|
| 26 |
namespace dmlc { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
struct Error : public std::runtime_error { |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
explicit Error(const std::string &s) : std::runtime_error(s) {} |
| 37 |
}; |
| 38 |
|
| 39 |
#if DMLC_LOG_STACK_TRACE |
| 40 |
|
| 41 |
inline size_t LogStackTraceLevel() { |
| 42 |
size_t level; |
| 43 |
if (auto var = std::getenv("DMLC_LOG_STACK_TRACE_DEPTH")) { |
| 44 |
if (1 == sscanf(var, "%zu", &level)) { |
| 45 |
return level + 1; |
| 46 |
} |
| 47 |
} |
| 48 |
return DMLC_LOG_STACK_TRACE_SIZE; |
| 49 |
} |
| 50 |
|
| 51 |
inline std::string Demangle(const char *msg_str) { |
| 52 |
using std::string; |
| 53 |
string msg(msg_str); |
| 54 |
size_t symbol_start = string::npos; |
| 55 |
size_t symbol_end = string::npos; |
| 56 |
if (((symbol_start = msg.find("_Z")) != string::npos) |
| 57 |
&& (symbol_end = msg.find_first_of(" +", symbol_start))) { |
| 58 |
string left_of_symbol(msg, 0, symbol_start); |
| 59 |
string symbol(msg, symbol_start, symbol_end - symbol_start); |
| 60 |
string right_of_symbol(msg, symbol_end); |
| 61 |
|
| 62 |
int status = 0; |
| 63 |
size_t length = string::npos; |
| 64 |
std::unique_ptr<char, void (*)(void *__ptr)> demangled_symbol |
| 65 |
= {abi::__cxa_demangle(symbol.c_str(), 0, &length, &status), &std::free}; |
| 66 |
if (demangled_symbol && status == 0 && length > 0) { |
| 67 |
string symbol_str(demangled_symbol.get()); |
| 68 |
std::ostringstream os; |
| 69 |
os << left_of_symbol << symbol_str << right_of_symbol; |
| 70 |
return os.str(); |
| 71 |
} |
| 72 |
} |
| 73 |
return string(msg_str); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
inline std::string StackTrace( |
| 79 |
size_t start_frame = 1, const size_t stack_size = DMLC_LOG_STACK_TRACE_SIZE) { |
| 80 |
using std::string; |
| 81 |
std::ostringstream stacktrace_os; |
| 82 |
std::vector<void *> stack(stack_size); |
| 83 |
int nframes = backtrace(stack.data(), static_cast<int>(stack_size)); |
| 84 |
if (start_frame < static_cast<size_t>(nframes)) { |
| 85 |
stacktrace_os << "Stack trace:\n"; |
| 86 |
} |
| 87 |
char **msgs = backtrace_symbols(stack.data(), nframes); |
| 88 |
if (msgs != nullptr) { |
| 89 |
for (int frameno = start_frame; frameno < nframes; ++frameno) { |
| 90 |
string msg = dmlc::Demangle(msgs[frameno]); |
| 91 |
stacktrace_os << " [bt] (" << frameno - start_frame << ") " << msg << "\n"; |
| 92 |
} |
| 93 |
} |
| 94 |
free(msgs); |
| 95 |
string stack_trace = stacktrace_os.str(); |
| 96 |
return stack_trace; |
| 97 |
} |
| 98 |
|
| 99 |
#else |
| 100 |
|
| 101 |
inline size_t LogStackTraceLevel() { |
| 102 |
return 0; |
| 103 |
} |
| 104 |
|
| 105 |
inline std::string demangle(const char *msg_str) { |
| 106 |
return std::string(); |
| 107 |
} |
| 108 |
|
| 109 |
inline std::string StackTrace(size_t start_frame = 1, const size_t stack_size = 0) { |
| 110 |
return std::string( |
| 111 |
"Stack trace not available when " |
| 112 |
"DMLC_LOG_STACK_TRACE is disabled at compile time."); |
| 113 |
} |
| 114 |
|
| 115 |
#endif |
| 116 |
} |
| 117 |
|
| 118 |
#if DMLC_USE_GLOG |
| 119 |
#include <glog/logging.h> |
| 120 |
|
| 121 |
namespace dmlc { |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
inline void InitLogging(const char *argv0) { |
| 127 |
google::InitGoogleLogging(argv0); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
#elif defined DMLC_USE_LOGGING_LIBRARY |
| 132 |
|
| 133 |
#include DMLC_USE_LOGGING_LIBRARY |
| 134 |
namespace dmlc { |
| 135 |
inline void InitLogging(const char *) { |
| 136 |
|
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
#else |
| 141 |
|
| 142 |
#include <ctime> |
| 143 |
#include <iostream> |
| 144 |
#include <sstream> |
| 145 |
|
| 146 |
#include <assert.h> |
| 147 |
|
| 148 |
#if defined(_MSC_VER) |
| 149 |
|
| 150 |
|
| 151 |
#endif |
| 152 |
|
| 153 |
namespace dmlc { |
| 154 |
inline void InitLogging(const char *) { |
| 155 |
|
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
inline bool DebugLoggingEnabled() { |
| 160 |
static int state = 0; |
| 161 |
if (state == 0) { |
| 162 |
if (auto var = std::getenv("DMLC_LOG_DEBUG")) { |
| 163 |
if (std::string(var) == "1") { |
| 164 |
state = 1; |
| 165 |
} else { |
| 166 |
state = -1; |
| 167 |
} |
| 168 |
} else { |
| 169 |
|
| 170 |
state = -1; |
| 171 |
} |
| 172 |
} |
| 173 |
return state == 1; |
| 174 |
} |
| 175 |
|
| 176 |
#ifndef DMLC_GLOG_DEFINED |
| 177 |
|
| 178 |
template <typename X, typename Y> |
| 179 |
std::unique_ptr<std::string> LogCheckFormat(const X &x, const Y &y) { |
| 180 |
std::ostringstream os; |
| 181 |
os << " (" << x << " vs. " << y << ") "; |
| 182 |
|
| 183 |
|
| 184 |
return std::unique_ptr<std::string>(new std::string(os.str())); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
#define DEFINE_CHECK_FUNC(name, op) \ |
| 189 |
template <typename X, typename Y> \ |
| 190 |
DMLC_ALWAYS_INLINE std::unique_ptr<std::string> LogCheck##name(const X &x, const Y &y) { \ |
| 191 |
if (x op y) \ |
| 192 |
return nullptr; \ |
| 193 |
return LogCheckFormat(x, y); \ |
| 194 |
} \ |
| 195 |
DMLC_ALWAYS_INLINE std::unique_ptr<std::string> LogCheck##name(int x, int y) { \ |
| 196 |
return LogCheck##name<int, int>(x, y); \ |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
DEFINE_CHECK_FUNC(_LT, <) |
| 202 |
DEFINE_CHECK_FUNC(_GT, >) |
| 203 |
DEFINE_CHECK_FUNC(_LE, <=) |
| 204 |
DEFINE_CHECK_FUNC(_GE, >=) |
| 205 |
DEFINE_CHECK_FUNC(_EQ, ==) |
| 206 |
DEFINE_CHECK_FUNC(_NE, !=) |
| 207 |
|
| 208 |
|
| 209 |
#define CHECK_BINARY_OP(name, op, x, y) \ |
| 210 |
if (auto __dmlc__log__err = dmlc::LogCheck##name(x, y)) \ |
| 211 |
dmlc::LogMessageFatal(__FILE__, __LINE__).stream() \ |
| 212 |
<< "Check failed: " << #x " " #op " " #y << *__dmlc__log__err << ": " |
| 213 |
|
| 214 |
|
| 215 |
#define CHECK(x) \ |
| 216 |
if (!(x)) \ |
| 217 |
dmlc::LogMessageFatal(__FILE__, __LINE__).stream() << "Check failed: " #x << ": " |
| 218 |
#define CHECK_LT(x, y) CHECK_BINARY_OP(_LT, <, x, y) |
| 219 |
#define CHECK_GT(x, y) CHECK_BINARY_OP(_GT, >, x, y) |
| 220 |
#define CHECK_LE(x, y) CHECK_BINARY_OP(_LE, <=, x, y) |
| 221 |
#define CHECK_GE(x, y) CHECK_BINARY_OP(_GE, >=, x, y) |
| 222 |
#define CHECK_EQ(x, y) CHECK_BINARY_OP(_EQ, ==, x, y) |
| 223 |
#define CHECK_NE(x, y) CHECK_BINARY_OP(_NE, !=, x, y) |
| 224 |
#define CHECK_NOTNULL(x) \ |
| 225 |
((x) == NULL \ |
| 226 |
? dmlc::LogMessageFatal(__FILE__, __LINE__).stream() << "Check notnull: " #x << ' ', \ |
| 227 |
(x) : (x)) |
| 228 |
|
| 229 |
|
| 230 |
#if DMLC_LOG_DEBUG |
| 231 |
#define DCHECK(x) \ |
| 232 |
while (false) \ |
| 233 |
CHECK(x) |
| 234 |
#define DCHECK_LT(x, y) \ |
| 235 |
while (false) \ |
| 236 |
CHECK((x) < (y)) |
| 237 |
#define DCHECK_GT(x, y) \ |
| 238 |
while (false) \ |
| 239 |
CHECK((x) > (y)) |
| 240 |
#define DCHECK_LE(x, y) \ |
| 241 |
while (false) \ |
| 242 |
CHECK((x) <= (y)) |
| 243 |
#define DCHECK_GE(x, y) \ |
| 244 |
while (false) \ |
| 245 |
CHECK((x) >= (y)) |
| 246 |
#define DCHECK_EQ(x, y) \ |
| 247 |
while (false) \ |
| 248 |
CHECK((x) == (y)) |
| 249 |
#define DCHECK_NE(x, y) \ |
| 250 |
while (false) \ |
| 251 |
CHECK((x) != (y)) |
| 252 |
#else |
| 253 |
#define DCHECK(x) CHECK(x) |
| 254 |
#define DCHECK_LT(x, y) CHECK((x) < (y)) |
| 255 |
#define DCHECK_GT(x, y) CHECK((x) > (y)) |
| 256 |
#define DCHECK_LE(x, y) CHECK((x) <= (y)) |
| 257 |
#define DCHECK_GE(x, y) CHECK((x) >= (y)) |
| 258 |
#define DCHECK_EQ(x, y) CHECK((x) == (y)) |
| 259 |
#define DCHECK_NE(x, y) CHECK((x) != (y)) |
| 260 |
#endif |
| 261 |
|
| 262 |
#if DMLC_LOG_CUSTOMIZE |
| 263 |
#define LOG_INFO dmlc::CustomLogMessage(__FILE__, __LINE__) |
| 264 |
#else |
| 265 |
#define LOG_INFO dmlc::LogMessage(__FILE__, __LINE__) |
| 266 |
#endif |
| 267 |
#define LOG_ERROR LOG_INFO |
| 268 |
#define LOG_WARNING LOG_INFO |
| 269 |
#define LOG_FATAL dmlc::LogMessageFatal(__FILE__, __LINE__) |
| 270 |
#define LOG_QFATAL LOG_FATAL |
| 271 |
|
| 272 |
|
| 273 |
#define VLOG(x) LOG_INFO.stream() |
| 274 |
|
| 275 |
#define LOG(severity) LOG_##severity.stream() |
| 276 |
#define LG LOG_INFO.stream() |
| 277 |
#define LOG_IF(severity, condition) \ |
| 278 |
!(condition) ? (void)0 : dmlc::LogMessageVoidify() & LOG(severity) |
| 279 |
|
| 280 |
#if DMLC_LOG_DEBUG |
| 281 |
|
| 282 |
#define LOG_DFATAL LOG_FATAL |
| 283 |
#define DFATAL FATAL |
| 284 |
#define DLOG(severity) LOG_IF(severity, ::dmlc::DebugLoggingEnabled()) |
| 285 |
#define DLOG_IF(severity, condition) \ |
| 286 |
LOG_IF(severity, ::dmlc::DebugLoggingEnabled() && (condition)) |
| 287 |
|
| 288 |
#else |
| 289 |
|
| 290 |
#define LOG_DFATAL LOG_ERROR |
| 291 |
#define DFATAL ERROR |
| 292 |
#define DLOG(severity) true ? (void)0 : dmlc::LogMessageVoidify() & LOG(severity) |
| 293 |
#define DLOG_IF(severity, condition) \ |
| 294 |
(true || !(condition)) ? (void)0 : dmlc::LogMessageVoidify() & LOG(severity) |
| 295 |
#endif |
| 296 |
|
| 297 |
|
| 298 |
#define LOG_EVERY_N(severity, n) LOG(severity) |
| 299 |
|
| 300 |
#endif |
| 301 |
|
| 302 |
class DateLogger { |
| 303 |
public: |
| 304 |
DateLogger() { |
| 305 |
#if defined(_MSC_VER) |
| 306 |
_tzset(); |
| 307 |
#endif |
| 308 |
} |
| 309 |
const char *HumanDate() { |
| 310 |
#if !defined(_LIBCPP_SGX_CONFIG) && DMLC_LOG_NODATE == 0 |
| 311 |
#if defined(_MSC_VER) |
| 312 |
_strtime_s(buffer_, sizeof(buffer_)); |
| 313 |
#else |
| 314 |
time_t time_value = time(NULL); |
| 315 |
struct tm *pnow; |
| 316 |
#if !defined(_WIN32) |
| 317 |
struct tm now; |
| 318 |
pnow = localtime_r(&time_value, &now); |
| 319 |
#else |
| 320 |
pnow = localtime(&time_value); |
| 321 |
#endif |
| 322 |
snprintf(buffer_, sizeof(buffer_), "%02d:%02d:%02d", pnow->tm_hour, pnow->tm_min, pnow->tm_sec); |
| 323 |
#endif |
| 324 |
return buffer_; |
| 325 |
#else |
| 326 |
return ""; |
| 327 |
#endif |
| 328 |
} |
| 329 |
|
| 330 |
private: |
| 331 |
char buffer_[9]; |
| 332 |
}; |
| 333 |
|
| 334 |
#ifndef _LIBCPP_SGX_NO_IOSTREAMS |
| 335 |
class LogMessage { |
| 336 |
public: |
| 337 |
LogMessage(const char *file, int line) |
| 338 |
: |
| 339 |
#ifdef __ANDROID__ |
| 340 |
log_stream_(std::cout) |
| 341 |
#else |
| 342 |
log_stream_(std::cerr) |
| 343 |
#endif |
| 344 |
{ |
| 345 |
log_stream_ << "[" << pretty_date_.HumanDate() << "] " << file << ":" << line << ": "; |
| 346 |
} |
| 347 |
~LogMessage() { |
| 348 |
log_stream_ << '\n'; |
| 349 |
} |
| 350 |
std::ostream &stream() { |
| 351 |
return log_stream_; |
| 352 |
} |
| 353 |
|
| 354 |
protected: |
| 355 |
std::ostream &log_stream_; |
| 356 |
|
| 357 |
private: |
| 358 |
DateLogger pretty_date_; |
| 359 |
LogMessage(const LogMessage &); |
| 360 |
void operator=(const LogMessage &); |
| 361 |
}; |
| 362 |
|
| 363 |
|
| 364 |
class CustomLogMessage { |
| 365 |
public: |
| 366 |
CustomLogMessage(const char *file, int line) { |
| 367 |
log_stream_ << "[" << DateLogger().HumanDate() << "] " << file << ":" << line << ": "; |
| 368 |
} |
| 369 |
~CustomLogMessage() { |
| 370 |
Log(log_stream_.str()); |
| 371 |
} |
| 372 |
std::ostream &stream() { |
| 373 |
return log_stream_; |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
static void Log(const std::string &msg); |
| 381 |
|
| 382 |
private: |
| 383 |
std::ostringstream log_stream_; |
| 384 |
}; |
| 385 |
#else |
| 386 |
class DummyOStream { |
| 387 |
public: |
| 388 |
template <typename T> |
| 389 |
DummyOStream &operator<<(T _) { |
| 390 |
return *this; |
| 391 |
} |
| 392 |
inline std::string str() { |
| 393 |
return ""; |
| 394 |
} |
| 395 |
}; |
| 396 |
class LogMessage { |
| 397 |
public: |
| 398 |
LogMessage(const char *file, int line) : log_stream_() {} |
| 399 |
DummyOStream &stream() { |
| 400 |
return log_stream_; |
| 401 |
} |
| 402 |
|
| 403 |
protected: |
| 404 |
DummyOStream log_stream_; |
| 405 |
|
| 406 |
private: |
| 407 |
LogMessage(const LogMessage &); |
| 408 |
void operator=(const LogMessage &); |
| 409 |
}; |
| 410 |
#endif |
| 411 |
|
| 412 |
#if defined(_LIBCPP_SGX_NO_IOSTREAMS) |
| 413 |
class LogMessageFatal : public LogMessage { |
| 414 |
public: |
| 415 |
LogMessageFatal(const char *file, int line) : LogMessage(file, line) {} |
| 416 |
~LogMessageFatal() { |
| 417 |
abort(); |
| 418 |
} |
| 419 |
|
| 420 |
private: |
| 421 |
LogMessageFatal(const LogMessageFatal &); |
| 422 |
void operator=(const LogMessageFatal &); |
| 423 |
}; |
| 424 |
#elif DMLC_LOG_FATAL_THROW == 0 |
| 425 |
class LogMessageFatal : public LogMessage { |
| 426 |
public: |
| 427 |
LogMessageFatal(const char *file, int line) : LogMessage(file, line) {} |
| 428 |
~LogMessageFatal() { |
| 429 |
log_stream_ << "\n" << StackTrace(1, LogStackTraceLevel()) << "\n"; |
| 430 |
abort(); |
| 431 |
} |
| 432 |
|
| 433 |
private: |
| 434 |
LogMessageFatal(const LogMessageFatal &); |
| 435 |
void operator=(const LogMessageFatal &); |
| 436 |
}; |
| 437 |
#else |
| 438 |
class LogMessageFatal { |
| 439 |
public: |
| 440 |
LogMessageFatal(const char *file, int line) { |
| 441 |
GetEntry().Init(file, line); |
| 442 |
} |
| 443 |
std::ostringstream &stream() { |
| 444 |
return GetEntry().log_stream; |
| 445 |
} |
| 446 |
DMLC_NO_INLINE ~LogMessageFatal() DMLC_THROW_EXCEPTION { |
| 447 |
#if DMLC_LOG_STACK_TRACE |
| 448 |
GetEntry().log_stream << "\n" << StackTrace(1, LogStackTraceLevel()) << "\n"; |
| 449 |
#endif |
| 450 |
throw GetEntry().Finalize(); |
| 451 |
} |
| 452 |
|
| 453 |
private: |
| 454 |
struct Entry { |
| 455 |
std::ostringstream log_stream; |
| 456 |
DMLC_NO_INLINE void Init(const char *file, int line) { |
| 457 |
DateLogger date; |
| 458 |
log_stream.str(""); |
| 459 |
log_stream.clear(); |
| 460 |
log_stream << "[" << date.HumanDate() << "] " << file << ":" << line << ": "; |
| 461 |
} |
| 462 |
dmlc::Error Finalize() { |
| 463 |
#if DMLC_LOG_BEFORE_THROW |
| 464 |
LOG(ERROR) << log_stream.str(); |
| 465 |
#endif |
| 466 |
return dmlc::Error(log_stream.str()); |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
#if !(defined(__MINGW32__) || defined(__MINGW64__)) |
| 472 |
DMLC_NO_INLINE static Entry &ThreadLocal() { |
| 473 |
static thread_local Entry result; |
| 474 |
return result; |
| 475 |
} |
| 476 |
#endif |
| 477 |
}; |
| 478 |
LogMessageFatal(const LogMessageFatal &); |
| 479 |
void operator=(const LogMessageFatal &); |
| 480 |
|
| 481 |
#if defined(__MINGW32__) || defined(__MINGW64__) |
| 482 |
DMLC_NO_INLINE Entry &GetEntry() { |
| 483 |
return entry_; |
| 484 |
} |
| 485 |
|
| 486 |
Entry entry_; |
| 487 |
#else |
| 488 |
DMLC_NO_INLINE Entry &GetEntry() { |
| 489 |
return Entry::ThreadLocal(); |
| 490 |
} |
| 491 |
#endif |
| 492 |
}; |
| 493 |
#endif |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
class LogMessageVoidify { |
| 499 |
public: |
| 500 |
LogMessageVoidify() {} |
| 501 |
|
| 502 |
|
| 503 |
#if !defined(_LIBCPP_SGX_NO_IOSTREAMS) |
| 504 |
void operator&(std::ostream &) {} |
| 505 |
#endif |
| 506 |
}; |
| 507 |
|
| 508 |
} |
| 509 |
|
| 510 |
#endif |
| 511 |
#endif |