akalin@chromium.org | 227369d | 2012-01-20 15:33:27 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [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 | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | |
brettw@google.com | e3c034a | 2008-08-08 03:31:40 +0900 | [diff] [blame] | 5 | #ifndef BASE_LOGGING_H_ |
| 6 | #define BASE_LOGGING_H_ |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 7 | |
tzik@chromium.org | 0a1ccf4 | 2011-06-18 20:53:14 +0900 | [diff] [blame] | 8 | #include <cassert> |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <cstring> |
| 11 | #include <sstream> |
| 12 | |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 13 | #include "base/base_export.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 14 | #include "base/basictypes.h" |
akalin@chromium.org | 836c6e6 | 2011-12-02 16:31:09 +0900 | [diff] [blame] | 15 | #include "base/debug/debugger.h" |
rvargas@google.com | 4891e7d | 2011-03-26 03:46:38 +0900 | [diff] [blame] | 16 | #include "build/build_config.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [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 | // |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 55 | // The CHECK(condition) macro is active in both debug and release builds and |
| 56 | // effectively performs a LOG(FATAL) which terminates the process and |
| 57 | // generates a crashdump unless a debugger is attached. |
| 58 | // |
| 59 | // There are also "debug mode" logging macros like the ones above: |
| 60 | // |
| 61 | // DLOG(INFO) << "Found cookies"; |
| 62 | // |
| 63 | // DLOG_IF(INFO, num_cookies > 10) << "Got lots of cookies"; |
| 64 | // |
| 65 | // All "debug mode" logging is compiled away to nothing for non-debug mode |
| 66 | // compiles. LOG_IF and development flags also work well together |
| 67 | // because the code can be compiled away sometimes. |
| 68 | // |
| 69 | // We also have |
| 70 | // |
| 71 | // LOG_ASSERT(assertion); |
| 72 | // DLOG_ASSERT(assertion); |
| 73 | // |
| 74 | // which is syntactic sugar for {,D}LOG_IF(FATAL, assert fails) << assertion; |
| 75 | // |
akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 76 | // There are "verbose level" logging macros. They look like |
| 77 | // |
| 78 | // VLOG(1) << "I'm printed when you run the program with --v=1 or more"; |
| 79 | // VLOG(2) << "I'm printed when you run the program with --v=2 or more"; |
| 80 | // |
| 81 | // These always log at the INFO log level (when they log at all). |
| 82 | // The verbose logging can also be turned on module-by-module. For instance, |
akalin@chromium.org | 859d7d4 | 2010-10-29 09:39:48 +0900 | [diff] [blame] | 83 | // --vmodule=profile=2,icon_loader=1,browser_*=3,*/chromeos/*=4 --v=0 |
akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 84 | // will cause: |
| 85 | // a. VLOG(2) and lower messages to be printed from profile.{h,cc} |
| 86 | // b. VLOG(1) and lower messages to be printed from icon_loader.{h,cc} |
| 87 | // c. VLOG(3) and lower messages to be printed from files prefixed with |
| 88 | // "browser" |
akalin@chromium.org | 55a8a81 | 2010-11-02 05:50:55 +0900 | [diff] [blame] | 89 | // d. VLOG(4) and lower messages to be printed from files under a |
akalin@chromium.org | 859d7d4 | 2010-10-29 09:39:48 +0900 | [diff] [blame] | 90 | // "chromeos" directory. |
akalin@chromium.org | 55a8a81 | 2010-11-02 05:50:55 +0900 | [diff] [blame] | 91 | // e. VLOG(0) and lower messages to be printed from elsewhere |
akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 92 | // |
| 93 | // The wildcarding functionality shown by (c) supports both '*' (match |
akalin@chromium.org | 859d7d4 | 2010-10-29 09:39:48 +0900 | [diff] [blame] | 94 | // 0 or more characters) and '?' (match any single character) |
| 95 | // wildcards. Any pattern containing a forward or backward slash will |
| 96 | // be tested against the whole pathname and not just the module. |
| 97 | // E.g., "*/foo/bar/*=2" would change the logging level for all code |
| 98 | // in source files under a "foo/bar" directory. |
akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 99 | // |
| 100 | // There's also VLOG_IS_ON(n) "verbose level" condition macro. To be used as |
| 101 | // |
| 102 | // if (VLOG_IS_ON(2)) { |
| 103 | // // do some logging preparation and logging |
| 104 | // // that can't be accomplished with just VLOG(2) << ...; |
| 105 | // } |
| 106 | // |
| 107 | // There is also a VLOG_IF "verbose level" condition macro for sample |
| 108 | // cases, when some extra computation and preparation for logs is not |
| 109 | // needed. |
| 110 | // |
| 111 | // VLOG_IF(1, (size > 1024)) |
| 112 | // << "I'm printed when size is more than 1024 and when you run the " |
| 113 | // "program with --v=1 or more"; |
| 114 | // |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 115 | // We also override the standard 'assert' to use 'DLOG_ASSERT'. |
| 116 | // |
tschmelcher@chromium.org | f29a4fc | 2009-10-10 08:52:20 +0900 | [diff] [blame] | 117 | // Lastly, there is: |
| 118 | // |
| 119 | // PLOG(ERROR) << "Couldn't do foo"; |
| 120 | // DPLOG(ERROR) << "Couldn't do foo"; |
| 121 | // PLOG_IF(ERROR, cond) << "Couldn't do foo"; |
| 122 | // DPLOG_IF(ERROR, cond) << "Couldn't do foo"; |
| 123 | // PCHECK(condition) << "Couldn't do foo"; |
| 124 | // DPCHECK(condition) << "Couldn't do foo"; |
| 125 | // |
| 126 | // which append the last system error to the message in string form (taken from |
| 127 | // GetLastError() on Windows and errno on POSIX). |
| 128 | // |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 129 | // The supported severity levels for macros that allow you to specify one |
viettrungluu@chromium.org | 96925ac | 2014-06-17 21:04:23 +0900 | [diff] [blame] | 130 | // are (in increasing order of severity) INFO, WARNING, ERROR, and FATAL. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 131 | // |
| 132 | // Very important: logging a message at the FATAL severity level causes |
| 133 | // the program to terminate (after the message is logged). |
huanr@chromium.org | 656253e | 2009-02-12 10:19:05 +0900 | [diff] [blame] | 134 | // |
viettrungluu@chromium.org | 96925ac | 2014-06-17 21:04:23 +0900 | [diff] [blame] | 135 | // There is the special severity of DFATAL, which logs FATAL in debug mode, |
| 136 | // ERROR in normal mode. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 137 | |
| 138 | namespace logging { |
| 139 | |
akalin@chromium.org | 6d98720 | 2013-06-22 06:15:33 +0900 | [diff] [blame] | 140 | // TODO(avi): do we want to do a unification of character types here? |
| 141 | #if defined(OS_WIN) |
| 142 | typedef wchar_t PathChar; |
| 143 | #else |
| 144 | typedef char PathChar; |
| 145 | #endif |
| 146 | |
| 147 | // Where to record logging output? A flat file and/or system debug log |
| 148 | // via OutputDebugString. |
| 149 | enum LoggingDestination { |
| 150 | LOG_NONE = 0, |
| 151 | LOG_TO_FILE = 1 << 0, |
| 152 | LOG_TO_SYSTEM_DEBUG_LOG = 1 << 1, |
| 153 | |
| 154 | LOG_TO_ALL = LOG_TO_FILE | LOG_TO_SYSTEM_DEBUG_LOG, |
| 155 | |
| 156 | // On Windows, use a file next to the exe; on POSIX platforms, where |
| 157 | // it may not even be possible to locate the executable on disk, use |
| 158 | // stderr. |
| 159 | #if defined(OS_WIN) |
| 160 | LOG_DEFAULT = LOG_TO_FILE, |
| 161 | #elif defined(OS_POSIX) |
| 162 | LOG_DEFAULT = LOG_TO_SYSTEM_DEBUG_LOG, |
| 163 | #endif |
| 164 | }; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 165 | |
| 166 | // Indicates that the log file should be locked when being written to. |
akalin@chromium.org | 6d98720 | 2013-06-22 06:15:33 +0900 | [diff] [blame] | 167 | // Unless there is only one single-threaded process that is logging to |
| 168 | // the log file, the file should be locked during writes to make each |
wangxianzhu@chromium.org | fbb128c | 2014-03-05 10:43:27 +0900 | [diff] [blame] | 169 | // log output atomic. Other writers will block. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 170 | // |
| 171 | // All processes writing to the log file must have their locking set for it to |
akalin@chromium.org | 6d98720 | 2013-06-22 06:15:33 +0900 | [diff] [blame] | 172 | // work properly. Defaults to LOCK_LOG_FILE. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 173 | enum LogLockingState { LOCK_LOG_FILE, DONT_LOCK_LOG_FILE }; |
| 174 | |
| 175 | // On startup, should we delete or append to an existing log file (if any)? |
| 176 | // Defaults to APPEND_TO_OLD_LOG_FILE. |
| 177 | enum OldFileDeletionState { DELETE_OLD_LOG_FILE, APPEND_TO_OLD_LOG_FILE }; |
| 178 | |
akalin@chromium.org | 6d98720 | 2013-06-22 06:15:33 +0900 | [diff] [blame] | 179 | struct BASE_EXPORT LoggingSettings { |
| 180 | // The defaults values are: |
| 181 | // |
| 182 | // logging_dest: LOG_DEFAULT |
| 183 | // log_file: NULL |
| 184 | // lock_log: LOCK_LOG_FILE |
| 185 | // delete_old: APPEND_TO_OLD_LOG_FILE |
akalin@chromium.org | 6d98720 | 2013-06-22 06:15:33 +0900 | [diff] [blame] | 186 | LoggingSettings(); |
| 187 | |
| 188 | LoggingDestination logging_dest; |
| 189 | |
| 190 | // The three settings below have an effect only when LOG_TO_FILE is |
| 191 | // set in |logging_dest|. |
| 192 | const PathChar* log_file; |
| 193 | LogLockingState lock_log; |
| 194 | OldFileDeletionState delete_old; |
akalin@chromium.org | 6d98720 | 2013-06-22 06:15:33 +0900 | [diff] [blame] | 195 | }; |
derat@chromium.org | b2aa7c4 | 2010-08-24 04:57:46 +0900 | [diff] [blame] | 196 | |
| 197 | // Define different names for the BaseInitLoggingImpl() function depending on |
| 198 | // whether NDEBUG is defined or not so that we'll fail to link if someone tries |
| 199 | // to compile logging.cc with NDEBUG but includes logging.h without defining it, |
| 200 | // or vice versa. |
| 201 | #if NDEBUG |
| 202 | #define BaseInitLoggingImpl BaseInitLoggingImpl_built_with_NDEBUG |
| 203 | #else |
| 204 | #define BaseInitLoggingImpl BaseInitLoggingImpl_built_without_NDEBUG |
| 205 | #endif |
| 206 | |
| 207 | // Implementation of the InitLogging() method declared below. We use a |
| 208 | // more-specific name so we can #define it above without affecting other code |
| 209 | // that has named stuff "InitLogging". |
akalin@chromium.org | 6d98720 | 2013-06-22 06:15:33 +0900 | [diff] [blame] | 210 | BASE_EXPORT bool BaseInitLoggingImpl(const LoggingSettings& settings); |
derat@chromium.org | b2aa7c4 | 2010-08-24 04:57:46 +0900 | [diff] [blame] | 211 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 212 | // Sets the log file name and other global logging state. Calling this function |
| 213 | // is recommended, and is normally done at the beginning of application init. |
| 214 | // If you don't call it, all the flags will be initialized to their default |
| 215 | // values, and there is a race condition that may leak a critical section |
| 216 | // object if two threads try to do the first log at the same time. |
| 217 | // See the definition of the enums above for descriptions and default values. |
| 218 | // |
| 219 | // The default log file is initialized to "debug.log" in the application |
| 220 | // directory. You probably don't want this, especially since the program |
| 221 | // directory may not be writable on an enduser's system. |
stevenjb@chromium.org | 6c56fa4 | 2011-12-03 09:30:08 +0900 | [diff] [blame] | 222 | // |
| 223 | // This function may be called a second time to re-direct logging (e.g after |
| 224 | // loging in to a user partition), however it should never be called more than |
| 225 | // twice. |
akalin@chromium.org | 6d98720 | 2013-06-22 06:15:33 +0900 | [diff] [blame] | 226 | inline bool InitLogging(const LoggingSettings& settings) { |
| 227 | return BaseInitLoggingImpl(settings); |
derat@chromium.org | b2aa7c4 | 2010-08-24 04:57:46 +0900 | [diff] [blame] | 228 | } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 229 | |
| 230 | // Sets the log level. Anything at or above this level will be written to the |
| 231 | // log file/displayed to the user (if applicable). Anything below this level |
siggi@chromium.org | 25396e1 | 2010-11-05 00:50:49 +0900 | [diff] [blame] | 232 | // will be silently ignored. The log level defaults to 0 (everything is logged |
| 233 | // up to level INFO) if this function is not called. |
| 234 | // Note that log messages for VLOG(x) are logged at level -x, so setting |
| 235 | // the min log level to negative values enables verbose logging. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 236 | BASE_EXPORT void SetMinLogLevel(int level); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 237 | |
ericroman@google.com | 3845085 | 2009-04-11 04:13:42 +0900 | [diff] [blame] | 238 | // Gets the current log level. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 239 | BASE_EXPORT int GetMinLogLevel(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 240 | |
siggi@chromium.org | 25396e1 | 2010-11-05 00:50:49 +0900 | [diff] [blame] | 241 | // Gets the VLOG default verbosity level. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 242 | BASE_EXPORT int GetVlogVerbosity(); |
siggi@chromium.org | 25396e1 | 2010-11-05 00:50:49 +0900 | [diff] [blame] | 243 | |
akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 244 | // Gets the current vlog level for the given file (usually taken from |
| 245 | // __FILE__). |
akalin@chromium.org | 59c9f21 | 2010-09-30 06:25:14 +0900 | [diff] [blame] | 246 | |
| 247 | // Note that |N| is the size *with* the null terminator. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 248 | BASE_EXPORT int GetVlogLevelHelper(const char* file_start, size_t N); |
akalin@chromium.org | 59c9f21 | 2010-09-30 06:25:14 +0900 | [diff] [blame] | 249 | |
akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 250 | template <size_t N> |
| 251 | int GetVlogLevel(const char (&file)[N]) { |
| 252 | return GetVlogLevelHelper(file, N); |
| 253 | } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 254 | |
| 255 | // Sets the common items you want to be prepended to each log message. |
| 256 | // process and thread IDs default to off, the timestamp defaults to on. |
| 257 | // If this function is not called, logging defaults to writing the timestamp |
| 258 | // only. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 259 | BASE_EXPORT void SetLogItems(bool enable_process_id, bool enable_thread_id, |
| 260 | bool enable_timestamp, bool enable_tickcount); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 261 | |
cmasone@google.com | f780248 | 2010-08-17 09:38:12 +0900 | [diff] [blame] | 262 | // Sets whether or not you'd like to see fatal debug messages popped up in |
| 263 | // a dialog box or not. |
| 264 | // Dialogs are not shown by default. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 265 | BASE_EXPORT void SetShowErrorDialogs(bool enable_dialogs); |
cmasone@google.com | f780248 | 2010-08-17 09:38:12 +0900 | [diff] [blame] | 266 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 267 | // Sets the Log Assert Handler that will be used to notify of check failures. |
huanr@chromium.org | 656253e | 2009-02-12 10:19:05 +0900 | [diff] [blame] | 268 | // The default handler shows a dialog box and then terminate the process, |
| 269 | // however clients can use this function to override with their own handling |
| 270 | // (e.g. a silent one for Unit Tests) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 271 | typedef void (*LogAssertHandlerFunction)(const std::string& str); |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 272 | BASE_EXPORT void SetLogAssertHandler(LogAssertHandlerFunction handler); |
hansl@google.com | 519197c | 2010-11-04 04:20:27 +0900 | [diff] [blame] | 273 | |
siggi@chromium.org | 4db6579 | 2009-11-26 00:26:34 +0900 | [diff] [blame] | 274 | // Sets the Log Message Handler that gets passed every log message before |
| 275 | // it's sent to other log destinations (if any). |
| 276 | // Returns true to signal that it handled the message and the message |
| 277 | // should not be sent to other log destinations. |
siggi@chromium.org | 25396e1 | 2010-11-05 00:50:49 +0900 | [diff] [blame] | 278 | typedef bool (*LogMessageHandlerFunction)(int severity, |
| 279 | const char* file, int line, size_t message_start, const std::string& str); |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 280 | BASE_EXPORT void SetLogMessageHandler(LogMessageHandlerFunction handler); |
| 281 | BASE_EXPORT LogMessageHandlerFunction GetLogMessageHandler(); |
siggi@chromium.org | 4db6579 | 2009-11-26 00:26:34 +0900 | [diff] [blame] | 282 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 283 | typedef int LogSeverity; |
siggi@chromium.org | 25396e1 | 2010-11-05 00:50:49 +0900 | [diff] [blame] | 284 | const LogSeverity LOG_VERBOSE = -1; // This is level 1 verbosity |
| 285 | // Note: the log severities are used to index into the array of names, |
| 286 | // see log_severity_names. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 287 | const LogSeverity LOG_INFO = 0; |
| 288 | const LogSeverity LOG_WARNING = 1; |
| 289 | const LogSeverity LOG_ERROR = 2; |
viettrungluu@chromium.org | 96925ac | 2014-06-17 21:04:23 +0900 | [diff] [blame] | 290 | const LogSeverity LOG_FATAL = 3; |
| 291 | const LogSeverity LOG_NUM_SEVERITIES = 4; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 292 | |
akalin@chromium.org | a88579b | 2010-10-02 08:02:36 +0900 | [diff] [blame] | 293 | // LOG_DFATAL is LOG_FATAL in debug mode, ERROR in normal mode |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 294 | #ifdef NDEBUG |
akalin@chromium.org | a88579b | 2010-10-02 08:02:36 +0900 | [diff] [blame] | 295 | const LogSeverity LOG_DFATAL = LOG_ERROR; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 296 | #else |
akalin@chromium.org | a88579b | 2010-10-02 08:02:36 +0900 | [diff] [blame] | 297 | const LogSeverity LOG_DFATAL = LOG_FATAL; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 298 | #endif |
| 299 | |
| 300 | // A few definitions of macros that don't generate much code. These are used |
| 301 | // by LOG() and LOG_IF, etc. Since these are used all over our code, it's |
| 302 | // better to have compact code for these operations. |
tschmelcher@chromium.org | f29a4fc | 2009-10-10 08:52:20 +0900 | [diff] [blame] | 303 | #define COMPACT_GOOGLE_LOG_EX_INFO(ClassName, ...) \ |
| 304 | logging::ClassName(__FILE__, __LINE__, logging::LOG_INFO , ##__VA_ARGS__) |
| 305 | #define COMPACT_GOOGLE_LOG_EX_WARNING(ClassName, ...) \ |
| 306 | logging::ClassName(__FILE__, __LINE__, logging::LOG_WARNING , ##__VA_ARGS__) |
| 307 | #define COMPACT_GOOGLE_LOG_EX_ERROR(ClassName, ...) \ |
| 308 | logging::ClassName(__FILE__, __LINE__, logging::LOG_ERROR , ##__VA_ARGS__) |
tschmelcher@chromium.org | f29a4fc | 2009-10-10 08:52:20 +0900 | [diff] [blame] | 309 | #define COMPACT_GOOGLE_LOG_EX_FATAL(ClassName, ...) \ |
| 310 | logging::ClassName(__FILE__, __LINE__, logging::LOG_FATAL , ##__VA_ARGS__) |
| 311 | #define COMPACT_GOOGLE_LOG_EX_DFATAL(ClassName, ...) \ |
akalin@chromium.org | a88579b | 2010-10-02 08:02:36 +0900 | [diff] [blame] | 312 | logging::ClassName(__FILE__, __LINE__, logging::LOG_DFATAL , ##__VA_ARGS__) |
tschmelcher@chromium.org | f29a4fc | 2009-10-10 08:52:20 +0900 | [diff] [blame] | 313 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 314 | #define COMPACT_GOOGLE_LOG_INFO \ |
tschmelcher@chromium.org | f29a4fc | 2009-10-10 08:52:20 +0900 | [diff] [blame] | 315 | COMPACT_GOOGLE_LOG_EX_INFO(LogMessage) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 316 | #define COMPACT_GOOGLE_LOG_WARNING \ |
tschmelcher@chromium.org | f29a4fc | 2009-10-10 08:52:20 +0900 | [diff] [blame] | 317 | COMPACT_GOOGLE_LOG_EX_WARNING(LogMessage) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 318 | #define COMPACT_GOOGLE_LOG_ERROR \ |
tschmelcher@chromium.org | f29a4fc | 2009-10-10 08:52:20 +0900 | [diff] [blame] | 319 | COMPACT_GOOGLE_LOG_EX_ERROR(LogMessage) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 320 | #define COMPACT_GOOGLE_LOG_FATAL \ |
tschmelcher@chromium.org | f29a4fc | 2009-10-10 08:52:20 +0900 | [diff] [blame] | 321 | COMPACT_GOOGLE_LOG_EX_FATAL(LogMessage) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 322 | #define COMPACT_GOOGLE_LOG_DFATAL \ |
tschmelcher@chromium.org | f29a4fc | 2009-10-10 08:52:20 +0900 | [diff] [blame] | 323 | COMPACT_GOOGLE_LOG_EX_DFATAL(LogMessage) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 324 | |
joth@chromium.org | c68f810 | 2013-01-10 11:41:57 +0900 | [diff] [blame] | 325 | #if defined(OS_WIN) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 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 |
tschmelcher@chromium.org | f29a4fc | 2009-10-10 08:52:20 +0900 | [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 |
akalin@chromium.org | a88579b | 2010-10-02 08:02:36 +0900 | [diff] [blame] | 335 | // Needed for LOG_IS_ON(ERROR). |
| 336 | const LogSeverity LOG_0 = LOG_ERROR; |
joth@chromium.org | c68f810 | 2013-01-10 11:41:57 +0900 | [diff] [blame] | 337 | #endif |
akalin@chromium.org | a88579b | 2010-10-02 08:02:36 +0900 | [diff] [blame] | 338 | |
viettrungluu@chromium.org | 96925ac | 2014-06-17 21:04:23 +0900 | [diff] [blame] | 339 | // As special cases, we can assume that LOG_IS_ON(FATAL) always holds. Also, |
| 340 | // LOG_IS_ON(DFATAL) always holds in debug mode. In particular, CHECK()s will |
| 341 | // always fire if they fail. |
akalin@chromium.org | a88579b | 2010-10-02 08:02:36 +0900 | [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 |
chcunningham | 457d6d7 | 2015-02-07 10:58:37 +0900 | [diff] [blame] | 353 | // the condition doesn't hold. Condition is evaluated once and only once. |
akalin@chromium.org | a88579b | 2010-10-02 08:02:36 +0900 | [diff] [blame] | 354 | #define LAZY_STREAM(stream, condition) \ |
| 355 | !(condition) ? (void) 0 : ::logging::LogMessageVoidify() & (stream) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [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. |
akalin@chromium.org | a88579b | 2010-10-02 08:02:36 +0900 | [diff] [blame] | 365 | #define LOG_STREAM(severity) COMPACT_GOOGLE_LOG_ ## severity.stream() |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 366 | |
akalin@chromium.org | a88579b | 2010-10-02 08:02:36 +0900 | [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 | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 371 | #define SYSLOG(severity) LOG(severity) |
akalin@chromium.org | a88579b | 2010-10-02 08:02:36 +0900 | [diff] [blame] | 372 | #define SYSLOG_IF(severity, condition) LOG_IF(severity, condition) |
| 373 | |
siggi@chromium.org | 25396e1 | 2010-11-05 00:50:49 +0900 | [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)) |
akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 384 | |
sail@chromium.org | 4015fc0 | 2011-03-07 03:16:31 +0900 | [diff] [blame] | 385 | #if defined (OS_WIN) |
| 386 | #define VPLOG_STREAM(verbose_level) \ |
| 387 | logging::Win32ErrorLogMessage(__FILE__, __LINE__, -verbose_level, \ |
| 388 | ::logging::GetLastSystemErrorCode()).stream() |
| 389 | #elif defined(OS_POSIX) |
| 390 | #define VPLOG_STREAM(verbose_level) \ |
| 391 | logging::ErrnoLogMessage(__FILE__, __LINE__, -verbose_level, \ |
| 392 | ::logging::GetLastSystemErrorCode()).stream() |
| 393 | #endif |
| 394 | |
| 395 | #define VPLOG(verbose_level) \ |
| 396 | LAZY_STREAM(VPLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level)) |
| 397 | |
| 398 | #define VPLOG_IF(verbose_level, condition) \ |
| 399 | LAZY_STREAM(VPLOG_STREAM(verbose_level), \ |
| 400 | VLOG_IS_ON(verbose_level) && (condition)) |
| 401 | |
akalin@chromium.org | f0ee79c | 2010-09-30 04:26:36 +0900 | [diff] [blame] | 402 | // TODO(akalin): Add more VLOG variants, e.g. VPLOG. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 403 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 404 | #define LOG_ASSERT(condition) \ |
| 405 | LOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". " |
| 406 | #define SYSLOG_ASSERT(condition) \ |
| 407 | SYSLOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". " |
| 408 | |
tschmelcher@chromium.org | f29a4fc | 2009-10-10 08:52:20 +0900 | [diff] [blame] | 409 | #if defined(OS_WIN) |
vitalybuka@chromium.org | e871736 | 2014-04-23 10:11:01 +0900 | [diff] [blame] | 410 | #define PLOG_STREAM(severity) \ |
tschmelcher@chromium.org | f29a4fc | 2009-10-10 08:52:20 +0900 | [diff] [blame] | 411 | COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \ |
| 412 | ::logging::GetLastSystemErrorCode()).stream() |
tschmelcher@chromium.org | f29a4fc | 2009-10-10 08:52:20 +0900 | [diff] [blame] | 413 | #elif defined(OS_POSIX) |
vitalybuka@chromium.org | e871736 | 2014-04-23 10:11:01 +0900 | [diff] [blame] | 414 | #define PLOG_STREAM(severity) \ |
tschmelcher@chromium.org | f29a4fc | 2009-10-10 08:52:20 +0900 | [diff] [blame] | 415 | COMPACT_GOOGLE_LOG_EX_ ## severity(ErrnoLogMessage, \ |
| 416 | ::logging::GetLastSystemErrorCode()).stream() |
tschmelcher@chromium.org | f29a4fc | 2009-10-10 08:52:20 +0900 | [diff] [blame] | 417 | #endif |
| 418 | |
akalin@chromium.org | a88579b | 2010-10-02 08:02:36 +0900 | [diff] [blame] | 419 | #define PLOG(severity) \ |
| 420 | LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity)) |
| 421 | |
tschmelcher@chromium.org | f29a4fc | 2009-10-10 08:52:20 +0900 | [diff] [blame] | 422 | #define PLOG_IF(severity, condition) \ |
akalin@chromium.org | a88579b | 2010-10-02 08:02:36 +0900 | [diff] [blame] | 423 | LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity) && (condition)) |
tschmelcher@chromium.org | f29a4fc | 2009-10-10 08:52:20 +0900 | [diff] [blame] | 424 | |
akalin@chromium.org | 836c6e6 | 2011-12-02 16:31:09 +0900 | [diff] [blame] | 425 | // The actual stream used isn't important. |
| 426 | #define EAT_STREAM_PARAMETERS \ |
| 427 | true ? (void) 0 : ::logging::LogMessageVoidify() & LOG_STREAM(FATAL) |
| 428 | |
erikwright | b02505e | 2015-07-23 05:05:52 +0900 | [diff] [blame] | 429 | // Captures the result of a CHECK_EQ (for example) and facilitates testing as a |
| 430 | // boolean. |
| 431 | class CheckOpResult { |
| 432 | public: |
| 433 | // |message| must be null if and only if the check failed. |
| 434 | CheckOpResult(std::string* message) : message_(message) {} |
| 435 | // Returns true if the check succeeded. |
| 436 | operator bool() const { return !message_; } |
| 437 | // Returns the message. |
| 438 | std::string* message() { return message_; } |
| 439 | |
| 440 | private: |
| 441 | std::string* message_; |
| 442 | }; |
| 443 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 444 | // CHECK dies with a fatal error if condition is not true. It is *not* |
| 445 | // controlled by NDEBUG, so the check will be executed regardless of |
| 446 | // compilation mode. |
akalin@chromium.org | a88579b | 2010-10-02 08:02:36 +0900 | [diff] [blame] | 447 | // |
| 448 | // We make sure CHECK et al. always evaluates their arguments, as |
| 449 | // doing CHECK(FunctionWithSideEffect()) is a common idiom. |
akalin@chromium.org | 836c6e6 | 2011-12-02 16:31:09 +0900 | [diff] [blame] | 450 | |
feng@chromium.org | 15135cd | 2014-07-09 07:52:33 +0900 | [diff] [blame] | 451 | #if defined(OFFICIAL_BUILD) && defined(NDEBUG) && !defined(OS_ANDROID) |
akalin@chromium.org | 836c6e6 | 2011-12-02 16:31:09 +0900 | [diff] [blame] | 452 | |
| 453 | // Make all CHECK functions discard their log strings to reduce code |
tnagel | 45944c2 | 2015-07-12 23:17:45 +0900 | [diff] [blame] | 454 | // bloat for official release builds (except Android). |
akalin@chromium.org | 836c6e6 | 2011-12-02 16:31:09 +0900 | [diff] [blame] | 455 | |
| 456 | // TODO(akalin): This would be more valuable if there were some way to |
| 457 | // remove BreakDebugger() from the backtrace, perhaps by turning it |
| 458 | // into a macro (like __debugbreak() on Windows). |
| 459 | #define CHECK(condition) \ |
| 460 | !(condition) ? ::base::debug::BreakDebugger() : EAT_STREAM_PARAMETERS |
| 461 | |
| 462 | #define PCHECK(condition) CHECK(condition) |
| 463 | |
| 464 | #define CHECK_OP(name, op, val1, val2) CHECK((val1) op (val2)) |
| 465 | |
| 466 | #else |
| 467 | |
brucedawson | cee3941 | 2014-10-24 05:14:14 +0900 | [diff] [blame] | 468 | #if defined(_PREFAST_) && defined(OS_WIN) |
| 469 | // Use __analysis_assume to tell the VC++ static analysis engine that |
| 470 | // assert conditions are true, to suppress warnings. The LAZY_STREAM |
| 471 | // parameter doesn't reference 'condition' in /analyze builds because |
| 472 | // this evaluation confuses /analyze. The !! before condition is because |
| 473 | // __analysis_assume gets confused on some conditions: |
| 474 | // http://randomascii.wordpress.com/2011/09/13/analyze-for-visual-studio-the-ugly-part-5/ |
| 475 | |
| 476 | #define CHECK(condition) \ |
| 477 | __analysis_assume(!!(condition)), \ |
| 478 | LAZY_STREAM(LOG_STREAM(FATAL), false) \ |
| 479 | << "Check failed: " #condition ". " |
| 480 | |
| 481 | #define PCHECK(condition) \ |
| 482 | __analysis_assume(!!(condition)), \ |
| 483 | LAZY_STREAM(PLOG_STREAM(FATAL), false) \ |
| 484 | << "Check failed: " #condition ". " |
| 485 | |
| 486 | #else // _PREFAST_ |
| 487 | |
tnagel | 8645808 | 2015-07-12 23:19:28 +0900 | [diff] [blame] | 488 | // Do as much work as possible out of line to reduce inline code size. |
| 489 | #define CHECK(condition) \ |
| 490 | LAZY_STREAM(logging::LogMessage(__FILE__, __LINE__, #condition).stream(), \ |
| 491 | !(condition)) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 492 | |
brucedawson | cee3941 | 2014-10-24 05:14:14 +0900 | [diff] [blame] | 493 | #define PCHECK(condition) \ |
akalin@chromium.org | a88579b | 2010-10-02 08:02:36 +0900 | [diff] [blame] | 494 | LAZY_STREAM(PLOG_STREAM(FATAL), !(condition)) \ |
| 495 | << "Check failed: " #condition ". " |
tschmelcher@chromium.org | f29a4fc | 2009-10-10 08:52:20 +0900 | [diff] [blame] | 496 | |
brucedawson | cee3941 | 2014-10-24 05:14:14 +0900 | [diff] [blame] | 497 | #endif // _PREFAST_ |
| 498 | |
akalin@chromium.org | 836c6e6 | 2011-12-02 16:31:09 +0900 | [diff] [blame] | 499 | // Helper macro for binary operators. |
| 500 | // Don't use this macro directly in your code, use CHECK_EQ et al below. |
erikwright | b02505e | 2015-07-23 05:05:52 +0900 | [diff] [blame] | 501 | // The 'switch' is used to prevent the 'else' from being ambiguous when the |
| 502 | // macro is used in an 'if' clause such as: |
| 503 | // if (a == 1) |
| 504 | // CHECK_EQ(2, a); |
| 505 | #define CHECK_OP(name, op, val1, val2) \ |
| 506 | switch (0) case 0: default: \ |
| 507 | if (logging::CheckOpResult true_if_passed = \ |
| 508 | logging::Check##name##Impl((val1), (val2), \ |
| 509 | #val1 " " #op " " #val2)) \ |
| 510 | ; \ |
| 511 | else \ |
| 512 | logging::LogMessage(__FILE__, __LINE__, true_if_passed.message()).stream() |
akalin@chromium.org | 836c6e6 | 2011-12-02 16:31:09 +0900 | [diff] [blame] | 513 | |
| 514 | #endif |
| 515 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 516 | // Build the error message string. This is separate from the "Impl" |
| 517 | // function template because it is not performance critical and so can |
akalin@chromium.org | 63dab76 | 2011-02-08 16:39:08 +0900 | [diff] [blame] | 518 | // be out of line, while the "Impl" code should be inline. Caller |
| 519 | // takes ownership of the returned string. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 520 | template<class t1, class t2> |
| 521 | std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) { |
| 522 | std::ostringstream ss; |
| 523 | ss << names << " (" << v1 << " vs. " << v2 << ")"; |
| 524 | std::string* msg = new std::string(ss.str()); |
| 525 | return msg; |
| 526 | } |
| 527 | |
erg@google.com | 6575f36 | 2010-10-01 04:10:03 +0900 | [diff] [blame] | 528 | // Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated |
| 529 | // in logging.cc. |
erg@chromium.org | 37128d4 | 2011-10-25 05:20:30 +0900 | [diff] [blame] | 530 | extern template BASE_EXPORT std::string* MakeCheckOpString<int, int>( |
erg@google.com | 6575f36 | 2010-10-01 04:10:03 +0900 | [diff] [blame] | 531 | const int&, const int&, const char* names); |
erg@chromium.org | 37128d4 | 2011-10-25 05:20:30 +0900 | [diff] [blame] | 532 | extern template BASE_EXPORT |
| 533 | std::string* MakeCheckOpString<unsigned long, unsigned long>( |
erg@google.com | 6575f36 | 2010-10-01 04:10:03 +0900 | [diff] [blame] | 534 | const unsigned long&, const unsigned long&, const char* names); |
erg@chromium.org | 37128d4 | 2011-10-25 05:20:30 +0900 | [diff] [blame] | 535 | extern template BASE_EXPORT |
| 536 | std::string* MakeCheckOpString<unsigned long, unsigned int>( |
erg@google.com | 6575f36 | 2010-10-01 04:10:03 +0900 | [diff] [blame] | 537 | const unsigned long&, const unsigned int&, const char* names); |
erg@chromium.org | 37128d4 | 2011-10-25 05:20:30 +0900 | [diff] [blame] | 538 | extern template BASE_EXPORT |
| 539 | std::string* MakeCheckOpString<unsigned int, unsigned long>( |
erg@google.com | 6575f36 | 2010-10-01 04:10:03 +0900 | [diff] [blame] | 540 | const unsigned int&, const unsigned long&, const char* names); |
erg@chromium.org | 37128d4 | 2011-10-25 05:20:30 +0900 | [diff] [blame] | 541 | extern template BASE_EXPORT |
| 542 | std::string* MakeCheckOpString<std::string, std::string>( |
erg@google.com | 6575f36 | 2010-10-01 04:10:03 +0900 | [diff] [blame] | 543 | const std::string&, const std::string&, const char* name); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 544 | |
akalin@chromium.org | ad6c172 | 2010-11-02 07:19:56 +0900 | [diff] [blame] | 545 | // Helper functions for CHECK_OP macro. |
| 546 | // The (int, int) specialization works around the issue that the compiler |
| 547 | // will not instantiate the template version of the function on values of |
| 548 | // unnamed enum type - see comment below. |
| 549 | #define DEFINE_CHECK_OP_IMPL(name, op) \ |
| 550 | template <class t1, class t2> \ |
| 551 | inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \ |
| 552 | const char* names) { \ |
| 553 | if (v1 op v2) return NULL; \ |
| 554 | else return MakeCheckOpString(v1, v2, names); \ |
| 555 | } \ |
| 556 | inline std::string* Check##name##Impl(int v1, int v2, const char* names) { \ |
| 557 | if (v1 op v2) return NULL; \ |
| 558 | else return MakeCheckOpString(v1, v2, names); \ |
| 559 | } |
| 560 | DEFINE_CHECK_OP_IMPL(EQ, ==) |
| 561 | DEFINE_CHECK_OP_IMPL(NE, !=) |
| 562 | DEFINE_CHECK_OP_IMPL(LE, <=) |
| 563 | DEFINE_CHECK_OP_IMPL(LT, < ) |
| 564 | DEFINE_CHECK_OP_IMPL(GE, >=) |
| 565 | DEFINE_CHECK_OP_IMPL(GT, > ) |
| 566 | #undef DEFINE_CHECK_OP_IMPL |
willchan@chromium.org | e5ec820 | 2010-03-02 09:41:12 +0900 | [diff] [blame] | 567 | |
| 568 | #define CHECK_EQ(val1, val2) CHECK_OP(EQ, ==, val1, val2) |
| 569 | #define CHECK_NE(val1, val2) CHECK_OP(NE, !=, val1, val2) |
| 570 | #define CHECK_LE(val1, val2) CHECK_OP(LE, <=, val1, val2) |
| 571 | #define CHECK_LT(val1, val2) CHECK_OP(LT, < , val1, val2) |
| 572 | #define CHECK_GE(val1, val2) CHECK_OP(GE, >=, val1, val2) |
| 573 | #define CHECK_GT(val1, val2) CHECK_OP(GT, > , val1, val2) |
| 574 | |
wangxianzhu@chromium.org | 75a5cd9 | 2014-03-11 03:23:38 +0900 | [diff] [blame] | 575 | #if defined(NDEBUG) |
akalin@chromium.org | 8551048 | 2010-10-01 06:12:33 +0900 | [diff] [blame] | 576 | #define ENABLE_DLOG 0 |
akalin@chromium.org | 8551048 | 2010-10-01 06:12:33 +0900 | [diff] [blame] | 577 | #else |
akalin@chromium.org | 8551048 | 2010-10-01 06:12:33 +0900 | [diff] [blame] | 578 | #define ENABLE_DLOG 1 |
wangxianzhu@chromium.org | 75a5cd9 | 2014-03-11 03:23:38 +0900 | [diff] [blame] | 579 | #endif |
| 580 | |
| 581 | #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
danakj | 9a89f24 | 2015-01-09 08:35:58 +0900 | [diff] [blame] | 582 | #define DCHECK_IS_ON() 0 |
wangxianzhu@chromium.org | 75a5cd9 | 2014-03-11 03:23:38 +0900 | [diff] [blame] | 583 | #else |
danakj | 9a89f24 | 2015-01-09 08:35:58 +0900 | [diff] [blame] | 584 | #define DCHECK_IS_ON() 1 |
mark@chromium.org | 667f621 | 2009-08-20 10:20:29 +0900 | [diff] [blame] | 585 | #endif |
| 586 | |
akalin@chromium.org | 8551048 | 2010-10-01 06:12:33 +0900 | [diff] [blame] | 587 | // Definitions for DLOG et al. |
| 588 | |
akalin@chromium.org | f6b59fa | 2010-10-01 11:58:24 +0900 | [diff] [blame] | 589 | #if ENABLE_DLOG |
| 590 | |
akalin@chromium.org | 25cef53 | 2010-11-02 04:49:22 +0900 | [diff] [blame] | 591 | #define DLOG_IS_ON(severity) LOG_IS_ON(severity) |
akalin@chromium.org | f6b59fa | 2010-10-01 11:58:24 +0900 | [diff] [blame] | 592 | #define DLOG_IF(severity, condition) LOG_IF(severity, condition) |
| 593 | #define DLOG_ASSERT(condition) LOG_ASSERT(condition) |
akalin@chromium.org | f6b59fa | 2010-10-01 11:58:24 +0900 | [diff] [blame] | 594 | #define DPLOG_IF(severity, condition) PLOG_IF(severity, condition) |
akalin@chromium.org | a88579b | 2010-10-02 08:02:36 +0900 | [diff] [blame] | 595 | #define DVLOG_IF(verboselevel, condition) VLOG_IF(verboselevel, condition) |
sail@chromium.org | 4015fc0 | 2011-03-07 03:16:31 +0900 | [diff] [blame] | 596 | #define DVPLOG_IF(verboselevel, condition) VPLOG_IF(verboselevel, condition) |
akalin@chromium.org | f6b59fa | 2010-10-01 11:58:24 +0900 | [diff] [blame] | 597 | |
| 598 | #else // ENABLE_DLOG |
| 599 | |
akalin@chromium.org | a88579b | 2010-10-02 08:02:36 +0900 | [diff] [blame] | 600 | // If ENABLE_DLOG is off, we want to avoid emitting any references to |
| 601 | // |condition| (which may reference a variable defined only if NDEBUG |
| 602 | // is not defined). Contrast this with DCHECK et al., which has |
| 603 | // different behavior. |
akalin@chromium.org | f6b59fa | 2010-10-01 11:58:24 +0900 | [diff] [blame] | 604 | |
akalin@chromium.org | 25cef53 | 2010-11-02 04:49:22 +0900 | [diff] [blame] | 605 | #define DLOG_IS_ON(severity) false |
akalin@chromium.org | 836c6e6 | 2011-12-02 16:31:09 +0900 | [diff] [blame] | 606 | #define DLOG_IF(severity, condition) EAT_STREAM_PARAMETERS |
| 607 | #define DLOG_ASSERT(condition) EAT_STREAM_PARAMETERS |
| 608 | #define DPLOG_IF(severity, condition) EAT_STREAM_PARAMETERS |
| 609 | #define DVLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS |
| 610 | #define DVPLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS |
akalin@chromium.org | f6b59fa | 2010-10-01 11:58:24 +0900 | [diff] [blame] | 611 | |
| 612 | #endif // ENABLE_DLOG |
| 613 | |
akalin@chromium.org | 8551048 | 2010-10-01 06:12:33 +0900 | [diff] [blame] | 614 | // DEBUG_MODE is for uses like |
| 615 | // if (DEBUG_MODE) foo.CheckThatFoo(); |
| 616 | // instead of |
| 617 | // #ifndef NDEBUG |
| 618 | // foo.CheckThatFoo(); |
| 619 | // #endif |
| 620 | // |
| 621 | // We tie its state to ENABLE_DLOG. |
| 622 | enum { DEBUG_MODE = ENABLE_DLOG }; |
| 623 | |
| 624 | #undef ENABLE_DLOG |
| 625 | |
akalin@chromium.org | a88579b | 2010-10-02 08:02:36 +0900 | [diff] [blame] | 626 | #define DLOG(severity) \ |
| 627 | LAZY_STREAM(LOG_STREAM(severity), DLOG_IS_ON(severity)) |
| 628 | |
akalin@chromium.org | a88579b | 2010-10-02 08:02:36 +0900 | [diff] [blame] | 629 | #define DPLOG(severity) \ |
| 630 | LAZY_STREAM(PLOG_STREAM(severity), DLOG_IS_ON(severity)) |
| 631 | |
akalin@chromium.org | a3c1d48 | 2011-10-25 15:28:45 +0900 | [diff] [blame] | 632 | #define DVLOG(verboselevel) DVLOG_IF(verboselevel, VLOG_IS_ON(verboselevel)) |
akalin@chromium.org | a88579b | 2010-10-02 08:02:36 +0900 | [diff] [blame] | 633 | |
sail@chromium.org | 4015fc0 | 2011-03-07 03:16:31 +0900 | [diff] [blame] | 634 | #define DVPLOG(verboselevel) DVPLOG_IF(verboselevel, VLOG_IS_ON(verboselevel)) |
| 635 | |
akalin@chromium.org | a88579b | 2010-10-02 08:02:36 +0900 | [diff] [blame] | 636 | // Definitions for DCHECK et al. |
akalin@chromium.org | f6b59fa | 2010-10-01 11:58:24 +0900 | [diff] [blame] | 637 | |
danakj | 9a89f24 | 2015-01-09 08:35:58 +0900 | [diff] [blame] | 638 | #if DCHECK_IS_ON() |
mark@chromium.org | 667f621 | 2009-08-20 10:20:29 +0900 | [diff] [blame] | 639 | |
akalin@chromium.org | 434c5b6 | 2010-11-03 14:30:14 +0900 | [diff] [blame] | 640 | #define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \ |
| 641 | COMPACT_GOOGLE_LOG_EX_FATAL(ClassName , ##__VA_ARGS__) |
| 642 | #define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_FATAL |
akalin@chromium.org | a88579b | 2010-10-02 08:02:36 +0900 | [diff] [blame] | 643 | const LogSeverity LOG_DCHECK = LOG_FATAL; |
| 644 | |
danakj | 9a89f24 | 2015-01-09 08:35:58 +0900 | [diff] [blame] | 645 | #else // DCHECK_IS_ON() |
akalin@chromium.org | a88579b | 2010-10-02 08:02:36 +0900 | [diff] [blame] | 646 | |
wangxianzhu@chromium.org | 1eea140 | 2014-03-15 03:39:53 +0900 | [diff] [blame] | 647 | // These are just dummy values. |
akalin@chromium.org | 434c5b6 | 2010-11-03 14:30:14 +0900 | [diff] [blame] | 648 | #define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \ |
| 649 | COMPACT_GOOGLE_LOG_EX_INFO(ClassName , ##__VA_ARGS__) |
| 650 | #define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_INFO |
| 651 | const LogSeverity LOG_DCHECK = LOG_INFO; |
akalin@chromium.org | a88579b | 2010-10-02 08:02:36 +0900 | [diff] [blame] | 652 | |
danakj | 9a89f24 | 2015-01-09 08:35:58 +0900 | [diff] [blame] | 653 | #endif // DCHECK_IS_ON() |
akalin@chromium.org | a88579b | 2010-10-02 08:02:36 +0900 | [diff] [blame] | 654 | |
akalin@chromium.org | 434c5b6 | 2010-11-03 14:30:14 +0900 | [diff] [blame] | 655 | // DCHECK et al. make sure to reference |condition| regardless of |
akalin@chromium.org | a88579b | 2010-10-02 08:02:36 +0900 | [diff] [blame] | 656 | // whether DCHECKs are enabled; this is so that we don't get unused |
| 657 | // variable warnings if the only use of a variable is in a DCHECK. |
| 658 | // This behavior is different from DLOG_IF et al. |
| 659 | |
brucedawson | cee3941 | 2014-10-24 05:14:14 +0900 | [diff] [blame] | 660 | #if defined(_PREFAST_) && defined(OS_WIN) |
| 661 | // See comments on the previous use of __analysis_assume. |
| 662 | |
| 663 | #define DCHECK(condition) \ |
| 664 | __analysis_assume(!!(condition)), \ |
| 665 | LAZY_STREAM(LOG_STREAM(DCHECK), false) \ |
| 666 | << "Check failed: " #condition ". " |
| 667 | |
| 668 | #define DPCHECK(condition) \ |
| 669 | __analysis_assume(!!(condition)), \ |
| 670 | LAZY_STREAM(PLOG_STREAM(DCHECK), false) \ |
| 671 | << "Check failed: " #condition ". " |
| 672 | |
| 673 | #else // _PREFAST_ |
| 674 | |
piman | 9b9688b | 2015-05-06 05:25:56 +0900 | [diff] [blame] | 675 | #define DCHECK(condition) \ |
| 676 | LAZY_STREAM(LOG_STREAM(DCHECK), DCHECK_IS_ON() ? !(condition) : false) \ |
danakj | 9a89f24 | 2015-01-09 08:35:58 +0900 | [diff] [blame] | 677 | << "Check failed: " #condition ". " |
akalin@chromium.org | a88579b | 2010-10-02 08:02:36 +0900 | [diff] [blame] | 678 | |
piman | 9b9688b | 2015-05-06 05:25:56 +0900 | [diff] [blame] | 679 | #define DPCHECK(condition) \ |
| 680 | LAZY_STREAM(PLOG_STREAM(DCHECK), DCHECK_IS_ON() ? !(condition) : false) \ |
danakj | 9a89f24 | 2015-01-09 08:35:58 +0900 | [diff] [blame] | 681 | << "Check failed: " #condition ". " |
akalin@chromium.org | f6b59fa | 2010-10-01 11:58:24 +0900 | [diff] [blame] | 682 | |
brucedawson | cee3941 | 2014-10-24 05:14:14 +0900 | [diff] [blame] | 683 | #endif // _PREFAST_ |
| 684 | |
akalin@chromium.org | f6b59fa | 2010-10-01 11:58:24 +0900 | [diff] [blame] | 685 | // Helper macro for binary operators. |
| 686 | // Don't use this macro directly in your code, use DCHECK_EQ et al below. |
erikwright | b02505e | 2015-07-23 05:05:52 +0900 | [diff] [blame] | 687 | // The 'switch' is used to prevent the 'else' from being ambiguous when the |
| 688 | // macro is used in an 'if' clause such as: |
| 689 | // if (a == 1) |
| 690 | // DCHECK_EQ(2, a); |
| 691 | #define DCHECK_OP(name, op, val1, val2) \ |
| 692 | switch (0) case 0: default: \ |
| 693 | if (logging::CheckOpResult true_if_passed = \ |
| 694 | DCHECK_IS_ON() ? \ |
| 695 | logging::Check##name##Impl((val1), (val2), \ |
| 696 | #val1 " " #op " " #val2) : nullptr) \ |
| 697 | ; \ |
| 698 | else \ |
| 699 | logging::LogMessage(__FILE__, __LINE__, ::logging::LOG_DCHECK, \ |
| 700 | true_if_passed.message()).stream() |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 701 | |
akalin@chromium.org | 434c5b6 | 2010-11-03 14:30:14 +0900 | [diff] [blame] | 702 | // Equality/Inequality checks - compare two values, and log a |
| 703 | // LOG_DCHECK message including the two values when the result is not |
| 704 | // as expected. The values must have operator<<(ostream, ...) |
| 705 | // defined. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 706 | // |
| 707 | // You may append to the error message like so: |
| 708 | // DCHECK_NE(1, 2) << ": The world must be ending!"; |
| 709 | // |
| 710 | // We are very careful to ensure that each argument is evaluated exactly |
| 711 | // once, and that anything which is legal to pass as a function argument is |
| 712 | // legal here. In particular, the arguments may be temporary expressions |
| 713 | // which will end up being destroyed at the end of the apparent statement, |
| 714 | // for example: |
| 715 | // DCHECK_EQ(string("abc")[1], 'b'); |
| 716 | // |
| 717 | // WARNING: These may not compile correctly if one of the arguments is a pointer |
| 718 | // and the other is NULL. To work around this, simply static_cast NULL to the |
| 719 | // type of the desired pointer. |
| 720 | |
| 721 | #define DCHECK_EQ(val1, val2) DCHECK_OP(EQ, ==, val1, val2) |
| 722 | #define DCHECK_NE(val1, val2) DCHECK_OP(NE, !=, val1, val2) |
| 723 | #define DCHECK_LE(val1, val2) DCHECK_OP(LE, <=, val1, val2) |
| 724 | #define DCHECK_LT(val1, val2) DCHECK_OP(LT, < , val1, val2) |
| 725 | #define DCHECK_GE(val1, val2) DCHECK_OP(GE, >=, val1, val2) |
| 726 | #define DCHECK_GT(val1, val2) DCHECK_OP(GT, > , val1, val2) |
| 727 | |
danakj | 9a89f24 | 2015-01-09 08:35:58 +0900 | [diff] [blame] | 728 | #if !DCHECK_IS_ON() && defined(OS_CHROMEOS) |
tnagel | 5c13f10 | 2015-05-24 21:59:14 +0900 | [diff] [blame] | 729 | // Implement logging of NOTREACHED() as a dedicated function to get function |
| 730 | // call overhead down to a minimum. |
| 731 | void LogErrorNotReached(const char* file, int line); |
| 732 | #define NOTREACHED() \ |
| 733 | true ? ::logging::LogErrorNotReached(__FILE__, __LINE__) \ |
| 734 | : EAT_STREAM_PARAMETERS |
zork@chromium.org | 7fd86b3 | 2013-09-26 16:55:21 +0900 | [diff] [blame] | 735 | #else |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 736 | #define NOTREACHED() DCHECK(false) |
zork@chromium.org | 7fd86b3 | 2013-09-26 16:55:21 +0900 | [diff] [blame] | 737 | #endif |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 738 | |
| 739 | // Redefine the standard assert to use our nice log files |
| 740 | #undef assert |
| 741 | #define assert(x) DLOG_ASSERT(x) |
| 742 | |
| 743 | // This class more or less represents a particular log message. You |
| 744 | // create an instance of LogMessage and then stream stuff to it. |
| 745 | // When you finish streaming to it, ~LogMessage is called and the |
| 746 | // full message gets streamed to the appropriate destination. |
| 747 | // |
| 748 | // You shouldn't actually use LogMessage's constructor to log things, |
| 749 | // though. You should use the LOG() macro (and variants thereof) |
| 750 | // above. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 751 | class BASE_EXPORT LogMessage { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 752 | public: |
viettrungluu@chromium.org | 3dc979c | 2014-06-19 00:02:22 +0900 | [diff] [blame] | 753 | // Used for LOG(severity). |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 754 | LogMessage(const char* file, int line, LogSeverity severity); |
| 755 | |
tnagel | 8645808 | 2015-07-12 23:19:28 +0900 | [diff] [blame] | 756 | // Used for CHECK(). Implied severity = LOG_FATAL. |
| 757 | LogMessage(const char* file, int line, const char* condition); |
| 758 | |
viettrungluu@chromium.org | 3dc979c | 2014-06-19 00:02:22 +0900 | [diff] [blame] | 759 | // Used for CHECK_EQ(), etc. Takes ownership of the given string. |
| 760 | // Implied severity = LOG_FATAL. |
akalin@chromium.org | 63dab76 | 2011-02-08 16:39:08 +0900 | [diff] [blame] | 761 | LogMessage(const char* file, int line, std::string* result); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 762 | |
viettrungluu@chromium.org | 3dc979c | 2014-06-19 00:02:22 +0900 | [diff] [blame] | 763 | // Used for DCHECK_EQ(), etc. Takes ownership of the given string. |
huanr@chromium.org | 656253e | 2009-02-12 10:19:05 +0900 | [diff] [blame] | 764 | LogMessage(const char* file, int line, LogSeverity severity, |
akalin@chromium.org | 63dab76 | 2011-02-08 16:39:08 +0900 | [diff] [blame] | 765 | std::string* result); |
huanr@chromium.org | 656253e | 2009-02-12 10:19:05 +0900 | [diff] [blame] | 766 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 767 | ~LogMessage(); |
| 768 | |
| 769 | std::ostream& stream() { return stream_; } |
| 770 | |
| 771 | private: |
| 772 | void Init(const char* file, int line); |
| 773 | |
| 774 | LogSeverity severity_; |
| 775 | std::ostringstream stream_; |
maruel@google.com | a855b0f | 2008-07-30 22:02:03 +0900 | [diff] [blame] | 776 | size_t message_start_; // Offset of the start of the message (past prefix |
| 777 | // info). |
siggi@chromium.org | 25396e1 | 2010-11-05 00:50:49 +0900 | [diff] [blame] | 778 | // The file and line information passed in to the constructor. |
| 779 | const char* file_; |
| 780 | const int line_; |
| 781 | |
tommi@google.com | 82788e1 | 2009-04-15 01:52:11 +0900 | [diff] [blame] | 782 | #if defined(OS_WIN) |
| 783 | // Stores the current value of GetLastError in the constructor and restores |
| 784 | // it in the destructor by calling SetLastError. |
| 785 | // This is useful since the LogMessage class uses a lot of Win32 calls |
| 786 | // that will lose the value of GLE and the code that called the log function |
| 787 | // will have lost the thread error value when the log call returns. |
| 788 | class SaveLastError { |
| 789 | public: |
| 790 | SaveLastError(); |
| 791 | ~SaveLastError(); |
| 792 | |
| 793 | unsigned long get_error() const { return last_error_; } |
| 794 | |
| 795 | protected: |
| 796 | unsigned long last_error_; |
| 797 | }; |
| 798 | |
| 799 | SaveLastError last_error_; |
| 800 | #endif |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 801 | |
brettw@google.com | e3c034a | 2008-08-08 03:31:40 +0900 | [diff] [blame] | 802 | DISALLOW_COPY_AND_ASSIGN(LogMessage); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 803 | }; |
| 804 | |
| 805 | // A non-macro interface to the log facility; (useful |
| 806 | // when the logging level is not a compile-time constant). |
ki.stfu | 41fcb04 | 2015-09-25 02:30:10 +0900 | [diff] [blame] | 807 | inline void LogAtLevel(int log_level, const std::string& msg) { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 808 | LogMessage(__FILE__, __LINE__, log_level).stream() << msg; |
| 809 | } |
| 810 | |
| 811 | // This class is used to explicitly ignore values in the conditional |
| 812 | // logging macros. This avoids compiler warnings like "value computed |
| 813 | // is not used" and "statement has no effect". |
rvargas@google.com | 97a20ec | 2011-04-22 07:22:10 +0900 | [diff] [blame] | 814 | class LogMessageVoidify { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 815 | public: |
| 816 | LogMessageVoidify() { } |
| 817 | // This has to be an operator with a precedence lower than << but |
| 818 | // higher than ?: |
| 819 | void operator&(std::ostream&) { } |
| 820 | }; |
| 821 | |
tschmelcher@chromium.org | f29a4fc | 2009-10-10 08:52:20 +0900 | [diff] [blame] | 822 | #if defined(OS_WIN) |
| 823 | typedef unsigned long SystemErrorCode; |
| 824 | #elif defined(OS_POSIX) |
| 825 | typedef int SystemErrorCode; |
| 826 | #endif |
| 827 | |
| 828 | // Alias for ::GetLastError() on Windows and errno on POSIX. Avoids having to |
| 829 | // pull in windows.h just for GetLastError() and DWORD. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 830 | BASE_EXPORT SystemErrorCode GetLastSystemErrorCode(); |
vitalybuka@chromium.org | e871736 | 2014-04-23 10:11:01 +0900 | [diff] [blame] | 831 | BASE_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code); |
tschmelcher@chromium.org | f29a4fc | 2009-10-10 08:52:20 +0900 | [diff] [blame] | 832 | |
| 833 | #if defined(OS_WIN) |
| 834 | // Appends a formatted system message of the GetLastError() type. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 835 | class BASE_EXPORT Win32ErrorLogMessage { |
tschmelcher@chromium.org | f29a4fc | 2009-10-10 08:52:20 +0900 | [diff] [blame] | 836 | public: |
| 837 | Win32ErrorLogMessage(const char* file, |
| 838 | int line, |
| 839 | LogSeverity severity, |
tschmelcher@chromium.org | f29a4fc | 2009-10-10 08:52:20 +0900 | [diff] [blame] | 840 | SystemErrorCode err); |
| 841 | |
tschmelcher@chromium.org | f29a4fc | 2009-10-10 08:52:20 +0900 | [diff] [blame] | 842 | // Appends the error message before destructing the encapsulated class. |
| 843 | ~Win32ErrorLogMessage(); |
| 844 | |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 845 | std::ostream& stream() { return log_message_.stream(); } |
| 846 | |
tschmelcher@chromium.org | f29a4fc | 2009-10-10 08:52:20 +0900 | [diff] [blame] | 847 | private: |
| 848 | SystemErrorCode err_; |
tschmelcher@chromium.org | f29a4fc | 2009-10-10 08:52:20 +0900 | [diff] [blame] | 849 | LogMessage log_message_; |
| 850 | |
| 851 | DISALLOW_COPY_AND_ASSIGN(Win32ErrorLogMessage); |
| 852 | }; |
| 853 | #elif defined(OS_POSIX) |
| 854 | // Appends a formatted system message of the errno type |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 855 | class BASE_EXPORT ErrnoLogMessage { |
tschmelcher@chromium.org | f29a4fc | 2009-10-10 08:52:20 +0900 | [diff] [blame] | 856 | public: |
| 857 | ErrnoLogMessage(const char* file, |
| 858 | int line, |
| 859 | LogSeverity severity, |
| 860 | SystemErrorCode err); |
| 861 | |
tschmelcher@chromium.org | f29a4fc | 2009-10-10 08:52:20 +0900 | [diff] [blame] | 862 | // Appends the error message before destructing the encapsulated class. |
| 863 | ~ErrnoLogMessage(); |
| 864 | |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 865 | std::ostream& stream() { return log_message_.stream(); } |
| 866 | |
tschmelcher@chromium.org | f29a4fc | 2009-10-10 08:52:20 +0900 | [diff] [blame] | 867 | private: |
| 868 | SystemErrorCode err_; |
| 869 | LogMessage log_message_; |
| 870 | |
| 871 | DISALLOW_COPY_AND_ASSIGN(ErrnoLogMessage); |
| 872 | }; |
| 873 | #endif // OS_WIN |
| 874 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 875 | // Closes the log file explicitly if open. |
| 876 | // NOTE: Since the log file is opened as necessary by the action of logging |
| 877 | // statements, there's no guarantee that it will stay closed |
| 878 | // after this call. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 879 | BASE_EXPORT void CloseLogFile(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 880 | |
willchan@chromium.org | 85113a1 | 2009-12-08 13:22:50 +0900 | [diff] [blame] | 881 | // Async signal safe logging mechanism. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 882 | BASE_EXPORT void RawLog(int level, const char* message); |
willchan@chromium.org | 85113a1 | 2009-12-08 13:22:50 +0900 | [diff] [blame] | 883 | |
| 884 | #define RAW_LOG(level, message) logging::RawLog(logging::LOG_ ## level, message) |
| 885 | |
| 886 | #define RAW_CHECK(condition) \ |
| 887 | do { \ |
| 888 | if (!(condition)) \ |
| 889 | logging::RawLog(logging::LOG_FATAL, "Check failed: " #condition "\n"); \ |
| 890 | } while (0) |
| 891 | |
ananta@chromium.org | 76bc94e | 2013-02-28 07:04:00 +0900 | [diff] [blame] | 892 | #if defined(OS_WIN) |
ananta | 99b87ef | 2015-09-18 10:00:09 +0900 | [diff] [blame] | 893 | // Returns true if logging to file is enabled. |
| 894 | BASE_EXPORT bool IsLoggingToFileEnabled(); |
| 895 | |
ananta@chromium.org | 76bc94e | 2013-02-28 07:04:00 +0900 | [diff] [blame] | 896 | // Returns the default log file path. |
| 897 | BASE_EXPORT std::wstring GetLogFileFullPath(); |
| 898 | #endif |
| 899 | |
brettw@google.com | e3c034a | 2008-08-08 03:31:40 +0900 | [diff] [blame] | 900 | } // namespace logging |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 901 | |
jyasskin@chromium.org | f01cfba | 2014-07-09 08:03:06 +0900 | [diff] [blame] | 902 | // Note that "The behavior of a C++ program is undefined if it adds declarations |
| 903 | // or definitions to namespace std or to a namespace within namespace std unless |
| 904 | // otherwise specified." --C++11[namespace.std] |
| 905 | // |
| 906 | // We've checked that this particular definition has the intended behavior on |
| 907 | // our implementations, but it's prone to breaking in the future, and please |
| 908 | // don't imitate this in your own definitions without checking with some |
| 909 | // standard library experts. |
| 910 | namespace std { |
thakis@chromium.org | b13bd1d | 2010-06-17 03:39:53 +0900 | [diff] [blame] | 911 | // These functions are provided as a convenience for logging, which is where we |
| 912 | // use streams (it is against Google style to use streams in other places). It |
| 913 | // is designed to allow you to emit non-ASCII Unicode strings to the log file, |
| 914 | // which is normally ASCII. It is relatively slow, so try not to use it for |
| 915 | // common cases. Non-ASCII characters will be converted to UTF-8 by these |
| 916 | // operators. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 917 | BASE_EXPORT std::ostream& operator<<(std::ostream& out, const wchar_t* wstr); |
thakis@chromium.org | b13bd1d | 2010-06-17 03:39:53 +0900 | [diff] [blame] | 918 | inline std::ostream& operator<<(std::ostream& out, const std::wstring& wstr) { |
| 919 | return out << wstr.c_str(); |
| 920 | } |
jyasskin@chromium.org | f01cfba | 2014-07-09 08:03:06 +0900 | [diff] [blame] | 921 | } // namespace std |
thakis@chromium.org | b13bd1d | 2010-06-17 03:39:53 +0900 | [diff] [blame] | 922 | |
ericroman@google.com | fa95b46 | 2008-08-25 12:44:40 +0900 | [diff] [blame] | 923 | // The NOTIMPLEMENTED() macro annotates codepaths which have |
| 924 | // not been implemented yet. |
| 925 | // |
| 926 | // The implementation of this macro is controlled by NOTIMPLEMENTED_POLICY: |
| 927 | // 0 -- Do nothing (stripped by compiler) |
| 928 | // 1 -- Warn at compile time |
| 929 | // 2 -- Fail at compile time |
| 930 | // 3 -- Fail at runtime (DCHECK) |
| 931 | // 4 -- [default] LOG(ERROR) at runtime |
| 932 | // 5 -- LOG(ERROR) at runtime, only once per call-site |
| 933 | |
| 934 | #ifndef NOTIMPLEMENTED_POLICY |
yfriedman@chromium.org | 6ecced4 | 2012-07-26 01:17:57 +0900 | [diff] [blame] | 935 | #if defined(OS_ANDROID) && defined(OFFICIAL_BUILD) |
| 936 | #define NOTIMPLEMENTED_POLICY 0 |
| 937 | #else |
ericroman@google.com | fa95b46 | 2008-08-25 12:44:40 +0900 | [diff] [blame] | 938 | // Select default policy: LOG(ERROR) |
| 939 | #define NOTIMPLEMENTED_POLICY 4 |
| 940 | #endif |
yfriedman@chromium.org | 6ecced4 | 2012-07-26 01:17:57 +0900 | [diff] [blame] | 941 | #endif |
ericroman@google.com | fa95b46 | 2008-08-25 12:44:40 +0900 | [diff] [blame] | 942 | |
agl@chromium.org | fc91061 | 2008-10-31 08:54:26 +0900 | [diff] [blame] | 943 | #if defined(COMPILER_GCC) |
| 944 | // On Linux, with GCC, we can use __PRETTY_FUNCTION__ to get the demangled name |
| 945 | // of the current function in the NOTIMPLEMENTED message. |
| 946 | #define NOTIMPLEMENTED_MSG "Not implemented reached in " << __PRETTY_FUNCTION__ |
| 947 | #else |
| 948 | #define NOTIMPLEMENTED_MSG "NOT IMPLEMENTED" |
| 949 | #endif |
| 950 | |
ericroman@google.com | fa95b46 | 2008-08-25 12:44:40 +0900 | [diff] [blame] | 951 | #if NOTIMPLEMENTED_POLICY == 0 |
torne@chromium.org | 186eab2 | 2012-01-31 04:41:54 +0900 | [diff] [blame] | 952 | #define NOTIMPLEMENTED() EAT_STREAM_PARAMETERS |
ericroman@google.com | fa95b46 | 2008-08-25 12:44:40 +0900 | [diff] [blame] | 953 | #elif NOTIMPLEMENTED_POLICY == 1 |
| 954 | // TODO, figure out how to generate a warning |
avi | 486c61f | 2015-11-24 23:26:24 +0900 | [diff] [blame] | 955 | #define NOTIMPLEMENTED() static_assert(false, "NOT_IMPLEMENTED") |
ericroman@google.com | fa95b46 | 2008-08-25 12:44:40 +0900 | [diff] [blame] | 956 | #elif NOTIMPLEMENTED_POLICY == 2 |
avi | 486c61f | 2015-11-24 23:26:24 +0900 | [diff] [blame] | 957 | #define NOTIMPLEMENTED() static_assert(false, "NOT_IMPLEMENTED") |
ericroman@google.com | fa95b46 | 2008-08-25 12:44:40 +0900 | [diff] [blame] | 958 | #elif NOTIMPLEMENTED_POLICY == 3 |
| 959 | #define NOTIMPLEMENTED() NOTREACHED() |
| 960 | #elif NOTIMPLEMENTED_POLICY == 4 |
agl@chromium.org | fc91061 | 2008-10-31 08:54:26 +0900 | [diff] [blame] | 961 | #define NOTIMPLEMENTED() LOG(ERROR) << NOTIMPLEMENTED_MSG |
ericroman@google.com | fa95b46 | 2008-08-25 12:44:40 +0900 | [diff] [blame] | 962 | #elif NOTIMPLEMENTED_POLICY == 5 |
| 963 | #define NOTIMPLEMENTED() do {\ |
miu@chromium.org | 00e3a07 | 2013-02-13 17:32:14 +0900 | [diff] [blame] | 964 | static bool logged_once = false;\ |
| 965 | LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ |
| 966 | logged_once = true;\ |
| 967 | } while(0);\ |
| 968 | EAT_STREAM_PARAMETERS |
ericroman@google.com | fa95b46 | 2008-08-25 12:44:40 +0900 | [diff] [blame] | 969 | #endif |
| 970 | |
brettw@google.com | e3c034a | 2008-08-08 03:31:40 +0900 | [diff] [blame] | 971 | #endif // BASE_LOGGING_H_ |