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(bool arg) generates compiler-specific annotations which | |
| 293 // prevent the static analyzer from analyzing the code using hypothetical | |
| 294 // values that are asserted to be impossible. It returns the truth value of | |
| 295 // |arg| as a bool. | |
| 296 // | |
| 297 // If |arg| is true, ANALYZER_ASSUME_TRUE has no effect and static analysis | |
| 298 // may proceed with analysis along the current path. | |
| 299 // | |
| 300 // If |arg| is false, static analysis is terminated and no further analysis | |
| 301 // errors will be generated for the current path. | |
|
brucedawson
2017/03/16 20:15:40
This doesn't fit my mental model for how /analyze
Kevin M
2017/03/16 23:08:13
Interesting. In the Clang case, noreturn is used f
| |
| 302 #if defined(__clang_analyzer__) | |
| 303 | |
| 304 inline void AnalyzerNoReturn() __attribute__((analyzer_noreturn)) {} | |
| 305 | |
| 306 inline constexpr bool AnalyzerAssumeTrue(bool arg) { | |
| 307 if (!arg) { | |
| 308 AnalyzerNoReturn(); | |
| 309 } | |
| 310 return arg; | |
| 311 } | |
| 312 | |
| 313 #define ANALYZER_ASSUME_TRUE(arg) ::logging::AnalyzerAssumeTrue(!!(arg)) | |
| 314 | |
| 315 #elif defined(_PREFAST_) && defined(OS_WIN) | |
| 316 | |
| 317 #define ANALYZER_ASSUME_TRUE(arg) (__analysis_assume(!!(arg)), !!(arg)) | |
|
brucedawson
2017/03/16 20:15:40
The second !! is not needed.
Kevin M
2017/03/16 23:08:13
It makes the expression value consistent with the
| |
| 318 | |
| 319 #else // _PREFAST_ & OS_WIN | |
| 320 | |
| 321 #define ANALYZER_ASSUME_TRUE(arg) !!(arg) | |
|
brucedawson
2017/03/16 20:15:40
The !! should not be needed, should it? It is need
Kevin M
2017/03/16 23:08:13
See above..
| |
| 322 | |
| 323 #endif // !__clang_analyzer && !_PREFAST_ | |
| 324 | |
| 292 typedef int LogSeverity; | 325 typedef int LogSeverity; |
| 293 const LogSeverity LOG_VERBOSE = -1; // This is level 1 verbosity | 326 const LogSeverity LOG_VERBOSE = -1; // This is level 1 verbosity |
| 294 // Note: the log severities are used to index into the array of names, | 327 // Note: the log severities are used to index into the array of names, |
| 295 // see log_severity_names. | 328 // see log_severity_names. |
| 296 const LogSeverity LOG_INFO = 0; | 329 const LogSeverity LOG_INFO = 0; |
| 297 const LogSeverity LOG_WARNING = 1; | 330 const LogSeverity LOG_WARNING = 1; |
| 298 const LogSeverity LOG_ERROR = 2; | 331 const LogSeverity LOG_ERROR = 2; |
| 299 const LogSeverity LOG_FATAL = 3; | 332 const LogSeverity LOG_FATAL = 3; |
| 300 const LogSeverity LOG_NUM_SEVERITIES = 4; | 333 const LogSeverity LOG_NUM_SEVERITIES = 4; |
| 301 | 334 |
| (...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 555 // compiler optimizations. | 588 // compiler optimizations. |
| 556 #define CHECK(condition) \ | 589 #define CHECK(condition) \ |
| 557 UNLIKELY(!(condition)) ? IMMEDIATE_CRASH() : EAT_STREAM_PARAMETERS | 590 UNLIKELY(!(condition)) ? IMMEDIATE_CRASH() : EAT_STREAM_PARAMETERS |
| 558 | 591 |
| 559 #define PCHECK(condition) CHECK(condition) | 592 #define PCHECK(condition) CHECK(condition) |
| 560 | 593 |
| 561 #define CHECK_OP(name, op, val1, val2) CHECK((val1) op (val2)) | 594 #define CHECK_OP(name, op, val1, val2) CHECK((val1) op (val2)) |
| 562 | 595 |
| 563 #else // !(OFFICIAL_BUILD && NDEBUG) | 596 #else // !(OFFICIAL_BUILD && NDEBUG) |
| 564 | 597 |
| 565 #if defined(_PREFAST_) && defined(OS_WIN) | |
| 566 // Use __analysis_assume to tell the VC++ static analysis engine that | |
| 567 // assert conditions are true, to suppress warnings. The LAZY_STREAM | |
| 568 // parameter doesn't reference 'condition' in /analyze builds because | |
| 569 // this evaluation confuses /analyze. The !! before condition is because | |
| 570 // __analysis_assume gets confused on some conditions: | |
| 571 // http://randomascii.wordpress.com/2011/09/13/analyze-for-visual-studio-the-ugl y-part-5/ | |
| 572 | |
| 573 #define CHECK(condition) \ | |
| 574 __analysis_assume(!!(condition)), \ | |
| 575 LAZY_STREAM(LOG_STREAM(FATAL), false) \ | |
| 576 << "Check failed: " #condition ". " | |
| 577 | |
| 578 #define PCHECK(condition) \ | |
| 579 __analysis_assume(!!(condition)), \ | |
| 580 LAZY_STREAM(PLOG_STREAM(FATAL), false) \ | |
| 581 << "Check failed: " #condition ". " | |
| 582 | |
| 583 #else // _PREFAST_ | |
| 584 | |
| 585 // Do as much work as possible out of line to reduce inline code size. | 598 // Do as much work as possible out of line to reduce inline code size. |
| 586 #define CHECK(condition) \ | 599 #define CHECK(condition) \ |
| 587 LAZY_STREAM(::logging::LogMessage(__FILE__, __LINE__, #condition).stream(), \ | 600 LAZY_STREAM(::logging::LogMessage(__FILE__, __LINE__, #condition).stream(), \ |
| 588 !(condition)) | 601 !ANALYZER_ASSUME_TRUE(condition)) |
| 589 | 602 |
| 590 #define PCHECK(condition) \ | 603 #define PCHECK(condition) \ |
| 591 LAZY_STREAM(PLOG_STREAM(FATAL), !(condition)) \ | 604 LAZY_STREAM(PLOG_STREAM(FATAL), !ANALYZER_ASSUME_TRUE(condition)) \ |
| 592 << "Check failed: " #condition ". " | 605 << "Check failed: " #condition ". " |
| 593 | 606 |
| 594 #endif // _PREFAST_ | |
| 595 | |
| 596 // Helper macro for binary operators. | 607 // Helper macro for binary operators. |
| 597 // Don't use this macro directly in your code, use CHECK_EQ et al below. | 608 // Don't use this macro directly in your code, use CHECK_EQ et al below. |
| 598 // The 'switch' is used to prevent the 'else' from being ambiguous when the | 609 // The 'switch' is used to prevent the 'else' from being ambiguous when the |
| 599 // macro is used in an 'if' clause such as: | 610 // macro is used in an 'if' clause such as: |
| 600 // if (a == 1) | 611 // if (a == 1) |
| 601 // CHECK_EQ(2, a); | 612 // CHECK_EQ(2, a); |
| 602 #define CHECK_OP(name, op, val1, val2) \ | 613 #define CHECK_OP(name, op, val1, val2) \ |
| 603 switch (0) case 0: default: \ | 614 switch (0) case 0: default: \ |
| 604 if (::logging::CheckOpResult true_if_passed = \ | 615 if (::logging::CheckOpResult true_if_passed = \ |
| 605 ::logging::Check##name##Impl((val1), (val2), \ | 616 ::logging::Check##name##Impl((val1), (val2), \ |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 678 std::string* MakeCheckOpString<unsigned int, unsigned long>( | 689 std::string* MakeCheckOpString<unsigned int, unsigned long>( |
| 679 const unsigned int&, const unsigned long&, const char* names); | 690 const unsigned int&, const unsigned long&, const char* names); |
| 680 extern template BASE_EXPORT | 691 extern template BASE_EXPORT |
| 681 std::string* MakeCheckOpString<std::string, std::string>( | 692 std::string* MakeCheckOpString<std::string, std::string>( |
| 682 const std::string&, const std::string&, const char* name); | 693 const std::string&, const std::string&, const char* name); |
| 683 | 694 |
| 684 // Helper functions for CHECK_OP macro. | 695 // Helper functions for CHECK_OP macro. |
| 685 // The (int, int) specialization works around the issue that the compiler | 696 // The (int, int) specialization works around the issue that the compiler |
| 686 // will not instantiate the template version of the function on values of | 697 // will not instantiate the template version of the function on values of |
| 687 // unnamed enum type - see comment below. | 698 // unnamed enum type - see comment below. |
| 699 // | |
| 700 // The checked condition is wrapped with ANALYZER_ASSUME_TRUE, which under | |
| 701 // static analysis builds, blocks analysis of the current path if the | |
| 702 // condition is false. | |
| 688 #define DEFINE_CHECK_OP_IMPL(name, op) \ | 703 #define DEFINE_CHECK_OP_IMPL(name, op) \ |
| 689 template <class t1, class t2> \ | 704 template <class t1, class t2> \ |
| 690 inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \ | 705 inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \ |
| 691 const char* names) { \ | 706 const char* names) { \ |
| 692 if (v1 op v2) \ | 707 if (ANALYZER_ASSUME_TRUE(v1 op v2)) \ |
| 693 return NULL; \ | 708 return NULL; \ |
| 694 else \ | 709 else \ |
| 695 return ::logging::MakeCheckOpString(v1, v2, names); \ | 710 return ::logging::MakeCheckOpString(v1, v2, names); \ |
| 696 } \ | 711 } \ |
| 697 inline std::string* Check##name##Impl(int v1, int v2, const char* names) { \ | 712 inline std::string* Check##name##Impl(int v1, int v2, const char* names) { \ |
| 698 if (v1 op v2) \ | 713 if (ANALYZER_ASSUME_TRUE(v1 op v2)) \ |
| 699 return NULL; \ | 714 return NULL; \ |
| 700 else \ | 715 else \ |
| 701 return ::logging::MakeCheckOpString(v1, v2, names); \ | 716 return ::logging::MakeCheckOpString(v1, v2, names); \ |
| 702 } | 717 } |
| 703 DEFINE_CHECK_OP_IMPL(EQ, ==) | 718 DEFINE_CHECK_OP_IMPL(EQ, ==) |
| 704 DEFINE_CHECK_OP_IMPL(NE, !=) | 719 DEFINE_CHECK_OP_IMPL(NE, !=) |
| 705 DEFINE_CHECK_OP_IMPL(LE, <=) | 720 DEFINE_CHECK_OP_IMPL(LE, <=) |
| 706 DEFINE_CHECK_OP_IMPL(LT, < ) | 721 DEFINE_CHECK_OP_IMPL(LT, < ) |
| 707 DEFINE_CHECK_OP_IMPL(GE, >=) | 722 DEFINE_CHECK_OP_IMPL(GE, >=) |
| 708 DEFINE_CHECK_OP_IMPL(GT, > ) | 723 DEFINE_CHECK_OP_IMPL(GT, > ) |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 785 | 800 |
| 786 // DCHECK et al. make sure to reference |condition| regardless of | 801 // DCHECK et al. make sure to reference |condition| regardless of |
| 787 // whether DCHECKs are enabled; this is so that we don't get unused | 802 // whether DCHECKs are enabled; this is so that we don't get unused |
| 788 // variable warnings if the only use of a variable is in a DCHECK. | 803 // variable warnings if the only use of a variable is in a DCHECK. |
| 789 // This behavior is different from DLOG_IF et al. | 804 // This behavior is different from DLOG_IF et al. |
| 790 // | 805 // |
| 791 // Note that the definition of the DCHECK macros depends on whether or not | 806 // Note that the definition of the DCHECK macros depends on whether or not |
| 792 // DCHECK_IS_ON() is true. When DCHECK_IS_ON() is false, the macros use | 807 // DCHECK_IS_ON() is true. When DCHECK_IS_ON() is false, the macros use |
| 793 // EAT_STREAM_PARAMETERS to avoid expressions that would create temporaries. | 808 // EAT_STREAM_PARAMETERS to avoid expressions that would create temporaries. |
| 794 | 809 |
| 795 #if defined(_PREFAST_) && defined(OS_WIN) | |
| 796 // See comments on the previous use of __analysis_assume. | |
| 797 | |
| 798 #define DCHECK(condition) \ | |
| 799 __analysis_assume(!!(condition)), \ | |
| 800 LAZY_STREAM(LOG_STREAM(DCHECK), false) \ | |
| 801 << "Check failed: " #condition ". " | |
| 802 | |
| 803 #define DPCHECK(condition) \ | |
| 804 __analysis_assume(!!(condition)), \ | |
| 805 LAZY_STREAM(PLOG_STREAM(DCHECK), false) \ | |
| 806 << "Check failed: " #condition ". " | |
| 807 | |
| 808 #elif defined(__clang_analyzer__) | |
| 809 | |
| 810 // Keeps the static analyzer from proceeding along the current codepath, | |
| 811 // otherwise false positive errors may be generated by null pointer checks. | |
| 812 inline constexpr bool AnalyzerNoReturn() __attribute__((analyzer_noreturn)) { | |
| 813 return false; | |
| 814 } | |
| 815 | |
| 816 #define DCHECK(condition) \ | |
| 817 LAZY_STREAM( \ | |
| 818 LOG_STREAM(DCHECK), \ | |
| 819 DCHECK_IS_ON() ? (logging::AnalyzerNoReturn() || !(condition)) : false) \ | |
| 820 << "Check failed: " #condition ". " | |
| 821 | |
| 822 #define DPCHECK(condition) \ | |
| 823 LAZY_STREAM( \ | |
| 824 PLOG_STREAM(DCHECK), \ | |
| 825 DCHECK_IS_ON() ? (logging::AnalyzerNoReturn() || !(condition)) : false) \ | |
| 826 << "Check failed: " #condition ". " | |
| 827 | |
| 828 #else | |
| 829 | |
| 830 #if DCHECK_IS_ON() | 810 #if DCHECK_IS_ON() |
| 831 | 811 |
| 832 #define DCHECK(condition) \ | 812 #define DCHECK(condition) \ |
| 833 LAZY_STREAM(LOG_STREAM(DCHECK), !(condition)) \ | 813 LAZY_STREAM(LOG_STREAM(DCHECK), !ANALYZER_ASSUME_TRUE(condition)) \ |
| 834 << "Check failed: " #condition ". " | 814 << "Check failed: " #condition ". " |
| 835 #define DPCHECK(condition) \ | 815 #define DPCHECK(condition) \ |
| 836 LAZY_STREAM(PLOG_STREAM(DCHECK), !(condition)) \ | 816 LAZY_STREAM(PLOG_STREAM(DCHECK), !ANALYZER_ASSUME_TRUE(condition)) \ |
| 837 << "Check failed: " #condition ". " | 817 << "Check failed: " #condition ". " |
| 838 | 818 |
| 839 #else // DCHECK_IS_ON() | 819 #else // DCHECK_IS_ON() |
| 840 | 820 |
| 841 #define DCHECK(condition) EAT_STREAM_PARAMETERS << !(condition) | 821 #define DCHECK(condition) EAT_STREAM_PARAMETERS << !(condition) |
| 842 #define DPCHECK(condition) EAT_STREAM_PARAMETERS << !(condition) | 822 #define DPCHECK(condition) EAT_STREAM_PARAMETERS << !(condition) |
| 843 | 823 |
| 844 #endif // DCHECK_IS_ON() | 824 #endif // DCHECK_IS_ON() |
| 845 | 825 |
| 846 #endif | |
| 847 | |
| 848 // Helper macro for binary operators. | 826 // Helper macro for binary operators. |
| 849 // Don't use this macro directly in your code, use DCHECK_EQ et al below. | 827 // Don't use this macro directly in your code, use DCHECK_EQ et al below. |
| 850 // The 'switch' is used to prevent the 'else' from being ambiguous when the | 828 // The 'switch' is used to prevent the 'else' from being ambiguous when the |
| 851 // macro is used in an 'if' clause such as: | 829 // macro is used in an 'if' clause such as: |
| 852 // if (a == 1) | 830 // if (a == 1) |
| 853 // DCHECK_EQ(2, a); | 831 // DCHECK_EQ(2, a); |
| 854 #if DCHECK_IS_ON() | 832 #if DCHECK_IS_ON() |
| 855 | 833 |
| 856 #define DCHECK_OP(name, op, val1, val2) \ | 834 #define DCHECK_OP(name, op, val1, val2) \ |
| 857 switch (0) case 0: default: \ | 835 switch (0) case 0: default: \ |
| 858 if (::logging::CheckOpResult true_if_passed = \ | 836 if (::logging::CheckOpResult true_if_passed = \ |
| 859 DCHECK_IS_ON() ? \ | 837 DCHECK_IS_ON() ? \ |
| 860 ::logging::Check##name##Impl((val1), (val2), \ | 838 ::logging::Check##name##Impl((val1), (val2), \ |
| 861 #val1 " " #op " " #val2) : nullptr) \ | 839 #val1 " " #op " " #val2) : nullptr) \ |
| 862 ; \ | 840 ; \ |
| 863 else \ | 841 else \ |
| 864 ::logging::LogMessage(__FILE__, __LINE__, ::logging::LOG_DCHECK, \ | 842 ::logging::LogMessage(__FILE__, __LINE__, ::logging::LOG_DCHECK, \ |
| 865 true_if_passed.message()).stream() | 843 true_if_passed.message()).stream() |
| 866 | 844 |
| 867 #else // DCHECK_IS_ON() | 845 #else // DCHECK_IS_ON() |
| 868 | 846 |
| 869 // When DCHECKs aren't enabled, DCHECK_OP still needs to reference operator<< | 847 // When DCHECKs aren't enabled, DCHECK_OP still needs to reference operator<< |
| 870 // overloads for |val1| and |val2| to avoid potential compiler warnings about | 848 // overloads for |val1| and |val2| to avoid potential compiler warnings about |
| 871 // unused functions. For the same reason, it also compares |val1| and |val2| | 849 // unused functions. For the same reason, it also compares |val1| and |val2| |
| 872 // using |op|. | 850 // using |op|. |
| 873 // | 851 // |
| 874 // Note that the contract of DCHECK_EQ, etc is that arguments are only evaluated | 852 // Note that the contract of DCHECK_EQ, etc is that arguments are only evaluated |
|
brucedawson
2017/03/16 20:15:40
Is this contract still respected? I'm not sure whe
Kevin M
2017/03/16 23:08:13
Actually, the potential for double evaluation of a
| |
| 875 // once. Even though |val1| and |val2| appear twice in this version of the macro | 853 // once. Even though |val1| and |val2| appear twice in this version of the macro |
| 876 // expansion, this is OK, since the expression is never actually evaluated. | 854 // expansion, this is OK, since the expression is never actually evaluated. |
| 877 #define DCHECK_OP(name, op, val1, val2) \ | 855 #define DCHECK_OP(name, op, val1, val2) \ |
| 878 EAT_STREAM_PARAMETERS << (::logging::MakeCheckOpValueString( \ | 856 EAT_STREAM_PARAMETERS << (::logging::MakeCheckOpValueString( \ |
| 879 ::logging::g_swallow_stream, val1), \ | 857 ::logging::g_swallow_stream, val1), \ |
| 880 ::logging::MakeCheckOpValueString( \ | 858 ::logging::MakeCheckOpValueString( \ |
| 881 ::logging::g_swallow_stream, val2), \ | 859 ::logging::g_swallow_stream, val2), \ |
| 882 (val1)op(val2)) | 860 ANALYZER_ASSUME_TRUE((val1)op(val2))) |
| 883 | 861 |
| 884 #endif // DCHECK_IS_ON() | 862 #endif // DCHECK_IS_ON() |
| 885 | 863 |
| 886 // Equality/Inequality checks - compare two values, and log a | 864 // Equality/Inequality checks - compare two values, and log a |
| 887 // LOG_DCHECK message including the two values when the result is not | 865 // LOG_DCHECK message including the two values when the result is not |
| 888 // as expected. The values must have operator<<(ostream, ...) | 866 // as expected. The values must have operator<<(ostream, ...) |
| 889 // defined. | 867 // defined. |
| 890 // | 868 // |
| 891 // You may append to the error message like so: | 869 // You may append to the error message like so: |
| 892 // DCHECK_NE(1, 2) << "The world must be ending!"; | 870 // DCHECK_NE(1, 2) << "The world must be ending!"; |
| (...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1146 #elif NOTIMPLEMENTED_POLICY == 5 | 1124 #elif NOTIMPLEMENTED_POLICY == 5 |
| 1147 #define NOTIMPLEMENTED() do {\ | 1125 #define NOTIMPLEMENTED() do {\ |
| 1148 static bool logged_once = false;\ | 1126 static bool logged_once = false;\ |
| 1149 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ | 1127 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ |
| 1150 logged_once = true;\ | 1128 logged_once = true;\ |
| 1151 } while(0);\ | 1129 } while(0);\ |
| 1152 EAT_STREAM_PARAMETERS | 1130 EAT_STREAM_PARAMETERS |
| 1153 #endif | 1131 #endif |
| 1154 | 1132 |
| 1155 #endif // BASE_LOGGING_H_ | 1133 #endif // BASE_LOGGING_H_ |
| OLD | NEW |