Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_LOGGING_H_ |
| 6 | #define BASE_LOGGING_H_ |
| 7 | |
| 8 | #include <cassert> |
| 9 | #include <string> |
| 10 | #include <cstring> |
| 11 | #include <sstream> |
| 12 | |
| 13 | #include "base/base_export.h" |
| 14 | #include "base/basictypes.h" |
Vitaly Buka | 8750b27 | 2015-08-18 18:39:08 -0700 | [diff] [blame] | 15 | #include "base/build/build_config.h" |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 16 | |
| 17 | // |
| 18 | // Optional message capabilities |
| 19 | // ----------------------------- |
| 20 | // Assertion failed messages and fatal errors are displayed in a dialog box |
| 21 | // before the application exits. However, running this UI creates a message |
| 22 | // loop, which causes application messages to be processed and potentially |
| 23 | // dispatched to existing application windows. Since the application is in a |
| 24 | // bad state when this assertion dialog is displayed, these messages may not |
| 25 | // get processed and hang the dialog, or the application might go crazy. |
| 26 | // |
| 27 | // Therefore, it can be beneficial to display the error dialog in a separate |
| 28 | // process from the main application. When the logging system needs to display |
| 29 | // a fatal error dialog box, it will look for a program called |
| 30 | // "DebugMessage.exe" in the same directory as the application executable. It |
| 31 | // will run this application with the message as the command line, and will |
| 32 | // not include the name of the application as is traditional for easier |
| 33 | // parsing. |
| 34 | // |
| 35 | // The code for DebugMessage.exe is only one line. In WinMain, do: |
| 36 | // MessageBox(NULL, GetCommandLineW(), L"Fatal Error", 0); |
| 37 | // |
| 38 | // If DebugMessage.exe is not found, the logging code will use a normal |
| 39 | // MessageBox, potentially causing the problems discussed above. |
| 40 | |
| 41 | |
| 42 | // Instructions |
| 43 | // ------------ |
| 44 | // |
| 45 | // Make a bunch of macros for logging. The way to log things is to stream |
| 46 | // things to LOG(<a particular severity level>). E.g., |
| 47 | // |
| 48 | // LOG(INFO) << "Found " << num_cookies << " cookies"; |
| 49 | // |
| 50 | // You can also do conditional logging: |
| 51 | // |
| 52 | // LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies"; |
| 53 | // |
| 54 | // The CHECK(condition) macro is active in both debug and release builds and |
| 55 | // effectively performs a LOG(FATAL) which terminates the process and |
| 56 | // generates a crashdump unless a debugger is attached. |
| 57 | // |
| 58 | // There are also "debug mode" logging macros like the ones above: |
| 59 | // |
| 60 | // DLOG(INFO) << "Found cookies"; |
| 61 | // |
| 62 | // DLOG_IF(INFO, num_cookies > 10) << "Got lots of cookies"; |
| 63 | // |
| 64 | // All "debug mode" logging is compiled away to nothing for non-debug mode |
| 65 | // compiles. LOG_IF and development flags also work well together |
| 66 | // because the code can be compiled away sometimes. |
| 67 | // |
| 68 | // We also have |
| 69 | // |
| 70 | // LOG_ASSERT(assertion); |
| 71 | // DLOG_ASSERT(assertion); |
| 72 | // |
| 73 | // which is syntactic sugar for {,D}LOG_IF(FATAL, assert fails) << assertion; |
| 74 | // |
| 75 | // There are "verbose level" logging macros. They look like |
| 76 | // |
| 77 | // VLOG(1) << "I'm printed when you run the program with --v=1 or more"; |
| 78 | // VLOG(2) << "I'm printed when you run the program with --v=2 or more"; |
| 79 | // |
| 80 | // These always log at the INFO log level (when they log at all). |
| 81 | // The verbose logging can also be turned on module-by-module. For instance, |
| 82 | // --vmodule=profile=2,icon_loader=1,browser_*=3,*/chromeos/*=4 --v=0 |
| 83 | // will cause: |
| 84 | // a. VLOG(2) and lower messages to be printed from profile.{h,cc} |
| 85 | // b. VLOG(1) and lower messages to be printed from icon_loader.{h,cc} |
| 86 | // c. VLOG(3) and lower messages to be printed from files prefixed with |
| 87 | // "browser" |
| 88 | // d. VLOG(4) and lower messages to be printed from files under a |
| 89 | // "chromeos" directory. |
| 90 | // e. VLOG(0) and lower messages to be printed from elsewhere |
| 91 | // |
| 92 | // The wildcarding functionality shown by (c) supports both '*' (match |
| 93 | // 0 or more characters) and '?' (match any single character) |
| 94 | // wildcards. Any pattern containing a forward or backward slash will |
| 95 | // be tested against the whole pathname and not just the module. |
| 96 | // E.g., "*/foo/bar/*=2" would change the logging level for all code |
| 97 | // in source files under a "foo/bar" directory. |
| 98 | // |
| 99 | // There's also VLOG_IS_ON(n) "verbose level" condition macro. To be used as |
| 100 | // |
| 101 | // if (VLOG_IS_ON(2)) { |
| 102 | // // do some logging preparation and logging |
| 103 | // // that can't be accomplished with just VLOG(2) << ...; |
| 104 | // } |
| 105 | // |
| 106 | // There is also a VLOG_IF "verbose level" condition macro for sample |
| 107 | // cases, when some extra computation and preparation for logs is not |
| 108 | // needed. |
| 109 | // |
| 110 | // VLOG_IF(1, (size > 1024)) |
| 111 | // << "I'm printed when size is more than 1024 and when you run the " |
| 112 | // "program with --v=1 or more"; |
| 113 | // |
| 114 | // We also override the standard 'assert' to use 'DLOG_ASSERT'. |
| 115 | // |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 116 | // The supported severity levels for macros that allow you to specify one |
| 117 | // are (in increasing order of severity) INFO, WARNING, ERROR, and FATAL. |
| 118 | // |
| 119 | // Very important: logging a message at the FATAL severity level causes |
| 120 | // the program to terminate (after the message is logged). |
| 121 | // |
| 122 | // There is the special severity of DFATAL, which logs FATAL in debug mode, |
| 123 | // ERROR in normal mode. |
| 124 | |
| 125 | namespace logging { |
| 126 | |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 127 | // Where to record logging output? A flat file and/or system debug log |
| 128 | // via OutputDebugString. |
| 129 | enum LoggingDestination { |
| 130 | LOG_NONE = 0, |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 131 | LOG_TO_SYSTEM_DEBUG_LOG = 1 << 1, |
| 132 | |
Vitaly Buka | 8750b27 | 2015-08-18 18:39:08 -0700 | [diff] [blame] | 133 | LOG_TO_ALL = LOG_TO_SYSTEM_DEBUG_LOG, |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 134 | |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 135 | LOG_DEFAULT = LOG_TO_SYSTEM_DEBUG_LOG, |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 136 | }; |
| 137 | |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 138 | struct BASE_EXPORT LoggingSettings { |
| 139 | // The defaults values are: |
| 140 | // |
| 141 | // logging_dest: LOG_DEFAULT |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 142 | LoggingSettings(); |
| 143 | |
| 144 | LoggingDestination logging_dest; |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | // Define different names for the BaseInitLoggingImpl() function depending on |
| 148 | // whether NDEBUG is defined or not so that we'll fail to link if someone tries |
| 149 | // to compile logging.cc with NDEBUG but includes logging.h without defining it, |
| 150 | // or vice versa. |
| 151 | #if NDEBUG |
| 152 | #define BaseInitLoggingImpl BaseInitLoggingImpl_built_with_NDEBUG |
| 153 | #else |
| 154 | #define BaseInitLoggingImpl BaseInitLoggingImpl_built_without_NDEBUG |
| 155 | #endif |
| 156 | |
| 157 | // Implementation of the InitLogging() method declared below. We use a |
| 158 | // more-specific name so we can #define it above without affecting other code |
| 159 | // that has named stuff "InitLogging". |
Vitaly Buka | 02fe593 | 2015-08-23 12:23:08 -0700 | [diff] [blame] | 160 | BASE_EXPORT bool BaseInitLoggingImpl(const LoggingSettings& settings); |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 161 | |
| 162 | // Sets the log file name and other global logging state. Calling this function |
| 163 | // is recommended, and is normally done at the beginning of application init. |
| 164 | // If you don't call it, all the flags will be initialized to their default |
| 165 | // values, and there is a race condition that may leak a critical section |
| 166 | // object if two threads try to do the first log at the same time. |
| 167 | // See the definition of the enums above for descriptions and default values. |
| 168 | // |
| 169 | // The default log file is initialized to "debug.log" in the application |
| 170 | // directory. You probably don't want this, especially since the program |
| 171 | // directory may not be writable on an enduser's system. |
| 172 | // |
| 173 | // This function may be called a second time to re-direct logging (e.g after |
| 174 | // loging in to a user partition), however it should never be called more than |
| 175 | // twice. |
| 176 | inline bool InitLogging(const LoggingSettings& settings) { |
| 177 | return BaseInitLoggingImpl(settings); |
| 178 | } |
| 179 | |
| 180 | // Sets the log level. Anything at or above this level will be written to the |
| 181 | // log file/displayed to the user (if applicable). Anything below this level |
| 182 | // will be silently ignored. The log level defaults to 0 (everything is logged |
| 183 | // up to level INFO) if this function is not called. |
| 184 | // Note that log messages for VLOG(x) are logged at level -x, so setting |
| 185 | // the min log level to negative values enables verbose logging. |
| 186 | BASE_EXPORT void SetMinLogLevel(int level); |
| 187 | |
| 188 | // Gets the current log level. |
| 189 | BASE_EXPORT int GetMinLogLevel(); |
| 190 | |
| 191 | // Gets the VLOG default verbosity level. |
| 192 | BASE_EXPORT int GetVlogVerbosity(); |
| 193 | |
| 194 | // Gets the current vlog level for the given file (usually taken from |
| 195 | // __FILE__). |
| 196 | |
| 197 | // Note that |N| is the size *with* the null terminator. |
Vitaly Buka | 890124a | 2015-10-09 16:18:51 -0700 | [diff] [blame] | 198 | inline int GetVlogLevelHelper(const char* file_start, size_t N) { |
| 199 | return GetVlogVerbosity(); |
| 200 | } |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 201 | |
| 202 | template <size_t N> |
| 203 | int GetVlogLevel(const char (&file)[N]) { |
| 204 | return GetVlogLevelHelper(file, N); |
| 205 | } |
| 206 | |
| 207 | // Sets the common items you want to be prepended to each log message. |
| 208 | // process and thread IDs default to off, the timestamp defaults to on. |
| 209 | // If this function is not called, logging defaults to writing the timestamp |
| 210 | // only. |
Vitaly Buka | 60b8f00 | 2015-08-20 13:47:48 -0700 | [diff] [blame] | 211 | BASE_EXPORT void SetLogItems(bool enable_process_id, |
| 212 | bool enable_thread_id, |
| 213 | bool enable_timestamp, |
| 214 | bool enable_tickcount); |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 215 | |
| 216 | // Sets whether or not you'd like to see fatal debug messages popped up in |
| 217 | // a dialog box or not. |
| 218 | // Dialogs are not shown by default. |
Vitaly Buka | 60b8f00 | 2015-08-20 13:47:48 -0700 | [diff] [blame] | 219 | void SetShowErrorDialogs(bool enable_dialogs); |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 220 | |
| 221 | // Sets the Log Assert Handler that will be used to notify of check failures. |
| 222 | // The default handler shows a dialog box and then terminate the process, |
| 223 | // however clients can use this function to override with their own handling |
| 224 | // (e.g. a silent one for Unit Tests) |
| 225 | typedef void (*LogAssertHandlerFunction)(const std::string& str); |
| 226 | BASE_EXPORT void SetLogAssertHandler(LogAssertHandlerFunction handler); |
| 227 | |
| 228 | // Sets the Log Message Handler that gets passed every log message before |
| 229 | // it's sent to other log destinations (if any). |
| 230 | // Returns true to signal that it handled the message and the message |
| 231 | // should not be sent to other log destinations. |
| 232 | typedef bool (*LogMessageHandlerFunction)(int severity, |
| 233 | const char* file, int line, size_t message_start, const std::string& str); |
| 234 | BASE_EXPORT void SetLogMessageHandler(LogMessageHandlerFunction handler); |
| 235 | BASE_EXPORT LogMessageHandlerFunction GetLogMessageHandler(); |
| 236 | |
| 237 | typedef int LogSeverity; |
| 238 | const LogSeverity LOG_VERBOSE = -1; // This is level 1 verbosity |
| 239 | // Note: the log severities are used to index into the array of names, |
| 240 | // see log_severity_names. |
| 241 | const LogSeverity LOG_INFO = 0; |
| 242 | const LogSeverity LOG_WARNING = 1; |
| 243 | const LogSeverity LOG_ERROR = 2; |
| 244 | const LogSeverity LOG_FATAL = 3; |
| 245 | const LogSeverity LOG_NUM_SEVERITIES = 4; |
| 246 | |
| 247 | // LOG_DFATAL is LOG_FATAL in debug mode, ERROR in normal mode |
| 248 | #ifdef NDEBUG |
| 249 | const LogSeverity LOG_DFATAL = LOG_ERROR; |
| 250 | #else |
| 251 | const LogSeverity LOG_DFATAL = LOG_FATAL; |
| 252 | #endif |
| 253 | |
| 254 | // A few definitions of macros that don't generate much code. These are used |
| 255 | // by LOG() and LOG_IF, etc. Since these are used all over our code, it's |
| 256 | // better to have compact code for these operations. |
| 257 | #define COMPACT_GOOGLE_LOG_EX_INFO(ClassName, ...) \ |
| 258 | logging::ClassName(__FILE__, __LINE__, logging::LOG_INFO , ##__VA_ARGS__) |
| 259 | #define COMPACT_GOOGLE_LOG_EX_WARNING(ClassName, ...) \ |
| 260 | logging::ClassName(__FILE__, __LINE__, logging::LOG_WARNING , ##__VA_ARGS__) |
| 261 | #define COMPACT_GOOGLE_LOG_EX_ERROR(ClassName, ...) \ |
| 262 | logging::ClassName(__FILE__, __LINE__, logging::LOG_ERROR , ##__VA_ARGS__) |
| 263 | #define COMPACT_GOOGLE_LOG_EX_FATAL(ClassName, ...) \ |
| 264 | logging::ClassName(__FILE__, __LINE__, logging::LOG_FATAL , ##__VA_ARGS__) |
| 265 | #define COMPACT_GOOGLE_LOG_EX_DFATAL(ClassName, ...) \ |
| 266 | logging::ClassName(__FILE__, __LINE__, logging::LOG_DFATAL , ##__VA_ARGS__) |
| 267 | |
| 268 | #define COMPACT_GOOGLE_LOG_INFO \ |
| 269 | COMPACT_GOOGLE_LOG_EX_INFO(LogMessage) |
| 270 | #define COMPACT_GOOGLE_LOG_WARNING \ |
| 271 | COMPACT_GOOGLE_LOG_EX_WARNING(LogMessage) |
| 272 | #define COMPACT_GOOGLE_LOG_ERROR \ |
| 273 | COMPACT_GOOGLE_LOG_EX_ERROR(LogMessage) |
| 274 | #define COMPACT_GOOGLE_LOG_FATAL \ |
| 275 | COMPACT_GOOGLE_LOG_EX_FATAL(LogMessage) |
| 276 | #define COMPACT_GOOGLE_LOG_DFATAL \ |
| 277 | COMPACT_GOOGLE_LOG_EX_DFATAL(LogMessage) |
| 278 | |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 279 | // As special cases, we can assume that LOG_IS_ON(FATAL) always holds. Also, |
| 280 | // LOG_IS_ON(DFATAL) always holds in debug mode. In particular, CHECK()s will |
| 281 | // always fire if they fail. |
| 282 | #define LOG_IS_ON(severity) \ |
| 283 | ((::logging::LOG_ ## severity) >= ::logging::GetMinLogLevel()) |
| 284 | |
| 285 | // We can't do any caching tricks with VLOG_IS_ON() like the |
| 286 | // google-glog version since it requires GCC extensions. This means |
| 287 | // that using the v-logging functions in conjunction with --vmodule |
| 288 | // may be slow. |
| 289 | #define VLOG_IS_ON(verboselevel) \ |
| 290 | ((verboselevel) <= ::logging::GetVlogLevel(__FILE__)) |
| 291 | |
| 292 | // Helper macro which avoids evaluating the arguments to a stream if |
| 293 | // the condition doesn't hold. Condition is evaluated once and only once. |
| 294 | #define LAZY_STREAM(stream, condition) \ |
| 295 | !(condition) ? (void) 0 : ::logging::LogMessageVoidify() & (stream) |
| 296 | |
| 297 | // We use the preprocessor's merging operator, "##", so that, e.g., |
| 298 | // LOG(INFO) becomes the token COMPACT_GOOGLE_LOG_INFO. There's some funny |
| 299 | // subtle difference between ostream member streaming functions (e.g., |
| 300 | // ostream::operator<<(int) and ostream non-member streaming functions |
| 301 | // (e.g., ::operator<<(ostream&, string&): it turns out that it's |
| 302 | // impossible to stream something like a string directly to an unnamed |
| 303 | // ostream. We employ a neat hack by calling the stream() member |
| 304 | // function of LogMessage which seems to avoid the problem. |
| 305 | #define LOG_STREAM(severity) COMPACT_GOOGLE_LOG_ ## severity.stream() |
| 306 | |
| 307 | #define LOG(severity) LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity)) |
| 308 | #define LOG_IF(severity, condition) \ |
| 309 | LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity) && (condition)) |
| 310 | |
| 311 | #define SYSLOG(severity) LOG(severity) |
| 312 | #define SYSLOG_IF(severity, condition) LOG_IF(severity, condition) |
| 313 | |
| 314 | // The VLOG macros log with negative verbosities. |
| 315 | #define VLOG_STREAM(verbose_level) \ |
| 316 | logging::LogMessage(__FILE__, __LINE__, -verbose_level).stream() |
| 317 | |
| 318 | #define VLOG(verbose_level) \ |
| 319 | LAZY_STREAM(VLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level)) |
| 320 | |
| 321 | #define VLOG_IF(verbose_level, condition) \ |
| 322 | LAZY_STREAM(VLOG_STREAM(verbose_level), \ |
| 323 | VLOG_IS_ON(verbose_level) && (condition)) |
| 324 | |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 325 | #define LOG_ASSERT(condition) \ |
| 326 | LOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". " |
| 327 | #define SYSLOG_ASSERT(condition) \ |
| 328 | SYSLOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". " |
| 329 | |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 330 | // The actual stream used isn't important. |
| 331 | #define EAT_STREAM_PARAMETERS \ |
| 332 | true ? (void) 0 : ::logging::LogMessageVoidify() & LOG_STREAM(FATAL) |
| 333 | |
| 334 | // CHECK dies with a fatal error if condition is not true. It is *not* |
| 335 | // controlled by NDEBUG, so the check will be executed regardless of |
| 336 | // compilation mode. |
| 337 | // |
| 338 | // We make sure CHECK et al. always evaluates their arguments, as |
| 339 | // doing CHECK(FunctionWithSideEffect()) is a common idiom. |
| 340 | |
| 341 | #if defined(OFFICIAL_BUILD) && defined(NDEBUG) && !defined(OS_ANDROID) |
| 342 | |
| 343 | // Make all CHECK functions discard their log strings to reduce code |
| 344 | // bloat for official release builds. |
| 345 | |
| 346 | // TODO(akalin): This would be more valuable if there were some way to |
| 347 | // remove BreakDebugger() from the backtrace, perhaps by turning it |
| 348 | // into a macro (like __debugbreak() on Windows). |
| 349 | #define CHECK(condition) \ |
| 350 | !(condition) ? ::base::debug::BreakDebugger() : EAT_STREAM_PARAMETERS |
| 351 | |
| 352 | #define PCHECK(condition) CHECK(condition) |
| 353 | |
| 354 | #define CHECK_OP(name, op, val1, val2) CHECK((val1) op (val2)) |
| 355 | |
| 356 | #else |
| 357 | |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 358 | #define CHECK(condition) \ |
| 359 | LAZY_STREAM(LOG_STREAM(FATAL), !(condition)) \ |
| 360 | << "Check failed: " #condition ". " |
| 361 | |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 362 | // Helper macro for binary operators. |
| 363 | // Don't use this macro directly in your code, use CHECK_EQ et al below. |
| 364 | // |
| 365 | // TODO(akalin): Rewrite this so that constructs like if (...) |
| 366 | // CHECK_EQ(...) else { ... } work properly. |
| 367 | #define CHECK_OP(name, op, val1, val2) \ |
| 368 | if (std::string* _result = \ |
| 369 | logging::Check##name##Impl((val1), (val2), \ |
| 370 | #val1 " " #op " " #val2)) \ |
| 371 | logging::LogMessage(__FILE__, __LINE__, _result).stream() |
| 372 | |
| 373 | #endif |
| 374 | |
| 375 | // Build the error message string. This is separate from the "Impl" |
| 376 | // function template because it is not performance critical and so can |
| 377 | // be out of line, while the "Impl" code should be inline. Caller |
| 378 | // takes ownership of the returned string. |
| 379 | template<class t1, class t2> |
| 380 | std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) { |
| 381 | std::ostringstream ss; |
| 382 | ss << names << " (" << v1 << " vs. " << v2 << ")"; |
| 383 | std::string* msg = new std::string(ss.str()); |
| 384 | return msg; |
| 385 | } |
| 386 | |
| 387 | // Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated |
| 388 | // in logging.cc. |
Vitaly Buka | 60b8f00 | 2015-08-20 13:47:48 -0700 | [diff] [blame] | 389 | extern template BASE_EXPORT std::string* |
| 390 | MakeCheckOpString<int, int>(const int&, const int&, const char* names); |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 391 | extern template BASE_EXPORT |
| 392 | std::string* MakeCheckOpString<unsigned long, unsigned long>( |
| 393 | const unsigned long&, const unsigned long&, const char* names); |
| 394 | extern template BASE_EXPORT |
| 395 | std::string* MakeCheckOpString<unsigned long, unsigned int>( |
| 396 | const unsigned long&, const unsigned int&, const char* names); |
| 397 | extern template BASE_EXPORT |
| 398 | std::string* MakeCheckOpString<unsigned int, unsigned long>( |
| 399 | const unsigned int&, const unsigned long&, const char* names); |
| 400 | extern template BASE_EXPORT |
| 401 | std::string* MakeCheckOpString<std::string, std::string>( |
| 402 | const std::string&, const std::string&, const char* name); |
| 403 | |
| 404 | // Helper functions for CHECK_OP macro. |
| 405 | // The (int, int) specialization works around the issue that the compiler |
| 406 | // will not instantiate the template version of the function on values of |
| 407 | // unnamed enum type - see comment below. |
| 408 | #define DEFINE_CHECK_OP_IMPL(name, op) \ |
| 409 | template <class t1, class t2> \ |
| 410 | inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \ |
| 411 | const char* names) { \ |
| 412 | if (v1 op v2) return NULL; \ |
| 413 | else return MakeCheckOpString(v1, v2, names); \ |
| 414 | } \ |
| 415 | inline std::string* Check##name##Impl(int v1, int v2, const char* names) { \ |
| 416 | if (v1 op v2) return NULL; \ |
| 417 | else return MakeCheckOpString(v1, v2, names); \ |
| 418 | } |
| 419 | DEFINE_CHECK_OP_IMPL(EQ, ==) |
| 420 | DEFINE_CHECK_OP_IMPL(NE, !=) |
| 421 | DEFINE_CHECK_OP_IMPL(LE, <=) |
| 422 | DEFINE_CHECK_OP_IMPL(LT, < ) |
| 423 | DEFINE_CHECK_OP_IMPL(GE, >=) |
| 424 | DEFINE_CHECK_OP_IMPL(GT, > ) |
| 425 | #undef DEFINE_CHECK_OP_IMPL |
| 426 | |
| 427 | #define CHECK_EQ(val1, val2) CHECK_OP(EQ, ==, val1, val2) |
| 428 | #define CHECK_NE(val1, val2) CHECK_OP(NE, !=, val1, val2) |
| 429 | #define CHECK_LE(val1, val2) CHECK_OP(LE, <=, val1, val2) |
| 430 | #define CHECK_LT(val1, val2) CHECK_OP(LT, < , val1, val2) |
| 431 | #define CHECK_GE(val1, val2) CHECK_OP(GE, >=, val1, val2) |
| 432 | #define CHECK_GT(val1, val2) CHECK_OP(GT, > , val1, val2) |
| 433 | #define CHECK_IMPLIES(val1, val2) CHECK(!(val1) || (val2)) |
| 434 | |
| 435 | #if defined(NDEBUG) |
| 436 | #define ENABLE_DLOG 0 |
| 437 | #else |
| 438 | #define ENABLE_DLOG 1 |
| 439 | #endif |
| 440 | |
| 441 | #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
| 442 | #define DCHECK_IS_ON() 0 |
| 443 | #else |
| 444 | #define DCHECK_IS_ON() 1 |
| 445 | #endif |
| 446 | |
| 447 | // Definitions for DLOG et al. |
| 448 | |
| 449 | #if ENABLE_DLOG |
| 450 | |
| 451 | #define DLOG_IS_ON(severity) LOG_IS_ON(severity) |
| 452 | #define DLOG_IF(severity, condition) LOG_IF(severity, condition) |
| 453 | #define DLOG_ASSERT(condition) LOG_ASSERT(condition) |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 454 | #define DVLOG_IF(verboselevel, condition) VLOG_IF(verboselevel, condition) |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 455 | |
| 456 | #else // ENABLE_DLOG |
| 457 | |
| 458 | // If ENABLE_DLOG is off, we want to avoid emitting any references to |
| 459 | // |condition| (which may reference a variable defined only if NDEBUG |
| 460 | // is not defined). Contrast this with DCHECK et al., which has |
| 461 | // different behavior. |
| 462 | |
| 463 | #define DLOG_IS_ON(severity) false |
| 464 | #define DLOG_IF(severity, condition) EAT_STREAM_PARAMETERS |
| 465 | #define DLOG_ASSERT(condition) EAT_STREAM_PARAMETERS |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 466 | #define DVLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 467 | |
| 468 | #endif // ENABLE_DLOG |
| 469 | |
| 470 | // DEBUG_MODE is for uses like |
| 471 | // if (DEBUG_MODE) foo.CheckThatFoo(); |
| 472 | // instead of |
| 473 | // #ifndef NDEBUG |
| 474 | // foo.CheckThatFoo(); |
| 475 | // #endif |
| 476 | // |
| 477 | // We tie its state to ENABLE_DLOG. |
| 478 | enum { DEBUG_MODE = ENABLE_DLOG }; |
| 479 | |
| 480 | #undef ENABLE_DLOG |
| 481 | |
| 482 | #define DLOG(severity) \ |
| 483 | LAZY_STREAM(LOG_STREAM(severity), DLOG_IS_ON(severity)) |
| 484 | |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 485 | #define DVLOG(verboselevel) DVLOG_IF(verboselevel, VLOG_IS_ON(verboselevel)) |
| 486 | |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 487 | // Definitions for DCHECK et al. |
| 488 | |
| 489 | #if DCHECK_IS_ON() |
| 490 | |
| 491 | #define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \ |
| 492 | COMPACT_GOOGLE_LOG_EX_FATAL(ClassName , ##__VA_ARGS__) |
| 493 | #define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_FATAL |
| 494 | const LogSeverity LOG_DCHECK = LOG_FATAL; |
| 495 | |
| 496 | #else // DCHECK_IS_ON() |
| 497 | |
| 498 | // These are just dummy values. |
| 499 | #define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \ |
| 500 | COMPACT_GOOGLE_LOG_EX_INFO(ClassName , ##__VA_ARGS__) |
| 501 | #define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_INFO |
| 502 | const LogSeverity LOG_DCHECK = LOG_INFO; |
| 503 | |
| 504 | #endif // DCHECK_IS_ON() |
| 505 | |
| 506 | // DCHECK et al. make sure to reference |condition| regardless of |
| 507 | // whether DCHECKs are enabled; this is so that we don't get unused |
| 508 | // variable warnings if the only use of a variable is in a DCHECK. |
| 509 | // This behavior is different from DLOG_IF et al. |
| 510 | |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 511 | #define DCHECK(condition) \ |
| 512 | LAZY_STREAM(LOG_STREAM(DCHECK), DCHECK_IS_ON() ? !(condition) : false) \ |
| 513 | << "Check failed: " #condition ". " |
| 514 | |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 515 | // Helper macro for binary operators. |
| 516 | // Don't use this macro directly in your code, use DCHECK_EQ et al below. |
| 517 | #define DCHECK_OP(name, op, val1, val2) \ |
| 518 | if (DCHECK_IS_ON()) \ |
| 519 | if (std::string* _result = logging::Check##name##Impl( \ |
| 520 | (val1), (val2), #val1 " " #op " " #val2)) \ |
| 521 | logging::LogMessage(__FILE__, __LINE__, ::logging::LOG_DCHECK, _result) \ |
| 522 | .stream() |
| 523 | |
| 524 | // Equality/Inequality checks - compare two values, and log a |
| 525 | // LOG_DCHECK message including the two values when the result is not |
| 526 | // as expected. The values must have operator<<(ostream, ...) |
| 527 | // defined. |
| 528 | // |
| 529 | // You may append to the error message like so: |
| 530 | // DCHECK_NE(1, 2) << ": The world must be ending!"; |
| 531 | // |
| 532 | // We are very careful to ensure that each argument is evaluated exactly |
| 533 | // once, and that anything which is legal to pass as a function argument is |
| 534 | // legal here. In particular, the arguments may be temporary expressions |
| 535 | // which will end up being destroyed at the end of the apparent statement, |
| 536 | // for example: |
| 537 | // DCHECK_EQ(string("abc")[1], 'b'); |
| 538 | // |
| 539 | // WARNING: These may not compile correctly if one of the arguments is a pointer |
| 540 | // and the other is NULL. To work around this, simply static_cast NULL to the |
| 541 | // type of the desired pointer. |
| 542 | |
| 543 | #define DCHECK_EQ(val1, val2) DCHECK_OP(EQ, ==, val1, val2) |
| 544 | #define DCHECK_NE(val1, val2) DCHECK_OP(NE, !=, val1, val2) |
| 545 | #define DCHECK_LE(val1, val2) DCHECK_OP(LE, <=, val1, val2) |
| 546 | #define DCHECK_LT(val1, val2) DCHECK_OP(LT, < , val1, val2) |
| 547 | #define DCHECK_GE(val1, val2) DCHECK_OP(GE, >=, val1, val2) |
| 548 | #define DCHECK_GT(val1, val2) DCHECK_OP(GT, > , val1, val2) |
| 549 | #define DCHECK_IMPLIES(val1, val2) DCHECK(!(val1) || (val2)) |
| 550 | |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 551 | #define NOTREACHED() DCHECK(false) |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 552 | |
| 553 | // Redefine the standard assert to use our nice log files |
| 554 | #undef assert |
| 555 | #define assert(x) DLOG_ASSERT(x) |
| 556 | |
| 557 | // This class more or less represents a particular log message. You |
| 558 | // create an instance of LogMessage and then stream stuff to it. |
| 559 | // When you finish streaming to it, ~LogMessage is called and the |
| 560 | // full message gets streamed to the appropriate destination. |
| 561 | // |
| 562 | // You shouldn't actually use LogMessage's constructor to log things, |
| 563 | // though. You should use the LOG() macro (and variants thereof) |
| 564 | // above. |
| 565 | class BASE_EXPORT LogMessage { |
| 566 | public: |
| 567 | // Used for LOG(severity). |
| 568 | LogMessage(const char* file, int line, LogSeverity severity); |
| 569 | |
| 570 | // Used for CHECK_EQ(), etc. Takes ownership of the given string. |
| 571 | // Implied severity = LOG_FATAL. |
| 572 | LogMessage(const char* file, int line, std::string* result); |
| 573 | |
| 574 | // Used for DCHECK_EQ(), etc. Takes ownership of the given string. |
| 575 | LogMessage(const char* file, int line, LogSeverity severity, |
| 576 | std::string* result); |
| 577 | |
| 578 | ~LogMessage(); |
| 579 | |
| 580 | std::ostream& stream() { return stream_; } |
| 581 | |
| 582 | private: |
| 583 | void Init(const char* file, int line); |
| 584 | |
| 585 | LogSeverity severity_; |
| 586 | std::ostringstream stream_; |
| 587 | size_t message_start_; // Offset of the start of the message (past prefix |
| 588 | // info). |
| 589 | // The file and line information passed in to the constructor. |
| 590 | const char* file_; |
| 591 | const int line_; |
| 592 | |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 593 | DISALLOW_COPY_AND_ASSIGN(LogMessage); |
| 594 | }; |
| 595 | |
| 596 | // A non-macro interface to the log facility; (useful |
| 597 | // when the logging level is not a compile-time constant). |
| 598 | inline void LogAtLevel(int const log_level, std::string const &msg) { |
| 599 | LogMessage(__FILE__, __LINE__, log_level).stream() << msg; |
| 600 | } |
| 601 | |
| 602 | // This class is used to explicitly ignore values in the conditional |
| 603 | // logging macros. This avoids compiler warnings like "value computed |
| 604 | // is not used" and "statement has no effect". |
| 605 | class LogMessageVoidify { |
| 606 | public: |
| 607 | LogMessageVoidify() { } |
| 608 | // This has to be an operator with a precedence lower than << but |
| 609 | // higher than ?: |
| 610 | void operator&(std::ostream&) { } |
| 611 | }; |
| 612 | |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 613 | // Async signal safe logging mechanism. |
| 614 | BASE_EXPORT void RawLog(int level, const char* message); |
| 615 | |
| 616 | #define RAW_LOG(level, message) logging::RawLog(logging::LOG_ ## level, message) |
| 617 | |
| 618 | #define RAW_CHECK(condition) \ |
| 619 | do { \ |
| 620 | if (!(condition)) \ |
| 621 | logging::RawLog(logging::LOG_FATAL, "Check failed: " #condition "\n"); \ |
| 622 | } while (0) |
| 623 | |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 624 | |
| 625 | } // namespace logging |
| 626 | |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 627 | // The NOTIMPLEMENTED() macro annotates codepaths which have |
| 628 | // not been implemented yet. |
| 629 | // |
| 630 | // The implementation of this macro is controlled by NOTIMPLEMENTED_POLICY: |
| 631 | // 0 -- Do nothing (stripped by compiler) |
| 632 | // 1 -- Warn at compile time |
| 633 | // 2 -- Fail at compile time |
| 634 | // 3 -- Fail at runtime (DCHECK) |
| 635 | // 4 -- [default] LOG(ERROR) at runtime |
| 636 | // 5 -- LOG(ERROR) at runtime, only once per call-site |
| 637 | |
| 638 | #ifndef NOTIMPLEMENTED_POLICY |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 639 | // Select default policy: LOG(ERROR) |
| 640 | #define NOTIMPLEMENTED_POLICY 4 |
| 641 | #endif |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 642 | |
| 643 | #if defined(COMPILER_GCC) |
| 644 | // On Linux, with GCC, we can use __PRETTY_FUNCTION__ to get the demangled name |
| 645 | // of the current function in the NOTIMPLEMENTED message. |
| 646 | #define NOTIMPLEMENTED_MSG "Not implemented reached in " << __PRETTY_FUNCTION__ |
| 647 | #else |
| 648 | #define NOTIMPLEMENTED_MSG "NOT IMPLEMENTED" |
| 649 | #endif |
| 650 | |
| 651 | #if NOTIMPLEMENTED_POLICY == 0 |
| 652 | #define NOTIMPLEMENTED() EAT_STREAM_PARAMETERS |
| 653 | #elif NOTIMPLEMENTED_POLICY == 1 |
| 654 | // TODO, figure out how to generate a warning |
| 655 | #define NOTIMPLEMENTED() COMPILE_ASSERT(false, NOT_IMPLEMENTED) |
| 656 | #elif NOTIMPLEMENTED_POLICY == 2 |
| 657 | #define NOTIMPLEMENTED() COMPILE_ASSERT(false, NOT_IMPLEMENTED) |
| 658 | #elif NOTIMPLEMENTED_POLICY == 3 |
| 659 | #define NOTIMPLEMENTED() NOTREACHED() |
| 660 | #elif NOTIMPLEMENTED_POLICY == 4 |
| 661 | #define NOTIMPLEMENTED() LOG(ERROR) << NOTIMPLEMENTED_MSG |
| 662 | #elif NOTIMPLEMENTED_POLICY == 5 |
| 663 | #define NOTIMPLEMENTED() do {\ |
| 664 | static bool logged_once = false;\ |
| 665 | LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ |
| 666 | logged_once = true;\ |
| 667 | } while(0);\ |
| 668 | EAT_STREAM_PARAMETERS |
| 669 | #endif |
| 670 | |
| 671 | #endif // BASE_LOGGING_H_ |