File size: 16,683 Bytes
17db41a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 | // Copyright (c) Meta Platforms, Inc. and affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#pragma once
#ifndef DMLC_LOGGING_H_
#define DMLC_LOGGING_H_
#include <cstdio>
#include <cstdlib>
#include <memory>
#include <stdexcept>
#include <string>
#include <vector>
#if DMLC_LOG_STACK_TRACE
#include <cxxabi.h>
#include <sstream>
#include DMLC_EXECINFO_H
#endif
/*! \brief whether use glog for logging */
#ifndef DMLC_USE_GLOG
#define DMLC_USE_GLOG 0
#endif
/*
* The preprocessor definition DMLC_USE_LOGGING_LIBRARY determines whether to
* use a user-defined logging library. If defined, dmlc will not define the
* macros CHECK() and LOG() and instead locate CHECK() and LOG() from the value
* of DMLC_USE_LOGGING_LIBRARY. The DMLC_USE_LOGGING_LIBRARY macro shall be of
* form <my_logging.h>:
*
* #define DMLC_USE_LOGGING_LIBRARY <my_logging.h>
*
* Make sure to define CHECK() and LOG() macros in the provided header;
* otherwise the build will fail.
*/
/*!
* \brief whether throw dmlc::Error instead of
* directly calling abort when FATAL error occured
* NOTE: this may still not be perfect.
* do not use FATAL and CHECK in destructors
*/
#ifndef DMLC_LOG_FATAL_THROW
#define DMLC_LOG_FATAL_THROW 1
#endif
/*!
* \brief whether always log a message before throw
* This can help identify the error that cannot be catched.
*/
#ifndef DMLC_LOG_BEFORE_THROW
#define DMLC_LOG_BEFORE_THROW 0
#endif
/*!
* \brief Whether to use customized logger,
* whose output can be decided by other libraries.
*/
#ifndef DMLC_LOG_CUSTOMIZE
#define DMLC_LOG_CUSTOMIZE 0
#endif
/*!
* \brief Whether to enable debug logging feature.
*/
#ifndef DMLC_LOG_DEBUG
#ifdef NDEBUG
#define DMLC_LOG_DEBUG 0
#else
#define DMLC_LOG_DEBUG 1
#endif
#endif
/*!
* \brief Whether to disable date message on the log.
*/
#ifndef DMLC_LOG_NODATE
#define DMLC_LOG_NODATE 0
#endif
/*! \brief helper macro to generate string concat */
#define DMLC_STR_CONCAT_(__x, __y) __x##__y
#define DMLC_STR_CONCAT(__x, __y) DMLC_STR_CONCAT_(__x, __y)
#define DMLC_THROW_EXCEPTION noexcept(false)
#define DMLC_NO_EXCEPTION noexcept(true)
#if defined(__GNUC__) || defined(__clang__)
#define DMLC_ALWAYS_INLINE inline __attribute__((__always_inline__))
#elif defined(_MSC_VER)
#define DMLC_ALWAYS_INLINE __forceinline
#else
#define DMLC_ALWAYS_INLINE inline
#endif
#if defined(_MSC_VER)
#define DMLC_NO_INLINE __declspec(noinline)
#else
#define DMLC_NO_INLINE __attribute__((noinline))
#endif
namespace dmlc {
/*!
* \brief exception class that will be thrown by
* default logger if DMLC_LOG_FATAL_THROW == 1
*/
struct Error : public std::runtime_error {
/*!
* \brief constructor
* \param s the error message
*/
explicit Error(const std::string& s) : std::runtime_error(s) {}
};
#if DMLC_LOG_STACK_TRACE
// get stack trace logging depth from env variable.
inline size_t LogStackTraceLevel() {
size_t level;
if (auto var = std::getenv("DMLC_LOG_STACK_TRACE_DEPTH")) {
if (1 == sscanf(var, "%zu", &level)) {
return level + 1;
}
}
return DMLC_LOG_STACK_TRACE_SIZE;
}
inline std::string Demangle(char const* msg_str) {
using std::string;
string msg(msg_str);
size_t symbol_start = string::npos;
size_t symbol_end = string::npos;
if (((symbol_start = msg.find("_Z")) != string::npos) &&
(symbol_end = msg.find_first_of(" +", symbol_start))) {
string left_of_symbol(msg, 0, symbol_start);
string symbol(msg, symbol_start, symbol_end - symbol_start);
string right_of_symbol(msg, symbol_end);
int status = 0;
size_t length = string::npos;
std::unique_ptr<char, void (*)(void* __ptr)> demangled_symbol = {
abi::__cxa_demangle(symbol.c_str(), 0, &length, &status), &std::free};
if (demangled_symbol && status == 0 && length > 0) {
string symbol_str(demangled_symbol.get());
std::ostringstream os;
os << left_of_symbol << symbol_str << right_of_symbol;
return os.str();
}
}
return string(msg_str);
}
// By default skip the first frame because
// that belongs to ~LogMessageFatal
inline std::string StackTrace(
size_t start_frame = 1,
const size_t stack_size = DMLC_LOG_STACK_TRACE_SIZE) {
using std::string;
std::ostringstream stacktrace_os;
std::vector<void*> stack(stack_size);
int nframes = backtrace(stack.data(), static_cast<int>(stack_size));
if (start_frame < static_cast<size_t>(nframes)) {
stacktrace_os << "Stack trace:\n";
}
char** msgs = backtrace_symbols(stack.data(), nframes);
if (msgs != nullptr) {
for (int frameno = start_frame; frameno < nframes; ++frameno) {
string msg = dmlc::Demangle(msgs[frameno]);
stacktrace_os << " [bt] (" << frameno - start_frame << ") " << msg
<< "\n";
}
}
free(msgs);
string stack_trace = stacktrace_os.str();
return stack_trace;
}
#else // DMLC_LOG_STACK_TRACE is off
inline size_t LogStackTraceLevel() {
return 0;
}
inline std::string demangle(char const* msg_str) {
return std::string();
}
inline std::string StackTrace(
size_t start_frame = 1,
const size_t stack_size = 0) {
return std::string(
"Stack trace not available when "
"DMLC_LOG_STACK_TRACE is disabled at compile time.");
}
#endif // DMLC_LOG_STACK_TRACE
} // namespace dmlc
#if DMLC_USE_GLOG
#include <glog/logging.h>
namespace dmlc {
/*!
* \brief optionally redirect to google's init log
* \param argv0 The arguments.
*/
inline void InitLogging(const char* argv0) {
google::InitGoogleLogging(argv0);
}
} // namespace dmlc
#elif defined DMLC_USE_LOGGING_LIBRARY
#include DMLC_USE_LOGGING_LIBRARY
namespace dmlc {
inline void InitLogging(const char*) {
// DO NOTHING
}
} // namespace dmlc
#else
// use a light version of glog
#include <assert.h>
#include <ctime>
#include <iostream>
#include <sstream>
#if defined(_MSC_VER)
#pragma warning(disable : 4722)
#pragma warning(disable : 4068)
#endif
namespace dmlc {
inline void InitLogging(const char*) {
// DO NOTHING
}
// get debug option from env variable.
inline bool DebugLoggingEnabled() {
static int state = 0;
if (state == 0) {
if (auto var = std::getenv("DMLC_LOG_DEBUG")) {
if (std::string(var) == "1") {
state = 1;
} else {
state = -1;
}
} else {
// by default hide debug logging.
state = -1;
}
}
return state == 1;
}
#ifndef DMLC_GLOG_DEFINED
template <typename X, typename Y>
std::unique_ptr<std::string> LogCheckFormat(const X& x, const Y& y) {
std::ostringstream os;
os << " (" << x << " vs. " << y
<< ") "; /* CHECK_XX(x, y) requires x and y can be serialized to string.
Use CHECK(x OP y) otherwise. NOLINT(*) */
// no std::make_unique until c++14
return std::unique_ptr<std::string>(new std::string(os.str()));
}
// This function allows us to ignore sign comparison in the right scope.
#define DEFINE_CHECK_FUNC(name, op) \
template <typename X, typename Y> \
DMLC_ALWAYS_INLINE std::unique_ptr<std::string> LogCheck##name( \
const X& x, const Y& y) { \
if (x op y) \
return nullptr; \
return LogCheckFormat(x, y); \
} \
DMLC_ALWAYS_INLINE std::unique_ptr<std::string> LogCheck##name( \
int x, int y) { \
return LogCheck##name<int, int>(x, y); \
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-compare"
DEFINE_CHECK_FUNC(_LT, <)
DEFINE_CHECK_FUNC(_GT, >)
DEFINE_CHECK_FUNC(_LE, <=)
DEFINE_CHECK_FUNC(_GE, >=)
DEFINE_CHECK_FUNC(_EQ, ==)
DEFINE_CHECK_FUNC(_NE, !=)
#pragma GCC diagnostic pop
#define CHECK_BINARY_OP(name, op, x, y) \
if (auto __dmlc__log__err = dmlc::LogCheck##name(x, y)) \
dmlc::LogMessageFatal(__FILE__, __LINE__).stream() \
<< "Check failed: " << #x " " #op " " #y << *__dmlc__log__err << ": "
// Always-on checking
#define CHECK(x) \
if (!(x)) \
dmlc::LogMessageFatal(__FILE__, __LINE__).stream() \
<< "Check failed: " #x << ": "
#define CHECK_LT(x, y) CHECK_BINARY_OP(_LT, <, x, y)
#define CHECK_GT(x, y) CHECK_BINARY_OP(_GT, >, x, y)
#define CHECK_LE(x, y) CHECK_BINARY_OP(_LE, <=, x, y)
#define CHECK_GE(x, y) CHECK_BINARY_OP(_GE, >=, x, y)
#define CHECK_EQ(x, y) CHECK_BINARY_OP(_EQ, ==, x, y)
#define CHECK_NE(x, y) CHECK_BINARY_OP(_NE, !=, x, y)
#define CHECK_NOTNULL(x) \
((x) == NULL ? dmlc::LogMessageFatal(__FILE__, __LINE__).stream() \
<< "Check notnull: " #x << ' ', \
(x) \
: (x)) // NOLINT(*)
// Debug-only checking.
#if DMLC_LOG_DEBUG
#define DCHECK(x) \
while (false) \
CHECK(x)
#define DCHECK_LT(x, y) \
while (false) \
CHECK((x) < (y))
#define DCHECK_GT(x, y) \
while (false) \
CHECK((x) > (y))
#define DCHECK_LE(x, y) \
while (false) \
CHECK((x) <= (y))
#define DCHECK_GE(x, y) \
while (false) \
CHECK((x) >= (y))
#define DCHECK_EQ(x, y) \
while (false) \
CHECK((x) == (y))
#define DCHECK_NE(x, y) \
while (false) \
CHECK((x) != (y))
#else
#define DCHECK(x) CHECK(x)
#define DCHECK_LT(x, y) CHECK((x) < (y))
#define DCHECK_GT(x, y) CHECK((x) > (y))
#define DCHECK_LE(x, y) CHECK((x) <= (y))
#define DCHECK_GE(x, y) CHECK((x) >= (y))
#define DCHECK_EQ(x, y) CHECK((x) == (y))
#define DCHECK_NE(x, y) CHECK((x) != (y))
#endif // DMLC_LOG_DEBUG
#if DMLC_LOG_CUSTOMIZE
#define LOG_INFO dmlc::CustomLogMessage(__FILE__, __LINE__)
#else
#define LOG_INFO dmlc::LogMessage(__FILE__, __LINE__)
#endif
#define LOG_ERROR LOG_INFO
#define LOG_WARNING LOG_INFO
#define LOG_FATAL dmlc::LogMessageFatal(__FILE__, __LINE__)
#define LOG_QFATAL LOG_FATAL
// Poor man version of VLOG
#define VLOG(x) LOG_INFO.stream()
#define LOG(severity) LOG_##severity.stream()
#define LG LOG_INFO.stream()
#define LOG_IF(severity, condition) \
!(condition) ? (void)0 : dmlc::LogMessageVoidify() & LOG(severity)
#if DMLC_LOG_DEBUG
#define LOG_DFATAL LOG_FATAL
#define DFATAL FATAL
#define DLOG(severity) LOG_IF(severity, ::dmlc::DebugLoggingEnabled())
#define DLOG_IF(severity, condition) \
LOG_IF(severity, ::dmlc::DebugLoggingEnabled() && (condition))
#else
#define LOG_DFATAL LOG_ERROR
#define DFATAL ERROR
#define DLOG(severity) \
true ? (void)0 : dmlc::LogMessageVoidify() & LOG(severity)
#define DLOG_IF(severity, condition) \
(true || !(condition)) ? (void)0 : dmlc::LogMessageVoidify() & LOG(severity)
#endif
// Poor man version of LOG_EVERY_N
#define LOG_EVERY_N(severity, n) LOG(severity)
#endif // DMLC_GLOG_DEFINED
class DateLogger {
public:
DateLogger() {
#if defined(_MSC_VER)
_tzset();
#endif
}
const char* HumanDate() {
#if !defined(_LIBCPP_SGX_CONFIG) && DMLC_LOG_NODATE == 0
#if defined(_MSC_VER)
_strtime_s(buffer_, sizeof(buffer_));
#else
time_t time_value = time(NULL);
struct tm* pnow;
#if !defined(_WIN32)
struct tm now;
pnow = localtime_r(&time_value, &now);
#else
pnow = localtime(&time_value); // NOLINT(*)
#endif
snprintf(
buffer_,
sizeof(buffer_),
"%02d:%02d:%02d",
pnow->tm_hour,
pnow->tm_min,
pnow->tm_sec);
#endif
return buffer_;
#else
return "";
#endif // _LIBCPP_SGX_CONFIG
}
private:
char buffer_[9];
};
#ifndef _LIBCPP_SGX_NO_IOSTREAMS
class LogMessage {
public:
LogMessage(const char* file, int line)
:
#ifdef __ANDROID__
log_stream_(std::cout)
#else
log_stream_(std::cerr)
#endif
{
log_stream_ << "[" << pretty_date_.HumanDate() << "] " << file << ":"
<< line << ": ";
}
~LogMessage() {
log_stream_ << '\n';
}
std::ostream& stream() {
return log_stream_;
}
protected:
std::ostream& log_stream_;
private:
DateLogger pretty_date_;
LogMessage(const LogMessage&);
void operator=(const LogMessage&);
};
// customized logger that can allow user to define where to log the message.
class CustomLogMessage {
public:
CustomLogMessage(const char* file, int line) {
log_stream_ << "[" << DateLogger().HumanDate() << "] " << file << ":"
<< line << ": ";
}
~CustomLogMessage() {
Log(log_stream_.str());
}
std::ostream& stream() {
return log_stream_;
}
/*!
* \brief customized logging of the message.
* This function won't be implemented by libdmlc
* \param msg The message to be logged.
*/
static void Log(const std::string& msg);
private:
std::ostringstream log_stream_;
};
#else
class DummyOStream {
public:
template <typename T>
DummyOStream& operator<<(T _) {
return *this;
}
inline std::string str() {
return "";
}
};
class LogMessage {
public:
LogMessage(const char* file, int line) : log_stream_() {}
DummyOStream& stream() {
return log_stream_;
}
protected:
DummyOStream log_stream_;
private:
LogMessage(const LogMessage&);
void operator=(const LogMessage&);
};
#endif
#if defined(_LIBCPP_SGX_NO_IOSTREAMS)
class LogMessageFatal : public LogMessage {
public:
LogMessageFatal(const char* file, int line) : LogMessage(file, line) {}
~LogMessageFatal() {
abort();
}
private:
LogMessageFatal(const LogMessageFatal&);
void operator=(const LogMessageFatal&);
};
#elif DMLC_LOG_FATAL_THROW == 0
class LogMessageFatal : public LogMessage {
public:
LogMessageFatal(const char* file, int line) : LogMessage(file, line) {}
~LogMessageFatal() {
log_stream_ << "\n" << StackTrace(1, LogStackTraceLevel()) << "\n";
abort();
}
private:
LogMessageFatal(const LogMessageFatal&);
void operator=(const LogMessageFatal&);
};
#else
class LogMessageFatal {
public:
LogMessageFatal(const char* file, int line) {
GetEntry().Init(file, line);
}
std::ostringstream& stream() {
return GetEntry().log_stream;
}
DMLC_NO_INLINE ~LogMessageFatal() DMLC_THROW_EXCEPTION {
#if DMLC_LOG_STACK_TRACE
GetEntry().log_stream << "\n"
<< StackTrace(1, LogStackTraceLevel()) << "\n";
#endif
throw GetEntry().Finalize();
}
private:
struct Entry {
std::ostringstream log_stream;
DMLC_NO_INLINE void Init(const char* file, int line) {
DateLogger date;
log_stream.str("");
log_stream.clear();
log_stream << "[" << date.HumanDate() << "] " << file << ":" << line
<< ": ";
}
dmlc::Error Finalize() {
#if DMLC_LOG_BEFORE_THROW
LOG(ERROR) << log_stream.str();
#endif
return dmlc::Error(log_stream.str());
}
// Due to a bug in MinGW, objects with non-trivial destructor cannot be
// thread-local. See https://sourceforge.net/p/mingw-w64/bugs/527/ Hence,
// don't use thread-local for the log stream if the compiler is MinGW.
#if !(defined(__MINGW32__) || defined(__MINGW64__))
DMLC_NO_INLINE static Entry& ThreadLocal() {
static thread_local Entry result;
return result;
}
#endif
};
LogMessageFatal(const LogMessageFatal&);
void operator=(const LogMessageFatal&);
#if defined(__MINGW32__) || defined(__MINGW64__)
DMLC_NO_INLINE Entry& GetEntry() {
return entry_;
}
Entry entry_;
#else
DMLC_NO_INLINE Entry& GetEntry() {
return Entry::ThreadLocal();
}
#endif
};
#endif
// This class is used to explicitly ignore values in the conditional
// logging macros. This avoids compiler warnings like "value computed
// is not used" and "statement has no effect".
class LogMessageVoidify {
public:
LogMessageVoidify() {}
// This has to be an operator with a precedence lower than << but
// higher than "?:". See its usage.
#if !defined(_LIBCPP_SGX_NO_IOSTREAMS)
void operator&(std::ostream&) {}
#endif
};
} // namespace dmlc
#endif
#endif // DMLC_LOGGING_H_
|