[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
[email protected] | 39be424 | 2008-08-07 18:31:40 | [diff] [blame] | 5 | #ifndef BASE_LOGGING_H_ |
| 6 | #define BASE_LOGGING_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 8 | |
| 9 | #include <string> |
| 10 | #include <cstring> |
| 11 | #include <sstream> |
| 12 | |
| 13 | #include "base/basictypes.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 14 | |
| 15 | // |
| 16 | // Optional message capabilities |
| 17 | // ----------------------------- |
| 18 | // Assertion failed messages and fatal errors are displayed in a dialog box |
| 19 | // before the application exits. However, running this UI creates a message |
| 20 | // loop, which causes application messages to be processed and potentially |
| 21 | // dispatched to existing application windows. Since the application is in a |
| 22 | // bad state when this assertion dialog is displayed, these messages may not |
| 23 | // get processed and hang the dialog, or the application might go crazy. |
| 24 | // |
| 25 | // Therefore, it can be beneficial to display the error dialog in a separate |
| 26 | // process from the main application. When the logging system needs to display |
| 27 | // a fatal error dialog box, it will look for a program called |
| 28 | // "DebugMessage.exe" in the same directory as the application executable. It |
| 29 | // will run this application with the message as the command line, and will |
| 30 | // not include the name of the application as is traditional for easier |
| 31 | // parsing. |
| 32 | // |
| 33 | // The code for DebugMessage.exe is only one line. In WinMain, do: |
| 34 | // MessageBox(NULL, GetCommandLineW(), L"Fatal Error", 0); |
| 35 | // |
| 36 | // If DebugMessage.exe is not found, the logging code will use a normal |
| 37 | // MessageBox, potentially causing the problems discussed above. |
| 38 | |
| 39 | |
| 40 | // Instructions |
| 41 | // ------------ |
| 42 | // |
| 43 | // Make a bunch of macros for logging. The way to log things is to stream |
| 44 | // things to LOG(<a particular severity level>). E.g., |
| 45 | // |
| 46 | // LOG(INFO) << "Found " << num_cookies << " cookies"; |
| 47 | // |
| 48 | // You can also do conditional logging: |
| 49 | // |
| 50 | // LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies"; |
| 51 | // |
| 52 | // The above will cause log messages to be output on the 1st, 11th, 21st, ... |
| 53 | // times it is executed. Note that the special COUNTER value is used to |
| 54 | // identify which repetition is happening. |
| 55 | // |
| 56 | // The CHECK(condition) macro is active in both debug and release builds and |
| 57 | // effectively performs a LOG(FATAL) which terminates the process and |
| 58 | // generates a crashdump unless a debugger is attached. |
| 59 | // |
| 60 | // There are also "debug mode" logging macros like the ones above: |
| 61 | // |
| 62 | // DLOG(INFO) << "Found cookies"; |
| 63 | // |
| 64 | // DLOG_IF(INFO, num_cookies > 10) << "Got lots of cookies"; |
| 65 | // |
| 66 | // All "debug mode" logging is compiled away to nothing for non-debug mode |
| 67 | // compiles. LOG_IF and development flags also work well together |
| 68 | // because the code can be compiled away sometimes. |
| 69 | // |
| 70 | // We also have |
| 71 | // |
| 72 | // LOG_ASSERT(assertion); |
| 73 | // DLOG_ASSERT(assertion); |
| 74 | // |
| 75 | // which is syntactic sugar for {,D}LOG_IF(FATAL, assert fails) << assertion; |
| 76 | // |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 77 | // There are "verbose level" logging macros. They look like |
| 78 | // |
| 79 | // VLOG(1) << "I'm printed when you run the program with --v=1 or more"; |
| 80 | // VLOG(2) << "I'm printed when you run the program with --v=2 or more"; |
| 81 | // |
| 82 | // These always log at the INFO log level (when they log at all). |
| 83 | // The verbose logging can also be turned on module-by-module. For instance, |
[email protected] | b0d38d4c | 2010-10-29 00:39:48 | [diff] [blame] | 84 | // --vmodule=profile=2,icon_loader=1,browser_*=3,*/chromeos/*=4 --v=0 |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 85 | // will cause: |
| 86 | // a. VLOG(2) and lower messages to be printed from profile.{h,cc} |
| 87 | // b. VLOG(1) and lower messages to be printed from icon_loader.{h,cc} |
| 88 | // c. VLOG(3) and lower messages to be printed from files prefixed with |
| 89 | // "browser" |
[email protected] | e11de72 | 2010-11-01 20:50:55 | [diff] [blame] | 90 | // d. VLOG(4) and lower messages to be printed from files under a |
[email protected] | b0d38d4c | 2010-10-29 00:39:48 | [diff] [blame] | 91 | // "chromeos" directory. |
[email protected] | e11de72 | 2010-11-01 20:50:55 | [diff] [blame] | 92 | // e. VLOG(0) and lower messages to be printed from elsewhere |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 93 | // |
| 94 | // The wildcarding functionality shown by (c) supports both '*' (match |
[email protected] | b0d38d4c | 2010-10-29 00:39:48 | [diff] [blame] | 95 | // 0 or more characters) and '?' (match any single character) |
| 96 | // wildcards. Any pattern containing a forward or backward slash will |
| 97 | // be tested against the whole pathname and not just the module. |
| 98 | // E.g., "*/foo/bar/*=2" would change the logging level for all code |
| 99 | // in source files under a "foo/bar" directory. |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 100 | // |
| 101 | // There's also VLOG_IS_ON(n) "verbose level" condition macro. To be used as |
| 102 | // |
| 103 | // if (VLOG_IS_ON(2)) { |
| 104 | // // do some logging preparation and logging |
| 105 | // // that can't be accomplished with just VLOG(2) << ...; |
| 106 | // } |
| 107 | // |
| 108 | // There is also a VLOG_IF "verbose level" condition macro for sample |
| 109 | // cases, when some extra computation and preparation for logs is not |
| 110 | // needed. |
| 111 | // |
| 112 | // VLOG_IF(1, (size > 1024)) |
| 113 | // << "I'm printed when size is more than 1024 and when you run the " |
| 114 | // "program with --v=1 or more"; |
| 115 | // |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 116 | // We also override the standard 'assert' to use 'DLOG_ASSERT'. |
| 117 | // |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 118 | // Lastly, there is: |
| 119 | // |
| 120 | // PLOG(ERROR) << "Couldn't do foo"; |
| 121 | // DPLOG(ERROR) << "Couldn't do foo"; |
| 122 | // PLOG_IF(ERROR, cond) << "Couldn't do foo"; |
| 123 | // DPLOG_IF(ERROR, cond) << "Couldn't do foo"; |
| 124 | // PCHECK(condition) << "Couldn't do foo"; |
| 125 | // DPCHECK(condition) << "Couldn't do foo"; |
| 126 | // |
| 127 | // which append the last system error to the message in string form (taken from |
| 128 | // GetLastError() on Windows and errno on POSIX). |
| 129 | // |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 130 | // The supported severity levels for macros that allow you to specify one |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 131 | // are (in increasing order of severity) INFO, WARNING, ERROR, ERROR_REPORT, |
| 132 | // and FATAL. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 133 | // |
| 134 | // Very important: logging a message at the FATAL severity level causes |
| 135 | // the program to terminate (after the message is logged). |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 136 | // |
| 137 | // Note the special severity of ERROR_REPORT only available/relevant in normal |
| 138 | // mode, which displays error dialog without terminating the program. There is |
| 139 | // no error dialog for severity ERROR or below in normal mode. |
| 140 | // |
| 141 | // There is also the special severity of DFATAL, which logs FATAL in |
[email protected] | 081bd4c | 2010-06-24 01:01:04 | [diff] [blame] | 142 | // debug mode, ERROR in normal mode. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 143 | |
| 144 | namespace logging { |
| 145 | |
| 146 | // Where to record logging output? A flat file and/or system debug log via |
[email protected] | 88aa41e8 | 2008-11-18 00:59:04 | [diff] [blame] | 147 | // OutputDebugString. Defaults on Windows to LOG_ONLY_TO_FILE, and on |
| 148 | // POSIX to LOG_ONLY_TO_SYSTEM_DEBUG_LOG (aka stderr). |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 149 | enum LoggingDestination { LOG_NONE, |
| 150 | LOG_ONLY_TO_FILE, |
| 151 | LOG_ONLY_TO_SYSTEM_DEBUG_LOG, |
| 152 | LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG }; |
| 153 | |
| 154 | // Indicates that the log file should be locked when being written to. |
| 155 | // Often, there is no locking, which is fine for a single threaded program. |
| 156 | // If logging is being done from multiple threads or there can be more than |
| 157 | // one process doing the logging, the file should be locked during writes to |
| 158 | // make each log outut atomic. Other writers will block. |
| 159 | // |
| 160 | // All processes writing to the log file must have their locking set for it to |
| 161 | // work properly. Defaults to DONT_LOCK_LOG_FILE. |
| 162 | enum LogLockingState { LOCK_LOG_FILE, DONT_LOCK_LOG_FILE }; |
| 163 | |
| 164 | // On startup, should we delete or append to an existing log file (if any)? |
| 165 | // Defaults to APPEND_TO_OLD_LOG_FILE. |
| 166 | enum OldFileDeletionState { DELETE_OLD_LOG_FILE, APPEND_TO_OLD_LOG_FILE }; |
| 167 | |
[email protected] | 7c10f755 | 2011-01-11 01:03:36 | [diff] [blame] | 168 | enum DcheckState { |
| 169 | DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS, |
| 170 | ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS |
| 171 | }; |
| 172 | |
[email protected] | ff3d0c3 | 2010-08-23 19:57:46 | [diff] [blame] | 173 | // TODO(avi): do we want to do a unification of character types here? |
| 174 | #if defined(OS_WIN) |
| 175 | typedef wchar_t PathChar; |
| 176 | #else |
| 177 | typedef char PathChar; |
| 178 | #endif |
| 179 | |
| 180 | // Define different names for the BaseInitLoggingImpl() function depending on |
| 181 | // whether NDEBUG is defined or not so that we'll fail to link if someone tries |
| 182 | // to compile logging.cc with NDEBUG but includes logging.h without defining it, |
| 183 | // or vice versa. |
| 184 | #if NDEBUG |
| 185 | #define BaseInitLoggingImpl BaseInitLoggingImpl_built_with_NDEBUG |
| 186 | #else |
| 187 | #define BaseInitLoggingImpl BaseInitLoggingImpl_built_without_NDEBUG |
| 188 | #endif |
| 189 | |
| 190 | // Implementation of the InitLogging() method declared below. We use a |
| 191 | // more-specific name so we can #define it above without affecting other code |
| 192 | // that has named stuff "InitLogging". |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 193 | bool BaseInitLoggingImpl(const PathChar* log_file, |
[email protected] | ff3d0c3 | 2010-08-23 19:57:46 | [diff] [blame] | 194 | LoggingDestination logging_dest, |
| 195 | LogLockingState lock_log, |
[email protected] | 7c10f755 | 2011-01-11 01:03:36 | [diff] [blame] | 196 | OldFileDeletionState delete_old, |
| 197 | DcheckState dcheck_state); |
[email protected] | ff3d0c3 | 2010-08-23 19:57:46 | [diff] [blame] | 198 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 199 | // Sets the log file name and other global logging state. Calling this function |
| 200 | // is recommended, and is normally done at the beginning of application init. |
| 201 | // If you don't call it, all the flags will be initialized to their default |
| 202 | // values, and there is a race condition that may leak a critical section |
| 203 | // object if two threads try to do the first log at the same time. |
| 204 | // See the definition of the enums above for descriptions and default values. |
| 205 | // |
| 206 | // The default log file is initialized to "debug.log" in the application |
| 207 | // directory. You probably don't want this, especially since the program |
| 208 | // directory may not be writable on an enduser's system. |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 209 | inline bool InitLogging(const PathChar* log_file, |
[email protected] | ff3d0c3 | 2010-08-23 19:57:46 | [diff] [blame] | 210 | LoggingDestination logging_dest, |
| 211 | LogLockingState lock_log, |
[email protected] | 7c10f755 | 2011-01-11 01:03:36 | [diff] [blame] | 212 | OldFileDeletionState delete_old, |
| 213 | DcheckState dcheck_state) { |
| 214 | return BaseInitLoggingImpl(log_file, logging_dest, lock_log, |
| 215 | delete_old, dcheck_state); |
[email protected] | ff3d0c3 | 2010-08-23 19:57:46 | [diff] [blame] | 216 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 217 | |
| 218 | // Sets the log level. Anything at or above this level will be written to the |
| 219 | // log file/displayed to the user (if applicable). Anything below this level |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 220 | // will be silently ignored. The log level defaults to 0 (everything is logged |
| 221 | // up to level INFO) if this function is not called. |
| 222 | // Note that log messages for VLOG(x) are logged at level -x, so setting |
| 223 | // the min log level to negative values enables verbose logging. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 224 | void SetMinLogLevel(int level); |
| 225 | |
[email protected] | 8a2986ca | 2009-04-10 19:13:42 | [diff] [blame] | 226 | // Gets the current log level. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 227 | int GetMinLogLevel(); |
| 228 | |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 229 | // Gets the VLOG default verbosity level. |
| 230 | int GetVlogVerbosity(); |
| 231 | |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 232 | // Gets the current vlog level for the given file (usually taken from |
| 233 | // __FILE__). |
[email protected] | 2f4e9a6 | 2010-09-29 21:25:14 | [diff] [blame] | 234 | |
| 235 | // Note that |N| is the size *with* the null terminator. |
| 236 | int GetVlogLevelHelper(const char* file_start, size_t N); |
| 237 | |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 238 | template <size_t N> |
| 239 | int GetVlogLevel(const char (&file)[N]) { |
| 240 | return GetVlogLevelHelper(file, N); |
| 241 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 242 | |
| 243 | // Sets the common items you want to be prepended to each log message. |
| 244 | // process and thread IDs default to off, the timestamp defaults to on. |
| 245 | // If this function is not called, logging defaults to writing the timestamp |
| 246 | // only. |
| 247 | void SetLogItems(bool enable_process_id, bool enable_thread_id, |
| 248 | bool enable_timestamp, bool enable_tickcount); |
| 249 | |
[email protected] | 81e0a85 | 2010-08-17 00:38:12 | [diff] [blame] | 250 | // Sets whether or not you'd like to see fatal debug messages popped up in |
| 251 | // a dialog box or not. |
| 252 | // Dialogs are not shown by default. |
| 253 | void SetShowErrorDialogs(bool enable_dialogs); |
| 254 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 255 | // Sets the Log Assert Handler that will be used to notify of check failures. |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 256 | // The default handler shows a dialog box and then terminate the process, |
| 257 | // however clients can use this function to override with their own handling |
| 258 | // (e.g. a silent one for Unit Tests) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 259 | typedef void (*LogAssertHandlerFunction)(const std::string& str); |
| 260 | void SetLogAssertHandler(LogAssertHandlerFunction handler); |
[email protected] | 64e5cc0 | 2010-11-03 19:20:27 | [diff] [blame] | 261 | |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 262 | // Sets the Log Report Handler that will be used to notify of check failures |
| 263 | // in non-debug mode. The default handler shows a dialog box and continues |
| 264 | // the execution, however clients can use this function to override with their |
| 265 | // own handling. |
| 266 | typedef void (*LogReportHandlerFunction)(const std::string& str); |
| 267 | void SetLogReportHandler(LogReportHandlerFunction handler); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 268 | |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 269 | // Sets the Log Message Handler that gets passed every log message before |
| 270 | // it's sent to other log destinations (if any). |
| 271 | // Returns true to signal that it handled the message and the message |
| 272 | // should not be sent to other log destinations. |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 273 | typedef bool (*LogMessageHandlerFunction)(int severity, |
| 274 | const char* file, int line, size_t message_start, const std::string& str); |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 275 | void SetLogMessageHandler(LogMessageHandlerFunction handler); |
[email protected] | 64e5cc0 | 2010-11-03 19:20:27 | [diff] [blame] | 276 | LogMessageHandlerFunction GetLogMessageHandler(); |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 277 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 278 | typedef int LogSeverity; |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 279 | const LogSeverity LOG_VERBOSE = -1; // This is level 1 verbosity |
| 280 | // Note: the log severities are used to index into the array of names, |
| 281 | // see log_severity_names. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 282 | const LogSeverity LOG_INFO = 0; |
| 283 | const LogSeverity LOG_WARNING = 1; |
| 284 | const LogSeverity LOG_ERROR = 2; |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 285 | const LogSeverity LOG_ERROR_REPORT = 3; |
| 286 | const LogSeverity LOG_FATAL = 4; |
| 287 | const LogSeverity LOG_NUM_SEVERITIES = 5; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 288 | |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 289 | // LOG_DFATAL is LOG_FATAL in debug mode, ERROR in normal mode |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 290 | #ifdef NDEBUG |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 291 | const LogSeverity LOG_DFATAL = LOG_ERROR; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 292 | #else |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 293 | const LogSeverity LOG_DFATAL = LOG_FATAL; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 294 | #endif |
| 295 | |
| 296 | // A few definitions of macros that don't generate much code. These are used |
| 297 | // by LOG() and LOG_IF, etc. Since these are used all over our code, it's |
| 298 | // better to have compact code for these operations. |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 299 | #define COMPACT_GOOGLE_LOG_EX_INFO(ClassName, ...) \ |
| 300 | logging::ClassName(__FILE__, __LINE__, logging::LOG_INFO , ##__VA_ARGS__) |
| 301 | #define COMPACT_GOOGLE_LOG_EX_WARNING(ClassName, ...) \ |
| 302 | logging::ClassName(__FILE__, __LINE__, logging::LOG_WARNING , ##__VA_ARGS__) |
| 303 | #define COMPACT_GOOGLE_LOG_EX_ERROR(ClassName, ...) \ |
| 304 | logging::ClassName(__FILE__, __LINE__, logging::LOG_ERROR , ##__VA_ARGS__) |
| 305 | #define COMPACT_GOOGLE_LOG_EX_ERROR_REPORT(ClassName, ...) \ |
| 306 | logging::ClassName(__FILE__, __LINE__, \ |
| 307 | logging::LOG_ERROR_REPORT , ##__VA_ARGS__) |
| 308 | #define COMPACT_GOOGLE_LOG_EX_FATAL(ClassName, ...) \ |
| 309 | logging::ClassName(__FILE__, __LINE__, logging::LOG_FATAL , ##__VA_ARGS__) |
| 310 | #define COMPACT_GOOGLE_LOG_EX_DFATAL(ClassName, ...) \ |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 311 | logging::ClassName(__FILE__, __LINE__, logging::LOG_DFATAL , ##__VA_ARGS__) |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 312 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 313 | #define COMPACT_GOOGLE_LOG_INFO \ |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 314 | COMPACT_GOOGLE_LOG_EX_INFO(LogMessage) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 315 | #define COMPACT_GOOGLE_LOG_WARNING \ |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 316 | COMPACT_GOOGLE_LOG_EX_WARNING(LogMessage) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 317 | #define COMPACT_GOOGLE_LOG_ERROR \ |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 318 | COMPACT_GOOGLE_LOG_EX_ERROR(LogMessage) |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 319 | #define COMPACT_GOOGLE_LOG_ERROR_REPORT \ |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 320 | COMPACT_GOOGLE_LOG_EX_ERROR_REPORT(LogMessage) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 321 | #define COMPACT_GOOGLE_LOG_FATAL \ |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 322 | COMPACT_GOOGLE_LOG_EX_FATAL(LogMessage) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 323 | #define COMPACT_GOOGLE_LOG_DFATAL \ |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 324 | COMPACT_GOOGLE_LOG_EX_DFATAL(LogMessage) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 325 | |
| 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 |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 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 |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 335 | // Needed for LOG_IS_ON(ERROR). |
| 336 | const LogSeverity LOG_0 = LOG_ERROR; |
| 337 | |
[email protected] | deba0ff | 2010-11-03 05:30:14 | [diff] [blame] | 338 | // As special cases, we can assume that LOG_IS_ON(ERROR_REPORT) and |
| 339 | // LOG_IS_ON(FATAL) always hold. Also, LOG_IS_ON(DFATAL) always holds |
| 340 | // in debug mode. In particular, CHECK()s will always fire if they |
| 341 | // fail. |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 342 | #define LOG_IS_ON(severity) \ |
| 343 | ((::logging::LOG_ ## severity) >= ::logging::GetMinLogLevel()) |
| 344 | |
| 345 | // We can't do any caching tricks with VLOG_IS_ON() like the |
| 346 | // google-glog version since it requires GCC extensions. This means |
| 347 | // that using the v-logging functions in conjunction with --vmodule |
| 348 | // may be slow. |
| 349 | #define VLOG_IS_ON(verboselevel) \ |
| 350 | ((verboselevel) <= ::logging::GetVlogLevel(__FILE__)) |
| 351 | |
| 352 | // Helper macro which avoids evaluating the arguments to a stream if |
| 353 | // the condition doesn't hold. |
| 354 | #define LAZY_STREAM(stream, condition) \ |
| 355 | !(condition) ? (void) 0 : ::logging::LogMessageVoidify() & (stream) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 356 | |
| 357 | // We use the preprocessor's merging operator, "##", so that, e.g., |
| 358 | // LOG(INFO) becomes the token COMPACT_GOOGLE_LOG_INFO. There's some funny |
| 359 | // subtle difference between ostream member streaming functions (e.g., |
| 360 | // ostream::operator<<(int) and ostream non-member streaming functions |
| 361 | // (e.g., ::operator<<(ostream&, string&): it turns out that it's |
| 362 | // impossible to stream something like a string directly to an unnamed |
| 363 | // ostream. We employ a neat hack by calling the stream() member |
| 364 | // function of LogMessage which seems to avoid the problem. |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 365 | #define LOG_STREAM(severity) COMPACT_GOOGLE_LOG_ ## severity.stream() |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 366 | |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 367 | #define LOG(severity) LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity)) |
| 368 | #define LOG_IF(severity, condition) \ |
| 369 | LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity) && (condition)) |
| 370 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 371 | #define SYSLOG(severity) LOG(severity) |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 372 | #define SYSLOG_IF(severity, condition) LOG_IF(severity, condition) |
| 373 | |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 374 | // The VLOG macros log with negative verbosities. |
| 375 | #define VLOG_STREAM(verbose_level) \ |
| 376 | logging::LogMessage(__FILE__, __LINE__, -verbose_level).stream() |
| 377 | |
| 378 | #define VLOG(verbose_level) \ |
| 379 | LAZY_STREAM(VLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level)) |
| 380 | |
| 381 | #define VLOG_IF(verbose_level, condition) \ |
| 382 | LAZY_STREAM(VLOG_STREAM(verbose_level), \ |
| 383 | VLOG_IS_ON(verbose_level) && (condition)) |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 384 | |
| 385 | // TODO(akalin): Add more VLOG variants, e.g. VPLOG. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 386 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 387 | #define LOG_ASSERT(condition) \ |
| 388 | LOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". " |
| 389 | #define SYSLOG_ASSERT(condition) \ |
| 390 | SYSLOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". " |
| 391 | |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 392 | #if defined(OS_WIN) |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 393 | #define LOG_GETLASTERROR_STREAM(severity) \ |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 394 | COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \ |
| 395 | ::logging::GetLastSystemErrorCode()).stream() |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 396 | #define LOG_GETLASTERROR(severity) \ |
| 397 | LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity), LOG_IS_ON(severity)) |
| 398 | #define LOG_GETLASTERROR_MODULE_STREAM(severity, module) \ |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 399 | COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \ |
| 400 | ::logging::GetLastSystemErrorCode(), module).stream() |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 401 | #define LOG_GETLASTERROR_MODULE(severity, module) \ |
| 402 | LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity, module), \ |
| 403 | LOG_IS_ON(severity)) |
| 404 | // PLOG_STREAM is used by PLOG, which is the usual error logging macro |
| 405 | // for each platform. |
| 406 | #define PLOG_STREAM(severity) LOG_GETLASTERROR_STREAM(severity) |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 407 | #elif defined(OS_POSIX) |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 408 | #define LOG_ERRNO_STREAM(severity) \ |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 409 | COMPACT_GOOGLE_LOG_EX_ ## severity(ErrnoLogMessage, \ |
| 410 | ::logging::GetLastSystemErrorCode()).stream() |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 411 | #define LOG_ERRNO(severity) \ |
| 412 | LAZY_STREAM(LOG_ERRNO_STREAM(severity), LOG_IS_ON(severity)) |
| 413 | // PLOG_STREAM is used by PLOG, which is the usual error logging macro |
| 414 | // for each platform. |
| 415 | #define PLOG_STREAM(severity) LOG_ERRNO_STREAM(severity) |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 416 | // TODO(tschmelcher): Should we add OSStatus logging for Mac? |
| 417 | #endif |
| 418 | |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 419 | #define PLOG(severity) \ |
| 420 | LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity)) |
| 421 | |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 422 | #define PLOG_IF(severity, condition) \ |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 423 | LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity) && (condition)) |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 424 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 425 | // CHECK dies with a fatal error if condition is not true. It is *not* |
| 426 | // controlled by NDEBUG, so the check will be executed regardless of |
| 427 | // compilation mode. |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 428 | // |
| 429 | // We make sure CHECK et al. always evaluates their arguments, as |
| 430 | // doing CHECK(FunctionWithSideEffect()) is a common idiom. |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 431 | #define CHECK(condition) \ |
| 432 | LAZY_STREAM(LOG_STREAM(FATAL), !(condition)) \ |
| 433 | << "Check failed: " #condition ". " |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 434 | |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 435 | #define PCHECK(condition) \ |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 436 | LAZY_STREAM(PLOG_STREAM(FATAL), !(condition)) \ |
| 437 | << "Check failed: " #condition ". " |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 438 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 439 | // Build the error message string. This is separate from the "Impl" |
| 440 | // function template because it is not performance critical and so can |
[email protected] | 9c7132e | 2011-02-08 07:39:08 | [diff] [blame^] | 441 | // be out of line, while the "Impl" code should be inline. Caller |
| 442 | // takes ownership of the returned string. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 443 | template<class t1, class t2> |
| 444 | std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) { |
| 445 | std::ostringstream ss; |
| 446 | ss << names << " (" << v1 << " vs. " << v2 << ")"; |
| 447 | std::string* msg = new std::string(ss.str()); |
| 448 | return msg; |
| 449 | } |
| 450 | |
[email protected] | 6d445d3 | 2010-09-30 19:10:03 | [diff] [blame] | 451 | // MSVC doesn't like complex extern templates and DLLs. |
| 452 | #if !defined(COMPILER_MSVC) |
| 453 | // Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated |
| 454 | // in logging.cc. |
| 455 | extern template std::string* MakeCheckOpString<int, int>( |
| 456 | const int&, const int&, const char* names); |
| 457 | extern template std::string* MakeCheckOpString<unsigned long, unsigned long>( |
| 458 | const unsigned long&, const unsigned long&, const char* names); |
| 459 | extern template std::string* MakeCheckOpString<unsigned long, unsigned int>( |
| 460 | const unsigned long&, const unsigned int&, const char* names); |
| 461 | extern template std::string* MakeCheckOpString<unsigned int, unsigned long>( |
| 462 | const unsigned int&, const unsigned long&, const char* names); |
| 463 | extern template std::string* MakeCheckOpString<std::string, std::string>( |
| 464 | const std::string&, const std::string&, const char* name); |
| 465 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 466 | |
[email protected] | e150c038 | 2010-03-02 00:41:12 | [diff] [blame] | 467 | // Helper macro for binary operators. |
| 468 | // Don't use this macro directly in your code, use CHECK_EQ et al below. |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 469 | // |
| 470 | // TODO(akalin): Rewrite this so that constructs like if (...) |
| 471 | // CHECK_EQ(...) else { ... } work properly. |
| 472 | #define CHECK_OP(name, op, val1, val2) \ |
[email protected] | 9c7132e | 2011-02-08 07:39:08 | [diff] [blame^] | 473 | if (std::string* _result = \ |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 474 | logging::Check##name##Impl((val1), (val2), \ |
| 475 | #val1 " " #op " " #val2)) \ |
[email protected] | 8b78210 | 2010-09-30 22:38:30 | [diff] [blame] | 476 | logging::LogMessage(__FILE__, __LINE__, _result).stream() |
[email protected] | e150c038 | 2010-03-02 00:41:12 | [diff] [blame] | 477 | |
[email protected] | 7151260 | 2010-11-01 22:19:56 | [diff] [blame] | 478 | // Helper functions for CHECK_OP macro. |
| 479 | // The (int, int) specialization works around the issue that the compiler |
| 480 | // will not instantiate the template version of the function on values of |
| 481 | // unnamed enum type - see comment below. |
| 482 | #define DEFINE_CHECK_OP_IMPL(name, op) \ |
| 483 | template <class t1, class t2> \ |
| 484 | inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \ |
| 485 | const char* names) { \ |
| 486 | if (v1 op v2) return NULL; \ |
| 487 | else return MakeCheckOpString(v1, v2, names); \ |
| 488 | } \ |
| 489 | inline std::string* Check##name##Impl(int v1, int v2, const char* names) { \ |
| 490 | if (v1 op v2) return NULL; \ |
| 491 | else return MakeCheckOpString(v1, v2, names); \ |
| 492 | } |
| 493 | DEFINE_CHECK_OP_IMPL(EQ, ==) |
| 494 | DEFINE_CHECK_OP_IMPL(NE, !=) |
| 495 | DEFINE_CHECK_OP_IMPL(LE, <=) |
| 496 | DEFINE_CHECK_OP_IMPL(LT, < ) |
| 497 | DEFINE_CHECK_OP_IMPL(GE, >=) |
| 498 | DEFINE_CHECK_OP_IMPL(GT, > ) |
| 499 | #undef DEFINE_CHECK_OP_IMPL |
[email protected] | e150c038 | 2010-03-02 00:41:12 | [diff] [blame] | 500 | |
| 501 | #define CHECK_EQ(val1, val2) CHECK_OP(EQ, ==, val1, val2) |
| 502 | #define CHECK_NE(val1, val2) CHECK_OP(NE, !=, val1, val2) |
| 503 | #define CHECK_LE(val1, val2) CHECK_OP(LE, <=, val1, val2) |
| 504 | #define CHECK_LT(val1, val2) CHECK_OP(LT, < , val1, val2) |
| 505 | #define CHECK_GE(val1, val2) CHECK_OP(GE, >=, val1, val2) |
| 506 | #define CHECK_GT(val1, val2) CHECK_OP(GT, > , val1, val2) |
| 507 | |
[email protected] | e3cca33 | 2009-08-20 01:20:29 | [diff] [blame] | 508 | // http://crbug.com/16512 is open for a real fix for this. For now, Windows |
| 509 | // uses OFFICIAL_BUILD and other platforms use the branding flag when NDEBUG is |
| 510 | // defined. |
| 511 | #if ( defined(OS_WIN) && defined(OFFICIAL_BUILD)) || \ |
| 512 | (!defined(OS_WIN) && defined(NDEBUG) && defined(GOOGLE_CHROME_BUILD)) |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 513 | // Used by unit tests. |
| 514 | #define LOGGING_IS_OFFICIAL_BUILD |
| 515 | |
[email protected] | e3cca33 | 2009-08-20 01:20:29 | [diff] [blame] | 516 | // In order to have optimized code for official builds, remove DLOGs and |
| 517 | // DCHECKs. |
[email protected] | d15e56c | 2010-09-30 21:12:33 | [diff] [blame] | 518 | #define ENABLE_DLOG 0 |
| 519 | #define ENABLE_DCHECK 0 |
| 520 | |
| 521 | #elif defined(NDEBUG) |
| 522 | // Otherwise, if we're a release build, remove DLOGs but not DCHECKs |
| 523 | // (since those can still be turned on via a command-line flag). |
| 524 | #define ENABLE_DLOG 0 |
| 525 | #define ENABLE_DCHECK 1 |
| 526 | |
| 527 | #else |
| 528 | // Otherwise, we're a debug build so enable DLOGs and DCHECKs. |
| 529 | #define ENABLE_DLOG 1 |
| 530 | #define ENABLE_DCHECK 1 |
[email protected] | e3cca33 | 2009-08-20 01:20:29 | [diff] [blame] | 531 | #endif |
| 532 | |
[email protected] | d15e56c | 2010-09-30 21:12:33 | [diff] [blame] | 533 | // Definitions for DLOG et al. |
| 534 | |
[email protected] | d926c20 | 2010-10-01 02:58:24 | [diff] [blame] | 535 | #if ENABLE_DLOG |
| 536 | |
[email protected] | 5e98780 | 2010-11-01 19:49:22 | [diff] [blame] | 537 | #define DLOG_IS_ON(severity) LOG_IS_ON(severity) |
[email protected] | d926c20 | 2010-10-01 02:58:24 | [diff] [blame] | 538 | #define DLOG_IF(severity, condition) LOG_IF(severity, condition) |
| 539 | #define DLOG_ASSERT(condition) LOG_ASSERT(condition) |
[email protected] | d926c20 | 2010-10-01 02:58:24 | [diff] [blame] | 540 | #define DPLOG_IF(severity, condition) PLOG_IF(severity, condition) |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 541 | #define DVLOG_IF(verboselevel, condition) VLOG_IF(verboselevel, condition) |
[email protected] | d926c20 | 2010-10-01 02:58:24 | [diff] [blame] | 542 | |
| 543 | #else // ENABLE_DLOG |
| 544 | |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 545 | // If ENABLE_DLOG is off, we want to avoid emitting any references to |
| 546 | // |condition| (which may reference a variable defined only if NDEBUG |
| 547 | // is not defined). Contrast this with DCHECK et al., which has |
| 548 | // different behavior. |
[email protected] | d926c20 | 2010-10-01 02:58:24 | [diff] [blame] | 549 | |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 550 | #define DLOG_EAT_STREAM_PARAMETERS \ |
| 551 | true ? (void) 0 : ::logging::LogMessageVoidify() & LOG_STREAM(FATAL) |
[email protected] | d926c20 | 2010-10-01 02:58:24 | [diff] [blame] | 552 | |
[email protected] | 5e98780 | 2010-11-01 19:49:22 | [diff] [blame] | 553 | #define DLOG_IS_ON(severity) false |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 554 | #define DLOG_IF(severity, condition) DLOG_EAT_STREAM_PARAMETERS |
| 555 | #define DLOG_ASSERT(condition) DLOG_EAT_STREAM_PARAMETERS |
| 556 | #define DPLOG_IF(severity, condition) DLOG_EAT_STREAM_PARAMETERS |
| 557 | #define DVLOG_IF(verboselevel, condition) DLOG_EAT_STREAM_PARAMETERS |
[email protected] | d926c20 | 2010-10-01 02:58:24 | [diff] [blame] | 558 | |
| 559 | #endif // ENABLE_DLOG |
| 560 | |
[email protected] | d15e56c | 2010-09-30 21:12:33 | [diff] [blame] | 561 | // DEBUG_MODE is for uses like |
| 562 | // if (DEBUG_MODE) foo.CheckThatFoo(); |
| 563 | // instead of |
| 564 | // #ifndef NDEBUG |
| 565 | // foo.CheckThatFoo(); |
| 566 | // #endif |
| 567 | // |
| 568 | // We tie its state to ENABLE_DLOG. |
| 569 | enum { DEBUG_MODE = ENABLE_DLOG }; |
| 570 | |
| 571 | #undef ENABLE_DLOG |
| 572 | |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 573 | #define DLOG(severity) \ |
| 574 | LAZY_STREAM(LOG_STREAM(severity), DLOG_IS_ON(severity)) |
| 575 | |
| 576 | #if defined(OS_WIN) |
| 577 | #define DLOG_GETLASTERROR(severity) \ |
| 578 | LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity), DLOG_IS_ON(severity)) |
| 579 | #define DLOG_GETLASTERROR_MODULE(severity, module) \ |
| 580 | LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity, module), \ |
| 581 | DLOG_IS_ON(severity)) |
| 582 | #elif defined(OS_POSIX) |
| 583 | #define DLOG_ERRNO(severity) \ |
| 584 | LAZY_STREAM(LOG_ERRNO_STREAM(severity), DLOG_IS_ON(severity)) |
| 585 | #endif |
| 586 | |
| 587 | #define DPLOG(severity) \ |
| 588 | LAZY_STREAM(PLOG_STREAM(severity), DLOG_IS_ON(severity)) |
| 589 | |
| 590 | #define DVLOG(verboselevel) DLOG_IF(INFO, VLOG_IS_ON(verboselevel)) |
| 591 | |
| 592 | // Definitions for DCHECK et al. |
[email protected] | d926c20 | 2010-10-01 02:58:24 | [diff] [blame] | 593 | |
[email protected] | d15e56c | 2010-09-30 21:12:33 | [diff] [blame] | 594 | #if ENABLE_DCHECK |
[email protected] | e3cca33 | 2009-08-20 01:20:29 | [diff] [blame] | 595 | |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 596 | #if defined(NDEBUG) |
[email protected] | d926c20 | 2010-10-01 02:58:24 | [diff] [blame] | 597 | |
[email protected] | deba0ff | 2010-11-03 05:30:14 | [diff] [blame] | 598 | #define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \ |
| 599 | COMPACT_GOOGLE_LOG_EX_ERROR_REPORT(ClassName , ##__VA_ARGS__) |
| 600 | #define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_ERROR_REPORT |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 601 | const LogSeverity LOG_DCHECK = LOG_ERROR_REPORT; |
[email protected] | 7c10f755 | 2011-01-11 01:03:36 | [diff] [blame] | 602 | extern DcheckState g_dcheck_state; |
| 603 | #define DCHECK_IS_ON() \ |
| 604 | ((::logging::g_dcheck_state == \ |
| 605 | ::logging::ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS) && \ |
| 606 | LOG_IS_ON(DCHECK)) |
[email protected] | d926c20 | 2010-10-01 02:58:24 | [diff] [blame] | 607 | |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 608 | #else // defined(NDEBUG) |
| 609 | |
[email protected] | 5e98780 | 2010-11-01 19:49:22 | [diff] [blame] | 610 | // On a regular debug build, we want to have DCHECKs enabled. |
[email protected] | deba0ff | 2010-11-03 05:30:14 | [diff] [blame] | 611 | #define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \ |
| 612 | COMPACT_GOOGLE_LOG_EX_FATAL(ClassName , ##__VA_ARGS__) |
| 613 | #define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_FATAL |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 614 | const LogSeverity LOG_DCHECK = LOG_FATAL; |
[email protected] | deba0ff | 2010-11-03 05:30:14 | [diff] [blame] | 615 | #define DCHECK_IS_ON() true |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 616 | |
| 617 | #endif // defined(NDEBUG) |
| 618 | |
| 619 | #else // ENABLE_DCHECK |
| 620 | |
[email protected] | deba0ff | 2010-11-03 05:30:14 | [diff] [blame] | 621 | // These are just dummy values since DCHECK_IS_ON() is always false in |
| 622 | // this case. |
| 623 | #define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \ |
| 624 | COMPACT_GOOGLE_LOG_EX_INFO(ClassName , ##__VA_ARGS__) |
| 625 | #define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_INFO |
| 626 | const LogSeverity LOG_DCHECK = LOG_INFO; |
[email protected] | 5e98780 | 2010-11-01 19:49:22 | [diff] [blame] | 627 | #define DCHECK_IS_ON() false |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 628 | |
| 629 | #endif // ENABLE_DCHECK |
[email protected] | 5e98780 | 2010-11-01 19:49:22 | [diff] [blame] | 630 | #undef ENABLE_DCHECK |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 631 | |
[email protected] | deba0ff | 2010-11-03 05:30:14 | [diff] [blame] | 632 | // DCHECK et al. make sure to reference |condition| regardless of |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 633 | // whether DCHECKs are enabled; this is so that we don't get unused |
| 634 | // variable warnings if the only use of a variable is in a DCHECK. |
| 635 | // This behavior is different from DLOG_IF et al. |
| 636 | |
[email protected] | deba0ff | 2010-11-03 05:30:14 | [diff] [blame] | 637 | #define DCHECK(condition) \ |
| 638 | LAZY_STREAM(LOG_STREAM(DCHECK), DCHECK_IS_ON() && !(condition)) \ |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 639 | << "Check failed: " #condition ". " |
| 640 | |
[email protected] | deba0ff | 2010-11-03 05:30:14 | [diff] [blame] | 641 | #define DPCHECK(condition) \ |
| 642 | LAZY_STREAM(PLOG_STREAM(DCHECK), DCHECK_IS_ON() && !(condition)) \ |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 643 | << "Check failed: " #condition ". " |
[email protected] | d926c20 | 2010-10-01 02:58:24 | [diff] [blame] | 644 | |
| 645 | // Helper macro for binary operators. |
| 646 | // Don't use this macro directly in your code, use DCHECK_EQ et al below. |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 647 | #define DCHECK_OP(name, op, val1, val2) \ |
[email protected] | 5e98780 | 2010-11-01 19:49:22 | [diff] [blame] | 648 | if (DCHECK_IS_ON()) \ |
[email protected] | 9c7132e | 2011-02-08 07:39:08 | [diff] [blame^] | 649 | if (std::string* _result = \ |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 650 | logging::Check##name##Impl((val1), (val2), \ |
| 651 | #val1 " " #op " " #val2)) \ |
| 652 | logging::LogMessage( \ |
| 653 | __FILE__, __LINE__, ::logging::LOG_DCHECK, \ |
| 654 | _result).stream() |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 655 | |
[email protected] | deba0ff | 2010-11-03 05:30:14 | [diff] [blame] | 656 | // Equality/Inequality checks - compare two values, and log a |
| 657 | // LOG_DCHECK message including the two values when the result is not |
| 658 | // as expected. The values must have operator<<(ostream, ...) |
| 659 | // defined. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 660 | // |
| 661 | // You may append to the error message like so: |
| 662 | // DCHECK_NE(1, 2) << ": The world must be ending!"; |
| 663 | // |
| 664 | // We are very careful to ensure that each argument is evaluated exactly |
| 665 | // once, and that anything which is legal to pass as a function argument is |
| 666 | // legal here. In particular, the arguments may be temporary expressions |
| 667 | // which will end up being destroyed at the end of the apparent statement, |
| 668 | // for example: |
| 669 | // DCHECK_EQ(string("abc")[1], 'b'); |
| 670 | // |
| 671 | // WARNING: These may not compile correctly if one of the arguments is a pointer |
| 672 | // and the other is NULL. To work around this, simply static_cast NULL to the |
| 673 | // type of the desired pointer. |
| 674 | |
| 675 | #define DCHECK_EQ(val1, val2) DCHECK_OP(EQ, ==, val1, val2) |
| 676 | #define DCHECK_NE(val1, val2) DCHECK_OP(NE, !=, val1, val2) |
| 677 | #define DCHECK_LE(val1, val2) DCHECK_OP(LE, <=, val1, val2) |
| 678 | #define DCHECK_LT(val1, val2) DCHECK_OP(LT, < , val1, val2) |
| 679 | #define DCHECK_GE(val1, val2) DCHECK_OP(GE, >=, val1, val2) |
| 680 | #define DCHECK_GT(val1, val2) DCHECK_OP(GT, > , val1, val2) |
| 681 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 682 | #define NOTREACHED() DCHECK(false) |
| 683 | |
| 684 | // Redefine the standard assert to use our nice log files |
| 685 | #undef assert |
| 686 | #define assert(x) DLOG_ASSERT(x) |
| 687 | |
| 688 | // This class more or less represents a particular log message. You |
| 689 | // create an instance of LogMessage and then stream stuff to it. |
| 690 | // When you finish streaming to it, ~LogMessage is called and the |
| 691 | // full message gets streamed to the appropriate destination. |
| 692 | // |
| 693 | // You shouldn't actually use LogMessage's constructor to log things, |
| 694 | // though. You should use the LOG() macro (and variants thereof) |
| 695 | // above. |
| 696 | class LogMessage { |
| 697 | public: |
| 698 | LogMessage(const char* file, int line, LogSeverity severity, int ctr); |
| 699 | |
| 700 | // Two special constructors that generate reduced amounts of code at |
| 701 | // LOG call sites for common cases. |
| 702 | // |
| 703 | // Used for LOG(INFO): Implied are: |
| 704 | // severity = LOG_INFO, ctr = 0 |
| 705 | // |
| 706 | // Using this constructor instead of the more complex constructor above |
| 707 | // saves a couple of bytes per call site. |
| 708 | LogMessage(const char* file, int line); |
| 709 | |
| 710 | // Used for LOG(severity) where severity != INFO. Implied |
| 711 | // are: ctr = 0 |
| 712 | // |
| 713 | // Using this constructor instead of the more complex constructor above |
| 714 | // saves a couple of bytes per call site. |
| 715 | LogMessage(const char* file, int line, LogSeverity severity); |
| 716 | |
[email protected] | 9c7132e | 2011-02-08 07:39:08 | [diff] [blame^] | 717 | // A special constructor used for check failures. Takes ownership |
| 718 | // of the given string. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 719 | // Implied severity = LOG_FATAL |
[email protected] | 9c7132e | 2011-02-08 07:39:08 | [diff] [blame^] | 720 | LogMessage(const char* file, int line, std::string* result); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 721 | |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 722 | // A special constructor used for check failures, with the option to |
[email protected] | 9c7132e | 2011-02-08 07:39:08 | [diff] [blame^] | 723 | // specify severity. Takes ownership of the given string. |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 724 | LogMessage(const char* file, int line, LogSeverity severity, |
[email protected] | 9c7132e | 2011-02-08 07:39:08 | [diff] [blame^] | 725 | std::string* result); |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 726 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 727 | ~LogMessage(); |
| 728 | |
| 729 | std::ostream& stream() { return stream_; } |
| 730 | |
| 731 | private: |
| 732 | void Init(const char* file, int line); |
| 733 | |
| 734 | LogSeverity severity_; |
| 735 | std::ostringstream stream_; |
[email protected] | c8887392 | 2008-07-30 13:02:03 | [diff] [blame] | 736 | size_t message_start_; // Offset of the start of the message (past prefix |
| 737 | // info). |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 738 | // The file and line information passed in to the constructor. |
| 739 | const char* file_; |
| 740 | const int line_; |
| 741 | |
[email protected] | 3f85caa | 2009-04-14 16:52:11 | [diff] [blame] | 742 | #if defined(OS_WIN) |
| 743 | // Stores the current value of GetLastError in the constructor and restores |
| 744 | // it in the destructor by calling SetLastError. |
| 745 | // This is useful since the LogMessage class uses a lot of Win32 calls |
| 746 | // that will lose the value of GLE and the code that called the log function |
| 747 | // will have lost the thread error value when the log call returns. |
| 748 | class SaveLastError { |
| 749 | public: |
| 750 | SaveLastError(); |
| 751 | ~SaveLastError(); |
| 752 | |
| 753 | unsigned long get_error() const { return last_error_; } |
| 754 | |
| 755 | protected: |
| 756 | unsigned long last_error_; |
| 757 | }; |
| 758 | |
| 759 | SaveLastError last_error_; |
| 760 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 761 | |
[email protected] | 39be424 | 2008-08-07 18:31:40 | [diff] [blame] | 762 | DISALLOW_COPY_AND_ASSIGN(LogMessage); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 763 | }; |
| 764 | |
| 765 | // A non-macro interface to the log facility; (useful |
| 766 | // when the logging level is not a compile-time constant). |
| 767 | inline void LogAtLevel(int const log_level, std::string const &msg) { |
| 768 | LogMessage(__FILE__, __LINE__, log_level).stream() << msg; |
| 769 | } |
| 770 | |
| 771 | // This class is used to explicitly ignore values in the conditional |
| 772 | // logging macros. This avoids compiler warnings like "value computed |
| 773 | // is not used" and "statement has no effect". |
| 774 | class LogMessageVoidify { |
| 775 | public: |
| 776 | LogMessageVoidify() { } |
| 777 | // This has to be an operator with a precedence lower than << but |
| 778 | // higher than ?: |
| 779 | void operator&(std::ostream&) { } |
| 780 | }; |
| 781 | |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 782 | #if defined(OS_WIN) |
| 783 | typedef unsigned long SystemErrorCode; |
| 784 | #elif defined(OS_POSIX) |
| 785 | typedef int SystemErrorCode; |
| 786 | #endif |
| 787 | |
| 788 | // Alias for ::GetLastError() on Windows and errno on POSIX. Avoids having to |
| 789 | // pull in windows.h just for GetLastError() and DWORD. |
| 790 | SystemErrorCode GetLastSystemErrorCode(); |
| 791 | |
| 792 | #if defined(OS_WIN) |
| 793 | // Appends a formatted system message of the GetLastError() type. |
| 794 | class Win32ErrorLogMessage { |
| 795 | public: |
| 796 | Win32ErrorLogMessage(const char* file, |
| 797 | int line, |
| 798 | LogSeverity severity, |
| 799 | SystemErrorCode err, |
| 800 | const char* module); |
| 801 | |
| 802 | Win32ErrorLogMessage(const char* file, |
| 803 | int line, |
| 804 | LogSeverity severity, |
| 805 | SystemErrorCode err); |
| 806 | |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 807 | // Appends the error message before destructing the encapsulated class. |
| 808 | ~Win32ErrorLogMessage(); |
| 809 | |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 810 | std::ostream& stream() { return log_message_.stream(); } |
| 811 | |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 812 | private: |
| 813 | SystemErrorCode err_; |
| 814 | // Optional name of the module defining the error. |
| 815 | const char* module_; |
| 816 | LogMessage log_message_; |
| 817 | |
| 818 | DISALLOW_COPY_AND_ASSIGN(Win32ErrorLogMessage); |
| 819 | }; |
| 820 | #elif defined(OS_POSIX) |
| 821 | // Appends a formatted system message of the errno type |
| 822 | class ErrnoLogMessage { |
| 823 | public: |
| 824 | ErrnoLogMessage(const char* file, |
| 825 | int line, |
| 826 | LogSeverity severity, |
| 827 | SystemErrorCode err); |
| 828 | |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 829 | // Appends the error message before destructing the encapsulated class. |
| 830 | ~ErrnoLogMessage(); |
| 831 | |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 832 | std::ostream& stream() { return log_message_.stream(); } |
| 833 | |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 834 | private: |
| 835 | SystemErrorCode err_; |
| 836 | LogMessage log_message_; |
| 837 | |
| 838 | DISALLOW_COPY_AND_ASSIGN(ErrnoLogMessage); |
| 839 | }; |
| 840 | #endif // OS_WIN |
| 841 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 842 | // Closes the log file explicitly if open. |
| 843 | // NOTE: Since the log file is opened as necessary by the action of logging |
| 844 | // statements, there's no guarantee that it will stay closed |
| 845 | // after this call. |
| 846 | void CloseLogFile(); |
| 847 | |
[email protected] | e36ddc8 | 2009-12-08 04:22:50 | [diff] [blame] | 848 | // Async signal safe logging mechanism. |
| 849 | void RawLog(int level, const char* message); |
| 850 | |
| 851 | #define RAW_LOG(level, message) logging::RawLog(logging::LOG_ ## level, message) |
| 852 | |
| 853 | #define RAW_CHECK(condition) \ |
| 854 | do { \ |
| 855 | if (!(condition)) \ |
| 856 | logging::RawLog(logging::LOG_FATAL, "Check failed: " #condition "\n"); \ |
| 857 | } while (0) |
| 858 | |
[email protected] | 39be424 | 2008-08-07 18:31:40 | [diff] [blame] | 859 | } // namespace logging |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 860 | |
[email protected] | 46ce5b56 | 2010-06-16 18:39:53 | [diff] [blame] | 861 | // These functions are provided as a convenience for logging, which is where we |
| 862 | // use streams (it is against Google style to use streams in other places). It |
| 863 | // is designed to allow you to emit non-ASCII Unicode strings to the log file, |
| 864 | // which is normally ASCII. It is relatively slow, so try not to use it for |
| 865 | // common cases. Non-ASCII characters will be converted to UTF-8 by these |
| 866 | // operators. |
| 867 | std::ostream& operator<<(std::ostream& out, const wchar_t* wstr); |
| 868 | inline std::ostream& operator<<(std::ostream& out, const std::wstring& wstr) { |
| 869 | return out << wstr.c_str(); |
| 870 | } |
| 871 | |
[email protected] | 0dfc81b | 2008-08-25 03:44:40 | [diff] [blame] | 872 | // The NOTIMPLEMENTED() macro annotates codepaths which have |
| 873 | // not been implemented yet. |
| 874 | // |
| 875 | // The implementation of this macro is controlled by NOTIMPLEMENTED_POLICY: |
| 876 | // 0 -- Do nothing (stripped by compiler) |
| 877 | // 1 -- Warn at compile time |
| 878 | // 2 -- Fail at compile time |
| 879 | // 3 -- Fail at runtime (DCHECK) |
| 880 | // 4 -- [default] LOG(ERROR) at runtime |
| 881 | // 5 -- LOG(ERROR) at runtime, only once per call-site |
| 882 | |
| 883 | #ifndef NOTIMPLEMENTED_POLICY |
| 884 | // Select default policy: LOG(ERROR) |
| 885 | #define NOTIMPLEMENTED_POLICY 4 |
| 886 | #endif |
| 887 | |
[email protected] | f6cda75 | 2008-10-30 23:54:26 | [diff] [blame] | 888 | #if defined(COMPILER_GCC) |
| 889 | // On Linux, with GCC, we can use __PRETTY_FUNCTION__ to get the demangled name |
| 890 | // of the current function in the NOTIMPLEMENTED message. |
| 891 | #define NOTIMPLEMENTED_MSG "Not implemented reached in " << __PRETTY_FUNCTION__ |
| 892 | #else |
| 893 | #define NOTIMPLEMENTED_MSG "NOT IMPLEMENTED" |
| 894 | #endif |
| 895 | |
[email protected] | 0dfc81b | 2008-08-25 03:44:40 | [diff] [blame] | 896 | #if NOTIMPLEMENTED_POLICY == 0 |
| 897 | #define NOTIMPLEMENTED() ; |
| 898 | #elif NOTIMPLEMENTED_POLICY == 1 |
| 899 | // TODO, figure out how to generate a warning |
| 900 | #define NOTIMPLEMENTED() COMPILE_ASSERT(false, NOT_IMPLEMENTED) |
| 901 | #elif NOTIMPLEMENTED_POLICY == 2 |
| 902 | #define NOTIMPLEMENTED() COMPILE_ASSERT(false, NOT_IMPLEMENTED) |
| 903 | #elif NOTIMPLEMENTED_POLICY == 3 |
| 904 | #define NOTIMPLEMENTED() NOTREACHED() |
| 905 | #elif NOTIMPLEMENTED_POLICY == 4 |
[email protected] | f6cda75 | 2008-10-30 23:54:26 | [diff] [blame] | 906 | #define NOTIMPLEMENTED() LOG(ERROR) << NOTIMPLEMENTED_MSG |
[email protected] | 0dfc81b | 2008-08-25 03:44:40 | [diff] [blame] | 907 | #elif NOTIMPLEMENTED_POLICY == 5 |
| 908 | #define NOTIMPLEMENTED() do {\ |
| 909 | static int count = 0;\ |
[email protected] | f6cda75 | 2008-10-30 23:54:26 | [diff] [blame] | 910 | LOG_IF(ERROR, 0 == count++) << NOTIMPLEMENTED_MSG;\ |
[email protected] | 0dfc81b | 2008-08-25 03:44:40 | [diff] [blame] | 911 | } while(0) |
| 912 | #endif |
| 913 | |
[email protected] | 39a749c | 2011-01-28 02:40:46 | [diff] [blame] | 914 | namespace base { |
| 915 | |
| 916 | class StringPiece; |
| 917 | |
| 918 | // allow StringPiece to be logged (needed for unit testing). |
| 919 | extern std::ostream& operator<<(std::ostream& o, const StringPiece& piece); |
| 920 | |
| 921 | } // namespace base |
| 922 | |
[email protected] | 39be424 | 2008-08-07 18:31:40 | [diff] [blame] | 923 | #endif // BASE_LOGGING_H_ |