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