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