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 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 // log output atomic. Other writers will block. | 174 // log output atomic. Other writers will block. |
175 // | 175 // |
176 // All processes writing to the log file must have their locking set for it to | 176 // All processes writing to the log file must have their locking set for it to |
177 // work properly. Defaults to LOCK_LOG_FILE. | 177 // work properly. Defaults to LOCK_LOG_FILE. |
178 enum LogLockingState { LOCK_LOG_FILE, DONT_LOCK_LOG_FILE }; | 178 enum LogLockingState { LOCK_LOG_FILE, DONT_LOCK_LOG_FILE }; |
179 | 179 |
180 // On startup, should we delete or append to an existing log file (if any)? | 180 // On startup, should we delete or append to an existing log file (if any)? |
181 // Defaults to APPEND_TO_OLD_LOG_FILE. | 181 // Defaults to APPEND_TO_OLD_LOG_FILE. |
182 enum OldFileDeletionState { DELETE_OLD_LOG_FILE, APPEND_TO_OLD_LOG_FILE }; | 182 enum OldFileDeletionState { DELETE_OLD_LOG_FILE, APPEND_TO_OLD_LOG_FILE }; |
183 | 183 |
184 enum DcheckState { | |
185 DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS, | |
186 ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS | |
187 }; | |
188 | |
189 struct BASE_EXPORT LoggingSettings { | 184 struct BASE_EXPORT LoggingSettings { |
190 // The defaults values are: | 185 // The defaults values are: |
191 // | 186 // |
192 // logging_dest: LOG_DEFAULT | 187 // logging_dest: LOG_DEFAULT |
193 // log_file: NULL | 188 // log_file: NULL |
194 // lock_log: LOCK_LOG_FILE | 189 // lock_log: LOCK_LOG_FILE |
195 // delete_old: APPEND_TO_OLD_LOG_FILE | 190 // delete_old: APPEND_TO_OLD_LOG_FILE |
196 // dcheck_state: DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS | |
197 LoggingSettings(); | 191 LoggingSettings(); |
198 | 192 |
199 LoggingDestination logging_dest; | 193 LoggingDestination logging_dest; |
200 | 194 |
201 // The three settings below have an effect only when LOG_TO_FILE is | 195 // The three settings below have an effect only when LOG_TO_FILE is |
202 // set in |logging_dest|. | 196 // set in |logging_dest|. |
203 const PathChar* log_file; | 197 const PathChar* log_file; |
204 LogLockingState lock_log; | 198 LogLockingState lock_log; |
205 OldFileDeletionState delete_old; | 199 OldFileDeletionState delete_old; |
206 | |
207 DcheckState dcheck_state; | |
208 }; | 200 }; |
209 | 201 |
210 // Define different names for the BaseInitLoggingImpl() function depending on | 202 // Define different names for the BaseInitLoggingImpl() function depending on |
211 // whether NDEBUG is defined or not so that we'll fail to link if someone tries | 203 // whether NDEBUG is defined or not so that we'll fail to link if someone tries |
212 // to compile logging.cc with NDEBUG but includes logging.h without defining it, | 204 // to compile logging.cc with NDEBUG but includes logging.h without defining it, |
213 // or vice versa. | 205 // or vice versa. |
214 #if NDEBUG | 206 #if NDEBUG |
215 #define BaseInitLoggingImpl BaseInitLoggingImpl_built_with_NDEBUG | 207 #define BaseInitLoggingImpl BaseInitLoggingImpl_built_with_NDEBUG |
216 #else | 208 #else |
217 #define BaseInitLoggingImpl BaseInitLoggingImpl_built_without_NDEBUG | 209 #define BaseInitLoggingImpl BaseInitLoggingImpl_built_without_NDEBUG |
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
458 // for each platform. | 450 // for each platform. |
459 #define PLOG_STREAM(severity) LOG_ERRNO_STREAM(severity) | 451 #define PLOG_STREAM(severity) LOG_ERRNO_STREAM(severity) |
460 #endif | 452 #endif |
461 | 453 |
462 #define PLOG(severity) \ | 454 #define PLOG(severity) \ |
463 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity)) | 455 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity)) |
464 | 456 |
465 #define PLOG_IF(severity, condition) \ | 457 #define PLOG_IF(severity, condition) \ |
466 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity) && (condition)) | 458 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity) && (condition)) |
467 | 459 |
468 #if !defined(NDEBUG) | |
469 // Debug builds always include DCHECK and DLOG. | |
470 #undef LOGGING_IS_OFFICIAL_BUILD | |
471 #define LOGGING_IS_OFFICIAL_BUILD 0 | |
472 #elif defined(OFFICIAL_BUILD) | |
473 // Official release builds always disable and remove DCHECK and DLOG. | |
474 #undef LOGGING_IS_OFFICIAL_BUILD | |
475 #define LOGGING_IS_OFFICIAL_BUILD 1 | |
476 #elif !defined(LOGGING_IS_OFFICIAL_BUILD) | |
477 // Unless otherwise specified, unofficial release builds include | |
478 // DCHECK and DLOG. | |
479 #define LOGGING_IS_OFFICIAL_BUILD 0 | |
480 #endif | |
481 | |
482 // The actual stream used isn't important. | 460 // The actual stream used isn't important. |
483 #define EAT_STREAM_PARAMETERS \ | 461 #define EAT_STREAM_PARAMETERS \ |
484 true ? (void) 0 : ::logging::LogMessageVoidify() & LOG_STREAM(FATAL) | 462 true ? (void) 0 : ::logging::LogMessageVoidify() & LOG_STREAM(FATAL) |
485 | 463 |
486 // CHECK dies with a fatal error if condition is not true. It is *not* | 464 // CHECK dies with a fatal error if condition is not true. It is *not* |
487 // controlled by NDEBUG, so the check will be executed regardless of | 465 // controlled by NDEBUG, so the check will be executed regardless of |
488 // compilation mode. | 466 // compilation mode. |
489 // | 467 // |
490 // We make sure CHECK et al. always evaluates their arguments, as | 468 // We make sure CHECK et al. always evaluates their arguments, as |
491 // doing CHECK(FunctionWithSideEffect()) is a common idiom. | 469 // doing CHECK(FunctionWithSideEffect()) is a common idiom. |
492 | 470 |
493 #if LOGGING_IS_OFFICIAL_BUILD | 471 #if defined(OFFICIAL_BUILD) && defined(NDEBUG) |
494 | 472 |
495 // Make all CHECK functions discard their log strings to reduce code | 473 // Make all CHECK functions discard their log strings to reduce code |
496 // bloat for official builds. | 474 // bloat for official release builds. |
497 | 475 |
498 // TODO(akalin): This would be more valuable if there were some way to | 476 // TODO(akalin): This would be more valuable if there were some way to |
499 // remove BreakDebugger() from the backtrace, perhaps by turning it | 477 // remove BreakDebugger() from the backtrace, perhaps by turning it |
500 // into a macro (like __debugbreak() on Windows). | 478 // into a macro (like __debugbreak() on Windows). |
501 #define CHECK(condition) \ | 479 #define CHECK(condition) \ |
502 !(condition) ? ::base::debug::BreakDebugger() : EAT_STREAM_PARAMETERS | 480 !(condition) ? ::base::debug::BreakDebugger() : EAT_STREAM_PARAMETERS |
503 | 481 |
504 #define PCHECK(condition) CHECK(condition) | 482 #define PCHECK(condition) CHECK(condition) |
505 | 483 |
506 #define CHECK_OP(name, op, val1, val2) CHECK((val1) op (val2)) | 484 #define CHECK_OP(name, op, val1, val2) CHECK((val1) op (val2)) |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
583 DEFINE_CHECK_OP_IMPL(GT, > ) | 561 DEFINE_CHECK_OP_IMPL(GT, > ) |
584 #undef DEFINE_CHECK_OP_IMPL | 562 #undef DEFINE_CHECK_OP_IMPL |
585 | 563 |
586 #define CHECK_EQ(val1, val2) CHECK_OP(EQ, ==, val1, val2) | 564 #define CHECK_EQ(val1, val2) CHECK_OP(EQ, ==, val1, val2) |
587 #define CHECK_NE(val1, val2) CHECK_OP(NE, !=, val1, val2) | 565 #define CHECK_NE(val1, val2) CHECK_OP(NE, !=, val1, val2) |
588 #define CHECK_LE(val1, val2) CHECK_OP(LE, <=, val1, val2) | 566 #define CHECK_LE(val1, val2) CHECK_OP(LE, <=, val1, val2) |
589 #define CHECK_LT(val1, val2) CHECK_OP(LT, < , val1, val2) | 567 #define CHECK_LT(val1, val2) CHECK_OP(LT, < , val1, val2) |
590 #define CHECK_GE(val1, val2) CHECK_OP(GE, >=, val1, val2) | 568 #define CHECK_GE(val1, val2) CHECK_OP(GE, >=, val1, val2) |
591 #define CHECK_GT(val1, val2) CHECK_OP(GT, > , val1, val2) | 569 #define CHECK_GT(val1, val2) CHECK_OP(GT, > , val1, val2) |
592 | 570 |
593 #if LOGGING_IS_OFFICIAL_BUILD | 571 #if defined(NDEBUG) |
594 // In order to have optimized code for official builds, remove DLOGs and | |
595 // DCHECKs. | |
596 #define ENABLE_DLOG 0 | 572 #define ENABLE_DLOG 0 |
| 573 #else |
| 574 #define ENABLE_DLOG 1 |
| 575 #endif |
| 576 |
| 577 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
597 #define ENABLE_DCHECK 0 | 578 #define ENABLE_DCHECK 0 |
598 | |
599 #elif defined(NDEBUG) | |
600 // Otherwise, if we're a release build, remove DLOGs but not DCHECKs | |
601 // (since those can still be turned on via a command-line flag). | |
602 #define ENABLE_DLOG 0 | |
603 #define ENABLE_DCHECK 1 | |
604 | |
605 #else | 579 #else |
606 // Otherwise, we're a debug build so enable DLOGs and DCHECKs. | |
607 #define ENABLE_DLOG 1 | |
608 #define ENABLE_DCHECK 1 | 580 #define ENABLE_DCHECK 1 |
609 #endif | 581 #endif |
610 | 582 |
611 // Definitions for DLOG et al. | 583 // Definitions for DLOG et al. |
612 | 584 |
613 #if ENABLE_DLOG | 585 #if ENABLE_DLOG |
614 | 586 |
615 #define DLOG_IS_ON(severity) LOG_IS_ON(severity) | 587 #define DLOG_IS_ON(severity) LOG_IS_ON(severity) |
616 #define DLOG_IF(severity, condition) LOG_IF(severity, condition) | 588 #define DLOG_IF(severity, condition) LOG_IF(severity, condition) |
617 #define DLOG_ASSERT(condition) LOG_ASSERT(condition) | 589 #define DLOG_ASSERT(condition) LOG_ASSERT(condition) |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
665 LAZY_STREAM(PLOG_STREAM(severity), DLOG_IS_ON(severity)) | 637 LAZY_STREAM(PLOG_STREAM(severity), DLOG_IS_ON(severity)) |
666 | 638 |
667 #define DVLOG(verboselevel) DVLOG_IF(verboselevel, VLOG_IS_ON(verboselevel)) | 639 #define DVLOG(verboselevel) DVLOG_IF(verboselevel, VLOG_IS_ON(verboselevel)) |
668 | 640 |
669 #define DVPLOG(verboselevel) DVPLOG_IF(verboselevel, VLOG_IS_ON(verboselevel)) | 641 #define DVPLOG(verboselevel) DVPLOG_IF(verboselevel, VLOG_IS_ON(verboselevel)) |
670 | 642 |
671 // Definitions for DCHECK et al. | 643 // Definitions for DCHECK et al. |
672 | 644 |
673 #if ENABLE_DCHECK | 645 #if ENABLE_DCHECK |
674 | 646 |
675 #if defined(NDEBUG) | |
676 | |
677 BASE_EXPORT extern DcheckState g_dcheck_state; | |
678 BASE_EXPORT void set_dcheck_state(DcheckState state); | |
679 | |
680 #if defined(DCHECK_ALWAYS_ON) | |
681 | |
682 #define DCHECK_IS_ON() true | |
683 #define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \ | |
684 COMPACT_GOOGLE_LOG_EX_FATAL(ClassName , ##__VA_ARGS__) | |
685 #define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_FATAL | |
686 const LogSeverity LOG_DCHECK = LOG_FATAL; | |
687 | |
688 #else | |
689 | |
690 #define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \ | |
691 COMPACT_GOOGLE_LOG_EX_ERROR_REPORT(ClassName , ##__VA_ARGS__) | |
692 #define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_ERROR_REPORT | |
693 const LogSeverity LOG_DCHECK = LOG_ERROR_REPORT; | |
694 | |
695 #define DCHECK_IS_ON() \ | |
696 UNLIKELY(::logging::g_dcheck_state == \ | |
697 ::logging::ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS) && \ | |
698 LOG_IS_ON(DCHECK) | |
699 | |
700 #endif // defined(DCHECK_ALWAYS_ON) | |
701 | |
702 #else // defined(NDEBUG) | |
703 | |
704 // On a regular debug build, we want to have DCHECKs enabled. | |
705 #define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \ | 647 #define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \ |
706 COMPACT_GOOGLE_LOG_EX_FATAL(ClassName , ##__VA_ARGS__) | 648 COMPACT_GOOGLE_LOG_EX_FATAL(ClassName , ##__VA_ARGS__) |
707 #define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_FATAL | 649 #define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_FATAL |
708 const LogSeverity LOG_DCHECK = LOG_FATAL; | 650 const LogSeverity LOG_DCHECK = LOG_FATAL; |
709 #define DCHECK_IS_ON() true | 651 #define DCHECK_IS_ON() true |
710 | 652 |
711 #endif // defined(NDEBUG) | |
712 | |
713 #else // ENABLE_DCHECK | 653 #else // ENABLE_DCHECK |
714 | 654 |
715 // These are just dummy values since DCHECK_IS_ON() is always false in | 655 // These are just dummy values since DCHECK_IS_ON() is always false in |
716 // this case. | 656 // this case. |
717 #define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \ | 657 #define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \ |
718 COMPACT_GOOGLE_LOG_EX_INFO(ClassName , ##__VA_ARGS__) | 658 COMPACT_GOOGLE_LOG_EX_INFO(ClassName , ##__VA_ARGS__) |
719 #define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_INFO | 659 #define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_INFO |
720 const LogSeverity LOG_DCHECK = LOG_INFO; | 660 const LogSeverity LOG_DCHECK = LOG_INFO; |
721 #define DCHECK_IS_ON() false | 661 #define DCHECK_IS_ON() false |
722 | 662 |
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1015 #elif NOTIMPLEMENTED_POLICY == 5 | 955 #elif NOTIMPLEMENTED_POLICY == 5 |
1016 #define NOTIMPLEMENTED() do {\ | 956 #define NOTIMPLEMENTED() do {\ |
1017 static bool logged_once = false;\ | 957 static bool logged_once = false;\ |
1018 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ | 958 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ |
1019 logged_once = true;\ | 959 logged_once = true;\ |
1020 } while(0);\ | 960 } while(0);\ |
1021 EAT_STREAM_PARAMETERS | 961 EAT_STREAM_PARAMETERS |
1022 #endif | 962 #endif |
1023 | 963 |
1024 #endif // BASE_LOGGING_H_ | 964 #endif // BASE_LOGGING_H_ |
OLD | NEW |