1 2 #ifndef BASE_LOGGING_H_ 3 #define BASE_LOGGING_H_ 4 5 #include <cassert> 6 #include <string> 7 #include <cstring> 8 #include <sstream> 9 10 #include "quipper/base/macros.h" 11 #include "quipper/base/basictypes.h" 12 13 // 14 // Logging macros designed to mimic those used in chromium_org/base. 15 // 16 17 // Instructions 18 // ------------ 19 // 20 // Make a bunch of macros for logging. The way to log things is to stream 21 // things to LOG(<a particular severity level>). E.g., 22 // 23 // LOG(INFO) << "Found " << num_cookies << " cookies"; 24 // 25 // You can also do conditional logging: 26 // 27 // LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies"; 28 // 29 // The CHECK(condition) macro is active in both debug and release builds and 30 // effectively performs a LOG(FATAL) which terminates the process and 31 // generates a crashdump unless a debugger is attached. 32 // 33 // There are also "debug mode" logging macros like the ones above: 34 // 35 // DLOG(INFO) << "Found cookies"; 36 // 37 // DLOG_IF(INFO, num_cookies > 10) << "Got lots of cookies"; 38 // 39 // All "debug mode" logging is compiled away to nothing for non-debug mode 40 // compiles. LOG_IF and development flags also work well together 41 // because the code can be compiled away sometimes. 42 // 43 // We also have 44 // 45 // LOG_ASSERT(assertion); 46 // DLOG_ASSERT(assertion); 47 // 48 // which is syntactic sugar for {,D}LOG_IF(FATAL, assert fails) << assertion; 49 // 50 // There are "verbose level" logging macros. They look like 51 // 52 // VLOG(1) << "I'm printed when you run the program with --v=1 or more"; 53 // VLOG(2) << "I'm printed when you run the program with --v=2 or more"; 54 // 55 // These always log at the INFO log level (when they log at all). 56 // The verbose logging can also be turned on module-by-module. For instance, 57 // --vmodule=profile=2,icon_loader=1,browser_*=3,*/chromeos/*=4 --v=0 58 // will cause: 59 // a. VLOG(2) and lower messages to be printed from profile.{h,cc} 60 // b. VLOG(1) and lower messages to be printed from icon_loader.{h,cc} 61 // c. VLOG(3) and lower messages to be printed from files prefixed with 62 // "browser" 63 // d. VLOG(4) and lower messages to be printed from files under a 64 // "chromeos" directory. 65 // e. VLOG(0) and lower messages to be printed from elsewhere 66 // 67 // The wildcarding functionality shown by (c) supports both '*' (match 68 // 0 or more characters) and '?' (match any single character) 69 // wildcards. Any pattern containing a forward or backward slash will 70 // be tested against the whole pathname and not just the module. 71 // E.g., "*/foo/bar/*=2" would change the logging level for all code 72 // in source files under a "foo/bar" directory. 73 // 74 // There's also VLOG_IS_ON(n) "verbose level" condition macro. To be used as 75 // 76 // if (VLOG_IS_ON(2)) { 77 // // do some logging preparation and logging 78 // // that can't be accomplished with just VLOG(2) << ...; 79 // } 80 // 81 // There is also a VLOG_IF "verbose level" condition macro for sample 82 // cases, when some extra computation and preparation for logs is not 83 // needed. 84 // 85 // VLOG_IF(1, (size > 1024)) 86 // << "I'm printed when size is more than 1024 and when you run the " 87 // "program with --v=1 or more"; 88 // 89 // We also override the standard 'assert' to use 'DLOG_ASSERT'. 90 // 91 // Lastly, there is: 92 // 93 // PLOG(ERROR) << "Couldn't do foo"; 94 // DPLOG(ERROR) << "Couldn't do foo"; 95 // PLOG_IF(ERROR, cond) << "Couldn't do foo"; 96 // DPLOG_IF(ERROR, cond) << "Couldn't do foo"; 97 // PCHECK(condition) << "Couldn't do foo"; 98 // DPCHECK(condition) << "Couldn't do foo"; 99 // 100 // which append the last system error to the message in string form (taken from 101 // GetLastError() on Windows and errno on POSIX). 102 // 103 // The supported severity levels for macros that allow you to specify one 104 // are (in increasing order of severity) INFO, WARNING, ERROR, and FATAL. 105 // 106 // Very important: logging a message at the FATAL severity level causes 107 // the program to terminate (after the message is logged). 108 // 109 // There is the special severity of DFATAL, which logs FATAL in debug mode, 110 // ERROR in normal mode. 111 112 #define BASE_EXPORT 113 114 namespace logging { 115 116 // Sets the log level. Anything at or above this level will be written to the 117 // log file/displayed to the user (if applicable). Anything below this level 118 // will be silently ignored. The log level defaults to 0 (everything is logged 119 // up to level INFO) if this function is not called. 120 // Note that log messages for VLOG(x) are logged at level -x, so setting 121 // the min log level to negative values enables verbose logging. 122 BASE_EXPORT void SetMinLogLevel(int level); 123 124 // Gets the current log level. 125 BASE_EXPORT int GetMinLogLevel(); 126 127 // Gets the VLOG default verbosity level. 128 BASE_EXPORT int GetVlogVerbosity(); 129 130 typedef int LogSeverity; 131 const LogSeverity LOG_VERBOSE = -1; // This is level 1 verbosity 132 // Note: the log severities are used to index into the array of names, 133 // see log_severity_names. 134 const LogSeverity LOG_INFO = 0; 135 const LogSeverity LOG_WARNING = 1; 136 const LogSeverity LOG_ERROR = 2; 137 const LogSeverity LOG_FATAL = 3; 138 const LogSeverity LOG_NUM_SEVERITIES = 4; 139 140 // A few definitions of macros that don't generate much code. These are used 141 // by LOG() and LOG_IF, etc. Since these are used all over our code, it's 142 // better to have compact code for these operations. 143 #define COMPACT_GOOGLE_LOG_EX_INFO(ClassName, ...) \ 144 logging::ClassName(__FILE__, __LINE__, logging::LOG_INFO , ##__VA_ARGS__) 145 #define COMPACT_GOOGLE_LOG_EX_WARNING(ClassName, ...) \ 146 logging::ClassName(__FILE__, __LINE__, logging::LOG_WARNING , ##__VA_ARGS__) 147 #define COMPACT_GOOGLE_LOG_EX_ERROR(ClassName, ...) \ 148 logging::ClassName(__FILE__, __LINE__, logging::LOG_ERROR , ##__VA_ARGS__) 149 #define COMPACT_GOOGLE_LOG_EX_FATAL(ClassName, ...) \ 150 logging::ClassName(__FILE__, __LINE__, logging::LOG_FATAL , ##__VA_ARGS__) 151 #define COMPACT_GOOGLE_LOG_EX_DFATAL(ClassName, ...) \ 152 logging::ClassName(__FILE__, __LINE__, logging::LOG_DFATAL , ##__VA_ARGS__) 153 154 #define COMPACT_GOOGLE_LOG_INFO \ 155 COMPACT_GOOGLE_LOG_EX_INFO(LogMessage) 156 #define COMPACT_GOOGLE_LOG_WARNING \ 157 COMPACT_GOOGLE_LOG_EX_WARNING(LogMessage) 158 #define COMPACT_GOOGLE_LOG_ERROR \ 159 COMPACT_GOOGLE_LOG_EX_ERROR(LogMessage) 160 #define COMPACT_GOOGLE_LOG_FATAL \ 161 COMPACT_GOOGLE_LOG_EX_FATAL(LogMessage) 162 #define COMPACT_GOOGLE_LOG_DFATAL \ 163 COMPACT_GOOGLE_LOG_EX_DFATAL(LogMessage) 164 165 // As special cases, we can assume that LOG_IS_ON(FATAL) always holds. Also, 166 // LOG_IS_ON(DFATAL) always holds in debug mode. In particular, CHECK()s will 167 // always fire if they fail. 168 #define LOG_IS_ON(severity) \ 169 ((::logging::LOG_ ## severity) >= ::logging::GetMinLogLevel()) 170 171 #define VLOG_IS_ON(verboselevel) false 172 173 // Helper macro which avoids evaluating the arguments to a stream if 174 // the condition doesn't hold. 175 #define LAZY_STREAM(stream, condition) \ 176 !(condition) ? (void) 0 : ::logging::LogMessageVoidify() & (stream) /* NOLINT */ 177 178 // We use the preprocessor's merging operator, "##", so that, e.g., 179 // LOG(INFO) becomes the token COMPACT_GOOGLE_LOG_INFO. There's some funny 180 // subtle difference between ostream member streaming functions (e.g., 181 // ostream::operator<<(int) and ostream non-member streaming functions 182 // (e.g., ::operator<<(ostream&, string&): it turns out that it's 183 // impossible to stream something like a string directly to an unnamed 184 // ostream. We employ a neat hack by calling the stream() member 185 // function of LogMessage which seems to avoid the problem. 186 #define LOG_STREAM(severity) COMPACT_GOOGLE_LOG_ ## severity.stream() 187 188 #define LOG(severity) LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity)) 189 #define LOG_IF(severity, condition) \ 190 LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity) && (condition)) 191 192 // The VLOG macros log with negative verbosities. 193 #define VLOG_STREAM(verbose_level) \ 194 logging::LogMessage(__FILE__, __LINE__, -(verbose_level)).stream() 195 196 #define VLOG(verbose_level) \ 197 LAZY_STREAM(VLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level)) 198 199 #define VLOG_IF(verbose_level, condition) \ 200 LAZY_STREAM(VLOG_STREAM(verbose_level), \ 201 VLOG_IS_ON(verbose_level) && (condition)) 202 203 // TODO(akalin): Add more VLOG variants, e.g. VPLOG. 204 205 #define LOG_ASSERT(condition) \ 206 LOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". " 207 #define SYSLOG_ASSERT(condition) \ 208 SYSLOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". " 209 210 #define PLOG(severity) \ 211 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity)) 212 213 #define PLOG_IF(severity, condition) \ 214 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity) && (condition)) 215 216 // The actual stream used isn't important. 217 #define EAT_STREAM_PARAMETERS \ 218 true ? (void) 0 : ::logging::LogMessageVoidify() & LOG_STREAM(FATAL) /* NOLINT */ 219 220 // CHECK dies with a fatal error if condition is not true. It is *not* 221 // controlled by NDEBUG, so the check will be executed regardless of 222 // compilation mode. 223 // 224 // We make sure CHECK et al. always evaluates their arguments, as 225 // doing CHECK(FunctionWithSideEffect()) is a common idiom. 226 227 #define CHECK(condition) \ 228 LAZY_STREAM(LOG_STREAM(FATAL), !(condition)) \ 229 << "Check failed: " #condition ". " 230 231 #define PCHECK(condition) \ 232 LAZY_STREAM(PLOG_STREAM(FATAL), !(condition)) \ 233 << "Check failed: " #condition ". " 234 235 // Helper macro for binary operators. 236 // Don't use this macro directly in your code, use CHECK_EQ et al below. 237 // 238 // TODO(akalin): Rewrite this so that constructs like if (...) 239 // CHECK_EQ(...) else { ... } work properly. 240 #define CHECK_OP(name, op, val1, val2) \ 241 if (std::string* _result = \ 242 logging::Check##name##Impl((val1), (val2), \ 243 #val1 " " #op " " #val2)) \ 244 logging::LogMessage(__FILE__, __LINE__, _result).stream() 245 246 // Build the error message string. This is separate from the "Impl" 247 // function template because it is not performance critical and so can 248 // be out of line, while the "Impl" code should be inline. Caller 249 // takes ownership of the returned string. 250 template<class t1, class t2> MakeCheckOpString(const t1 & v1,const t2 & v2,const char * names)251 std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) { 252 std::ostringstream ss; 253 ss << names << " (" << v1 << " vs. " << v2 << ")"; 254 std::string* msg = new std::string(ss.str()); 255 return msg; 256 } 257 258 // MSVC doesn't like complex extern templates and DLLs. 259 #if !defined(COMPILER_MSVC) 260 // Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated 261 // in logging.cc. 262 extern template BASE_EXPORT std::string* MakeCheckOpString<int, int>( 263 const int&, const int&, const char* names); 264 extern template BASE_EXPORT 265 std::string* MakeCheckOpString<unsigned long, unsigned long>( 266 const unsigned long&, const unsigned long&, const char* names); 267 extern template BASE_EXPORT 268 std::string* MakeCheckOpString<unsigned long, unsigned int>( 269 const unsigned long&, const unsigned int&, const char* names); 270 extern template BASE_EXPORT 271 std::string* MakeCheckOpString<unsigned int, unsigned long>( 272 const unsigned int&, const unsigned long&, const char* names); 273 extern template BASE_EXPORT 274 std::string* MakeCheckOpString<std::string, std::string>( 275 const std::string&, const std::string&, const char* name); 276 #endif 277 278 // Helper functions for CHECK_OP macro. 279 // The (int, int) specialization works around the issue that the compiler 280 // will not instantiate the template version of the function on values of 281 // unnamed enum type - see comment below. 282 #define DEFINE_CHECK_OP_IMPL(name, op) \ 283 template <class t1, class t2> \ 284 inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \ 285 const char* names) { \ 286 if (v1 op v2) return NULL; \ 287 else return MakeCheckOpString(v1, v2, names); \ 288 } \ 289 inline std::string* Check##name##Impl(int v1, int v2, const char* names) { \ 290 if (v1 op v2) return NULL; \ 291 else return MakeCheckOpString(v1, v2, names); \ 292 } 293 DEFINE_CHECK_OP_IMPL(EQ, ==) 294 DEFINE_CHECK_OP_IMPL(NE, !=) 295 DEFINE_CHECK_OP_IMPL(LE, <=) 296 DEFINE_CHECK_OP_IMPL(LT, < ) 297 DEFINE_CHECK_OP_IMPL(GE, >=) 298 DEFINE_CHECK_OP_IMPL(GT, > ) 299 #undef DEFINE_CHECK_OP_IMPL 300 301 #define CHECK_EQ(val1, val2) CHECK_OP(EQ, ==, val1, val2) 302 #define CHECK_NE(val1, val2) CHECK_OP(NE, !=, val1, val2) 303 #define CHECK_LE(val1, val2) CHECK_OP(LE, <=, val1, val2) 304 #define CHECK_LT(val1, val2) CHECK_OP(LT, < , val1, val2) 305 #define CHECK_GE(val1, val2) CHECK_OP(GE, >=, val1, val2) 306 #define CHECK_GT(val1, val2) CHECK_OP(GT, > , val1, val2) 307 308 #if defined(NDEBUG) 309 #define ENABLE_DLOG 0 310 #else 311 #define ENABLE_DLOG 1 312 #endif 313 314 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) 315 #define DCHECK_IS_ON 0 316 #else 317 #define DCHECK_IS_ON 1 318 #endif 319 320 // Definitions for DLOG et al. 321 322 #if ENABLE_DLOG 323 324 #define DLOG_IS_ON(severity) LOG_IS_ON(severity) 325 #define DLOG_IF(severity, condition) LOG_IF(severity, condition) 326 #define DLOG_ASSERT(condition) LOG_ASSERT(condition) 327 #define DPLOG_IF(severity, condition) PLOG_IF(severity, condition) 328 #define DVLOG_IF(verboselevel, condition) VLOG_IF(verboselevel, condition) 329 #define DVPLOG_IF(verboselevel, condition) VPLOG_IF(verboselevel, condition) 330 331 #else // ENABLE_DLOG 332 333 // If ENABLE_DLOG is off, we want to avoid emitting any references to 334 // |condition| (which may reference a variable defined only if NDEBUG 335 // is not defined). Contrast this with DCHECK et al., which has 336 // different behavior. 337 338 #define DLOG_IS_ON(severity) false 339 #define DLOG_IF(severity, condition) EAT_STREAM_PARAMETERS 340 #define DLOG_ASSERT(condition) EAT_STREAM_PARAMETERS 341 #define DPLOG_IF(severity, condition) EAT_STREAM_PARAMETERS 342 #define DVLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS 343 #define DVPLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS 344 345 #endif // ENABLE_DLOG 346 347 // DEBUG_MODE is for uses like 348 // if (DEBUG_MODE) foo.CheckThatFoo(); 349 // instead of 350 // #ifndef NDEBUG 351 // foo.CheckThatFoo(); 352 // #endif 353 // 354 // We tie its state to ENABLE_DLOG. 355 enum { DEBUG_MODE = ENABLE_DLOG }; 356 357 #undef ENABLE_DLOG 358 359 #define DLOG(severity) \ 360 LAZY_STREAM(LOG_STREAM(severity), DLOG_IS_ON(severity)) 361 362 #define DPLOG(severity) \ 363 LAZY_STREAM(PLOG_STREAM(severity), DLOG_IS_ON(severity)) 364 365 #define DVLOG(verboselevel) DVLOG_IF(verboselevel, VLOG_IS_ON(verboselevel)) 366 367 #define DVPLOG(verboselevel) DVPLOG_IF(verboselevel, VLOG_IS_ON(verboselevel)) 368 369 // Definitions for DCHECK et al. 370 371 #if DCHECK_IS_ON 372 373 #define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \ 374 COMPACT_GOOGLE_LOG_EX_FATAL(ClassName , ##__VA_ARGS__) 375 #define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_FATAL 376 const LogSeverity LOG_DCHECK = LOG_FATAL; 377 378 #else // DCHECK_IS_ON 379 380 // These are just dummy values. 381 #define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \ 382 COMPACT_GOOGLE_LOG_EX_INFO(ClassName , ##__VA_ARGS__) 383 #define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_INFO 384 const LogSeverity LOG_DCHECK = LOG_INFO; 385 386 #endif // DCHECK_IS_ON 387 388 // DCHECK et al. make sure to reference |condition| regardless of 389 // whether DCHECKs are enabled; this is so that we don't get unused 390 // variable warnings if the only use of a variable is in a DCHECK. 391 // This behavior is different from DLOG_IF et al. 392 393 #define DCHECK(condition) \ 394 LAZY_STREAM(LOG_STREAM(DCHECK), DCHECK_IS_ON && !(condition)) \ 395 << "Check failed: " #condition ". " 396 397 #define DPCHECK(condition) \ 398 LAZY_STREAM(PLOG_STREAM(DCHECK), DCHECK_IS_ON && !(condition)) \ 399 << "Check failed: " #condition ". " 400 401 // Helper macro for binary operators. 402 // Don't use this macro directly in your code, use DCHECK_EQ et al below. 403 #define DCHECK_OP(name, op, val1, val2) \ 404 if (DCHECK_IS_ON) \ 405 if (std::string* _result = \ 406 logging::Check##name##Impl((val1), (val2), \ 407 #val1 " " #op " " #val2)) \ 408 logging::LogMessage( \ 409 __FILE__, __LINE__, ::logging::LOG_DCHECK, \ 410 _result).stream() 411 412 // Equality/Inequality checks - compare two values, and log a 413 // LOG_DCHECK message including the two values when the result is not 414 // as expected. The values must have operator<<(ostream, ...) 415 // defined. 416 // 417 // You may append to the error message like so: 418 // DCHECK_NE(1, 2) << ": The world must be ending!"; 419 // 420 // We are very careful to ensure that each argument is evaluated exactly 421 // once, and that anything which is legal to pass as a function argument is 422 // legal here. In particular, the arguments may be temporary expressions 423 // which will end up being destroyed at the end of the apparent statement, 424 // for example: 425 // DCHECK_EQ(string("abc")[1], 'b'); 426 // 427 // WARNING: These may not compile correctly if one of the arguments is a pointer 428 // and the other is NULL. To work around this, simply static_cast NULL to the 429 // type of the desired pointer. 430 431 #define DCHECK_EQ(val1, val2) DCHECK_OP(EQ, ==, val1, val2) 432 #define DCHECK_NE(val1, val2) DCHECK_OP(NE, !=, val1, val2) 433 #define DCHECK_LE(val1, val2) DCHECK_OP(LE, <=, val1, val2) 434 #define DCHECK_LT(val1, val2) DCHECK_OP(LT, < , val1, val2) 435 #define DCHECK_GE(val1, val2) DCHECK_OP(GE, >=, val1, val2) 436 #define DCHECK_GT(val1, val2) DCHECK_OP(GT, > , val1, val2) 437 438 #if defined(NDEBUG) && defined(OS_CHROMEOS) 439 #define NOTREACHED() LOG(ERROR) << "NOTREACHED() hit in " << \ 440 __FUNCTION__ << ". " 441 #else 442 #define NOTREACHED() DCHECK(false) 443 #endif 444 445 // Redefine the standard assert to use our nice log files 446 #undef assert 447 #define assert(x) DLOG_ASSERT(x) 448 449 // This class more or less represents a particular log message. You 450 // create an instance of LogMessage and then stream stuff to it. 451 // When you finish streaming to it, ~LogMessage is called and the 452 // full message gets streamed to the appropriate destination. 453 // 454 // You shouldn't actually use LogMessage's constructor to log things, 455 // though. You should use the LOG() macro (and variants thereof) 456 // above. 457 class BASE_EXPORT LogMessage { 458 public: 459 // Used for LOG(severity). 460 LogMessage(const char* file, int line, LogSeverity severity); 461 462 // Used for CHECK_EQ(), etc. Takes ownership of the given string. 463 // Implied severity = LOG_FATAL. 464 LogMessage(const char* file, int line, std::string* result); 465 466 // Used for DCHECK_EQ(), etc. Takes ownership of the given string. 467 LogMessage(const char* file, int line, LogSeverity severity, 468 std::string* result); 469 470 ~LogMessage(); 471 stream()472 std::ostream& stream() { return stream_; } 473 474 private: 475 void Init(const char* file, int line); 476 477 LogSeverity severity_; 478 std::ostringstream stream_; 479 size_t message_start_; // Offset of the start of the message (past prefix 480 // info). 481 // The file and line information passed in to the constructor. 482 const char* file_; 483 const int line_; 484 485 #if defined(OS_WIN) 486 // Stores the current value of GetLastError in the constructor and restores 487 // it in the destructor by calling SetLastError. 488 // This is useful since the LogMessage class uses a lot of Win32 calls 489 // that will lose the value of GLE and the code that called the log function 490 // will have lost the thread error value when the log call returns. 491 class SaveLastError { 492 public: 493 SaveLastError(); 494 ~SaveLastError(); 495 get_error()496 unsigned long get_error() const { return last_error_; } 497 498 protected: 499 unsigned long last_error_; 500 }; 501 502 SaveLastError last_error_; 503 #endif 504 505 DISALLOW_COPY_AND_ASSIGN(LogMessage); 506 }; 507 508 // A non-macro interface to the log facility; (useful 509 // when the logging level is not a compile-time constant). LogAtLevel(int const log_level,std::string const & msg)510 inline void LogAtLevel(int const log_level, std::string const &msg) { 511 LogMessage(__FILE__, __LINE__, log_level).stream() << msg; 512 } 513 514 // This class is used to explicitly ignore values in the conditional 515 // logging macros. This avoids compiler warnings like "value computed 516 // is not used" and "statement has no effect". 517 class LogMessageVoidify { 518 public: LogMessageVoidify()519 LogMessageVoidify() { } 520 // This has to be an operator with a precedence lower than << but 521 // higher than ?: 522 void operator&(std::ostream&) { } 523 }; 524 525 #if defined(OS_WIN) 526 typedef unsigned long SystemErrorCode; 527 #elif defined(OS_POSIX) 528 typedef int SystemErrorCode; 529 #endif 530 531 // Alias for ::GetLastError() on Windows and errno on POSIX. Avoids having to 532 // pull in windows.h just for GetLastError() and DWORD. 533 BASE_EXPORT SystemErrorCode GetLastSystemErrorCode(); 534 BASE_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code); 535 536 #if defined(OS_WIN) 537 // Appends a formatted system message of the GetLastError() type. 538 class BASE_EXPORT Win32ErrorLogMessage { 539 public: 540 Win32ErrorLogMessage(const char* file, 541 int line, 542 LogSeverity severity, 543 SystemErrorCode err); 544 545 // Appends the error message before destructing the encapsulated class. 546 ~Win32ErrorLogMessage(); 547 stream()548 std::ostream& stream() { return log_message_.stream(); } 549 550 private: 551 SystemErrorCode err_; 552 LogMessage log_message_; 553 554 DISALLOW_COPY_AND_ASSIGN(Win32ErrorLogMessage); 555 }; 556 #elif defined(OS_POSIX) 557 // Appends a formatted system message of the errno type 558 class BASE_EXPORT ErrnoLogMessage { 559 public: 560 ErrnoLogMessage(const char* file, 561 int line, 562 LogSeverity severity, 563 SystemErrorCode err); 564 565 // Appends the error message before destructing the encapsulated class. 566 ~ErrnoLogMessage(); 567 stream()568 std::ostream& stream() { return log_message_.stream(); } 569 570 private: 571 SystemErrorCode err_; 572 LogMessage log_message_; 573 574 DISALLOW_COPY_AND_ASSIGN(ErrnoLogMessage); 575 }; 576 #endif // OS_WIN 577 578 // Closes the log file explicitly if open. 579 // NOTE: Since the log file is opened as necessary by the action of logging 580 // statements, there's no guarantee that it will stay closed 581 // after this call. 582 BASE_EXPORT void CloseLogFile(); 583 584 // Async signal safe logging mechanism. 585 BASE_EXPORT void RawLog(int level, const char* message); 586 587 #define RAW_LOG(level, message) logging::RawLog(logging::LOG_ ## level, message) 588 589 #define RAW_CHECK(condition) \ 590 do { \ 591 if (!(condition)) \ 592 logging::RawLog(logging::LOG_FATAL, "Check failed: " #condition "\n"); \ 593 } while (0) 594 595 #if defined(OS_WIN) 596 // Returns the default log file path. 597 BASE_EXPORT std::wstring GetLogFileFullPath(); 598 #endif 599 600 } // namespace logging 601 602 // Note that "The behavior of a C++ program is undefined if it adds declarations 603 // or definitions to namespace std or to a namespace within namespace std unless 604 // otherwise specified." --C++11[namespace.std] 605 // 606 // We've checked that this particular definition has the intended behavior on 607 // our implementations, but it's prone to breaking in the future, and please 608 // don't imitate this in your own definitions without checking with some 609 // standard library experts. 610 namespace std { 611 // These functions are provided as a convenience for logging, which is where we 612 // use streams (it is against Google style to use streams in other places). It 613 // is designed to allow you to emit non-ASCII Unicode strings to the log file, 614 // which is normally ASCII. It is relatively slow, so try not to use it for 615 // common cases. Non-ASCII characters will be converted to UTF-8 by these 616 // operators. 617 BASE_EXPORT std::ostream& operator<<(std::ostream& out, const wchar_t* wstr); 618 inline std::ostream& operator<<(std::ostream& out, const std::wstring& wstr) { 619 return out << wstr.c_str(); 620 } 621 } // namespace std 622 623 // The NOTIMPLEMENTED() macro annotates codepaths which have 624 // not been implemented yet. 625 // 626 // The implementation of this macro is controlled by NOTIMPLEMENTED_POLICY: 627 // 0 -- Do nothing (stripped by compiler) 628 // 1 -- Warn at compile time 629 // 2 -- Fail at compile time 630 // 3 -- Fail at runtime (DCHECK) 631 // 4 -- [default] LOG(ERROR) at runtime 632 // 5 -- LOG(ERROR) at runtime, only once per call-site 633 634 #ifndef NOTIMPLEMENTED_POLICY 635 #if defined(OS_ANDROID) && defined(OFFICIAL_BUILD) 636 #define NOTIMPLEMENTED_POLICY 0 637 #else 638 // WebView: Hide NOTIMPLEMENTED entirely in Android release branch. 639 #define NOTIMPLEMENTED_POLICY 0 640 #endif 641 #endif 642 643 #if defined(COMPILER_GCC) 644 // On Linux, with GCC, we can use __PRETTY_FUNCTION__ to get the demangled name 645 // of the current function in the NOTIMPLEMENTED message. 646 #define NOTIMPLEMENTED_MSG "Not implemented reached in " << __PRETTY_FUNCTION__ 647 #else 648 #define NOTIMPLEMENTED_MSG "NOT IMPLEMENTED" 649 #endif 650 651 #if NOTIMPLEMENTED_POLICY == 0 652 #define NOTIMPLEMENTED() EAT_STREAM_PARAMETERS 653 #elif NOTIMPLEMENTED_POLICY == 1 654 // TODO, figure out how to generate a warning 655 #define NOTIMPLEMENTED() COMPILE_ASSERT(false, NOT_IMPLEMENTED) 656 #elif NOTIMPLEMENTED_POLICY == 2 657 #define NOTIMPLEMENTED() COMPILE_ASSERT(false, NOT_IMPLEMENTED) 658 #elif NOTIMPLEMENTED_POLICY == 3 659 #define NOTIMPLEMENTED() NOTREACHED() 660 #elif NOTIMPLEMENTED_POLICY == 4 661 #define NOTIMPLEMENTED() LOG(ERROR) << NOTIMPLEMENTED_MSG 662 #elif NOTIMPLEMENTED_POLICY == 5 663 #define NOTIMPLEMENTED() do {\ 664 static bool logged_once = false;\ 665 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ 666 logged_once = true;\ 667 } while(0);\ 668 EAT_STREAM_PARAMETERS 669 #endif 670 671 #endif // BASE_LOGGING_H_ 672