OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <cassert> | 9 #include <cassert> |
10 #include <string> | 10 #include <string> |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 | 166 |
167 // On startup, should we delete or append to an existing log file (if any)? | 167 // On startup, should we delete or append to an existing log file (if any)? |
168 // Defaults to APPEND_TO_OLD_LOG_FILE. | 168 // Defaults to APPEND_TO_OLD_LOG_FILE. |
169 enum OldFileDeletionState { DELETE_OLD_LOG_FILE, APPEND_TO_OLD_LOG_FILE }; | 169 enum OldFileDeletionState { DELETE_OLD_LOG_FILE, APPEND_TO_OLD_LOG_FILE }; |
170 | 170 |
171 enum DcheckState { | 171 enum DcheckState { |
172 DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS, | 172 DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS, |
173 ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS | 173 ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS |
174 }; | 174 }; |
175 | 175 |
| 176 enum DlogState { |
| 177 DISABLE_DLOG_FOR_NON_OFFICIAL_RELEASE_BUILDS, |
| 178 ENABLE_DLOG_FOR_NON_OFFICIAL_RELEASE_BUILDS |
| 179 }; |
| 180 |
176 // TODO(avi): do we want to do a unification of character types here? | 181 // TODO(avi): do we want to do a unification of character types here? |
177 #if defined(OS_WIN) | 182 #if defined(OS_WIN) |
178 typedef wchar_t PathChar; | 183 typedef wchar_t PathChar; |
179 #else | 184 #else |
180 typedef char PathChar; | 185 typedef char PathChar; |
181 #endif | 186 #endif |
182 | 187 |
183 // Define different names for the BaseInitLoggingImpl() function depending on | 188 // Define different names for the BaseInitLoggingImpl() function depending on |
184 // whether NDEBUG is defined or not so that we'll fail to link if someone tries | 189 // whether NDEBUG is defined or not so that we'll fail to link if someone tries |
185 // to compile logging.cc with NDEBUG but includes logging.h without defining it, | 190 // to compile logging.cc with NDEBUG but includes logging.h without defining it, |
186 // or vice versa. | 191 // or vice versa. |
187 #if NDEBUG | 192 #if NDEBUG |
188 #define BaseInitLoggingImpl BaseInitLoggingImpl_built_with_NDEBUG | 193 #define BaseInitLoggingImpl BaseInitLoggingImpl_built_with_NDEBUG |
189 #else | 194 #else |
190 #define BaseInitLoggingImpl BaseInitLoggingImpl_built_without_NDEBUG | 195 #define BaseInitLoggingImpl BaseInitLoggingImpl_built_without_NDEBUG |
191 #endif | 196 #endif |
192 | 197 |
193 // Implementation of the InitLogging() method declared below. We use a | 198 // Implementation of the InitLogging() method declared below. We use a |
194 // more-specific name so we can #define it above without affecting other code | 199 // more-specific name so we can #define it above without affecting other code |
195 // that has named stuff "InitLogging". | 200 // that has named stuff "InitLogging". |
196 BASE_EXPORT bool BaseInitLoggingImpl(const PathChar* log_file, | 201 BASE_EXPORT bool BaseInitLoggingImpl(const PathChar* log_file, |
197 LoggingDestination logging_dest, | 202 LoggingDestination logging_dest, |
198 LogLockingState lock_log, | 203 LogLockingState lock_log, |
199 OldFileDeletionState delete_old, | 204 OldFileDeletionState delete_old, |
200 DcheckState dcheck_state); | 205 DcheckState dcheck_state, |
| 206 DlogState dlog_state); |
201 | 207 |
202 // Sets the log file name and other global logging state. Calling this function | 208 // Sets the log file name and other global logging state. Calling this function |
203 // is recommended, and is normally done at the beginning of application init. | 209 // is recommended, and is normally done at the beginning of application init. |
204 // If you don't call it, all the flags will be initialized to their default | 210 // If you don't call it, all the flags will be initialized to their default |
205 // values, and there is a race condition that may leak a critical section | 211 // values, and there is a race condition that may leak a critical section |
206 // object if two threads try to do the first log at the same time. | 212 // object if two threads try to do the first log at the same time. |
207 // See the definition of the enums above for descriptions and default values. | 213 // See the definition of the enums above for descriptions and default values. |
208 // | 214 // |
209 // The default log file is initialized to "debug.log" in the application | 215 // The default log file is initialized to "debug.log" in the application |
210 // directory. You probably don't want this, especially since the program | 216 // directory. You probably don't want this, especially since the program |
211 // directory may not be writable on an enduser's system. | 217 // directory may not be writable on an enduser's system. |
212 inline bool InitLogging(const PathChar* log_file, | 218 inline bool InitLogging(const PathChar* log_file, |
213 LoggingDestination logging_dest, | 219 LoggingDestination logging_dest, |
214 LogLockingState lock_log, | 220 LogLockingState lock_log, |
215 OldFileDeletionState delete_old, | 221 OldFileDeletionState delete_old, |
216 DcheckState dcheck_state) { | 222 DcheckState dcheck_state, |
| 223 DlogState dlog_state) { |
217 return BaseInitLoggingImpl(log_file, logging_dest, lock_log, | 224 return BaseInitLoggingImpl(log_file, logging_dest, lock_log, |
218 delete_old, dcheck_state); | 225 delete_old, dcheck_state, dlog_state); |
219 } | 226 } |
220 | 227 |
221 // Sets the log level. Anything at or above this level will be written to the | 228 // Sets the log level. Anything at or above this level will be written to the |
222 // log file/displayed to the user (if applicable). Anything below this level | 229 // log file/displayed to the user (if applicable). Anything below this level |
223 // will be silently ignored. The log level defaults to 0 (everything is logged | 230 // will be silently ignored. The log level defaults to 0 (everything is logged |
224 // up to level INFO) if this function is not called. | 231 // up to level INFO) if this function is not called. |
225 // Note that log messages for VLOG(x) are logged at level -x, so setting | 232 // Note that log messages for VLOG(x) are logged at level -x, so setting |
226 // the min log level to negative values enables verbose logging. | 233 // the min log level to negative values enables verbose logging. |
227 BASE_EXPORT void SetMinLogLevel(int level); | 234 BASE_EXPORT void SetMinLogLevel(int level); |
228 | 235 |
(...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
535 #if ( defined(OS_WIN) && defined(OFFICIAL_BUILD)) || \ | 542 #if ( defined(OS_WIN) && defined(OFFICIAL_BUILD)) || \ |
536 (!defined(OS_WIN) && defined(NDEBUG) && defined(GOOGLE_CHROME_BUILD)) | 543 (!defined(OS_WIN) && defined(NDEBUG) && defined(GOOGLE_CHROME_BUILD)) |
537 // Used by unit tests. | 544 // Used by unit tests. |
538 #define LOGGING_IS_OFFICIAL_BUILD | 545 #define LOGGING_IS_OFFICIAL_BUILD |
539 | 546 |
540 // In order to have optimized code for official builds, remove DLOGs and | 547 // In order to have optimized code for official builds, remove DLOGs and |
541 // DCHECKs. | 548 // DCHECKs. |
542 #define ENABLE_DLOG 0 | 549 #define ENABLE_DLOG 0 |
543 #define ENABLE_DCHECK 0 | 550 #define ENABLE_DCHECK 0 |
544 | 551 |
545 #elif defined(NDEBUG) | |
546 // Otherwise, if we're a release build, remove DLOGs but not DCHECKs | |
547 // (since those can still be turned on via a command-line flag). | |
548 #define ENABLE_DLOG 0 | |
549 #define ENABLE_DCHECK 1 | |
550 | |
551 #else | 552 #else |
552 // Otherwise, we're a debug build so enable DLOGs and DCHECKs. | 553 // For both non offical release builds and debug builds we enable them. |
| 554 // In case of non official release builds they could be enabled by commandline. |
553 #define ENABLE_DLOG 1 | 555 #define ENABLE_DLOG 1 |
554 #define ENABLE_DCHECK 1 | 556 #define ENABLE_DCHECK 1 |
555 #endif | 557 #endif |
556 | 558 |
557 // Definitions for DLOG et al. | 559 // Definitions for DLOG et al. |
558 | 560 |
559 #if ENABLE_DLOG | 561 #if ENABLE_DLOG |
560 | 562 |
| 563 #if defined(NDEBUG) |
| 564 BASE_EXPORT extern DlogState g_dlog_state; |
| 565 |
| 566 #define DLOG_ENABLED (::logging::g_dlog_state == \ |
| 567 ::logging::ENABLE_DLOG_FOR_NON_OFFICIAL_RELEASE_BUILDS) |
| 568 #define DLOG_IS_ON(severity) (DLOG_ENABLED && LOG_IS_ON(severity)) |
| 569 #define DLOG_IF(severity, condition) \ |
| 570 LOG_IF(severity,(DLOG_IS_ON(severity) && condition)) |
| 571 #define DLOG_ASSERT(condition) LOG_ASSERT(DLOG_ENABLED && condition) |
| 572 #define DPLOG_IF(severity, condition) PLOG_IF(severity, \ |
| 573 (DLOG_IS_ON(severity) && condition)) |
| 574 #define DVLOG_IF(verboselevel, condition) \ |
| 575 VLOG_IF(verboselevel, (DLOG_ENABLED && condition)) |
| 576 #define DVPLOG_IF(verboselevel, condition) \ |
| 577 VPLOG_IF(verboselevel, (DLOG_ENABLED && condition)) |
| 578 #else // NDEBUG |
| 579 // Debug builds. |
561 #define DLOG_IS_ON(severity) LOG_IS_ON(severity) | 580 #define DLOG_IS_ON(severity) LOG_IS_ON(severity) |
562 #define DLOG_IF(severity, condition) LOG_IF(severity, condition) | 581 #define DLOG_IF(severity, condition) LOG_IF(severity, condition) |
563 #define DLOG_ASSERT(condition) LOG_ASSERT(condition) | 582 #define DLOG_ASSERT(condition) LOG_ASSERT(condition) |
564 #define DPLOG_IF(severity, condition) PLOG_IF(severity, condition) | 583 #define DPLOG_IF(severity, condition) PLOG_IF(severity, condition) |
565 #define DVLOG_IF(verboselevel, condition) VLOG_IF(verboselevel, condition) | 584 #define DVLOG_IF(verboselevel, condition) VLOG_IF(verboselevel, condition) |
566 #define DVPLOG_IF(verboselevel, condition) VPLOG_IF(verboselevel, condition) | 585 #define DVPLOG_IF(verboselevel, condition) VPLOG_IF(verboselevel, condition) |
567 | 586 |
| 587 #endif // NDEBUG |
| 588 |
568 #else // ENABLE_DLOG | 589 #else // ENABLE_DLOG |
569 | 590 |
570 // If ENABLE_DLOG is off, we want to avoid emitting any references to | 591 // If ENABLE_DLOG is off, we want to avoid emitting any references to |
571 // |condition| (which may reference a variable defined only if NDEBUG | 592 // |condition| (which may reference a variable defined only if NDEBUG |
572 // is not defined). Contrast this with DCHECK et al., which has | 593 // is not defined). Contrast this with DCHECK et al., which has |
573 // different behavior. | 594 // different behavior. |
574 | 595 |
575 #define DLOG_EAT_STREAM_PARAMETERS \ | 596 #define DLOG_EAT_STREAM_PARAMETERS \ |
576 true ? (void) 0 : ::logging::LogMessageVoidify() & LOG_STREAM(FATAL) | 597 true ? (void) 0 : ::logging::LogMessageVoidify() & LOG_STREAM(FATAL) |
577 | 598 |
(...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
955 namespace base { | 976 namespace base { |
956 | 977 |
957 class StringPiece; | 978 class StringPiece; |
958 | 979 |
959 // Allows StringPiece to be logged. | 980 // Allows StringPiece to be logged. |
960 BASE_EXPORT std::ostream& operator<<(std::ostream& o, const StringPiece& piece); | 981 BASE_EXPORT std::ostream& operator<<(std::ostream& o, const StringPiece& piece); |
961 | 982 |
962 } // namespace base | 983 } // namespace base |
963 | 984 |
964 #endif // BASE_LOGGING_H_ | 985 #endif // BASE_LOGGING_H_ |
OLD | NEW |