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 <cassert> | 8 #include <cassert> |
9 #include <string> | 9 #include <string> |
10 #include <cstring> | 10 #include <cstring> |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 // | 131 // |
132 // Very important: logging a message at the FATAL severity level causes | 132 // Very important: logging a message at the FATAL severity level causes |
133 // the program to terminate (after the message is logged). | 133 // the program to terminate (after the message is logged). |
134 // | 134 // |
135 // There is the special severity of DFATAL, which logs FATAL in debug mode, | 135 // There is the special severity of DFATAL, which logs FATAL in debug mode, |
136 // ERROR in normal mode. | 136 // ERROR in normal mode. |
137 | 137 |
138 namespace logging { | 138 namespace logging { |
139 | 139 |
140 // TODO(avi): do we want to do a unification of character types here? | 140 // TODO(avi): do we want to do a unification of character types here? |
141 #if defined(OS_WIN) | |
142 typedef wchar_t PathChar; | |
143 #else | |
144 typedef char PathChar; | 141 typedef char PathChar; |
145 #endif | |
146 | 142 |
147 // Where to record logging output? A flat file and/or system debug log | 143 // Where to record logging output? A flat file and/or system debug log |
148 // via OutputDebugString. | 144 // via OutputDebugString. |
149 enum LoggingDestination { | 145 enum LoggingDestination { |
150 LOG_NONE = 0, | 146 LOG_NONE = 0, |
151 LOG_TO_FILE = 1 << 0, | 147 LOG_TO_FILE = 1 << 0, |
152 LOG_TO_SYSTEM_DEBUG_LOG = 1 << 1, | 148 LOG_TO_SYSTEM_DEBUG_LOG = 1 << 1, |
153 | 149 |
154 LOG_TO_ALL = LOG_TO_FILE | LOG_TO_SYSTEM_DEBUG_LOG, | 150 LOG_TO_ALL = LOG_TO_FILE | LOG_TO_SYSTEM_DEBUG_LOG, |
155 | 151 |
156 // On Windows, use a file next to the exe; on POSIX platforms, where | 152 // On POSIX platforms, where it may not even be possible to locate the |
157 // it may not even be possible to locate the executable on disk, use | 153 // executable on disk, use stderr. |
158 // stderr. | 154 #if defined(OS_POSIX) |
159 #if defined(OS_WIN) | |
160 LOG_DEFAULT = LOG_TO_FILE, | |
161 #elif defined(OS_POSIX) | |
162 LOG_DEFAULT = LOG_TO_SYSTEM_DEBUG_LOG, | 155 LOG_DEFAULT = LOG_TO_SYSTEM_DEBUG_LOG, |
163 #endif | 156 #endif |
164 }; | 157 }; |
165 | 158 |
166 // Indicates that the log file should be locked when being written to. | 159 // Indicates that the log file should be locked when being written to. |
167 // Unless there is only one single-threaded process that is logging to | 160 // Unless there is only one single-threaded process that is logging to |
168 // the log file, the file should be locked during writes to make each | 161 // the log file, the file should be locked during writes to make each |
169 // log output atomic. Other writers will block. | 162 // log output atomic. Other writers will block. |
170 // | 163 // |
171 // All processes writing to the log file must have their locking set for it to | 164 // All processes writing to the log file must have their locking set for it to |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 COMPACT_GOOGLE_LOG_EX_INFO(LogMessage) | 308 COMPACT_GOOGLE_LOG_EX_INFO(LogMessage) |
316 #define COMPACT_GOOGLE_LOG_WARNING \ | 309 #define COMPACT_GOOGLE_LOG_WARNING \ |
317 COMPACT_GOOGLE_LOG_EX_WARNING(LogMessage) | 310 COMPACT_GOOGLE_LOG_EX_WARNING(LogMessage) |
318 #define COMPACT_GOOGLE_LOG_ERROR \ | 311 #define COMPACT_GOOGLE_LOG_ERROR \ |
319 COMPACT_GOOGLE_LOG_EX_ERROR(LogMessage) | 312 COMPACT_GOOGLE_LOG_EX_ERROR(LogMessage) |
320 #define COMPACT_GOOGLE_LOG_FATAL \ | 313 #define COMPACT_GOOGLE_LOG_FATAL \ |
321 COMPACT_GOOGLE_LOG_EX_FATAL(LogMessage) | 314 COMPACT_GOOGLE_LOG_EX_FATAL(LogMessage) |
322 #define COMPACT_GOOGLE_LOG_DFATAL \ | 315 #define COMPACT_GOOGLE_LOG_DFATAL \ |
323 COMPACT_GOOGLE_LOG_EX_DFATAL(LogMessage) | 316 COMPACT_GOOGLE_LOG_EX_DFATAL(LogMessage) |
324 | 317 |
325 #if defined(OS_WIN) | |
326 // wingdi.h defines ERROR to be 0. When we call LOG(ERROR), it gets | |
327 // substituted with 0, and it expands to COMPACT_GOOGLE_LOG_0. To allow us | |
328 // to keep using this syntax, we define this macro to do the same thing | |
329 // as COMPACT_GOOGLE_LOG_ERROR, and also define ERROR the same way that | |
330 // the Windows SDK does for consistency. | |
331 #define ERROR 0 | |
332 #define COMPACT_GOOGLE_LOG_EX_0(ClassName, ...) \ | |
333 COMPACT_GOOGLE_LOG_EX_ERROR(ClassName , ##__VA_ARGS__) | |
334 #define COMPACT_GOOGLE_LOG_0 COMPACT_GOOGLE_LOG_ERROR | |
335 // Needed for LOG_IS_ON(ERROR). | |
336 const LogSeverity LOG_0 = LOG_ERROR; | |
337 #endif | |
338 | |
339 // As special cases, we can assume that LOG_IS_ON(FATAL) always holds. Also, | 318 // As special cases, we can assume that LOG_IS_ON(FATAL) always holds. Also, |
340 // LOG_IS_ON(DFATAL) always holds in debug mode. In particular, CHECK()s will | 319 // LOG_IS_ON(DFATAL) always holds in debug mode. In particular, CHECK()s will |
341 // always fire if they fail. | 320 // always fire if they fail. |
342 #define LOG_IS_ON(severity) \ | 321 #define LOG_IS_ON(severity) \ |
343 ((::logging::LOG_ ## severity) >= ::logging::GetMinLogLevel()) | 322 ((::logging::LOG_ ## severity) >= ::logging::GetMinLogLevel()) |
344 | 323 |
345 // We can't do any caching tricks with VLOG_IS_ON() like the | 324 // We can't do any caching tricks with VLOG_IS_ON() like the |
346 // google-glog version since it requires GCC extensions. This means | 325 // google-glog version since it requires GCC extensions. This means |
347 // that using the v-logging functions in conjunction with --vmodule | 326 // that using the v-logging functions in conjunction with --vmodule |
348 // may be slow. | 327 // may be slow. |
(...skipping 26 matching lines...) Expand all Loading... |
375 #define VLOG_STREAM(verbose_level) \ | 354 #define VLOG_STREAM(verbose_level) \ |
376 logging::LogMessage(__FILE__, __LINE__, -verbose_level).stream() | 355 logging::LogMessage(__FILE__, __LINE__, -verbose_level).stream() |
377 | 356 |
378 #define VLOG(verbose_level) \ | 357 #define VLOG(verbose_level) \ |
379 LAZY_STREAM(VLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level)) | 358 LAZY_STREAM(VLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level)) |
380 | 359 |
381 #define VLOG_IF(verbose_level, condition) \ | 360 #define VLOG_IF(verbose_level, condition) \ |
382 LAZY_STREAM(VLOG_STREAM(verbose_level), \ | 361 LAZY_STREAM(VLOG_STREAM(verbose_level), \ |
383 VLOG_IS_ON(verbose_level) && (condition)) | 362 VLOG_IS_ON(verbose_level) && (condition)) |
384 | 363 |
385 #if defined (OS_WIN) | 364 #if defined(OS_POSIX) |
386 #define VPLOG_STREAM(verbose_level) \ | |
387 logging::Win32ErrorLogMessage(__FILE__, __LINE__, -verbose_level, \ | |
388 ::logging::GetLastSystemErrorCode()).stream() | |
389 #elif defined(OS_POSIX) | |
390 #define VPLOG_STREAM(verbose_level) \ | 365 #define VPLOG_STREAM(verbose_level) \ |
391 logging::ErrnoLogMessage(__FILE__, __LINE__, -verbose_level, \ | 366 logging::ErrnoLogMessage(__FILE__, __LINE__, -verbose_level, \ |
392 ::logging::GetLastSystemErrorCode()).stream() | 367 ::logging::GetLastSystemErrorCode()).stream() |
393 #endif | 368 #endif |
394 | 369 |
395 #define VPLOG(verbose_level) \ | 370 #define VPLOG(verbose_level) \ |
396 LAZY_STREAM(VPLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level)) | 371 LAZY_STREAM(VPLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level)) |
397 | 372 |
398 #define VPLOG_IF(verbose_level, condition) \ | 373 #define VPLOG_IF(verbose_level, condition) \ |
399 LAZY_STREAM(VPLOG_STREAM(verbose_level), \ | 374 LAZY_STREAM(VPLOG_STREAM(verbose_level), \ |
400 VLOG_IS_ON(verbose_level) && (condition)) | 375 VLOG_IS_ON(verbose_level) && (condition)) |
401 | 376 |
402 // TODO(akalin): Add more VLOG variants, e.g. VPLOG. | 377 // TODO(akalin): Add more VLOG variants, e.g. VPLOG. |
403 | 378 |
404 #define LOG_ASSERT(condition) \ | 379 #define LOG_ASSERT(condition) \ |
405 LOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". " | 380 LOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". " |
406 #define SYSLOG_ASSERT(condition) \ | 381 #define SYSLOG_ASSERT(condition) \ |
407 SYSLOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". " | 382 SYSLOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". " |
408 | 383 |
409 #if defined(OS_WIN) | 384 #if defined(OS_POSIX) |
410 #define PLOG_STREAM(severity) \ | |
411 COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \ | |
412 ::logging::GetLastSystemErrorCode()).stream() | |
413 #elif defined(OS_POSIX) | |
414 #define PLOG_STREAM(severity) \ | 385 #define PLOG_STREAM(severity) \ |
415 COMPACT_GOOGLE_LOG_EX_ ## severity(ErrnoLogMessage, \ | 386 COMPACT_GOOGLE_LOG_EX_ ## severity(ErrnoLogMessage, \ |
416 ::logging::GetLastSystemErrorCode()).stream() | 387 ::logging::GetLastSystemErrorCode()).stream() |
417 #endif | 388 #endif |
418 | 389 |
419 #define PLOG(severity) \ | 390 #define PLOG(severity) \ |
420 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity)) | 391 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity)) |
421 | 392 |
422 #define PLOG_IF(severity, condition) \ | 393 #define PLOG_IF(severity, condition) \ |
423 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity) && (condition)) | 394 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity) && (condition)) |
(...skipping 19 matching lines...) Expand all Loading... |
443 // into a macro (like __debugbreak() on Windows). | 414 // into a macro (like __debugbreak() on Windows). |
444 #define CHECK(condition) \ | 415 #define CHECK(condition) \ |
445 !(condition) ? ::base::debug::BreakDebugger() : EAT_STREAM_PARAMETERS | 416 !(condition) ? ::base::debug::BreakDebugger() : EAT_STREAM_PARAMETERS |
446 | 417 |
447 #define PCHECK(condition) CHECK(condition) | 418 #define PCHECK(condition) CHECK(condition) |
448 | 419 |
449 #define CHECK_OP(name, op, val1, val2) CHECK((val1) op (val2)) | 420 #define CHECK_OP(name, op, val1, val2) CHECK((val1) op (val2)) |
450 | 421 |
451 #else | 422 #else |
452 | 423 |
453 #if defined(_PREFAST_) && defined(OS_WIN) | |
454 // Use __analysis_assume to tell the VC++ static analysis engine that | |
455 // assert conditions are true, to suppress warnings. The LAZY_STREAM | |
456 // parameter doesn't reference 'condition' in /analyze builds because | |
457 // this evaluation confuses /analyze. The !! before condition is because | |
458 // __analysis_assume gets confused on some conditions: | |
459 // http://randomascii.wordpress.com/2011/09/13/analyze-for-visual-studio-the-ugl
y-part-5/ | |
460 | |
461 #define CHECK(condition) \ | |
462 __analysis_assume(!!(condition)), \ | |
463 LAZY_STREAM(LOG_STREAM(FATAL), false) \ | |
464 << "Check failed: " #condition ". " | |
465 | |
466 #define PCHECK(condition) \ | |
467 __analysis_assume(!!(condition)), \ | |
468 LAZY_STREAM(PLOG_STREAM(FATAL), false) \ | |
469 << "Check failed: " #condition ". " | |
470 | |
471 #else // _PREFAST_ | |
472 | |
473 #define CHECK(condition) \ | 424 #define CHECK(condition) \ |
474 LAZY_STREAM(LOG_STREAM(FATAL), !(condition)) \ | 425 LAZY_STREAM(LOG_STREAM(FATAL), !(condition)) \ |
475 << "Check failed: " #condition ". " | 426 << "Check failed: " #condition ". " |
476 | 427 |
477 #define PCHECK(condition) \ | 428 #define PCHECK(condition) \ |
478 LAZY_STREAM(PLOG_STREAM(FATAL), !(condition)) \ | 429 LAZY_STREAM(PLOG_STREAM(FATAL), !(condition)) \ |
479 << "Check failed: " #condition ". " | 430 << "Check failed: " #condition ". " |
480 | 431 |
481 #endif // _PREFAST_ | |
482 | |
483 // Helper macro for binary operators. | 432 // Helper macro for binary operators. |
484 // Don't use this macro directly in your code, use CHECK_EQ et al below. | 433 // Don't use this macro directly in your code, use CHECK_EQ et al below. |
485 // | 434 // |
486 // TODO(akalin): Rewrite this so that constructs like if (...) | 435 // TODO(akalin): Rewrite this so that constructs like if (...) |
487 // CHECK_EQ(...) else { ... } work properly. | 436 // CHECK_EQ(...) else { ... } work properly. |
488 #define CHECK_OP(name, op, val1, val2) \ | 437 #define CHECK_OP(name, op, val1, val2) \ |
489 if (std::string* _result = \ | 438 if (std::string* _result = \ |
490 logging::Check##name##Impl((val1), (val2), \ | 439 logging::Check##name##Impl((val1), (val2), \ |
491 #val1 " " #op " " #val2)) \ | 440 #val1 " " #op " " #val2)) \ |
492 logging::LogMessage(__FILE__, __LINE__, _result).stream() | 441 logging::LogMessage(__FILE__, __LINE__, _result).stream() |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
631 #define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_INFO | 580 #define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_INFO |
632 const LogSeverity LOG_DCHECK = LOG_INFO; | 581 const LogSeverity LOG_DCHECK = LOG_INFO; |
633 | 582 |
634 #endif // DCHECK_IS_ON() | 583 #endif // DCHECK_IS_ON() |
635 | 584 |
636 // DCHECK et al. make sure to reference |condition| regardless of | 585 // DCHECK et al. make sure to reference |condition| regardless of |
637 // whether DCHECKs are enabled; this is so that we don't get unused | 586 // whether DCHECKs are enabled; this is so that we don't get unused |
638 // variable warnings if the only use of a variable is in a DCHECK. | 587 // variable warnings if the only use of a variable is in a DCHECK. |
639 // This behavior is different from DLOG_IF et al. | 588 // This behavior is different from DLOG_IF et al. |
640 | 589 |
641 #if defined(_PREFAST_) && defined(OS_WIN) | |
642 // See comments on the previous use of __analysis_assume. | |
643 | |
644 #define DCHECK(condition) \ | |
645 __analysis_assume(!!(condition)), \ | |
646 LAZY_STREAM(LOG_STREAM(DCHECK), false) \ | |
647 << "Check failed: " #condition ". " | |
648 | |
649 #define DPCHECK(condition) \ | |
650 __analysis_assume(!!(condition)), \ | |
651 LAZY_STREAM(PLOG_STREAM(DCHECK), false) \ | |
652 << "Check failed: " #condition ". " | |
653 | |
654 #else // _PREFAST_ | |
655 | |
656 #define DCHECK(condition) \ | 590 #define DCHECK(condition) \ |
657 LAZY_STREAM(LOG_STREAM(DCHECK), DCHECK_IS_ON() ? !(condition) : false) \ | 591 LAZY_STREAM(LOG_STREAM(DCHECK), DCHECK_IS_ON() ? !(condition) : false) \ |
658 << "Check failed: " #condition ". " | 592 << "Check failed: " #condition ". " |
659 | 593 |
660 #define DPCHECK(condition) \ | 594 #define DPCHECK(condition) \ |
661 LAZY_STREAM(PLOG_STREAM(DCHECK), DCHECK_IS_ON() ? !(condition) : false) \ | 595 LAZY_STREAM(PLOG_STREAM(DCHECK), DCHECK_IS_ON() ? !(condition) : false) \ |
662 << "Check failed: " #condition ". " | 596 << "Check failed: " #condition ". " |
663 | 597 |
664 #endif // _PREFAST_ | |
665 | |
666 // Helper macro for binary operators. | 598 // Helper macro for binary operators. |
667 // Don't use this macro directly in your code, use DCHECK_EQ et al below. | 599 // Don't use this macro directly in your code, use DCHECK_EQ et al below. |
668 #define DCHECK_OP(name, op, val1, val2) \ | 600 #define DCHECK_OP(name, op, val1, val2) \ |
669 if (DCHECK_IS_ON()) \ | 601 if (DCHECK_IS_ON()) \ |
670 if (std::string* _result = logging::Check##name##Impl( \ | 602 if (std::string* _result = logging::Check##name##Impl( \ |
671 (val1), (val2), #val1 " " #op " " #val2)) \ | 603 (val1), (val2), #val1 " " #op " " #val2)) \ |
672 logging::LogMessage(__FILE__, __LINE__, ::logging::LOG_DCHECK, _result) \ | 604 logging::LogMessage(__FILE__, __LINE__, ::logging::LOG_DCHECK, _result) \ |
673 .stream() | 605 .stream() |
674 | 606 |
675 // Equality/Inequality checks - compare two values, and log a | 607 // Equality/Inequality checks - compare two values, and log a |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
743 void Init(const char* file, int line); | 675 void Init(const char* file, int line); |
744 | 676 |
745 LogSeverity severity_; | 677 LogSeverity severity_; |
746 std::ostringstream stream_; | 678 std::ostringstream stream_; |
747 size_t message_start_; // Offset of the start of the message (past prefix | 679 size_t message_start_; // Offset of the start of the message (past prefix |
748 // info). | 680 // info). |
749 // The file and line information passed in to the constructor. | 681 // The file and line information passed in to the constructor. |
750 const char* file_; | 682 const char* file_; |
751 const int line_; | 683 const int line_; |
752 | 684 |
753 #if defined(OS_WIN) | |
754 // Stores the current value of GetLastError in the constructor and restores | |
755 // it in the destructor by calling SetLastError. | |
756 // This is useful since the LogMessage class uses a lot of Win32 calls | |
757 // that will lose the value of GLE and the code that called the log function | |
758 // will have lost the thread error value when the log call returns. | |
759 class SaveLastError { | |
760 public: | |
761 SaveLastError(); | |
762 ~SaveLastError(); | |
763 | |
764 unsigned long get_error() const { return last_error_; } | |
765 | |
766 protected: | |
767 unsigned long last_error_; | |
768 }; | |
769 | |
770 SaveLastError last_error_; | |
771 #endif | |
772 | |
773 DISALLOW_COPY_AND_ASSIGN(LogMessage); | 685 DISALLOW_COPY_AND_ASSIGN(LogMessage); |
774 }; | 686 }; |
775 | 687 |
776 // A non-macro interface to the log facility; (useful | 688 // A non-macro interface to the log facility; (useful |
777 // when the logging level is not a compile-time constant). | 689 // when the logging level is not a compile-time constant). |
778 inline void LogAtLevel(int const log_level, std::string const &msg) { | 690 inline void LogAtLevel(int const log_level, std::string const &msg) { |
779 LogMessage(__FILE__, __LINE__, log_level).stream() << msg; | 691 LogMessage(__FILE__, __LINE__, log_level).stream() << msg; |
780 } | 692 } |
781 | 693 |
782 // This class is used to explicitly ignore values in the conditional | 694 // This class is used to explicitly ignore values in the conditional |
783 // logging macros. This avoids compiler warnings like "value computed | 695 // logging macros. This avoids compiler warnings like "value computed |
784 // is not used" and "statement has no effect". | 696 // is not used" and "statement has no effect". |
785 class LogMessageVoidify { | 697 class LogMessageVoidify { |
786 public: | 698 public: |
787 LogMessageVoidify() { } | 699 LogMessageVoidify() { } |
788 // This has to be an operator with a precedence lower than << but | 700 // This has to be an operator with a precedence lower than << but |
789 // higher than ?: | 701 // higher than ?: |
790 void operator&(std::ostream&) { } | 702 void operator&(std::ostream&) { } |
791 }; | 703 }; |
792 | 704 |
793 #if defined(OS_WIN) | 705 #if defined(OS_POSIX) |
794 typedef unsigned long SystemErrorCode; | |
795 #elif defined(OS_POSIX) | |
796 typedef int SystemErrorCode; | 706 typedef int SystemErrorCode; |
797 #endif | 707 #endif |
798 | 708 |
799 // Alias for ::GetLastError() on Windows and errno on POSIX. Avoids having to | 709 // Alias for ::GetLastError() on Windows and errno on POSIX. Avoids having to |
800 // pull in windows.h just for GetLastError() and DWORD. | 710 // pull in windows.h just for GetLastError() and DWORD. |
801 BASE_EXPORT SystemErrorCode GetLastSystemErrorCode(); | 711 BASE_EXPORT SystemErrorCode GetLastSystemErrorCode(); |
802 BASE_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code); | 712 BASE_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code); |
803 | 713 |
804 #if defined(OS_WIN) | 714 #if defined(OS_POSIX) |
805 // Appends a formatted system message of the GetLastError() type. | |
806 class BASE_EXPORT Win32ErrorLogMessage { | |
807 public: | |
808 Win32ErrorLogMessage(const char* file, | |
809 int line, | |
810 LogSeverity severity, | |
811 SystemErrorCode err); | |
812 | |
813 // Appends the error message before destructing the encapsulated class. | |
814 ~Win32ErrorLogMessage(); | |
815 | |
816 std::ostream& stream() { return log_message_.stream(); } | |
817 | |
818 private: | |
819 SystemErrorCode err_; | |
820 LogMessage log_message_; | |
821 | |
822 DISALLOW_COPY_AND_ASSIGN(Win32ErrorLogMessage); | |
823 }; | |
824 #elif defined(OS_POSIX) | |
825 // Appends a formatted system message of the errno type | 715 // Appends a formatted system message of the errno type |
826 class BASE_EXPORT ErrnoLogMessage { | 716 class BASE_EXPORT ErrnoLogMessage { |
827 public: | 717 public: |
828 ErrnoLogMessage(const char* file, | 718 ErrnoLogMessage(const char* file, |
829 int line, | 719 int line, |
830 LogSeverity severity, | 720 LogSeverity severity, |
831 SystemErrorCode err); | 721 SystemErrorCode err); |
832 | 722 |
833 // Appends the error message before destructing the encapsulated class. | 723 // Appends the error message before destructing the encapsulated class. |
834 ~ErrnoLogMessage(); | 724 ~ErrnoLogMessage(); |
835 | 725 |
836 std::ostream& stream() { return log_message_.stream(); } | 726 std::ostream& stream() { return log_message_.stream(); } |
837 | 727 |
838 private: | 728 private: |
839 SystemErrorCode err_; | 729 SystemErrorCode err_; |
840 LogMessage log_message_; | 730 LogMessage log_message_; |
841 | 731 |
842 DISALLOW_COPY_AND_ASSIGN(ErrnoLogMessage); | 732 DISALLOW_COPY_AND_ASSIGN(ErrnoLogMessage); |
843 }; | 733 }; |
844 #endif // OS_WIN | 734 #endif // OS_POSIX |
845 | 735 |
846 // Closes the log file explicitly if open. | 736 // Closes the log file explicitly if open. |
847 // NOTE: Since the log file is opened as necessary by the action of logging | 737 // NOTE: Since the log file is opened as necessary by the action of logging |
848 // statements, there's no guarantee that it will stay closed | 738 // statements, there's no guarantee that it will stay closed |
849 // after this call. | 739 // after this call. |
850 BASE_EXPORT void CloseLogFile(); | 740 BASE_EXPORT void CloseLogFile(); |
851 | 741 |
852 // Async signal safe logging mechanism. | 742 // Async signal safe logging mechanism. |
853 BASE_EXPORT void RawLog(int level, const char* message); | 743 BASE_EXPORT void RawLog(int level, const char* message); |
854 | 744 |
855 #define RAW_LOG(level, message) logging::RawLog(logging::LOG_ ## level, message) | 745 #define RAW_LOG(level, message) logging::RawLog(logging::LOG_ ## level, message) |
856 | 746 |
857 #define RAW_CHECK(condition) \ | 747 #define RAW_CHECK(condition) \ |
858 do { \ | 748 do { \ |
859 if (!(condition)) \ | 749 if (!(condition)) \ |
860 logging::RawLog(logging::LOG_FATAL, "Check failed: " #condition "\n"); \ | 750 logging::RawLog(logging::LOG_FATAL, "Check failed: " #condition "\n"); \ |
861 } while (0) | 751 } while (0) |
862 | 752 |
863 #if defined(OS_WIN) | |
864 // Returns the default log file path. | |
865 BASE_EXPORT std::wstring GetLogFileFullPath(); | |
866 #endif | |
867 | |
868 } // namespace logging | 753 } // namespace logging |
869 | 754 |
870 // Note that "The behavior of a C++ program is undefined if it adds declarations | 755 // Note that "The behavior of a C++ program is undefined if it adds declarations |
871 // or definitions to namespace std or to a namespace within namespace std unless | 756 // or definitions to namespace std or to a namespace within namespace std unless |
872 // otherwise specified." --C++11[namespace.std] | 757 // otherwise specified." --C++11[namespace.std] |
873 // | 758 // |
874 // We've checked that this particular definition has the intended behavior on | 759 // We've checked that this particular definition has the intended behavior on |
875 // our implementations, but it's prone to breaking in the future, and please | 760 // our implementations, but it's prone to breaking in the future, and please |
876 // don't imitate this in your own definitions without checking with some | 761 // don't imitate this in your own definitions without checking with some |
877 // standard library experts. | 762 // standard library experts. |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
930 #elif NOTIMPLEMENTED_POLICY == 5 | 815 #elif NOTIMPLEMENTED_POLICY == 5 |
931 #define NOTIMPLEMENTED() do {\ | 816 #define NOTIMPLEMENTED() do {\ |
932 static bool logged_once = false;\ | 817 static bool logged_once = false;\ |
933 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ | 818 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ |
934 logged_once = true;\ | 819 logged_once = true;\ |
935 } while(0);\ | 820 } while(0);\ |
936 EAT_STREAM_PARAMETERS | 821 EAT_STREAM_PARAMETERS |
937 #endif | 822 #endif |
938 | 823 |
939 #endif // BASE_LOGGING_H_ | 824 #endif // BASE_LOGGING_H_ |
OLD | NEW |