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