[email protected] | 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 | |
[email protected] | 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 | |
[email protected] | 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 | |
[email protected] | 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" |
[email protected] | ddb9b33 | 2011-12-02 07:31:09 | [diff] [blame] | 15 | #include "base/debug/debugger.h" |
[email protected] | 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 | // |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [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 | // |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [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, |
[email protected] | b0d38d4c | 2010-10-29 00:39:48 | [diff] [blame] | 83 | // --vmodule=profile=2,icon_loader=1,browser_*=3,*/chromeos/*=4 --v=0 |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [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" |
[email protected] | e11de72 | 2010-11-01 20:50:55 | [diff] [blame] | 89 | // d. VLOG(4) and lower messages to be printed from files under a |
[email protected] | b0d38d4c | 2010-10-29 00:39:48 | [diff] [blame] | 90 | // "chromeos" directory. |
[email protected] | e11de72 | 2010-11-01 20:50:55 | [diff] [blame] | 91 | // e. VLOG(0) and lower messages to be printed from elsewhere |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 92 | // |
| 93 | // The wildcarding functionality shown by (c) supports both '*' (match |
[email protected] | b0d38d4c | 2010-10-29 00:39:48 | [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. |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [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 | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 115 | // We also override the standard 'assert' to use 'DLOG_ASSERT'. |
| 116 | // |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [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 | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 129 | // The supported severity levels for macros that allow you to specify one |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 130 | // are (in increasing order of severity) INFO, WARNING, ERROR, ERROR_REPORT, |
| 131 | // and FATAL. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 132 | // |
| 133 | // Very important: logging a message at the FATAL severity level causes |
| 134 | // the program to terminate (after the message is logged). |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 135 | // |
| 136 | // Note the special severity of ERROR_REPORT only available/relevant in normal |
| 137 | // mode, which displays error dialog without terminating the program. There is |
| 138 | // no error dialog for severity ERROR or below in normal mode. |
| 139 | // |
| 140 | // There is also the special severity of DFATAL, which logs FATAL in |
[email protected] | 081bd4c | 2010-06-24 01:01:04 | [diff] [blame] | 141 | // debug mode, ERROR in normal mode. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 142 | |
| 143 | namespace logging { |
| 144 | |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 145 | // TODO(avi): do we want to do a unification of character types here? |
| 146 | #if defined(OS_WIN) |
| 147 | typedef wchar_t PathChar; |
| 148 | #else |
| 149 | typedef char PathChar; |
| 150 | #endif |
| 151 | |
| 152 | // Where to record logging output? A flat file and/or system debug log |
| 153 | // via OutputDebugString. |
| 154 | enum LoggingDestination { |
| 155 | LOG_NONE = 0, |
| 156 | LOG_TO_FILE = 1 << 0, |
| 157 | LOG_TO_SYSTEM_DEBUG_LOG = 1 << 1, |
| 158 | |
| 159 | LOG_TO_ALL = LOG_TO_FILE | LOG_TO_SYSTEM_DEBUG_LOG, |
| 160 | |
| 161 | // On Windows, use a file next to the exe; on POSIX platforms, where |
| 162 | // it may not even be possible to locate the executable on disk, use |
| 163 | // stderr. |
| 164 | #if defined(OS_WIN) |
| 165 | LOG_DEFAULT = LOG_TO_FILE, |
| 166 | #elif defined(OS_POSIX) |
| 167 | LOG_DEFAULT = LOG_TO_SYSTEM_DEBUG_LOG, |
| 168 | #endif |
| 169 | }; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 170 | |
| 171 | // Indicates that the log file should be locked when being written to. |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 172 | // Unless there is only one single-threaded process that is logging to |
| 173 | // the log file, the file should be locked during writes to make each |
[email protected] | 3ee50d1 | 2014-03-05 01:43:27 | [diff] [blame] | 174 | // log output atomic. Other writers will block. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 175 | // |
| 176 | // All processes writing to the log file must have their locking set for it to |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 177 | // work properly. Defaults to LOCK_LOG_FILE. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 178 | enum LogLockingState { LOCK_LOG_FILE, DONT_LOCK_LOG_FILE }; |
| 179 | |
| 180 | // On startup, should we delete or append to an existing log file (if any)? |
| 181 | // Defaults to APPEND_TO_OLD_LOG_FILE. |
| 182 | enum OldFileDeletionState { DELETE_OLD_LOG_FILE, APPEND_TO_OLD_LOG_FILE }; |
| 183 | |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 184 | struct BASE_EXPORT LoggingSettings { |
| 185 | // The defaults values are: |
| 186 | // |
| 187 | // logging_dest: LOG_DEFAULT |
| 188 | // log_file: NULL |
| 189 | // lock_log: LOCK_LOG_FILE |
| 190 | // delete_old: APPEND_TO_OLD_LOG_FILE |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 191 | LoggingSettings(); |
| 192 | |
| 193 | LoggingDestination logging_dest; |
| 194 | |
| 195 | // The three settings below have an effect only when LOG_TO_FILE is |
| 196 | // set in |logging_dest|. |
| 197 | const PathChar* log_file; |
| 198 | LogLockingState lock_log; |
| 199 | OldFileDeletionState delete_old; |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 200 | }; |
[email protected] | ff3d0c3 | 2010-08-23 19:57:46 | [diff] [blame] | 201 | |
| 202 | // Define different names for the BaseInitLoggingImpl() function depending on |
| 203 | // whether NDEBUG is defined or not so that we'll fail to link if someone tries |
| 204 | // to compile logging.cc with NDEBUG but includes logging.h without defining it, |
| 205 | // or vice versa. |
| 206 | #if NDEBUG |
| 207 | #define BaseInitLoggingImpl BaseInitLoggingImpl_built_with_NDEBUG |
| 208 | #else |
| 209 | #define BaseInitLoggingImpl BaseInitLoggingImpl_built_without_NDEBUG |
| 210 | #endif |
| 211 | |
| 212 | // Implementation of the InitLogging() method declared below. We use a |
| 213 | // more-specific name so we can #define it above without affecting other code |
| 214 | // that has named stuff "InitLogging". |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 215 | BASE_EXPORT bool BaseInitLoggingImpl(const LoggingSettings& settings); |
[email protected] | ff3d0c3 | 2010-08-23 19:57:46 | [diff] [blame] | 216 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 217 | // Sets the log file name and other global logging state. Calling this function |
| 218 | // is recommended, and is normally done at the beginning of application init. |
| 219 | // If you don't call it, all the flags will be initialized to their default |
| 220 | // values, and there is a race condition that may leak a critical section |
| 221 | // object if two threads try to do the first log at the same time. |
| 222 | // See the definition of the enums above for descriptions and default values. |
| 223 | // |
| 224 | // The default log file is initialized to "debug.log" in the application |
| 225 | // directory. You probably don't want this, especially since the program |
| 226 | // directory may not be writable on an enduser's system. |
[email protected] | 064aa16 | 2011-12-03 00:30:08 | [diff] [blame] | 227 | // |
| 228 | // This function may be called a second time to re-direct logging (e.g after |
| 229 | // loging in to a user partition), however it should never be called more than |
| 230 | // twice. |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 231 | inline bool InitLogging(const LoggingSettings& settings) { |
| 232 | return BaseInitLoggingImpl(settings); |
[email protected] | ff3d0c3 | 2010-08-23 19:57:46 | [diff] [blame] | 233 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 234 | |
| 235 | // Sets the log level. Anything at or above this level will be written to the |
| 236 | // log file/displayed to the user (if applicable). Anything below this level |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 237 | // will be silently ignored. The log level defaults to 0 (everything is logged |
| 238 | // up to level INFO) if this function is not called. |
| 239 | // Note that log messages for VLOG(x) are logged at level -x, so setting |
| 240 | // the min log level to negative values enables verbose logging. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 241 | BASE_EXPORT void SetMinLogLevel(int level); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 242 | |
[email protected] | 8a2986ca | 2009-04-10 19:13:42 | [diff] [blame] | 243 | // Gets the current log level. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 244 | BASE_EXPORT int GetMinLogLevel(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 245 | |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 246 | // Gets the VLOG default verbosity level. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 247 | BASE_EXPORT int GetVlogVerbosity(); |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 248 | |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 249 | // Gets the current vlog level for the given file (usually taken from |
| 250 | // __FILE__). |
[email protected] | 2f4e9a6 | 2010-09-29 21:25:14 | [diff] [blame] | 251 | |
| 252 | // Note that |N| is the size *with* the null terminator. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 253 | BASE_EXPORT int GetVlogLevelHelper(const char* file_start, size_t N); |
[email protected] | 2f4e9a6 | 2010-09-29 21:25:14 | [diff] [blame] | 254 | |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 255 | template <size_t N> |
| 256 | int GetVlogLevel(const char (&file)[N]) { |
| 257 | return GetVlogLevelHelper(file, N); |
| 258 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 259 | |
| 260 | // Sets the common items you want to be prepended to each log message. |
| 261 | // process and thread IDs default to off, the timestamp defaults to on. |
| 262 | // If this function is not called, logging defaults to writing the timestamp |
| 263 | // only. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 264 | BASE_EXPORT void SetLogItems(bool enable_process_id, bool enable_thread_id, |
| 265 | bool enable_timestamp, bool enable_tickcount); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 266 | |
[email protected] | 81e0a85 | 2010-08-17 00:38:12 | [diff] [blame] | 267 | // Sets whether or not you'd like to see fatal debug messages popped up in |
| 268 | // a dialog box or not. |
| 269 | // Dialogs are not shown by default. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 270 | BASE_EXPORT void SetShowErrorDialogs(bool enable_dialogs); |
[email protected] | 81e0a85 | 2010-08-17 00:38:12 | [diff] [blame] | 271 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 272 | // Sets the Log Assert Handler that will be used to notify of check failures. |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 273 | // The default handler shows a dialog box and then terminate the process, |
| 274 | // however clients can use this function to override with their own handling |
| 275 | // (e.g. a silent one for Unit Tests) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 276 | typedef void (*LogAssertHandlerFunction)(const std::string& str); |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 277 | BASE_EXPORT void SetLogAssertHandler(LogAssertHandlerFunction handler); |
[email protected] | 64e5cc0 | 2010-11-03 19:20:27 | [diff] [blame] | 278 | |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 279 | // Sets the Log Report Handler that will be used to notify of check failures |
| 280 | // in non-debug mode. The default handler shows a dialog box and continues |
| 281 | // the execution, however clients can use this function to override with their |
| 282 | // own handling. |
| 283 | typedef void (*LogReportHandlerFunction)(const std::string& str); |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 284 | BASE_EXPORT void SetLogReportHandler(LogReportHandlerFunction handler); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 285 | |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 286 | // Sets the Log Message Handler that gets passed every log message before |
| 287 | // it's sent to other log destinations (if any). |
| 288 | // Returns true to signal that it handled the message and the message |
| 289 | // should not be sent to other log destinations. |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 290 | typedef bool (*LogMessageHandlerFunction)(int severity, |
| 291 | const char* file, int line, size_t message_start, const std::string& str); |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 292 | BASE_EXPORT void SetLogMessageHandler(LogMessageHandlerFunction handler); |
| 293 | BASE_EXPORT LogMessageHandlerFunction GetLogMessageHandler(); |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 294 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 295 | typedef int LogSeverity; |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 296 | const LogSeverity LOG_VERBOSE = -1; // This is level 1 verbosity |
| 297 | // Note: the log severities are used to index into the array of names, |
| 298 | // see log_severity_names. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 299 | const LogSeverity LOG_INFO = 0; |
| 300 | const LogSeverity LOG_WARNING = 1; |
| 301 | const LogSeverity LOG_ERROR = 2; |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 302 | const LogSeverity LOG_ERROR_REPORT = 3; |
| 303 | const LogSeverity LOG_FATAL = 4; |
| 304 | const LogSeverity LOG_NUM_SEVERITIES = 5; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 305 | |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 306 | // LOG_DFATAL is LOG_FATAL in debug mode, ERROR in normal mode |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 307 | #ifdef NDEBUG |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 308 | const LogSeverity LOG_DFATAL = LOG_ERROR; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 309 | #else |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 310 | const LogSeverity LOG_DFATAL = LOG_FATAL; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 311 | #endif |
| 312 | |
| 313 | // A few definitions of macros that don't generate much code. These are used |
| 314 | // by LOG() and LOG_IF, etc. Since these are used all over our code, it's |
| 315 | // better to have compact code for these operations. |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 316 | #define COMPACT_GOOGLE_LOG_EX_INFO(ClassName, ...) \ |
| 317 | logging::ClassName(__FILE__, __LINE__, logging::LOG_INFO , ##__VA_ARGS__) |
| 318 | #define COMPACT_GOOGLE_LOG_EX_WARNING(ClassName, ...) \ |
| 319 | logging::ClassName(__FILE__, __LINE__, logging::LOG_WARNING , ##__VA_ARGS__) |
| 320 | #define COMPACT_GOOGLE_LOG_EX_ERROR(ClassName, ...) \ |
| 321 | logging::ClassName(__FILE__, __LINE__, logging::LOG_ERROR , ##__VA_ARGS__) |
| 322 | #define COMPACT_GOOGLE_LOG_EX_ERROR_REPORT(ClassName, ...) \ |
| 323 | logging::ClassName(__FILE__, __LINE__, \ |
| 324 | logging::LOG_ERROR_REPORT , ##__VA_ARGS__) |
| 325 | #define COMPACT_GOOGLE_LOG_EX_FATAL(ClassName, ...) \ |
| 326 | logging::ClassName(__FILE__, __LINE__, logging::LOG_FATAL , ##__VA_ARGS__) |
| 327 | #define COMPACT_GOOGLE_LOG_EX_DFATAL(ClassName, ...) \ |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 328 | logging::ClassName(__FILE__, __LINE__, logging::LOG_DFATAL , ##__VA_ARGS__) |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 329 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 330 | #define COMPACT_GOOGLE_LOG_INFO \ |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 331 | COMPACT_GOOGLE_LOG_EX_INFO(LogMessage) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 332 | #define COMPACT_GOOGLE_LOG_WARNING \ |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 333 | COMPACT_GOOGLE_LOG_EX_WARNING(LogMessage) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 334 | #define COMPACT_GOOGLE_LOG_ERROR \ |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 335 | COMPACT_GOOGLE_LOG_EX_ERROR(LogMessage) |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 336 | #define COMPACT_GOOGLE_LOG_ERROR_REPORT \ |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 337 | COMPACT_GOOGLE_LOG_EX_ERROR_REPORT(LogMessage) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 338 | #define COMPACT_GOOGLE_LOG_FATAL \ |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 339 | COMPACT_GOOGLE_LOG_EX_FATAL(LogMessage) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 340 | #define COMPACT_GOOGLE_LOG_DFATAL \ |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 341 | COMPACT_GOOGLE_LOG_EX_DFATAL(LogMessage) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 342 | |
[email protected] | 8d12730 | 2013-01-10 02:41:57 | [diff] [blame] | 343 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 344 | // wingdi.h defines ERROR to be 0. When we call LOG(ERROR), it gets |
| 345 | // substituted with 0, and it expands to COMPACT_GOOGLE_LOG_0. To allow us |
| 346 | // to keep using this syntax, we define this macro to do the same thing |
| 347 | // as COMPACT_GOOGLE_LOG_ERROR, and also define ERROR the same way that |
| 348 | // the Windows SDK does for consistency. |
| 349 | #define ERROR 0 |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 350 | #define COMPACT_GOOGLE_LOG_EX_0(ClassName, ...) \ |
| 351 | COMPACT_GOOGLE_LOG_EX_ERROR(ClassName , ##__VA_ARGS__) |
| 352 | #define COMPACT_GOOGLE_LOG_0 COMPACT_GOOGLE_LOG_ERROR |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 353 | // Needed for LOG_IS_ON(ERROR). |
| 354 | const LogSeverity LOG_0 = LOG_ERROR; |
[email protected] | 8d12730 | 2013-01-10 02:41:57 | [diff] [blame] | 355 | #endif |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 356 | |
[email protected] | deba0ff | 2010-11-03 05:30:14 | [diff] [blame] | 357 | // As special cases, we can assume that LOG_IS_ON(ERROR_REPORT) and |
| 358 | // LOG_IS_ON(FATAL) always hold. Also, LOG_IS_ON(DFATAL) always holds |
| 359 | // in debug mode. In particular, CHECK()s will always fire if they |
| 360 | // fail. |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 361 | #define LOG_IS_ON(severity) \ |
| 362 | ((::logging::LOG_ ## severity) >= ::logging::GetMinLogLevel()) |
| 363 | |
| 364 | // We can't do any caching tricks with VLOG_IS_ON() like the |
| 365 | // google-glog version since it requires GCC extensions. This means |
| 366 | // that using the v-logging functions in conjunction with --vmodule |
| 367 | // may be slow. |
| 368 | #define VLOG_IS_ON(verboselevel) \ |
| 369 | ((verboselevel) <= ::logging::GetVlogLevel(__FILE__)) |
| 370 | |
| 371 | // Helper macro which avoids evaluating the arguments to a stream if |
| 372 | // the condition doesn't hold. |
| 373 | #define LAZY_STREAM(stream, condition) \ |
| 374 | !(condition) ? (void) 0 : ::logging::LogMessageVoidify() & (stream) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 375 | |
| 376 | // We use the preprocessor's merging operator, "##", so that, e.g., |
| 377 | // LOG(INFO) becomes the token COMPACT_GOOGLE_LOG_INFO. There's some funny |
| 378 | // subtle difference between ostream member streaming functions (e.g., |
| 379 | // ostream::operator<<(int) and ostream non-member streaming functions |
| 380 | // (e.g., ::operator<<(ostream&, string&): it turns out that it's |
| 381 | // impossible to stream something like a string directly to an unnamed |
| 382 | // ostream. We employ a neat hack by calling the stream() member |
| 383 | // function of LogMessage which seems to avoid the problem. |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 384 | #define LOG_STREAM(severity) COMPACT_GOOGLE_LOG_ ## severity.stream() |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 385 | |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 386 | #define LOG(severity) LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity)) |
| 387 | #define LOG_IF(severity, condition) \ |
| 388 | LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity) && (condition)) |
| 389 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 390 | #define SYSLOG(severity) LOG(severity) |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 391 | #define SYSLOG_IF(severity, condition) LOG_IF(severity, condition) |
| 392 | |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 393 | // The VLOG macros log with negative verbosities. |
| 394 | #define VLOG_STREAM(verbose_level) \ |
| 395 | logging::LogMessage(__FILE__, __LINE__, -verbose_level).stream() |
| 396 | |
| 397 | #define VLOG(verbose_level) \ |
| 398 | LAZY_STREAM(VLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level)) |
| 399 | |
| 400 | #define VLOG_IF(verbose_level, condition) \ |
| 401 | LAZY_STREAM(VLOG_STREAM(verbose_level), \ |
| 402 | VLOG_IS_ON(verbose_level) && (condition)) |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 403 | |
[email protected] | fb879b1a | 2011-03-06 18:16:31 | [diff] [blame] | 404 | #if defined (OS_WIN) |
| 405 | #define VPLOG_STREAM(verbose_level) \ |
| 406 | logging::Win32ErrorLogMessage(__FILE__, __LINE__, -verbose_level, \ |
| 407 | ::logging::GetLastSystemErrorCode()).stream() |
| 408 | #elif defined(OS_POSIX) |
| 409 | #define VPLOG_STREAM(verbose_level) \ |
| 410 | logging::ErrnoLogMessage(__FILE__, __LINE__, -verbose_level, \ |
| 411 | ::logging::GetLastSystemErrorCode()).stream() |
| 412 | #endif |
| 413 | |
| 414 | #define VPLOG(verbose_level) \ |
| 415 | LAZY_STREAM(VPLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level)) |
| 416 | |
| 417 | #define VPLOG_IF(verbose_level, condition) \ |
| 418 | LAZY_STREAM(VPLOG_STREAM(verbose_level), \ |
| 419 | VLOG_IS_ON(verbose_level) && (condition)) |
| 420 | |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 421 | // TODO(akalin): Add more VLOG variants, e.g. VPLOG. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 422 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 423 | #define LOG_ASSERT(condition) \ |
| 424 | LOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". " |
| 425 | #define SYSLOG_ASSERT(condition) \ |
| 426 | SYSLOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". " |
| 427 | |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 428 | #if defined(OS_WIN) |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 429 | #define LOG_GETLASTERROR_STREAM(severity) \ |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 430 | COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \ |
| 431 | ::logging::GetLastSystemErrorCode()).stream() |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 432 | #define LOG_GETLASTERROR(severity) \ |
| 433 | LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity), LOG_IS_ON(severity)) |
| 434 | #define LOG_GETLASTERROR_MODULE_STREAM(severity, module) \ |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 435 | COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \ |
| 436 | ::logging::GetLastSystemErrorCode(), module).stream() |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 437 | #define LOG_GETLASTERROR_MODULE(severity, module) \ |
| 438 | LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity, module), \ |
| 439 | LOG_IS_ON(severity)) |
| 440 | // PLOG_STREAM is used by PLOG, which is the usual error logging macro |
| 441 | // for each platform. |
| 442 | #define PLOG_STREAM(severity) LOG_GETLASTERROR_STREAM(severity) |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 443 | #elif defined(OS_POSIX) |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 444 | #define LOG_ERRNO_STREAM(severity) \ |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 445 | COMPACT_GOOGLE_LOG_EX_ ## severity(ErrnoLogMessage, \ |
| 446 | ::logging::GetLastSystemErrorCode()).stream() |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 447 | #define LOG_ERRNO(severity) \ |
| 448 | LAZY_STREAM(LOG_ERRNO_STREAM(severity), LOG_IS_ON(severity)) |
| 449 | // PLOG_STREAM is used by PLOG, which is the usual error logging macro |
| 450 | // for each platform. |
| 451 | #define PLOG_STREAM(severity) LOG_ERRNO_STREAM(severity) |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 452 | #endif |
| 453 | |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 454 | #define PLOG(severity) \ |
| 455 | LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity)) |
| 456 | |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 457 | #define PLOG_IF(severity, condition) \ |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 458 | LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity) && (condition)) |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 459 | |
[email protected] | ddb9b33 | 2011-12-02 07:31:09 | [diff] [blame] | 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. |
[email protected] | 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. |
[email protected] | ddb9b33 | 2011-12-02 07:31:09 | [diff] [blame] | 470 | |
[email protected] | 1a150551 | 2014-03-10 18:23:38 | [diff] [blame^] | 471 | #if defined(OFFICIAL_BUILD) && defined(NDEBUG) |
[email protected] | ddb9b33 | 2011-12-02 07:31:09 | [diff] [blame] | 472 | |
| 473 | // Make all CHECK functions discard their log strings to reduce code |
[email protected] | 1a150551 | 2014-03-10 18:23:38 | [diff] [blame^] | 474 | // bloat for official release builds. |
[email protected] | ddb9b33 | 2011-12-02 07:31:09 | [diff] [blame] | 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 | |
[email protected] | 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 | |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 492 | #define PCHECK(condition) \ |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 493 | LAZY_STREAM(PLOG_STREAM(FATAL), !(condition)) \ |
| 494 | << "Check failed: " #condition ". " |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 495 | |
[email protected] | 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 |
[email protected] | 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 | |
[email protected] | 6d445d3 | 2010-09-30 19:10:03 | [diff] [blame] | 521 | // MSVC doesn't like complex extern templates and DLLs. |
[email protected] | dc72da3 | 2011-10-24 20:20:30 | [diff] [blame] | 522 | #if !defined(COMPILER_MSVC) |
[email protected] | 6d445d3 | 2010-09-30 19:10:03 | [diff] [blame] | 523 | // Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated |
| 524 | // in logging.cc. |
[email protected] | dc72da3 | 2011-10-24 20:20:30 | [diff] [blame] | 525 | extern template BASE_EXPORT std::string* MakeCheckOpString<int, int>( |
[email protected] | 6d445d3 | 2010-09-30 19:10:03 | [diff] [blame] | 526 | const int&, const int&, const char* names); |
[email protected] | dc72da3 | 2011-10-24 20:20:30 | [diff] [blame] | 527 | extern template BASE_EXPORT |
| 528 | std::string* MakeCheckOpString<unsigned long, unsigned long>( |
[email protected] | 6d445d3 | 2010-09-30 19:10:03 | [diff] [blame] | 529 | const unsigned long&, const unsigned long&, const char* names); |
[email protected] | dc72da3 | 2011-10-24 20:20:30 | [diff] [blame] | 530 | extern template BASE_EXPORT |
| 531 | std::string* MakeCheckOpString<unsigned long, unsigned int>( |
[email protected] | 6d445d3 | 2010-09-30 19:10:03 | [diff] [blame] | 532 | const unsigned long&, const unsigned int&, const char* names); |
[email protected] | dc72da3 | 2011-10-24 20:20:30 | [diff] [blame] | 533 | extern template BASE_EXPORT |
| 534 | std::string* MakeCheckOpString<unsigned int, unsigned long>( |
[email protected] | 6d445d3 | 2010-09-30 19:10:03 | [diff] [blame] | 535 | const unsigned int&, const unsigned long&, const char* names); |
[email protected] | dc72da3 | 2011-10-24 20:20:30 | [diff] [blame] | 536 | extern template BASE_EXPORT |
| 537 | std::string* MakeCheckOpString<std::string, std::string>( |
[email protected] | 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 | |
[email protected] | 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 |
[email protected] | 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 | |
[email protected] | 1a150551 | 2014-03-10 18:23:38 | [diff] [blame^] | 571 | #if defined(NDEBUG) |
[email protected] | d15e56c | 2010-09-30 21:12:33 | [diff] [blame] | 572 | #define ENABLE_DLOG 0 |
[email protected] | d15e56c | 2010-09-30 21:12:33 | [diff] [blame] | 573 | #else |
[email protected] | d15e56c | 2010-09-30 21:12:33 | [diff] [blame] | 574 | #define ENABLE_DLOG 1 |
[email protected] | 1a150551 | 2014-03-10 18:23:38 | [diff] [blame^] | 575 | #endif |
| 576 | |
| 577 | #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
| 578 | #define ENABLE_DCHECK 0 |
| 579 | #else |
[email protected] | d15e56c | 2010-09-30 21:12:33 | [diff] [blame] | 580 | #define ENABLE_DCHECK 1 |
[email protected] | e3cca33 | 2009-08-20 01:20:29 | [diff] [blame] | 581 | #endif |
| 582 | |
[email protected] | d15e56c | 2010-09-30 21:12:33 | [diff] [blame] | 583 | // Definitions for DLOG et al. |
| 584 | |
[email protected] | d926c20 | 2010-10-01 02:58:24 | [diff] [blame] | 585 | #if ENABLE_DLOG |
| 586 | |
[email protected] | 5e98780 | 2010-11-01 19:49:22 | [diff] [blame] | 587 | #define DLOG_IS_ON(severity) LOG_IS_ON(severity) |
[email protected] | d926c20 | 2010-10-01 02:58:24 | [diff] [blame] | 588 | #define DLOG_IF(severity, condition) LOG_IF(severity, condition) |
| 589 | #define DLOG_ASSERT(condition) LOG_ASSERT(condition) |
[email protected] | d926c20 | 2010-10-01 02:58:24 | [diff] [blame] | 590 | #define DPLOG_IF(severity, condition) PLOG_IF(severity, condition) |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 591 | #define DVLOG_IF(verboselevel, condition) VLOG_IF(verboselevel, condition) |
[email protected] | fb879b1a | 2011-03-06 18:16:31 | [diff] [blame] | 592 | #define DVPLOG_IF(verboselevel, condition) VPLOG_IF(verboselevel, condition) |
[email protected] | d926c20 | 2010-10-01 02:58:24 | [diff] [blame] | 593 | |
| 594 | #else // ENABLE_DLOG |
| 595 | |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 596 | // If ENABLE_DLOG is off, we want to avoid emitting any references to |
| 597 | // |condition| (which may reference a variable defined only if NDEBUG |
| 598 | // is not defined). Contrast this with DCHECK et al., which has |
| 599 | // different behavior. |
[email protected] | d926c20 | 2010-10-01 02:58:24 | [diff] [blame] | 600 | |
[email protected] | 5e98780 | 2010-11-01 19:49:22 | [diff] [blame] | 601 | #define DLOG_IS_ON(severity) false |
[email protected] | ddb9b33 | 2011-12-02 07:31:09 | [diff] [blame] | 602 | #define DLOG_IF(severity, condition) EAT_STREAM_PARAMETERS |
| 603 | #define DLOG_ASSERT(condition) EAT_STREAM_PARAMETERS |
| 604 | #define DPLOG_IF(severity, condition) EAT_STREAM_PARAMETERS |
| 605 | #define DVLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS |
| 606 | #define DVPLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS |
[email protected] | d926c20 | 2010-10-01 02:58:24 | [diff] [blame] | 607 | |
| 608 | #endif // ENABLE_DLOG |
| 609 | |
[email protected] | d15e56c | 2010-09-30 21:12:33 | [diff] [blame] | 610 | // DEBUG_MODE is for uses like |
| 611 | // if (DEBUG_MODE) foo.CheckThatFoo(); |
| 612 | // instead of |
| 613 | // #ifndef NDEBUG |
| 614 | // foo.CheckThatFoo(); |
| 615 | // #endif |
| 616 | // |
| 617 | // We tie its state to ENABLE_DLOG. |
| 618 | enum { DEBUG_MODE = ENABLE_DLOG }; |
| 619 | |
| 620 | #undef ENABLE_DLOG |
| 621 | |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 622 | #define DLOG(severity) \ |
| 623 | LAZY_STREAM(LOG_STREAM(severity), DLOG_IS_ON(severity)) |
| 624 | |
| 625 | #if defined(OS_WIN) |
| 626 | #define DLOG_GETLASTERROR(severity) \ |
| 627 | LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity), DLOG_IS_ON(severity)) |
| 628 | #define DLOG_GETLASTERROR_MODULE(severity, module) \ |
| 629 | LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity, module), \ |
| 630 | DLOG_IS_ON(severity)) |
| 631 | #elif defined(OS_POSIX) |
| 632 | #define DLOG_ERRNO(severity) \ |
| 633 | LAZY_STREAM(LOG_ERRNO_STREAM(severity), DLOG_IS_ON(severity)) |
| 634 | #endif |
| 635 | |
| 636 | #define DPLOG(severity) \ |
| 637 | LAZY_STREAM(PLOG_STREAM(severity), DLOG_IS_ON(severity)) |
| 638 | |
[email protected] | c3ab11c | 2011-10-25 06:28:45 | [diff] [blame] | 639 | #define DVLOG(verboselevel) DVLOG_IF(verboselevel, VLOG_IS_ON(verboselevel)) |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 640 | |
[email protected] | fb879b1a | 2011-03-06 18:16:31 | [diff] [blame] | 641 | #define DVPLOG(verboselevel) DVPLOG_IF(verboselevel, VLOG_IS_ON(verboselevel)) |
| 642 | |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 643 | // Definitions for DCHECK et al. |
[email protected] | d926c20 | 2010-10-01 02:58:24 | [diff] [blame] | 644 | |
[email protected] | d15e56c | 2010-09-30 21:12:33 | [diff] [blame] | 645 | #if ENABLE_DCHECK |
[email protected] | e3cca33 | 2009-08-20 01:20:29 | [diff] [blame] | 646 | |
[email protected] | deba0ff | 2010-11-03 05:30:14 | [diff] [blame] | 647 | #define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \ |
| 648 | COMPACT_GOOGLE_LOG_EX_FATAL(ClassName , ##__VA_ARGS__) |
| 649 | #define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_FATAL |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 650 | const LogSeverity LOG_DCHECK = LOG_FATAL; |
[email protected] | deba0ff | 2010-11-03 05:30:14 | [diff] [blame] | 651 | #define DCHECK_IS_ON() true |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 652 | |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 653 | #else // ENABLE_DCHECK |
| 654 | |
[email protected] | deba0ff | 2010-11-03 05:30:14 | [diff] [blame] | 655 | // These are just dummy values since DCHECK_IS_ON() is always false in |
| 656 | // this case. |
| 657 | #define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \ |
| 658 | COMPACT_GOOGLE_LOG_EX_INFO(ClassName , ##__VA_ARGS__) |
| 659 | #define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_INFO |
| 660 | const LogSeverity LOG_DCHECK = LOG_INFO; |
[email protected] | 5e98780 | 2010-11-01 19:49:22 | [diff] [blame] | 661 | #define DCHECK_IS_ON() false |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 662 | |
| 663 | #endif // ENABLE_DCHECK |
[email protected] | 5e98780 | 2010-11-01 19:49:22 | [diff] [blame] | 664 | #undef ENABLE_DCHECK |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 665 | |
[email protected] | deba0ff | 2010-11-03 05:30:14 | [diff] [blame] | 666 | // DCHECK et al. make sure to reference |condition| regardless of |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 667 | // whether DCHECKs are enabled; this is so that we don't get unused |
| 668 | // variable warnings if the only use of a variable is in a DCHECK. |
| 669 | // This behavior is different from DLOG_IF et al. |
| 670 | |
[email protected] | deba0ff | 2010-11-03 05:30:14 | [diff] [blame] | 671 | #define DCHECK(condition) \ |
| 672 | LAZY_STREAM(LOG_STREAM(DCHECK), DCHECK_IS_ON() && !(condition)) \ |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 673 | << "Check failed: " #condition ". " |
| 674 | |
[email protected] | deba0ff | 2010-11-03 05:30:14 | [diff] [blame] | 675 | #define DPCHECK(condition) \ |
| 676 | LAZY_STREAM(PLOG_STREAM(DCHECK), DCHECK_IS_ON() && !(condition)) \ |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 677 | << "Check failed: " #condition ". " |
[email protected] | d926c20 | 2010-10-01 02:58:24 | [diff] [blame] | 678 | |
| 679 | // Helper macro for binary operators. |
| 680 | // Don't use this macro directly in your code, use DCHECK_EQ et al below. |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 681 | #define DCHECK_OP(name, op, val1, val2) \ |
[email protected] | 5e98780 | 2010-11-01 19:49:22 | [diff] [blame] | 682 | if (DCHECK_IS_ON()) \ |
[email protected] | 9c7132e | 2011-02-08 07:39:08 | [diff] [blame] | 683 | if (std::string* _result = \ |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 684 | logging::Check##name##Impl((val1), (val2), \ |
| 685 | #val1 " " #op " " #val2)) \ |
| 686 | logging::LogMessage( \ |
| 687 | __FILE__, __LINE__, ::logging::LOG_DCHECK, \ |
| 688 | _result).stream() |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 689 | |
[email protected] | deba0ff | 2010-11-03 05:30:14 | [diff] [blame] | 690 | // Equality/Inequality checks - compare two values, and log a |
| 691 | // LOG_DCHECK message including the two values when the result is not |
| 692 | // as expected. The values must have operator<<(ostream, ...) |
| 693 | // defined. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 694 | // |
| 695 | // You may append to the error message like so: |
| 696 | // DCHECK_NE(1, 2) << ": The world must be ending!"; |
| 697 | // |
| 698 | // We are very careful to ensure that each argument is evaluated exactly |
| 699 | // once, and that anything which is legal to pass as a function argument is |
| 700 | // legal here. In particular, the arguments may be temporary expressions |
| 701 | // which will end up being destroyed at the end of the apparent statement, |
| 702 | // for example: |
| 703 | // DCHECK_EQ(string("abc")[1], 'b'); |
| 704 | // |
| 705 | // WARNING: These may not compile correctly if one of the arguments is a pointer |
| 706 | // and the other is NULL. To work around this, simply static_cast NULL to the |
| 707 | // type of the desired pointer. |
| 708 | |
| 709 | #define DCHECK_EQ(val1, val2) DCHECK_OP(EQ, ==, val1, val2) |
| 710 | #define DCHECK_NE(val1, val2) DCHECK_OP(NE, !=, val1, val2) |
| 711 | #define DCHECK_LE(val1, val2) DCHECK_OP(LE, <=, val1, val2) |
| 712 | #define DCHECK_LT(val1, val2) DCHECK_OP(LT, < , val1, val2) |
| 713 | #define DCHECK_GE(val1, val2) DCHECK_OP(GE, >=, val1, val2) |
| 714 | #define DCHECK_GT(val1, val2) DCHECK_OP(GT, > , val1, val2) |
| 715 | |
[email protected] | 7c67fbe | 2013-09-26 07:55:21 | [diff] [blame] | 716 | #if defined(NDEBUG) && defined(OS_CHROMEOS) |
| 717 | #define NOTREACHED() LOG(ERROR) << "NOTREACHED() hit in " << \ |
| 718 | __FUNCTION__ << ". " |
| 719 | #else |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 720 | #define NOTREACHED() DCHECK(false) |
[email protected] | 7c67fbe | 2013-09-26 07:55:21 | [diff] [blame] | 721 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 722 | |
| 723 | // Redefine the standard assert to use our nice log files |
| 724 | #undef assert |
| 725 | #define assert(x) DLOG_ASSERT(x) |
| 726 | |
| 727 | // This class more or less represents a particular log message. You |
| 728 | // create an instance of LogMessage and then stream stuff to it. |
| 729 | // When you finish streaming to it, ~LogMessage is called and the |
| 730 | // full message gets streamed to the appropriate destination. |
| 731 | // |
| 732 | // You shouldn't actually use LogMessage's constructor to log things, |
| 733 | // though. You should use the LOG() macro (and variants thereof) |
| 734 | // above. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 735 | class BASE_EXPORT LogMessage { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 736 | public: |
| 737 | LogMessage(const char* file, int line, LogSeverity severity, int ctr); |
| 738 | |
| 739 | // Two special constructors that generate reduced amounts of code at |
| 740 | // LOG call sites for common cases. |
| 741 | // |
| 742 | // Used for LOG(INFO): Implied are: |
| 743 | // severity = LOG_INFO, ctr = 0 |
| 744 | // |
| 745 | // Using this constructor instead of the more complex constructor above |
| 746 | // saves a couple of bytes per call site. |
| 747 | LogMessage(const char* file, int line); |
| 748 | |
| 749 | // Used for LOG(severity) where severity != INFO. Implied |
| 750 | // are: ctr = 0 |
| 751 | // |
| 752 | // Using this constructor instead of the more complex constructor above |
| 753 | // saves a couple of bytes per call site. |
| 754 | LogMessage(const char* file, int line, LogSeverity severity); |
| 755 | |
[email protected] | 9c7132e | 2011-02-08 07:39:08 | [diff] [blame] | 756 | // A special constructor used for check failures. Takes ownership |
| 757 | // of the given string. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 758 | // Implied severity = LOG_FATAL |
[email protected] | 9c7132e | 2011-02-08 07:39:08 | [diff] [blame] | 759 | LogMessage(const char* file, int line, std::string* result); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 760 | |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 761 | // A special constructor used for check failures, with the option to |
[email protected] | 9c7132e | 2011-02-08 07:39:08 | [diff] [blame] | 762 | // specify severity. Takes ownership of the given string. |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 763 | LogMessage(const char* file, int line, LogSeverity severity, |
[email protected] | 9c7132e | 2011-02-08 07:39:08 | [diff] [blame] | 764 | std::string* result); |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 765 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 766 | ~LogMessage(); |
| 767 | |
| 768 | std::ostream& stream() { return stream_; } |
| 769 | |
| 770 | private: |
| 771 | void Init(const char* file, int line); |
| 772 | |
| 773 | LogSeverity severity_; |
| 774 | std::ostringstream stream_; |
[email protected] | c8887392 | 2008-07-30 13:02:03 | [diff] [blame] | 775 | size_t message_start_; // Offset of the start of the message (past prefix |
| 776 | // info). |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 777 | // The file and line information passed in to the constructor. |
| 778 | const char* file_; |
| 779 | const int line_; |
| 780 | |
[email protected] | 3f85caa | 2009-04-14 16:52:11 | [diff] [blame] | 781 | #if defined(OS_WIN) |
| 782 | // Stores the current value of GetLastError in the constructor and restores |
| 783 | // it in the destructor by calling SetLastError. |
| 784 | // This is useful since the LogMessage class uses a lot of Win32 calls |
| 785 | // that will lose the value of GLE and the code that called the log function |
| 786 | // will have lost the thread error value when the log call returns. |
| 787 | class SaveLastError { |
| 788 | public: |
| 789 | SaveLastError(); |
| 790 | ~SaveLastError(); |
| 791 | |
| 792 | unsigned long get_error() const { return last_error_; } |
| 793 | |
| 794 | protected: |
| 795 | unsigned long last_error_; |
| 796 | }; |
| 797 | |
| 798 | SaveLastError last_error_; |
| 799 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 800 | |
[email protected] | 39be424 | 2008-08-07 18:31:40 | [diff] [blame] | 801 | DISALLOW_COPY_AND_ASSIGN(LogMessage); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 802 | }; |
| 803 | |
| 804 | // A non-macro interface to the log facility; (useful |
| 805 | // when the logging level is not a compile-time constant). |
| 806 | inline void LogAtLevel(int const log_level, std::string const &msg) { |
| 807 | LogMessage(__FILE__, __LINE__, log_level).stream() << msg; |
| 808 | } |
| 809 | |
| 810 | // This class is used to explicitly ignore values in the conditional |
| 811 | // logging macros. This avoids compiler warnings like "value computed |
| 812 | // is not used" and "statement has no effect". |
[email protected] | 23bb71f | 2011-04-21 22:22:10 | [diff] [blame] | 813 | class LogMessageVoidify { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 814 | public: |
| 815 | LogMessageVoidify() { } |
| 816 | // This has to be an operator with a precedence lower than << but |
| 817 | // higher than ?: |
| 818 | void operator&(std::ostream&) { } |
| 819 | }; |
| 820 | |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 821 | #if defined(OS_WIN) |
| 822 | typedef unsigned long SystemErrorCode; |
| 823 | #elif defined(OS_POSIX) |
| 824 | typedef int SystemErrorCode; |
| 825 | #endif |
| 826 | |
| 827 | // Alias for ::GetLastError() on Windows and errno on POSIX. Avoids having to |
| 828 | // pull in windows.h just for GetLastError() and DWORD. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 829 | BASE_EXPORT SystemErrorCode GetLastSystemErrorCode(); |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 830 | |
| 831 | #if defined(OS_WIN) |
| 832 | // Appends a formatted system message of the GetLastError() type. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 833 | class BASE_EXPORT Win32ErrorLogMessage { |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 834 | public: |
| 835 | Win32ErrorLogMessage(const char* file, |
| 836 | int line, |
| 837 | LogSeverity severity, |
| 838 | SystemErrorCode err, |
| 839 | const char* module); |
| 840 | |
| 841 | Win32ErrorLogMessage(const char* file, |
| 842 | int line, |
| 843 | LogSeverity severity, |
| 844 | SystemErrorCode err); |
| 845 | |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 846 | // Appends the error message before destructing the encapsulated class. |
| 847 | ~Win32ErrorLogMessage(); |
| 848 | |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 849 | std::ostream& stream() { return log_message_.stream(); } |
| 850 | |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 851 | private: |
| 852 | SystemErrorCode err_; |
| 853 | // Optional name of the module defining the error. |
| 854 | const char* module_; |
| 855 | LogMessage log_message_; |
| 856 | |
| 857 | DISALLOW_COPY_AND_ASSIGN(Win32ErrorLogMessage); |
| 858 | }; |
| 859 | #elif defined(OS_POSIX) |
| 860 | // Appends a formatted system message of the errno type |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 861 | class BASE_EXPORT ErrnoLogMessage { |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 862 | public: |
| 863 | ErrnoLogMessage(const char* file, |
| 864 | int line, |
| 865 | LogSeverity severity, |
| 866 | SystemErrorCode err); |
| 867 | |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 868 | // Appends the error message before destructing the encapsulated class. |
| 869 | ~ErrnoLogMessage(); |
| 870 | |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 871 | std::ostream& stream() { return log_message_.stream(); } |
| 872 | |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 873 | private: |
| 874 | SystemErrorCode err_; |
| 875 | LogMessage log_message_; |
| 876 | |
| 877 | DISALLOW_COPY_AND_ASSIGN(ErrnoLogMessage); |
| 878 | }; |
| 879 | #endif // OS_WIN |
| 880 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 881 | // Closes the log file explicitly if open. |
| 882 | // NOTE: Since the log file is opened as necessary by the action of logging |
| 883 | // statements, there's no guarantee that it will stay closed |
| 884 | // after this call. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 885 | BASE_EXPORT void CloseLogFile(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 886 | |
[email protected] | e36ddc8 | 2009-12-08 04:22:50 | [diff] [blame] | 887 | // Async signal safe logging mechanism. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 888 | BASE_EXPORT void RawLog(int level, const char* message); |
[email protected] | e36ddc8 | 2009-12-08 04:22:50 | [diff] [blame] | 889 | |
| 890 | #define RAW_LOG(level, message) logging::RawLog(logging::LOG_ ## level, message) |
| 891 | |
| 892 | #define RAW_CHECK(condition) \ |
| 893 | do { \ |
| 894 | if (!(condition)) \ |
| 895 | logging::RawLog(logging::LOG_FATAL, "Check failed: " #condition "\n"); \ |
| 896 | } while (0) |
| 897 | |
[email protected] | f01b88a | 2013-02-27 22:04:00 | [diff] [blame] | 898 | #if defined(OS_WIN) |
| 899 | // Returns the default log file path. |
| 900 | BASE_EXPORT std::wstring GetLogFileFullPath(); |
| 901 | #endif |
| 902 | |
[email protected] | 39be424 | 2008-08-07 18:31:40 | [diff] [blame] | 903 | } // namespace logging |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 904 | |
[email protected] | 46ce5b56 | 2010-06-16 18:39:53 | [diff] [blame] | 905 | // These functions are provided as a convenience for logging, which is where we |
| 906 | // use streams (it is against Google style to use streams in other places). It |
| 907 | // is designed to allow you to emit non-ASCII Unicode strings to the log file, |
| 908 | // which is normally ASCII. It is relatively slow, so try not to use it for |
| 909 | // common cases. Non-ASCII characters will be converted to UTF-8 by these |
| 910 | // operators. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 911 | BASE_EXPORT std::ostream& operator<<(std::ostream& out, const wchar_t* wstr); |
[email protected] | 46ce5b56 | 2010-06-16 18:39:53 | [diff] [blame] | 912 | inline std::ostream& operator<<(std::ostream& out, const std::wstring& wstr) { |
| 913 | return out << wstr.c_str(); |
| 914 | } |
| 915 | |
[email protected] | 0dfc81b | 2008-08-25 03:44:40 | [diff] [blame] | 916 | // The NOTIMPLEMENTED() macro annotates codepaths which have |
| 917 | // not been implemented yet. |
| 918 | // |
| 919 | // The implementation of this macro is controlled by NOTIMPLEMENTED_POLICY: |
| 920 | // 0 -- Do nothing (stripped by compiler) |
| 921 | // 1 -- Warn at compile time |
| 922 | // 2 -- Fail at compile time |
| 923 | // 3 -- Fail at runtime (DCHECK) |
| 924 | // 4 -- [default] LOG(ERROR) at runtime |
| 925 | // 5 -- LOG(ERROR) at runtime, only once per call-site |
| 926 | |
| 927 | #ifndef NOTIMPLEMENTED_POLICY |
[email protected] | f5c7758a | 2012-07-25 16:17:57 | [diff] [blame] | 928 | #if defined(OS_ANDROID) && defined(OFFICIAL_BUILD) |
| 929 | #define NOTIMPLEMENTED_POLICY 0 |
| 930 | #else |
[email protected] | 0dfc81b | 2008-08-25 03:44:40 | [diff] [blame] | 931 | // Select default policy: LOG(ERROR) |
| 932 | #define NOTIMPLEMENTED_POLICY 4 |
| 933 | #endif |
[email protected] | f5c7758a | 2012-07-25 16:17:57 | [diff] [blame] | 934 | #endif |
[email protected] | 0dfc81b | 2008-08-25 03:44:40 | [diff] [blame] | 935 | |
[email protected] | f6cda75 | 2008-10-30 23:54:26 | [diff] [blame] | 936 | #if defined(COMPILER_GCC) |
| 937 | // On Linux, with GCC, we can use __PRETTY_FUNCTION__ to get the demangled name |
| 938 | // of the current function in the NOTIMPLEMENTED message. |
| 939 | #define NOTIMPLEMENTED_MSG "Not implemented reached in " << __PRETTY_FUNCTION__ |
| 940 | #else |
| 941 | #define NOTIMPLEMENTED_MSG "NOT IMPLEMENTED" |
| 942 | #endif |
| 943 | |
[email protected] | 0dfc81b | 2008-08-25 03:44:40 | [diff] [blame] | 944 | #if NOTIMPLEMENTED_POLICY == 0 |
[email protected] | 3822729 | 2012-01-30 19:41:54 | [diff] [blame] | 945 | #define NOTIMPLEMENTED() EAT_STREAM_PARAMETERS |
[email protected] | 0dfc81b | 2008-08-25 03:44:40 | [diff] [blame] | 946 | #elif NOTIMPLEMENTED_POLICY == 1 |
| 947 | // TODO, figure out how to generate a warning |
| 948 | #define NOTIMPLEMENTED() COMPILE_ASSERT(false, NOT_IMPLEMENTED) |
| 949 | #elif NOTIMPLEMENTED_POLICY == 2 |
| 950 | #define NOTIMPLEMENTED() COMPILE_ASSERT(false, NOT_IMPLEMENTED) |
| 951 | #elif NOTIMPLEMENTED_POLICY == 3 |
| 952 | #define NOTIMPLEMENTED() NOTREACHED() |
| 953 | #elif NOTIMPLEMENTED_POLICY == 4 |
[email protected] | f6cda75 | 2008-10-30 23:54:26 | [diff] [blame] | 954 | #define NOTIMPLEMENTED() LOG(ERROR) << NOTIMPLEMENTED_MSG |
[email protected] | 0dfc81b | 2008-08-25 03:44:40 | [diff] [blame] | 955 | #elif NOTIMPLEMENTED_POLICY == 5 |
| 956 | #define NOTIMPLEMENTED() do {\ |
[email protected] | b70ff01 | 2013-02-13 08:32:14 | [diff] [blame] | 957 | static bool logged_once = false;\ |
| 958 | LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ |
| 959 | logged_once = true;\ |
| 960 | } while(0);\ |
| 961 | EAT_STREAM_PARAMETERS |
[email protected] | 0dfc81b | 2008-08-25 03:44:40 | [diff] [blame] | 962 | #endif |
| 963 | |
[email protected] | 39be424 | 2008-08-07 18:31:40 | [diff] [blame] | 964 | #endif // BASE_LOGGING_H_ |