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 // The ANALYZER_ASSUME_TRUE(bool arg) macro adds compiler-specific hints |
| 293 // to Clang which control what code paths are statically analyzed, |
| 294 // and is meant to be used in conjunction with assert & assert-like functions. |
| 295 // The expression is passed straight through if analysis isn't enabled. |
| 296 #if defined(__clang_analyzer__) |
| 297 |
| 298 inline constexpr bool AnalyzerNoReturn() __attribute__((analyzer_noreturn)) { |
| 299 return false; |
| 300 } |
| 301 |
| 302 inline constexpr bool AnalyzerAssumeTrue(bool arg) { |
| 303 // AnalyzerNoReturn() is invoked and analysis is terminated if |arg| is |
| 304 // false. |
| 305 return arg || AnalyzerNoReturn(); |
| 306 } |
| 307 |
| 308 #define ANALYZER_ASSUME_TRUE(arg) ::logging::AnalyzerAssumeTrue(!!(arg)) |
| 309 |
| 310 #else // !defined(__clang_analyzer__) |
| 311 |
| 312 #define ANALYZER_ASSUME_TRUE(arg) (arg) |
| 313 |
| 314 #endif // defined(__clang_analyzer__) |
| 315 |
292 typedef int LogSeverity; | 316 typedef int LogSeverity; |
293 const LogSeverity LOG_VERBOSE = -1; // This is level 1 verbosity | 317 const LogSeverity LOG_VERBOSE = -1; // This is level 1 verbosity |
294 // Note: the log severities are used to index into the array of names, | 318 // Note: the log severities are used to index into the array of names, |
295 // see log_severity_names. | 319 // see log_severity_names. |
296 const LogSeverity LOG_INFO = 0; | 320 const LogSeverity LOG_INFO = 0; |
297 const LogSeverity LOG_WARNING = 1; | 321 const LogSeverity LOG_WARNING = 1; |
298 const LogSeverity LOG_ERROR = 2; | 322 const LogSeverity LOG_ERROR = 2; |
299 const LogSeverity LOG_FATAL = 3; | 323 const LogSeverity LOG_FATAL = 3; |
300 const LogSeverity LOG_NUM_SEVERITIES = 4; | 324 const LogSeverity LOG_NUM_SEVERITIES = 4; |
301 | 325 |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
401 | 425 |
402 #define VPLOG(verbose_level) \ | 426 #define VPLOG(verbose_level) \ |
403 LAZY_STREAM(VPLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level)) | 427 LAZY_STREAM(VPLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level)) |
404 | 428 |
405 #define VPLOG_IF(verbose_level, condition) \ | 429 #define VPLOG_IF(verbose_level, condition) \ |
406 LAZY_STREAM(VPLOG_STREAM(verbose_level), \ | 430 LAZY_STREAM(VPLOG_STREAM(verbose_level), \ |
407 VLOG_IS_ON(verbose_level) && (condition)) | 431 VLOG_IS_ON(verbose_level) && (condition)) |
408 | 432 |
409 // TODO(akalin): Add more VLOG variants, e.g. VPLOG. | 433 // TODO(akalin): Add more VLOG variants, e.g. VPLOG. |
410 | 434 |
411 #define LOG_ASSERT(condition) \ | 435 #define LOG_ASSERT(condition) \ |
412 LOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". " | 436 LOG_IF(FATAL, !(ANALYZER_ASSUME_TRUE(condition))) \ |
| 437 << "Assert failed: " #condition ". " |
413 | 438 |
414 #if defined(OS_WIN) | 439 #if defined(OS_WIN) |
415 #define PLOG_STREAM(severity) \ | 440 #define PLOG_STREAM(severity) \ |
416 COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \ | 441 COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \ |
417 ::logging::GetLastSystemErrorCode()).stream() | 442 ::logging::GetLastSystemErrorCode()).stream() |
418 #elif defined(OS_POSIX) | 443 #elif defined(OS_POSIX) |
419 #define PLOG_STREAM(severity) \ | 444 #define PLOG_STREAM(severity) \ |
420 COMPACT_GOOGLE_LOG_EX_ ## severity(ErrnoLogMessage, \ | 445 COMPACT_GOOGLE_LOG_EX_ ## severity(ErrnoLogMessage, \ |
421 ::logging::GetLastSystemErrorCode()).stream() | 446 ::logging::GetLastSystemErrorCode()).stream() |
422 #endif | 447 #endif |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
578 #define PCHECK(condition) \ | 603 #define PCHECK(condition) \ |
579 __analysis_assume(!!(condition)), \ | 604 __analysis_assume(!!(condition)), \ |
580 LAZY_STREAM(PLOG_STREAM(FATAL), false) \ | 605 LAZY_STREAM(PLOG_STREAM(FATAL), false) \ |
581 << "Check failed: " #condition ". " | 606 << "Check failed: " #condition ". " |
582 | 607 |
583 #else // _PREFAST_ | 608 #else // _PREFAST_ |
584 | 609 |
585 // Do as much work as possible out of line to reduce inline code size. | 610 // Do as much work as possible out of line to reduce inline code size. |
586 #define CHECK(condition) \ | 611 #define CHECK(condition) \ |
587 LAZY_STREAM(::logging::LogMessage(__FILE__, __LINE__, #condition).stream(), \ | 612 LAZY_STREAM(::logging::LogMessage(__FILE__, __LINE__, #condition).stream(), \ |
588 !(condition)) | 613 !ANALYZER_ASSUME_TRUE(condition)) |
589 | 614 |
590 #define PCHECK(condition) \ | 615 #define PCHECK(condition) \ |
591 LAZY_STREAM(PLOG_STREAM(FATAL), !(condition)) \ | 616 LAZY_STREAM(PLOG_STREAM(FATAL), !ANALYZER_ASSUME_TRUE(condition)) \ |
592 << "Check failed: " #condition ". " | 617 << "Check failed: " #condition ". " |
593 | 618 |
594 #endif // _PREFAST_ | 619 #endif // _PREFAST_ |
595 | 620 |
596 // Helper macro for binary operators. | 621 // Helper macro for binary operators. |
597 // Don't use this macro directly in your code, use CHECK_EQ et al below. | 622 // 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 | 623 // The 'switch' is used to prevent the 'else' from being ambiguous when the |
599 // macro is used in an 'if' clause such as: | 624 // macro is used in an 'if' clause such as: |
600 // if (a == 1) | 625 // if (a == 1) |
601 // CHECK_EQ(2, a); | 626 // CHECK_EQ(2, a); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
678 std::string* MakeCheckOpString<unsigned int, unsigned long>( | 703 std::string* MakeCheckOpString<unsigned int, unsigned long>( |
679 const unsigned int&, const unsigned long&, const char* names); | 704 const unsigned int&, const unsigned long&, const char* names); |
680 extern template BASE_EXPORT | 705 extern template BASE_EXPORT |
681 std::string* MakeCheckOpString<std::string, std::string>( | 706 std::string* MakeCheckOpString<std::string, std::string>( |
682 const std::string&, const std::string&, const char* name); | 707 const std::string&, const std::string&, const char* name); |
683 | 708 |
684 // Helper functions for CHECK_OP macro. | 709 // Helper functions for CHECK_OP macro. |
685 // The (int, int) specialization works around the issue that the compiler | 710 // The (int, int) specialization works around the issue that the compiler |
686 // will not instantiate the template version of the function on values of | 711 // will not instantiate the template version of the function on values of |
687 // unnamed enum type - see comment below. | 712 // unnamed enum type - see comment below. |
| 713 // |
| 714 // The checked condition is wrapped with ANALYZER_ASSUME_TRUE, which under |
| 715 // static analysis builds, blocks analysis of the current path if the |
| 716 // condition is false. |
688 #define DEFINE_CHECK_OP_IMPL(name, op) \ | 717 #define DEFINE_CHECK_OP_IMPL(name, op) \ |
689 template <class t1, class t2> \ | 718 template <class t1, class t2> \ |
690 inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \ | 719 inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \ |
691 const char* names) { \ | 720 const char* names) { \ |
692 if (v1 op v2) \ | 721 if (ANALYZER_ASSUME_TRUE(v1 op v2)) \ |
693 return NULL; \ | 722 return NULL; \ |
694 else \ | 723 else \ |
695 return ::logging::MakeCheckOpString(v1, v2, names); \ | 724 return ::logging::MakeCheckOpString(v1, v2, names); \ |
696 } \ | 725 } \ |
697 inline std::string* Check##name##Impl(int v1, int v2, const char* names) { \ | 726 inline std::string* Check##name##Impl(int v1, int v2, const char* names) { \ |
698 if (v1 op v2) \ | 727 if (ANALYZER_ASSUME_TRUE(v1 op v2)) \ |
699 return NULL; \ | 728 return NULL; \ |
700 else \ | 729 else \ |
701 return ::logging::MakeCheckOpString(v1, v2, names); \ | 730 return ::logging::MakeCheckOpString(v1, v2, names); \ |
702 } | 731 } |
703 DEFINE_CHECK_OP_IMPL(EQ, ==) | 732 DEFINE_CHECK_OP_IMPL(EQ, ==) |
704 DEFINE_CHECK_OP_IMPL(NE, !=) | 733 DEFINE_CHECK_OP_IMPL(NE, !=) |
705 DEFINE_CHECK_OP_IMPL(LE, <=) | 734 DEFINE_CHECK_OP_IMPL(LE, <=) |
706 DEFINE_CHECK_OP_IMPL(LT, < ) | 735 DEFINE_CHECK_OP_IMPL(LT, < ) |
707 DEFINE_CHECK_OP_IMPL(GE, >=) | 736 DEFINE_CHECK_OP_IMPL(GE, >=) |
708 DEFINE_CHECK_OP_IMPL(GT, > ) | 737 DEFINE_CHECK_OP_IMPL(GT, > ) |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
791 #define DCHECK(condition) \ | 820 #define DCHECK(condition) \ |
792 __analysis_assume(!!(condition)), \ | 821 __analysis_assume(!!(condition)), \ |
793 LAZY_STREAM(LOG_STREAM(DCHECK), false) \ | 822 LAZY_STREAM(LOG_STREAM(DCHECK), false) \ |
794 << "Check failed: " #condition ". " | 823 << "Check failed: " #condition ". " |
795 | 824 |
796 #define DPCHECK(condition) \ | 825 #define DPCHECK(condition) \ |
797 __analysis_assume(!!(condition)), \ | 826 __analysis_assume(!!(condition)), \ |
798 LAZY_STREAM(PLOG_STREAM(DCHECK), false) \ | 827 LAZY_STREAM(PLOG_STREAM(DCHECK), false) \ |
799 << "Check failed: " #condition ". " | 828 << "Check failed: " #condition ". " |
800 | 829 |
801 #elif defined(__clang_analyzer__) | 830 #else // !(defined(_PREFAST_) && defined(OS_WIN)) |
802 | |
803 // Keeps the static analyzer from proceeding along the current codepath, | |
804 // otherwise false positive errors may be generated by null pointer checks. | |
805 inline constexpr bool AnalyzerNoReturn() __attribute__((analyzer_noreturn)) { | |
806 return false; | |
807 } | |
808 | |
809 #define DCHECK(condition) \ | |
810 LAZY_STREAM( \ | |
811 LOG_STREAM(DCHECK), \ | |
812 DCHECK_IS_ON() ? (logging::AnalyzerNoReturn() || !(condition)) : false) \ | |
813 << "Check failed: " #condition ". " | |
814 | |
815 #define DPCHECK(condition) \ | |
816 LAZY_STREAM( \ | |
817 PLOG_STREAM(DCHECK), \ | |
818 DCHECK_IS_ON() ? (logging::AnalyzerNoReturn() || !(condition)) : false) \ | |
819 << "Check failed: " #condition ". " | |
820 | |
821 #else | |
822 | 831 |
823 #if DCHECK_IS_ON() | 832 #if DCHECK_IS_ON() |
824 | 833 |
825 #define DCHECK(condition) \ | 834 #define DCHECK(condition) \ |
826 LAZY_STREAM(LOG_STREAM(DCHECK), !(condition)) \ | 835 LAZY_STREAM(LOG_STREAM(DCHECK), !ANALYZER_ASSUME_TRUE(condition)) \ |
827 << "Check failed: " #condition ". " | 836 << "Check failed: " #condition ". " |
828 #define DPCHECK(condition) \ | 837 #define DPCHECK(condition) \ |
829 LAZY_STREAM(PLOG_STREAM(DCHECK), !(condition)) \ | 838 LAZY_STREAM(PLOG_STREAM(DCHECK), !ANALYZER_ASSUME_TRUE(condition)) \ |
830 << "Check failed: " #condition ". " | 839 << "Check failed: " #condition ". " |
831 | 840 |
832 #else // DCHECK_IS_ON() | 841 #else // DCHECK_IS_ON() |
833 | 842 |
834 #define DCHECK(condition) EAT_STREAM_PARAMETERS << !(condition) | 843 #define DCHECK(condition) EAT_STREAM_PARAMETERS << !(condition) |
835 #define DPCHECK(condition) EAT_STREAM_PARAMETERS << !(condition) | 844 #define DPCHECK(condition) EAT_STREAM_PARAMETERS << !(condition) |
836 | 845 |
837 #endif // DCHECK_IS_ON() | 846 #endif // DCHECK_IS_ON() |
838 | 847 |
839 #endif | 848 #endif // defined(_PREFAST_) && defined(OS_WIN) |
840 | 849 |
841 // Helper macro for binary operators. | 850 // Helper macro for binary operators. |
842 // Don't use this macro directly in your code, use DCHECK_EQ et al below. | 851 // Don't use this macro directly in your code, use DCHECK_EQ et al below. |
843 // The 'switch' is used to prevent the 'else' from being ambiguous when the | 852 // The 'switch' is used to prevent the 'else' from being ambiguous when the |
844 // macro is used in an 'if' clause such as: | 853 // macro is used in an 'if' clause such as: |
845 // if (a == 1) | 854 // if (a == 1) |
846 // DCHECK_EQ(2, a); | 855 // DCHECK_EQ(2, a); |
847 #if DCHECK_IS_ON() | 856 #if DCHECK_IS_ON() |
848 | 857 |
849 #define DCHECK_OP(name, op, val1, val2) \ | 858 #define DCHECK_OP(name, op, val1, val2) \ |
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1139 #elif NOTIMPLEMENTED_POLICY == 5 | 1148 #elif NOTIMPLEMENTED_POLICY == 5 |
1140 #define NOTIMPLEMENTED() do {\ | 1149 #define NOTIMPLEMENTED() do {\ |
1141 static bool logged_once = false;\ | 1150 static bool logged_once = false;\ |
1142 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ | 1151 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ |
1143 logged_once = true;\ | 1152 logged_once = true;\ |
1144 } while(0);\ | 1153 } while(0);\ |
1145 EAT_STREAM_PARAMETERS | 1154 EAT_STREAM_PARAMETERS |
1146 #endif | 1155 #endif |
1147 | 1156 |
1148 #endif // BASE_LOGGING_H_ | 1157 #endif // BASE_LOGGING_H_ |
OLD | NEW |