Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_LOGGING_H_ | 5 #ifndef BASE_LOGGING_H_ |
| 6 #define BASE_LOGGING_H_ | 6 #define BASE_LOGGING_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <cassert> | 10 #include <cassert> |
| (...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 282 | 282 |
| 283 // Sets the Log Message Handler that gets passed every log message before | 283 // Sets the Log Message Handler that gets passed every log message before |
| 284 // it's sent to other log destinations (if any). | 284 // it's sent to other log destinations (if any). |
| 285 // Returns true to signal that it handled the message and the message | 285 // Returns true to signal that it handled the message and the message |
| 286 // should not be sent to other log destinations. | 286 // should not be sent to other log destinations. |
| 287 typedef bool (*LogMessageHandlerFunction)(int severity, | 287 typedef bool (*LogMessageHandlerFunction)(int severity, |
| 288 const char* file, int line, size_t message_start, const std::string& str); | 288 const char* file, int line, size_t message_start, const std::string& str); |
| 289 BASE_EXPORT void SetLogMessageHandler(LogMessageHandlerFunction handler); | 289 BASE_EXPORT void SetLogMessageHandler(LogMessageHandlerFunction handler); |
| 290 BASE_EXPORT LogMessageHandlerFunction GetLogMessageHandler(); | 290 BASE_EXPORT LogMessageHandlerFunction GetLogMessageHandler(); |
| 291 | 291 |
| 292 // ANALYZER_ASSUME_TRUE(...) generates compiler-specific annotations which | |
| 293 // prevent the static analyzer from analyzing the code using hypothetical | |
| 294 // values that are asserted to be impossible. | |
| 295 #if defined(__clang_analyzer__) | |
| 296 | |
| 297 inline void AnalyzerNoReturn() __attribute__((analyzer_noreturn)) {} | |
| 298 | |
| 299 template <typename TVal> | |
| 300 inline constexpr TVal&& AnalysisAssumeTrue(TVal&& arg) { | |
| 301 if (!arg) { | |
| 302 AnalyzerNoReturn(); | |
| 303 } | |
| 304 return std::forward<TVal>(arg); | |
| 305 } | |
| 306 | |
| 307 #define ANALYZER_ASSUME_TRUE(val) ::logging::AnalysisAssumeTrue(val) | |
| 308 | |
| 309 #elif defined(_PREFAST_) && defined(OS_WIN) | |
| 310 | |
| 311 #define ANALYZER_ASSUME_TRUE(val) __analysis_assume(!!(val)) | |
| 312 | |
| 313 #else // !_PREFAST_ & !__clang_analyzer__ | |
| 314 | |
| 315 #define ANALYZER_ASSUME_TRUE(val) (val) | |
| 316 | |
| 317 #endif // !_PREFAST_ & !__clang_analyzer__ | |
| 318 | |
| 292 typedef int LogSeverity; | 319 typedef int LogSeverity; |
| 293 const LogSeverity LOG_VERBOSE = -1; // This is level 1 verbosity | 320 const LogSeverity LOG_VERBOSE = -1; // This is level 1 verbosity |
| 294 // Note: the log severities are used to index into the array of names, | 321 // Note: the log severities are used to index into the array of names, |
| 295 // see log_severity_names. | 322 // see log_severity_names. |
| 296 const LogSeverity LOG_INFO = 0; | 323 const LogSeverity LOG_INFO = 0; |
| 297 const LogSeverity LOG_WARNING = 1; | 324 const LogSeverity LOG_WARNING = 1; |
| 298 const LogSeverity LOG_ERROR = 2; | 325 const LogSeverity LOG_ERROR = 2; |
| 299 const LogSeverity LOG_FATAL = 3; | 326 const LogSeverity LOG_FATAL = 3; |
| 300 const LogSeverity LOG_NUM_SEVERITIES = 4; | 327 const LogSeverity LOG_NUM_SEVERITIES = 4; |
| 301 | 328 |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 401 | 428 |
| 402 #define VPLOG(verbose_level) \ | 429 #define VPLOG(verbose_level) \ |
| 403 LAZY_STREAM(VPLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level)) | 430 LAZY_STREAM(VPLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level)) |
| 404 | 431 |
| 405 #define VPLOG_IF(verbose_level, condition) \ | 432 #define VPLOG_IF(verbose_level, condition) \ |
| 406 LAZY_STREAM(VPLOG_STREAM(verbose_level), \ | 433 LAZY_STREAM(VPLOG_STREAM(verbose_level), \ |
| 407 VLOG_IS_ON(verbose_level) && (condition)) | 434 VLOG_IS_ON(verbose_level) && (condition)) |
| 408 | 435 |
| 409 // TODO(akalin): Add more VLOG variants, e.g. VPLOG. | 436 // TODO(akalin): Add more VLOG variants, e.g. VPLOG. |
| 410 | 437 |
| 411 #define LOG_ASSERT(condition) \ | 438 #define LOG_ASSERT(condition) \ |
| 412 LOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". " | 439 LOG_IF(FATAL, !ANALYZER_ASSUME_TRUE(condition)) \ |
| 440 << "Assert failed: " #condition ". " | |
| 413 | 441 |
| 414 #if defined(OS_WIN) | 442 #if defined(OS_WIN) |
| 415 #define PLOG_STREAM(severity) \ | 443 #define PLOG_STREAM(severity) \ |
| 416 COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \ | 444 COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \ |
| 417 ::logging::GetLastSystemErrorCode()).stream() | 445 ::logging::GetLastSystemErrorCode()).stream() |
| 418 #elif defined(OS_POSIX) | 446 #elif defined(OS_POSIX) |
| 419 #define PLOG_STREAM(severity) \ | 447 #define PLOG_STREAM(severity) \ |
| 420 COMPACT_GOOGLE_LOG_EX_ ## severity(ErrnoLogMessage, \ | 448 COMPACT_GOOGLE_LOG_EX_ ## severity(ErrnoLogMessage, \ |
| 421 ::logging::GetLastSystemErrorCode()).stream() | 449 ::logging::GetLastSystemErrorCode()).stream() |
| 422 #endif | 450 #endif |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 485 #define CHECK(condition) \ | 513 #define CHECK(condition) \ |
| 486 UNLIKELY(!(condition)) ? IMMEDIATE_CRASH() : EAT_STREAM_PARAMETERS | 514 UNLIKELY(!(condition)) ? IMMEDIATE_CRASH() : EAT_STREAM_PARAMETERS |
| 487 | 515 |
| 488 #define PCHECK(condition) CHECK(condition) | 516 #define PCHECK(condition) CHECK(condition) |
| 489 | 517 |
| 490 #define CHECK_OP(name, op, val1, val2) CHECK((val1) op (val2)) | 518 #define CHECK_OP(name, op, val1, val2) CHECK((val1) op (val2)) |
| 491 | 519 |
| 492 #else // !(OFFICIAL_BUILD && NDEBUG) | 520 #else // !(OFFICIAL_BUILD && NDEBUG) |
| 493 | 521 |
| 494 #if defined(_PREFAST_) && defined(OS_WIN) | 522 #if defined(_PREFAST_) && defined(OS_WIN) |
| 495 // Use __analysis_assume to tell the VC++ static analysis engine that | 523 // The LAZY_STREAM parameter doesn't reference 'condition' in /analyze builds |
| 496 // assert conditions are true, to suppress warnings. The LAZY_STREAM | 524 // because this evaluation confuses /analyze. The !! before condition is because |
| 497 // parameter doesn't reference 'condition' in /analyze builds because | |
| 498 // this evaluation confuses /analyze. The !! before condition is because | |
| 499 // __analysis_assume gets confused on some conditions: | 525 // __analysis_assume gets confused on some conditions: |
| 500 // http://randomascii.wordpress.com/2011/09/13/analyze-for-visual-studio-the-ugl y-part-5/ | 526 // http://randomascii.wordpress.com/2011/09/13/analyze-for-visual-studio-the-ugl y-part-5/ |
| 501 | 527 |
| 502 #define CHECK(condition) \ | 528 #define CHECK(condition) \ |
| 503 __analysis_assume(!!(condition)), \ | 529 ANALYZER_ASSUME_TRUE(condition) \ |
| 504 LAZY_STREAM(LOG_STREAM(FATAL), false) \ | 530 , LAZY_STREAM(LOG_STREAM(FATAL), false) << "Check failed: " #condition ". " |
| 505 << "Check failed: " #condition ". " | |
| 506 | 531 |
| 507 #define PCHECK(condition) \ | 532 #define PCHECK(condition) \ |
| 508 __analysis_assume(!!(condition)), \ | 533 ANALYZER_ASSUME_TRUE(condition) \ |
| 509 LAZY_STREAM(PLOG_STREAM(FATAL), false) \ | 534 , LAZY_STREAM(PLOG_STREAM(FATAL), false) << "Check failed: " #condition ". " |
| 510 << "Check failed: " #condition ". " | |
| 511 | 535 |
| 512 #else // _PREFAST_ | 536 #else // _PREFAST_ |
| 513 | 537 |
| 514 // Do as much work as possible out of line to reduce inline code size. | 538 // Do as much work as possible out of line to reduce inline code size. |
| 515 #define CHECK(condition) \ | 539 #define CHECK(condition) \ |
| 516 LAZY_STREAM(::logging::LogMessage(__FILE__, __LINE__, #condition).stream(), \ | 540 LAZY_STREAM(::logging::LogMessage(__FILE__, __LINE__, #condition).stream(), \ |
| 517 !(condition)) | 541 !ANALYZER_ASSUME_TRUE(condition)) |
| 518 | 542 |
| 519 #define PCHECK(condition) \ | 543 #define PCHECK(condition) \ |
| 520 LAZY_STREAM(PLOG_STREAM(FATAL), !(condition)) \ | 544 LAZY_STREAM(PLOG_STREAM(FATAL), !ANALYZER_ASSUME_TRUE(condition)) \ |
| 521 << "Check failed: " #condition ". " | 545 << "Check failed: " #condition ". " |
| 522 | 546 |
| 523 #endif // _PREFAST_ | 547 #endif // _PREFAST_ |
| 524 | 548 |
| 525 // Helper macro for binary operators. | 549 // Helper macro for binary operators. |
| 526 // Don't use this macro directly in your code, use CHECK_EQ et al below. | 550 // Don't use this macro directly in your code, use CHECK_EQ et al below. |
| 527 // The 'switch' is used to prevent the 'else' from being ambiguous when the | 551 // The 'switch' is used to prevent the 'else' from being ambiguous when the |
| 528 // macro is used in an 'if' clause such as: | 552 // macro is used in an 'if' clause such as: |
| 529 // if (a == 1) | 553 // if (a == 1) |
| 530 // CHECK_EQ(2, a); | 554 // CHECK_EQ(2, a); |
| 531 #define CHECK_OP(name, op, val1, val2) \ | 555 #define CHECK_OP(name, op, val1, val2) \ |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 711 // DCHECK et al. make sure to reference |condition| regardless of | 735 // DCHECK et al. make sure to reference |condition| regardless of |
| 712 // whether DCHECKs are enabled; this is so that we don't get unused | 736 // whether DCHECKs are enabled; this is so that we don't get unused |
| 713 // variable warnings if the only use of a variable is in a DCHECK. | 737 // variable warnings if the only use of a variable is in a DCHECK. |
| 714 // This behavior is different from DLOG_IF et al. | 738 // This behavior is different from DLOG_IF et al. |
| 715 // | 739 // |
| 716 // Note that the definition of the DCHECK macros depends on whether or not | 740 // Note that the definition of the DCHECK macros depends on whether or not |
| 717 // DCHECK_IS_ON() is true. When DCHECK_IS_ON() is false, the macros use | 741 // DCHECK_IS_ON() is true. When DCHECK_IS_ON() is false, the macros use |
| 718 // EAT_STREAM_PARAMETERS to avoid expressions that would create temporaries. | 742 // EAT_STREAM_PARAMETERS to avoid expressions that would create temporaries. |
| 719 | 743 |
| 720 #if defined(_PREFAST_) && defined(OS_WIN) | 744 #if defined(_PREFAST_) && defined(OS_WIN) |
| 721 // See comments on the previous use of __analysis_assume. | 745 // See comments on the previous use of __analysis_assume. |
|
Wez
2017/02/06 21:04:39
See comment above; do we need this if we have a "s
Kevin M
2017/02/09 23:36:25
Done.
Wez
2017/02/10 00:40:45
Woohoo, that looks great. Hopefully it doesn't tri
| |
| 722 | 746 |
| 723 #define DCHECK(condition) \ | 747 #define DCHECK(condition) \ |
| 724 __analysis_assume(!!(condition)), \ | 748 ANALYZER_ASSUME_TRUE(condition) \ |
| 725 LAZY_STREAM(LOG_STREAM(DCHECK), false) \ | 749 , LAZY_STREAM(LOG_STREAM(DCHECK), false) << "Check failed: " #condition ". " |
| 726 << "Check failed: " #condition ". " | |
| 727 | 750 |
| 728 #define DPCHECK(condition) \ | 751 #define DPCHECK(condition) \ |
| 729 __analysis_assume(!!(condition)), \ | 752 ANALYZER_ASSUME_TRUE(condition) \ |
| 730 LAZY_STREAM(PLOG_STREAM(DCHECK), false) \ | 753 , LAZY_STREAM(PLOG_STREAM(DCHECK), false) << "Check failed: " #condition \ |
| 731 << "Check failed: " #condition ". " | 754 "." \ |
| 732 | 755 " " |
| 733 #elif defined(__clang_analyzer__) | |
| 734 | |
| 735 // Keeps the static analyzer from proceeding along the current codepath, | |
| 736 // otherwise false positive errors may be generated by null pointer checks. | |
| 737 inline constexpr bool AnalyzerNoReturn() __attribute__((analyzer_noreturn)) { | |
| 738 return false; | |
| 739 } | |
| 740 | |
| 741 #define DCHECK(condition) \ | |
| 742 LAZY_STREAM( \ | |
| 743 LOG_STREAM(DCHECK), \ | |
| 744 DCHECK_IS_ON() ? (logging::AnalyzerNoReturn() || !(condition)) : false) \ | |
| 745 << "Check failed: " #condition ". " | |
| 746 | |
| 747 #define DPCHECK(condition) \ | |
| 748 LAZY_STREAM( \ | |
| 749 PLOG_STREAM(DCHECK), \ | |
| 750 DCHECK_IS_ON() ? (logging::AnalyzerNoReturn() || !(condition)) : false) \ | |
| 751 << "Check failed: " #condition ". " | |
| 752 | 756 |
| 753 #else | 757 #else |
| 754 | 758 |
| 755 #if DCHECK_IS_ON() | 759 #if DCHECK_IS_ON() |
| 756 | 760 |
| 757 #define DCHECK(condition) \ | 761 #define DCHECK(condition) \ |
| 758 LAZY_STREAM(LOG_STREAM(DCHECK), !(condition)) \ | 762 LAZY_STREAM(LOG_STREAM(DCHECK), !ANALYZER_ASSUME_TRUE(condition)) \ |
| 759 << "Check failed: " #condition ". " | 763 << "Check failed: " #condition ". " |
| 760 #define DPCHECK(condition) \ | 764 #define DPCHECK(condition) \ |
| 761 LAZY_STREAM(PLOG_STREAM(DCHECK), !(condition)) \ | 765 LAZY_STREAM(PLOG_STREAM(DCHECK), !ANALYZER_ASSUME_TRUE(condition)) \ |
| 762 << "Check failed: " #condition ". " | 766 << "Check failed: " #condition ". " |
| 763 | 767 |
| 764 #else // DCHECK_IS_ON() | 768 #else // DCHECK_IS_ON() |
| 765 | 769 |
| 766 #define DCHECK(condition) EAT_STREAM_PARAMETERS << !(condition) | 770 #define DCHECK(condition) \ |
| 767 #define DPCHECK(condition) EAT_STREAM_PARAMETERS << !(condition) | 771 EAT_STREAM_PARAMETERS << !ANALYZER_ASSUME_TRUE(condition) |
| 772 #define DPCHECK(condition) \ | |
| 773 EAT_STREAM_PARAMETERS << !ANALYZER_ASSUME_TRUE(condition) | |
| 768 | 774 |
| 769 #endif // DCHECK_IS_ON() | 775 #endif // DCHECK_IS_ON() |
| 770 | 776 |
| 771 #endif | 777 #endif |
| 772 | 778 |
| 773 // Helper macro for binary operators. | 779 // Helper macro for binary operators. |
| 774 // Don't use this macro directly in your code, use DCHECK_EQ et al below. | 780 // Don't use this macro directly in your code, use DCHECK_EQ et al below. |
| 775 // The 'switch' is used to prevent the 'else' from being ambiguous when the | 781 // The 'switch' is used to prevent the 'else' from being ambiguous when the |
| 776 // macro is used in an 'if' clause such as: | 782 // macro is used in an 'if' clause such as: |
| 777 // if (a == 1) | 783 // if (a == 1) |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 797 // using |op|. | 803 // using |op|. |
| 798 // | 804 // |
| 799 // Note that the contract of DCHECK_EQ, etc is that arguments are only evaluated | 805 // Note that the contract of DCHECK_EQ, etc is that arguments are only evaluated |
| 800 // once. Even though |val1| and |val2| appear twice in this version of the macro | 806 // once. Even though |val1| and |val2| appear twice in this version of the macro |
| 801 // expansion, this is OK, since the expression is never actually evaluated. | 807 // expansion, this is OK, since the expression is never actually evaluated. |
| 802 #define DCHECK_OP(name, op, val1, val2) \ | 808 #define DCHECK_OP(name, op, val1, val2) \ |
| 803 EAT_STREAM_PARAMETERS << (::logging::MakeCheckOpValueString( \ | 809 EAT_STREAM_PARAMETERS << (::logging::MakeCheckOpValueString( \ |
| 804 ::logging::g_swallow_stream, val1), \ | 810 ::logging::g_swallow_stream, val1), \ |
| 805 ::logging::MakeCheckOpValueString( \ | 811 ::logging::MakeCheckOpValueString( \ |
| 806 ::logging::g_swallow_stream, val2), \ | 812 ::logging::g_swallow_stream, val2), \ |
| 807 (val1)op(val2)) | 813 ANALYZER_ASSUME_TRUE((val1)op(val2))) |
| 808 | 814 |
| 809 #endif // DCHECK_IS_ON() | 815 #endif // DCHECK_IS_ON() |
| 810 | 816 |
| 811 // Equality/Inequality checks - compare two values, and log a | 817 // Equality/Inequality checks - compare two values, and log a |
| 812 // LOG_DCHECK message including the two values when the result is not | 818 // LOG_DCHECK message including the two values when the result is not |
| 813 // as expected. The values must have operator<<(ostream, ...) | 819 // as expected. The values must have operator<<(ostream, ...) |
| 814 // defined. | 820 // defined. |
| 815 // | 821 // |
| 816 // You may append to the error message like so: | 822 // You may append to the error message like so: |
| 817 // DCHECK_NE(1, 2) << "The world must be ending!"; | 823 // DCHECK_NE(1, 2) << "The world must be ending!"; |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 986 BASE_EXPORT void CloseLogFile(); | 992 BASE_EXPORT void CloseLogFile(); |
| 987 | 993 |
| 988 // Async signal safe logging mechanism. | 994 // Async signal safe logging mechanism. |
| 989 BASE_EXPORT void RawLog(int level, const char* message); | 995 BASE_EXPORT void RawLog(int level, const char* message); |
| 990 | 996 |
| 991 #define RAW_LOG(level, message) \ | 997 #define RAW_LOG(level, message) \ |
| 992 ::logging::RawLog(::logging::LOG_##level, message) | 998 ::logging::RawLog(::logging::LOG_##level, message) |
| 993 | 999 |
| 994 #define RAW_CHECK(condition) \ | 1000 #define RAW_CHECK(condition) \ |
| 995 do { \ | 1001 do { \ |
| 996 if (!(condition)) \ | 1002 if (!ANALYZER_ASSUME_TRUE(condition)) \ |
| 997 ::logging::RawLog(::logging::LOG_FATAL, \ | 1003 ::logging::RawLog(::logging::LOG_FATAL, \ |
| 998 "Check failed: " #condition "\n"); \ | 1004 "Check failed: " #condition "\n"); \ |
| 999 } while (0) | 1005 } while (0) |
| 1000 | 1006 |
| 1001 #if defined(OS_WIN) | 1007 #if defined(OS_WIN) |
| 1002 // Returns true if logging to file is enabled. | 1008 // Returns true if logging to file is enabled. |
| 1003 BASE_EXPORT bool IsLoggingToFileEnabled(); | 1009 BASE_EXPORT bool IsLoggingToFileEnabled(); |
| 1004 | 1010 |
| 1005 // Returns the default log file path. | 1011 // Returns the default log file path. |
| 1006 BASE_EXPORT std::wstring GetLogFileFullPath(); | 1012 BASE_EXPORT std::wstring GetLogFileFullPath(); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1071 #elif NOTIMPLEMENTED_POLICY == 5 | 1077 #elif NOTIMPLEMENTED_POLICY == 5 |
| 1072 #define NOTIMPLEMENTED() do {\ | 1078 #define NOTIMPLEMENTED() do {\ |
| 1073 static bool logged_once = false;\ | 1079 static bool logged_once = false;\ |
| 1074 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ | 1080 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ |
| 1075 logged_once = true;\ | 1081 logged_once = true;\ |
| 1076 } while(0);\ | 1082 } while(0);\ |
| 1077 EAT_STREAM_PARAMETERS | 1083 EAT_STREAM_PARAMETERS |
| 1078 #endif | 1084 #endif |
| 1079 | 1085 |
| 1080 #endif // BASE_LOGGING_H_ | 1086 #endif // BASE_LOGGING_H_ |
| OLD | NEW |