[email protected] | 34a90773 | 2012-01-20 06:33:27 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
[email protected] | 39be424 | 2008-08-07 18:31:40 | [diff] [blame] | 5 | #ifndef BASE_LOGGING_H_ |
| 6 | #define BASE_LOGGING_H_ |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 7 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | |
[email protected] | e7972d1 | 2011-06-18 11:53:14 | [diff] [blame] | 10 | #include <cassert> |
Sharon Yang | 7cb919a | 2019-05-20 20:27:15 | [diff] [blame] | 11 | #include <cstdint> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 12 | #include <cstring> |
| 13 | #include <sstream> |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 14 | #include <string> |
jbroman | 6bcfec42 | 2016-05-26 00:28:46 | [diff] [blame] | 15 | #include <type_traits> |
| 16 | #include <utility> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 17 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 18 | #include "base/base_export.h" |
alex-ac | cc1bde6 | 2017-04-19 08:33:55 | [diff] [blame] | 19 | #include "base/callback_forward.h" |
danakj | cb7c529 | 2016-12-20 19:05:35 | [diff] [blame] | 20 | #include "base/compiler_specific.h" |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 21 | #include "base/immediate_crash.h" |
Xiaohan Wang | ee536b21 | 2019-05-07 16:16:07 | [diff] [blame] | 22 | #include "base/logging_buildflags.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 23 | #include "base/macros.h" |
Etienne Pierre-Doray | d120ebf | 2018-09-14 23:38:21 | [diff] [blame] | 24 | #include "base/scoped_clear_last_error.h" |
alex-ac | cc1bde6 | 2017-04-19 08:33:55 | [diff] [blame] | 25 | #include "base/strings/string_piece_forward.h" |
jbroman | 6bcfec42 | 2016-05-26 00:28:46 | [diff] [blame] | 26 | #include "base/template_util.h" |
[email protected] | 90509cb | 2011-03-25 18:46:38 | [diff] [blame] | 27 | #include "build/build_config.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 28 | |
Robbie McElrath | 8bf4984 | 2019-08-20 22:22:53 | [diff] [blame] | 29 | #if defined(OS_CHROMEOS) |
| 30 | #include <cstdio> |
| 31 | #endif |
| 32 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 33 | // |
| 34 | // Optional message capabilities |
| 35 | // ----------------------------- |
| 36 | // Assertion failed messages and fatal errors are displayed in a dialog box |
| 37 | // before the application exits. However, running this UI creates a message |
| 38 | // loop, which causes application messages to be processed and potentially |
| 39 | // dispatched to existing application windows. Since the application is in a |
| 40 | // bad state when this assertion dialog is displayed, these messages may not |
| 41 | // get processed and hang the dialog, or the application might go crazy. |
| 42 | // |
| 43 | // Therefore, it can be beneficial to display the error dialog in a separate |
| 44 | // process from the main application. When the logging system needs to display |
| 45 | // a fatal error dialog box, it will look for a program called |
| 46 | // "DebugMessage.exe" in the same directory as the application executable. It |
| 47 | // will run this application with the message as the command line, and will |
| 48 | // not include the name of the application as is traditional for easier |
| 49 | // parsing. |
| 50 | // |
| 51 | // The code for DebugMessage.exe is only one line. In WinMain, do: |
| 52 | // MessageBox(NULL, GetCommandLineW(), L"Fatal Error", 0); |
| 53 | // |
| 54 | // If DebugMessage.exe is not found, the logging code will use a normal |
| 55 | // MessageBox, potentially causing the problems discussed above. |
| 56 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 57 | // Instructions |
| 58 | // ------------ |
| 59 | // |
| 60 | // Make a bunch of macros for logging. The way to log things is to stream |
| 61 | // things to LOG(<a particular severity level>). E.g., |
| 62 | // |
| 63 | // LOG(INFO) << "Found " << num_cookies << " cookies"; |
| 64 | // |
| 65 | // You can also do conditional logging: |
| 66 | // |
| 67 | // LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies"; |
| 68 | // |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 69 | // The CHECK(condition) macro is active in both debug and release builds and |
| 70 | // effectively performs a LOG(FATAL) which terminates the process and |
| 71 | // generates a crashdump unless a debugger is attached. |
| 72 | // |
| 73 | // There are also "debug mode" logging macros like the ones above: |
| 74 | // |
| 75 | // DLOG(INFO) << "Found cookies"; |
| 76 | // |
| 77 | // DLOG_IF(INFO, num_cookies > 10) << "Got lots of cookies"; |
| 78 | // |
| 79 | // All "debug mode" logging is compiled away to nothing for non-debug mode |
| 80 | // compiles. LOG_IF and development flags also work well together |
| 81 | // because the code can be compiled away sometimes. |
| 82 | // |
| 83 | // We also have |
| 84 | // |
| 85 | // LOG_ASSERT(assertion); |
| 86 | // DLOG_ASSERT(assertion); |
| 87 | // |
| 88 | // which is syntactic sugar for {,D}LOG_IF(FATAL, assert fails) << assertion; |
| 89 | // |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 90 | // There are "verbose level" logging macros. They look like |
| 91 | // |
| 92 | // VLOG(1) << "I'm printed when you run the program with --v=1 or more"; |
| 93 | // VLOG(2) << "I'm printed when you run the program with --v=2 or more"; |
| 94 | // |
| 95 | // These always log at the INFO log level (when they log at all). |
| 96 | // The verbose logging can also be turned on module-by-module. For instance, |
[email protected] | b0d38d4c | 2010-10-29 00:39:48 | [diff] [blame] | 97 | // --vmodule=profile=2,icon_loader=1,browser_*=3,*/chromeos/*=4 --v=0 |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 98 | // will cause: |
| 99 | // a. VLOG(2) and lower messages to be printed from profile.{h,cc} |
| 100 | // b. VLOG(1) and lower messages to be printed from icon_loader.{h,cc} |
| 101 | // c. VLOG(3) and lower messages to be printed from files prefixed with |
| 102 | // "browser" |
[email protected] | e11de72 | 2010-11-01 20:50:55 | [diff] [blame] | 103 | // d. VLOG(4) and lower messages to be printed from files under a |
[email protected] | b0d38d4c | 2010-10-29 00:39:48 | [diff] [blame] | 104 | // "chromeos" directory. |
[email protected] | e11de72 | 2010-11-01 20:50:55 | [diff] [blame] | 105 | // e. VLOG(0) and lower messages to be printed from elsewhere |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 106 | // |
| 107 | // The wildcarding functionality shown by (c) supports both '*' (match |
[email protected] | b0d38d4c | 2010-10-29 00:39:48 | [diff] [blame] | 108 | // 0 or more characters) and '?' (match any single character) |
| 109 | // wildcards. Any pattern containing a forward or backward slash will |
| 110 | // be tested against the whole pathname and not just the module. |
| 111 | // E.g., "*/foo/bar/*=2" would change the logging level for all code |
| 112 | // in source files under a "foo/bar" directory. |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 113 | // |
| 114 | // There's also VLOG_IS_ON(n) "verbose level" condition macro. To be used as |
| 115 | // |
| 116 | // if (VLOG_IS_ON(2)) { |
| 117 | // // do some logging preparation and logging |
| 118 | // // that can't be accomplished with just VLOG(2) << ...; |
| 119 | // } |
| 120 | // |
| 121 | // There is also a VLOG_IF "verbose level" condition macro for sample |
| 122 | // cases, when some extra computation and preparation for logs is not |
| 123 | // needed. |
| 124 | // |
| 125 | // VLOG_IF(1, (size > 1024)) |
| 126 | // << "I'm printed when size is more than 1024 and when you run the " |
| 127 | // "program with --v=1 or more"; |
| 128 | // |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 129 | // We also override the standard 'assert' to use 'DLOG_ASSERT'. |
| 130 | // |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 131 | // Lastly, there is: |
| 132 | // |
| 133 | // PLOG(ERROR) << "Couldn't do foo"; |
| 134 | // DPLOG(ERROR) << "Couldn't do foo"; |
| 135 | // PLOG_IF(ERROR, cond) << "Couldn't do foo"; |
| 136 | // DPLOG_IF(ERROR, cond) << "Couldn't do foo"; |
| 137 | // PCHECK(condition) << "Couldn't do foo"; |
| 138 | // DPCHECK(condition) << "Couldn't do foo"; |
| 139 | // |
| 140 | // which append the last system error to the message in string form (taken from |
| 141 | // GetLastError() on Windows and errno on POSIX). |
| 142 | // |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 143 | // The supported severity levels for macros that allow you to specify one |
[email protected] | f2c0549 | 2014-06-17 12:04:23 | [diff] [blame] | 144 | // are (in increasing order of severity) INFO, WARNING, ERROR, and FATAL. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 145 | // |
| 146 | // Very important: logging a message at the FATAL severity level causes |
| 147 | // the program to terminate (after the message is logged). |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 148 | // |
[email protected] | f2c0549 | 2014-06-17 12:04:23 | [diff] [blame] | 149 | // There is the special severity of DFATAL, which logs FATAL in debug mode, |
| 150 | // ERROR in normal mode. |
Rob Schonberger | 4563721 | 2018-12-03 04:46:25 | [diff] [blame] | 151 | // |
| 152 | // Output is of the format, for example: |
| 153 | // [3816:3877:0812/234555.406952:VERBOSE1:drm_device_handle.cc(90)] Succeeded |
| 154 | // authenticating /dev/dri/card0 in 0 ms with 1 attempt(s) |
| 155 | // |
| 156 | // The colon separated fields inside the brackets are as follows: |
| 157 | // 0. An optional Logfile prefix (not included in this example) |
| 158 | // 1. Process ID |
| 159 | // 2. Thread ID |
| 160 | // 3. The date/time of the log message, in MMDD/HHMMSS.Milliseconds format |
| 161 | // 4. The log level |
| 162 | // 5. The filename and line number where the log was instantiated |
| 163 | // |
| 164 | // Note that the visibility can be changed by setting preferences in |
| 165 | // SetLogItems() |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 166 | |
| 167 | namespace logging { |
| 168 | |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 169 | // TODO(avi): do we want to do a unification of character types here? |
| 170 | #if defined(OS_WIN) |
jdoerrie | 5c4dc4e | 2019-02-01 18:02:33 | [diff] [blame] | 171 | typedef base::char16 PathChar; |
Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 172 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 173 | typedef char PathChar; |
| 174 | #endif |
| 175 | |
Sharon Yang | 7cb919a | 2019-05-20 20:27:15 | [diff] [blame] | 176 | // A bitmask of potential logging destinations. |
| 177 | using LoggingDestination = uint32_t; |
| 178 | // Specifies where logs will be written. Multiple destinations can be specified |
| 179 | // with bitwise OR. |
| 180 | // Unless destination is LOG_NONE, all logs with severity ERROR and above will |
| 181 | // be written to stderr in addition to the specified destination. |
| 182 | enum : uint32_t { |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 183 | LOG_NONE = 0, |
| 184 | LOG_TO_FILE = 1 << 0, |
| 185 | LOG_TO_SYSTEM_DEBUG_LOG = 1 << 1, |
Sharon Yang | 7cb919a | 2019-05-20 20:27:15 | [diff] [blame] | 186 | LOG_TO_STDERR = 1 << 2, |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 187 | |
Sharon Yang | 7cb919a | 2019-05-20 20:27:15 | [diff] [blame] | 188 | LOG_TO_ALL = LOG_TO_FILE | LOG_TO_SYSTEM_DEBUG_LOG | LOG_TO_STDERR, |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 189 | |
Sharon Yang | 7cb919a | 2019-05-20 20:27:15 | [diff] [blame] | 190 | // On Windows, use a file next to the exe. |
| 191 | // On POSIX platforms, where it may not even be possible to locate the |
| 192 | // executable on disk, use stderr. |
| 193 | // On Fuchsia, use the Fuchsia logging service. |
| 194 | #if defined(OS_FUCHSIA) || defined(OS_NACL) |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 195 | LOG_DEFAULT = LOG_TO_SYSTEM_DEBUG_LOG, |
Sharon Yang | 7cb919a | 2019-05-20 20:27:15 | [diff] [blame] | 196 | #elif defined(OS_WIN) |
| 197 | LOG_DEFAULT = LOG_TO_FILE, |
| 198 | #elif defined(OS_POSIX) |
| 199 | LOG_DEFAULT = LOG_TO_SYSTEM_DEBUG_LOG | LOG_TO_STDERR, |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 200 | #endif |
| 201 | }; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 202 | |
| 203 | // Indicates that the log file should be locked when being written to. |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 204 | // Unless there is only one single-threaded process that is logging to |
| 205 | // the log file, the file should be locked during writes to make each |
[email protected] | 3ee50d1 | 2014-03-05 01:43:27 | [diff] [blame] | 206 | // log output atomic. Other writers will block. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 207 | // |
| 208 | // All processes writing to the log file must have their locking set for it to |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 209 | // work properly. Defaults to LOCK_LOG_FILE. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 210 | enum LogLockingState { LOCK_LOG_FILE, DONT_LOCK_LOG_FILE }; |
| 211 | |
| 212 | // On startup, should we delete or append to an existing log file (if any)? |
| 213 | // Defaults to APPEND_TO_OLD_LOG_FILE. |
| 214 | enum OldFileDeletionState { DELETE_OLD_LOG_FILE, APPEND_TO_OLD_LOG_FILE }; |
| 215 | |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 216 | struct BASE_EXPORT LoggingSettings { |
Sharon Yang | 7cb919a | 2019-05-20 20:27:15 | [diff] [blame] | 217 | // Equivalent to logging destination enum, but allows for multiple |
| 218 | // destinations. |
Wez | 7e12562 | 2019-05-29 22:11:28 | [diff] [blame] | 219 | uint32_t logging_dest = LOG_DEFAULT; |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 220 | |
Robbie McElrath | 8bf4984 | 2019-08-20 22:22:53 | [diff] [blame] | 221 | // The four settings below have an effect only when LOG_TO_FILE is |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 222 | // set in |logging_dest|. |
Robbie McElrath | 8bf4984 | 2019-08-20 22:22:53 | [diff] [blame] | 223 | const PathChar* log_file_path = nullptr; |
Wez | 7e12562 | 2019-05-29 22:11:28 | [diff] [blame] | 224 | LogLockingState lock_log = LOCK_LOG_FILE; |
| 225 | OldFileDeletionState delete_old = APPEND_TO_OLD_LOG_FILE; |
Robbie McElrath | 8bf4984 | 2019-08-20 22:22:53 | [diff] [blame] | 226 | #if defined(OS_CHROMEOS) |
| 227 | // Contains an optional file that logs should be written to. If present, |
| 228 | // |log_file_path| will be ignored, and the logging system will take ownership |
| 229 | // of the FILE. If there's an error writing to this file, no fallback paths |
| 230 | // will be opened. |
| 231 | FILE* log_file = nullptr; |
| 232 | #endif |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 233 | }; |
[email protected] | ff3d0c3 | 2010-08-23 19:57:46 | [diff] [blame] | 234 | |
| 235 | // Define different names for the BaseInitLoggingImpl() function depending on |
| 236 | // whether NDEBUG is defined or not so that we'll fail to link if someone tries |
| 237 | // to compile logging.cc with NDEBUG but includes logging.h without defining it, |
| 238 | // or vice versa. |
wez | a245bd07 | 2017-06-18 23:26:34 | [diff] [blame] | 239 | #if defined(NDEBUG) |
[email protected] | ff3d0c3 | 2010-08-23 19:57:46 | [diff] [blame] | 240 | #define BaseInitLoggingImpl BaseInitLoggingImpl_built_with_NDEBUG |
| 241 | #else |
| 242 | #define BaseInitLoggingImpl BaseInitLoggingImpl_built_without_NDEBUG |
| 243 | #endif |
| 244 | |
| 245 | // Implementation of the InitLogging() method declared below. We use a |
| 246 | // more-specific name so we can #define it above without affecting other code |
| 247 | // that has named stuff "InitLogging". |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 248 | BASE_EXPORT bool BaseInitLoggingImpl(const LoggingSettings& settings); |
[email protected] | ff3d0c3 | 2010-08-23 19:57:46 | [diff] [blame] | 249 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 250 | // Sets the log file name and other global logging state. Calling this function |
| 251 | // is recommended, and is normally done at the beginning of application init. |
| 252 | // If you don't call it, all the flags will be initialized to their default |
| 253 | // values, and there is a race condition that may leak a critical section |
| 254 | // object if two threads try to do the first log at the same time. |
| 255 | // See the definition of the enums above for descriptions and default values. |
| 256 | // |
| 257 | // The default log file is initialized to "debug.log" in the application |
| 258 | // directory. You probably don't want this, especially since the program |
| 259 | // directory may not be writable on an enduser's system. |
[email protected] | 064aa16 | 2011-12-03 00:30:08 | [diff] [blame] | 260 | // |
| 261 | // This function may be called a second time to re-direct logging (e.g after |
| 262 | // loging in to a user partition), however it should never be called more than |
| 263 | // twice. |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 264 | inline bool InitLogging(const LoggingSettings& settings) { |
| 265 | return BaseInitLoggingImpl(settings); |
[email protected] | ff3d0c3 | 2010-08-23 19:57:46 | [diff] [blame] | 266 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 267 | |
| 268 | // Sets the log level. Anything at or above this level will be written to the |
| 269 | // log file/displayed to the user (if applicable). Anything below this level |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 270 | // will be silently ignored. The log level defaults to 0 (everything is logged |
| 271 | // up to level INFO) if this function is not called. |
| 272 | // Note that log messages for VLOG(x) are logged at level -x, so setting |
| 273 | // the min log level to negative values enables verbose logging. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 274 | BASE_EXPORT void SetMinLogLevel(int level); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 275 | |
[email protected] | 8a2986ca | 2009-04-10 19:13:42 | [diff] [blame] | 276 | // Gets the current log level. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 277 | BASE_EXPORT int GetMinLogLevel(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 278 | |
skobes | c78c0ad7 | 2015-12-07 20:21:23 | [diff] [blame] | 279 | // Used by LOG_IS_ON to lazy-evaluate stream arguments. |
| 280 | BASE_EXPORT bool ShouldCreateLogMessage(int severity); |
| 281 | |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 282 | // Gets the VLOG default verbosity level. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 283 | BASE_EXPORT int GetVlogVerbosity(); |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 284 | |
[email protected] | 2f4e9a6 | 2010-09-29 21:25:14 | [diff] [blame] | 285 | // Note that |N| is the size *with* the null terminator. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 286 | BASE_EXPORT int GetVlogLevelHelper(const char* file_start, size_t N); |
[email protected] | 2f4e9a6 | 2010-09-29 21:25:14 | [diff] [blame] | 287 | |
tnagel | 270da92 | 2017-05-24 12:10:44 | [diff] [blame] | 288 | // Gets the current vlog level for the given file (usually taken from __FILE__). |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 289 | template <size_t N> |
| 290 | int GetVlogLevel(const char (&file)[N]) { |
| 291 | return GetVlogLevelHelper(file, N); |
| 292 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 293 | |
| 294 | // Sets the common items you want to be prepended to each log message. |
| 295 | // process and thread IDs default to off, the timestamp defaults to on. |
| 296 | // If this function is not called, logging defaults to writing the timestamp |
| 297 | // only. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 298 | BASE_EXPORT void SetLogItems(bool enable_process_id, bool enable_thread_id, |
| 299 | bool enable_timestamp, bool enable_tickcount); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 300 | |
James Cook | a0536c3 | 2018-08-01 20:13:31 | [diff] [blame] | 301 | // Sets an optional prefix to add to each log message. |prefix| is not copied |
| 302 | // and should be a raw string constant. |prefix| must only contain ASCII letters |
| 303 | // to avoid confusion with PIDs and timestamps. Pass null to remove the prefix. |
| 304 | // Logging defaults to no prefix. |
| 305 | BASE_EXPORT void SetLogPrefix(const char* prefix); |
| 306 | |
[email protected] | 81e0a85 | 2010-08-17 00:38:12 | [diff] [blame] | 307 | // Sets whether or not you'd like to see fatal debug messages popped up in |
| 308 | // a dialog box or not. |
| 309 | // Dialogs are not shown by default. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 310 | BASE_EXPORT void SetShowErrorDialogs(bool enable_dialogs); |
[email protected] | 81e0a85 | 2010-08-17 00:38:12 | [diff] [blame] | 311 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 312 | // Sets the Log Assert Handler that will be used to notify of check failures. |
alex-ac | cc1bde6 | 2017-04-19 08:33:55 | [diff] [blame] | 313 | // Resets Log Assert Handler on object destruction. |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 314 | // The default handler shows a dialog box and then terminate the process, |
| 315 | // however clients can use this function to override with their own handling |
| 316 | // (e.g. a silent one for Unit Tests) |
alex-ac | cc1bde6 | 2017-04-19 08:33:55 | [diff] [blame] | 317 | using LogAssertHandlerFunction = |
kylechar | 83fb51e5 | 2019-03-14 15:30:43 | [diff] [blame] | 318 | base::RepeatingCallback<void(const char* file, |
| 319 | int line, |
| 320 | const base::StringPiece message, |
| 321 | const base::StringPiece stack_trace)>; |
alex-ac | cc1bde6 | 2017-04-19 08:33:55 | [diff] [blame] | 322 | |
| 323 | class BASE_EXPORT ScopedLogAssertHandler { |
| 324 | public: |
| 325 | explicit ScopedLogAssertHandler(LogAssertHandlerFunction handler); |
| 326 | ~ScopedLogAssertHandler(); |
| 327 | |
| 328 | private: |
| 329 | DISALLOW_COPY_AND_ASSIGN(ScopedLogAssertHandler); |
| 330 | }; |
[email protected] | 64e5cc0 | 2010-11-03 19:20:27 | [diff] [blame] | 331 | |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 332 | // Sets the Log Message Handler that gets passed every log message before |
| 333 | // it's sent to other log destinations (if any). |
| 334 | // Returns true to signal that it handled the message and the message |
| 335 | // should not be sent to other log destinations. |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 336 | typedef bool (*LogMessageHandlerFunction)(int severity, |
| 337 | const char* file, int line, size_t message_start, const std::string& str); |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 338 | BASE_EXPORT void SetLogMessageHandler(LogMessageHandlerFunction handler); |
| 339 | BASE_EXPORT LogMessageHandlerFunction GetLogMessageHandler(); |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 340 | |
kmarshall | fe2f09f8 | 2017-04-20 21:05:26 | [diff] [blame] | 341 | // The ANALYZER_ASSUME_TRUE(bool arg) macro adds compiler-specific hints |
| 342 | // to Clang which control what code paths are statically analyzed, |
| 343 | // and is meant to be used in conjunction with assert & assert-like functions. |
| 344 | // The expression is passed straight through if analysis isn't enabled. |
Kevin Marshall | 7273edd | 2017-06-20 22:19:36 | [diff] [blame] | 345 | // |
| 346 | // ANALYZER_SKIP_THIS_PATH() suppresses static analysis for the current |
| 347 | // codepath and any other branching codepaths that might follow. |
kmarshall | fe2f09f8 | 2017-04-20 21:05:26 | [diff] [blame] | 348 | #if defined(__clang_analyzer__) |
| 349 | |
| 350 | inline constexpr bool AnalyzerNoReturn() __attribute__((analyzer_noreturn)) { |
| 351 | return false; |
| 352 | } |
| 353 | |
| 354 | inline constexpr bool AnalyzerAssumeTrue(bool arg) { |
| 355 | // AnalyzerNoReturn() is invoked and analysis is terminated if |arg| is |
| 356 | // false. |
| 357 | return arg || AnalyzerNoReturn(); |
| 358 | } |
| 359 | |
Kevin Marshall | 7273edd | 2017-06-20 22:19:36 | [diff] [blame] | 360 | #define ANALYZER_ASSUME_TRUE(arg) logging::AnalyzerAssumeTrue(!!(arg)) |
| 361 | #define ANALYZER_SKIP_THIS_PATH() \ |
| 362 | static_cast<void>(::logging::AnalyzerNoReturn()) |
Kevin Marshall | 089565ec | 2017-07-13 02:57:21 | [diff] [blame] | 363 | #define ANALYZER_ALLOW_UNUSED(var) static_cast<void>(var); |
kmarshall | fe2f09f8 | 2017-04-20 21:05:26 | [diff] [blame] | 364 | |
| 365 | #else // !defined(__clang_analyzer__) |
| 366 | |
| 367 | #define ANALYZER_ASSUME_TRUE(arg) (arg) |
Kevin Marshall | 7273edd | 2017-06-20 22:19:36 | [diff] [blame] | 368 | #define ANALYZER_SKIP_THIS_PATH() |
Kevin Marshall | 089565ec | 2017-07-13 02:57:21 | [diff] [blame] | 369 | #define ANALYZER_ALLOW_UNUSED(var) static_cast<void>(var); |
kmarshall | fe2f09f8 | 2017-04-20 21:05:26 | [diff] [blame] | 370 | |
| 371 | #endif // defined(__clang_analyzer__) |
| 372 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 373 | typedef int LogSeverity; |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 374 | const LogSeverity LOG_VERBOSE = -1; // This is level 1 verbosity |
| 375 | // Note: the log severities are used to index into the array of names, |
| 376 | // see log_severity_names. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 377 | const LogSeverity LOG_INFO = 0; |
| 378 | const LogSeverity LOG_WARNING = 1; |
| 379 | const LogSeverity LOG_ERROR = 2; |
[email protected] | f2c0549 | 2014-06-17 12:04:23 | [diff] [blame] | 380 | const LogSeverity LOG_FATAL = 3; |
| 381 | const LogSeverity LOG_NUM_SEVERITIES = 4; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 382 | |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 383 | // LOG_DFATAL is LOG_FATAL in debug mode, ERROR in normal mode |
wez | a245bd07 | 2017-06-18 23:26:34 | [diff] [blame] | 384 | #if defined(NDEBUG) |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 385 | const LogSeverity LOG_DFATAL = LOG_ERROR; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 386 | #else |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 387 | const LogSeverity LOG_DFATAL = LOG_FATAL; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 388 | #endif |
| 389 | |
| 390 | // A few definitions of macros that don't generate much code. These are used |
| 391 | // by LOG() and LOG_IF, etc. Since these are used all over our code, it's |
| 392 | // better to have compact code for these operations. |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 393 | #define COMPACT_GOOGLE_LOG_EX_INFO(ClassName, ...) \ |
tsniatowski | 612550f | 2016-07-21 18:26:20 | [diff] [blame] | 394 | ::logging::ClassName(__FILE__, __LINE__, ::logging::LOG_INFO, ##__VA_ARGS__) |
| 395 | #define COMPACT_GOOGLE_LOG_EX_WARNING(ClassName, ...) \ |
| 396 | ::logging::ClassName(__FILE__, __LINE__, ::logging::LOG_WARNING, \ |
| 397 | ##__VA_ARGS__) |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 398 | #define COMPACT_GOOGLE_LOG_EX_ERROR(ClassName, ...) \ |
tsniatowski | 612550f | 2016-07-21 18:26:20 | [diff] [blame] | 399 | ::logging::ClassName(__FILE__, __LINE__, ::logging::LOG_ERROR, ##__VA_ARGS__) |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 400 | #define COMPACT_GOOGLE_LOG_EX_FATAL(ClassName, ...) \ |
tsniatowski | 612550f | 2016-07-21 18:26:20 | [diff] [blame] | 401 | ::logging::ClassName(__FILE__, __LINE__, ::logging::LOG_FATAL, ##__VA_ARGS__) |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 402 | #define COMPACT_GOOGLE_LOG_EX_DFATAL(ClassName, ...) \ |
tsniatowski | 612550f | 2016-07-21 18:26:20 | [diff] [blame] | 403 | ::logging::ClassName(__FILE__, __LINE__, ::logging::LOG_DFATAL, ##__VA_ARGS__) |
Wez | 289477f | 2017-08-24 20:51:30 | [diff] [blame] | 404 | #define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \ |
| 405 | ::logging::ClassName(__FILE__, __LINE__, ::logging::LOG_DCHECK, ##__VA_ARGS__) |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 406 | |
Wez | 289477f | 2017-08-24 20:51:30 | [diff] [blame] | 407 | #define COMPACT_GOOGLE_LOG_INFO COMPACT_GOOGLE_LOG_EX_INFO(LogMessage) |
| 408 | #define COMPACT_GOOGLE_LOG_WARNING COMPACT_GOOGLE_LOG_EX_WARNING(LogMessage) |
| 409 | #define COMPACT_GOOGLE_LOG_ERROR COMPACT_GOOGLE_LOG_EX_ERROR(LogMessage) |
| 410 | #define COMPACT_GOOGLE_LOG_FATAL COMPACT_GOOGLE_LOG_EX_FATAL(LogMessage) |
| 411 | #define COMPACT_GOOGLE_LOG_DFATAL COMPACT_GOOGLE_LOG_EX_DFATAL(LogMessage) |
| 412 | #define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_EX_DCHECK(LogMessage) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 413 | |
[email protected] | 8d12730 | 2013-01-10 02:41:57 | [diff] [blame] | 414 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 415 | // wingdi.h defines ERROR to be 0. When we call LOG(ERROR), it gets |
| 416 | // substituted with 0, and it expands to COMPACT_GOOGLE_LOG_0. To allow us |
| 417 | // to keep using this syntax, we define this macro to do the same thing |
| 418 | // as COMPACT_GOOGLE_LOG_ERROR, and also define ERROR the same way that |
| 419 | // the Windows SDK does for consistency. |
| 420 | #define ERROR 0 |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 421 | #define COMPACT_GOOGLE_LOG_EX_0(ClassName, ...) \ |
| 422 | COMPACT_GOOGLE_LOG_EX_ERROR(ClassName , ##__VA_ARGS__) |
| 423 | #define COMPACT_GOOGLE_LOG_0 COMPACT_GOOGLE_LOG_ERROR |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 424 | // Needed for LOG_IS_ON(ERROR). |
| 425 | const LogSeverity LOG_0 = LOG_ERROR; |
[email protected] | 8d12730 | 2013-01-10 02:41:57 | [diff] [blame] | 426 | #endif |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 427 | |
[email protected] | f2c0549 | 2014-06-17 12:04:23 | [diff] [blame] | 428 | // As special cases, we can assume that LOG_IS_ON(FATAL) always holds. Also, |
| 429 | // LOG_IS_ON(DFATAL) always holds in debug mode. In particular, CHECK()s will |
| 430 | // always fire if they fail. |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 431 | #define LOG_IS_ON(severity) \ |
skobes | c78c0ad7 | 2015-12-07 20:21:23 | [diff] [blame] | 432 | (::logging::ShouldCreateLogMessage(::logging::LOG_##severity)) |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 433 | |
Ken MacKay | 70e886700 | 2019-01-16 00:22:15 | [diff] [blame] | 434 | // We don't do any caching tricks with VLOG_IS_ON() like the |
| 435 | // google-glog version since it increases binary size. This means |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 436 | // that using the v-logging functions in conjunction with --vmodule |
| 437 | // may be slow. |
| 438 | #define VLOG_IS_ON(verboselevel) \ |
| 439 | ((verboselevel) <= ::logging::GetVlogLevel(__FILE__)) |
| 440 | |
| 441 | // Helper macro which avoids evaluating the arguments to a stream if |
chcunningham | f6a9608 | 2015-02-07 01:58:37 | [diff] [blame] | 442 | // the condition doesn't hold. Condition is evaluated once and only once. |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 443 | #define LAZY_STREAM(stream, condition) \ |
| 444 | !(condition) ? (void) 0 : ::logging::LogMessageVoidify() & (stream) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 445 | |
| 446 | // We use the preprocessor's merging operator, "##", so that, e.g., |
| 447 | // LOG(INFO) becomes the token COMPACT_GOOGLE_LOG_INFO. There's some funny |
| 448 | // subtle difference between ostream member streaming functions (e.g., |
| 449 | // ostream::operator<<(int) and ostream non-member streaming functions |
| 450 | // (e.g., ::operator<<(ostream&, string&): it turns out that it's |
| 451 | // impossible to stream something like a string directly to an unnamed |
| 452 | // ostream. We employ a neat hack by calling the stream() member |
| 453 | // function of LogMessage which seems to avoid the problem. |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 454 | #define LOG_STREAM(severity) COMPACT_GOOGLE_LOG_ ## severity.stream() |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 455 | |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 456 | #define LOG(severity) LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity)) |
| 457 | #define LOG_IF(severity, condition) \ |
| 458 | LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity) && (condition)) |
| 459 | |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 460 | // The VLOG macros log with negative verbosities. |
| 461 | #define VLOG_STREAM(verbose_level) \ |
tsniatowski | 612550f | 2016-07-21 18:26:20 | [diff] [blame] | 462 | ::logging::LogMessage(__FILE__, __LINE__, -verbose_level).stream() |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 463 | |
| 464 | #define VLOG(verbose_level) \ |
| 465 | LAZY_STREAM(VLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level)) |
| 466 | |
| 467 | #define VLOG_IF(verbose_level, condition) \ |
| 468 | LAZY_STREAM(VLOG_STREAM(verbose_level), \ |
| 469 | VLOG_IS_ON(verbose_level) && (condition)) |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 470 | |
[email protected] | fb879b1a | 2011-03-06 18:16:31 | [diff] [blame] | 471 | #if defined (OS_WIN) |
| 472 | #define VPLOG_STREAM(verbose_level) \ |
tsniatowski | 612550f | 2016-07-21 18:26:20 | [diff] [blame] | 473 | ::logging::Win32ErrorLogMessage(__FILE__, __LINE__, -verbose_level, \ |
[email protected] | fb879b1a | 2011-03-06 18:16:31 | [diff] [blame] | 474 | ::logging::GetLastSystemErrorCode()).stream() |
Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 475 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
[email protected] | fb879b1a | 2011-03-06 18:16:31 | [diff] [blame] | 476 | #define VPLOG_STREAM(verbose_level) \ |
tsniatowski | 612550f | 2016-07-21 18:26:20 | [diff] [blame] | 477 | ::logging::ErrnoLogMessage(__FILE__, __LINE__, -verbose_level, \ |
[email protected] | fb879b1a | 2011-03-06 18:16:31 | [diff] [blame] | 478 | ::logging::GetLastSystemErrorCode()).stream() |
| 479 | #endif |
| 480 | |
| 481 | #define VPLOG(verbose_level) \ |
| 482 | LAZY_STREAM(VPLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level)) |
| 483 | |
| 484 | #define VPLOG_IF(verbose_level, condition) \ |
| 485 | LAZY_STREAM(VPLOG_STREAM(verbose_level), \ |
| 486 | VLOG_IS_ON(verbose_level) && (condition)) |
| 487 | |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 488 | // TODO(akalin): Add more VLOG variants, e.g. VPLOG. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 489 | |
kmarshall | fe2f09f8 | 2017-04-20 21:05:26 | [diff] [blame] | 490 | #define LOG_ASSERT(condition) \ |
| 491 | LOG_IF(FATAL, !(ANALYZER_ASSUME_TRUE(condition))) \ |
| 492 | << "Assert failed: " #condition ". " |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 493 | |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 494 | #if defined(OS_WIN) |
[email protected] | c914d8a | 2014-04-23 01:11:01 | [diff] [blame] | 495 | #define PLOG_STREAM(severity) \ |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 496 | COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \ |
| 497 | ::logging::GetLastSystemErrorCode()).stream() |
Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 498 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
[email protected] | c914d8a | 2014-04-23 01:11:01 | [diff] [blame] | 499 | #define PLOG_STREAM(severity) \ |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 500 | COMPACT_GOOGLE_LOG_EX_ ## severity(ErrnoLogMessage, \ |
| 501 | ::logging::GetLastSystemErrorCode()).stream() |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 502 | #endif |
| 503 | |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 504 | #define PLOG(severity) \ |
| 505 | LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity)) |
| 506 | |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 507 | #define PLOG_IF(severity, condition) \ |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 508 | LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity) && (condition)) |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 509 | |
scottmg | 3c957a5 | 2016-12-10 20:57:59 | [diff] [blame] | 510 | BASE_EXPORT extern std::ostream* g_swallow_stream; |
| 511 | |
| 512 | // Note that g_swallow_stream is used instead of an arbitrary LOG() stream to |
| 513 | // avoid the creation of an object with a non-trivial destructor (LogMessage). |
| 514 | // On MSVC x86 (checked on 2015 Update 3), this causes a few additional |
| 515 | // pointless instructions to be emitted even at full optimization level, even |
| 516 | // though the : arm of the ternary operator is clearly never executed. Using a |
| 517 | // simpler object to be &'d with Voidify() avoids these extra instructions. |
| 518 | // Using a simpler POD object with a templated operator<< also works to avoid |
| 519 | // these instructions. However, this causes warnings on statically defined |
| 520 | // implementations of operator<<(std::ostream, ...) in some .cc files, because |
| 521 | // they become defined-but-unreferenced functions. A reinterpret_cast of 0 to an |
| 522 | // ostream* also is not suitable, because some compilers warn of undefined |
| 523 | // behavior. |
| 524 | #define EAT_STREAM_PARAMETERS \ |
| 525 | true ? (void)0 \ |
| 526 | : ::logging::LogMessageVoidify() & (*::logging::g_swallow_stream) |
[email protected] | ddb9b33 | 2011-12-02 07:31:09 | [diff] [blame] | 527 | |
erikwright | 6ad937b | 2015-07-22 20:05:52 | [diff] [blame] | 528 | // Captures the result of a CHECK_EQ (for example) and facilitates testing as a |
| 529 | // boolean. |
| 530 | class CheckOpResult { |
| 531 | public: |
wez | f01a9b7 | 2016-03-19 01:18:07 | [diff] [blame] | 532 | // |message| must be non-null if and only if the check failed. |
erikwright | 6ad937b | 2015-07-22 20:05:52 | [diff] [blame] | 533 | CheckOpResult(std::string* message) : message_(message) {} |
| 534 | // Returns true if the check succeeded. |
| 535 | operator bool() const { return !message_; } |
| 536 | // Returns the message. |
| 537 | std::string* message() { return message_; } |
| 538 | |
| 539 | private: |
| 540 | std::string* message_; |
| 541 | }; |
| 542 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 543 | // CHECK dies with a fatal error if condition is not true. It is *not* |
| 544 | // controlled by NDEBUG, so the check will be executed regardless of |
| 545 | // compilation mode. |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 546 | // |
| 547 | // We make sure CHECK et al. always evaluates their arguments, as |
| 548 | // doing CHECK(FunctionWithSideEffect()) is a common idiom. |
[email protected] | ddb9b33 | 2011-12-02 07:31:09 | [diff] [blame] | 549 | |
danakj | b9d5931 | 2016-05-04 20:06:31 | [diff] [blame] | 550 | #if defined(OFFICIAL_BUILD) && defined(NDEBUG) |
[email protected] | ddb9b33 | 2011-12-02 07:31:09 | [diff] [blame] | 551 | |
Chris Palmer | 61343b0 | 2016-11-29 20:44:10 | [diff] [blame] | 552 | // Make all CHECK functions discard their log strings to reduce code bloat, and |
| 553 | // improve performance, for official release builds. |
| 554 | // |
primiano | ba910a6 | 2016-07-07 22:14:48 | [diff] [blame] | 555 | // This is not calling BreakDebugger since this is called frequently, and |
| 556 | // calling an out-of-line function instead of a noreturn inline macro prevents |
| 557 | // compiler optimizations. |
Chris Palmer | 61343b0 | 2016-11-29 20:44:10 | [diff] [blame] | 558 | #define CHECK(condition) \ |
danakj | cb7c529 | 2016-12-20 19:05:35 | [diff] [blame] | 559 | UNLIKELY(!(condition)) ? IMMEDIATE_CRASH() : EAT_STREAM_PARAMETERS |
[email protected] | ddb9b33 | 2011-12-02 07:31:09 | [diff] [blame] | 560 | |
Robert Sesek | d2f495f | 2017-07-25 22:03:14 | [diff] [blame] | 561 | // PCHECK includes the system error code, which is useful for determining |
| 562 | // why the condition failed. In official builds, preserve only the error code |
| 563 | // message so that it is available in crash reports. The stringified |
| 564 | // condition and any additional stream parameters are dropped. |
| 565 | #define PCHECK(condition) \ |
| 566 | LAZY_STREAM(PLOG_STREAM(FATAL), UNLIKELY(!(condition))); \ |
| 567 | EAT_STREAM_PARAMETERS |
[email protected] | ddb9b33 | 2011-12-02 07:31:09 | [diff] [blame] | 568 | |
| 569 | #define CHECK_OP(name, op, val1, val2) CHECK((val1) op (val2)) |
| 570 | |
danakj | b9d5931 | 2016-05-04 20:06:31 | [diff] [blame] | 571 | #else // !(OFFICIAL_BUILD && NDEBUG) |
[email protected] | ddb9b33 | 2011-12-02 07:31:09 | [diff] [blame] | 572 | |
tnagel | 4a045d3f | 2015-07-12 14:19:28 | [diff] [blame] | 573 | // Do as much work as possible out of line to reduce inline code size. |
tsniatowski | 612550f | 2016-07-21 18:26:20 | [diff] [blame] | 574 | #define CHECK(condition) \ |
| 575 | LAZY_STREAM(::logging::LogMessage(__FILE__, __LINE__, #condition).stream(), \ |
kmarshall | fe2f09f8 | 2017-04-20 21:05:26 | [diff] [blame] | 576 | !ANALYZER_ASSUME_TRUE(condition)) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 577 | |
kmarshall | fe2f09f8 | 2017-04-20 21:05:26 | [diff] [blame] | 578 | #define PCHECK(condition) \ |
| 579 | LAZY_STREAM(PLOG_STREAM(FATAL), !ANALYZER_ASSUME_TRUE(condition)) \ |
kmarshall | e23eed0 | 2017-02-11 02:13:23 | [diff] [blame] | 580 | << "Check failed: " #condition ". " |
brucedawson | 9d16025 | 2014-10-23 20:14:14 | [diff] [blame] | 581 | |
[email protected] | ddb9b33 | 2011-12-02 07:31:09 | [diff] [blame] | 582 | // Helper macro for binary operators. |
| 583 | // Don't use this macro directly in your code, use CHECK_EQ et al below. |
erikwright | 6ad937b | 2015-07-22 20:05:52 | [diff] [blame] | 584 | // The 'switch' is used to prevent the 'else' from being ambiguous when the |
| 585 | // macro is used in an 'if' clause such as: |
| 586 | // if (a == 1) |
| 587 | // CHECK_EQ(2, a); |
| 588 | #define CHECK_OP(name, op, val1, val2) \ |
| 589 | switch (0) case 0: default: \ |
tsniatowski | 612550f | 2016-07-21 18:26:20 | [diff] [blame] | 590 | if (::logging::CheckOpResult true_if_passed = \ |
| 591 | ::logging::Check##name##Impl((val1), (val2), \ |
| 592 | #val1 " " #op " " #val2)) \ |
erikwright | 6ad937b | 2015-07-22 20:05:52 | [diff] [blame] | 593 | ; \ |
| 594 | else \ |
tsniatowski | 612550f | 2016-07-21 18:26:20 | [diff] [blame] | 595 | ::logging::LogMessage(__FILE__, __LINE__, true_if_passed.message()).stream() |
[email protected] | ddb9b33 | 2011-12-02 07:31:09 | [diff] [blame] | 596 | |
danakj | b9d5931 | 2016-05-04 20:06:31 | [diff] [blame] | 597 | #endif // !(OFFICIAL_BUILD && NDEBUG) |
[email protected] | ddb9b33 | 2011-12-02 07:31:09 | [diff] [blame] | 598 | |
brucedawson | 93a60b8c | 2016-04-28 20:46:16 | [diff] [blame] | 599 | // This formats a value for a failing CHECK_XX statement. Ordinarily, |
| 600 | // it uses the definition for operator<<, with a few special cases below. |
| 601 | template <typename T> |
jbroman | 6bcfec42 | 2016-05-26 00:28:46 | [diff] [blame] | 602 | inline typename std::enable_if< |
raphael.kubo.da.costa | 81f2120 | 2016-11-28 18:36:36 | [diff] [blame] | 603 | base::internal::SupportsOstreamOperator<const T&>::value && |
| 604 | !std::is_function<typename std::remove_pointer<T>::type>::value, |
jbroman | 6bcfec42 | 2016-05-26 00:28:46 | [diff] [blame] | 605 | void>::type |
| 606 | MakeCheckOpValueString(std::ostream* os, const T& v) { |
brucedawson | 93a60b8c | 2016-04-28 20:46:16 | [diff] [blame] | 607 | (*os) << v; |
| 608 | } |
| 609 | |
Collin Baker | 89e9e07 | 2019-06-10 22:39:05 | [diff] [blame] | 610 | // Overload for types that no operator<< but do have .ToString() defined. |
| 611 | template <typename T> |
| 612 | inline typename std::enable_if< |
| 613 | !base::internal::SupportsOstreamOperator<const T&>::value && |
| 614 | base::internal::SupportsToString<const T&>::value, |
| 615 | void>::type |
| 616 | MakeCheckOpValueString(std::ostream* os, const T& v) { |
| 617 | (*os) << v.ToString(); |
| 618 | } |
| 619 | |
raphael.kubo.da.costa | 81f2120 | 2016-11-28 18:36:36 | [diff] [blame] | 620 | // Provide an overload for functions and function pointers. Function pointers |
| 621 | // don't implicitly convert to void* but do implicitly convert to bool, so |
| 622 | // without this function pointers are always printed as 1 or 0. (MSVC isn't |
| 623 | // standards-conforming here and converts function pointers to regular |
| 624 | // pointers, so this is a no-op for MSVC.) |
| 625 | template <typename T> |
| 626 | inline typename std::enable_if< |
| 627 | std::is_function<typename std::remove_pointer<T>::type>::value, |
| 628 | void>::type |
| 629 | MakeCheckOpValueString(std::ostream* os, const T& v) { |
| 630 | (*os) << reinterpret_cast<const void*>(v); |
| 631 | } |
| 632 | |
jbroman | 6bcfec42 | 2016-05-26 00:28:46 | [diff] [blame] | 633 | // We need overloads for enums that don't support operator<<. |
| 634 | // (i.e. scoped enums where no operator<< overload was declared). |
| 635 | template <typename T> |
| 636 | inline typename std::enable_if< |
| 637 | !base::internal::SupportsOstreamOperator<const T&>::value && |
| 638 | std::is_enum<T>::value, |
| 639 | void>::type |
| 640 | MakeCheckOpValueString(std::ostream* os, const T& v) { |
danakj | 6d0446e5 | 2017-04-05 16:22:29 | [diff] [blame] | 641 | (*os) << static_cast<typename std::underlying_type<T>::type>(v); |
jbroman | 6bcfec42 | 2016-05-26 00:28:46 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | // We need an explicit overload for std::nullptr_t. |
| 645 | BASE_EXPORT void MakeCheckOpValueString(std::ostream* os, std::nullptr_t p); |
brucedawson | 93a60b8c | 2016-04-28 20:46:16 | [diff] [blame] | 646 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 647 | // Build the error message string. This is separate from the "Impl" |
| 648 | // function template because it is not performance critical and so can |
[email protected] | 9c7132e | 2011-02-08 07:39:08 | [diff] [blame] | 649 | // be out of line, while the "Impl" code should be inline. Caller |
| 650 | // takes ownership of the returned string. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 651 | template<class t1, class t2> |
| 652 | std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) { |
| 653 | std::ostringstream ss; |
brucedawson | 93a60b8c | 2016-04-28 20:46:16 | [diff] [blame] | 654 | ss << names << " ("; |
| 655 | MakeCheckOpValueString(&ss, v1); |
| 656 | ss << " vs. "; |
| 657 | MakeCheckOpValueString(&ss, v2); |
| 658 | ss << ")"; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 659 | std::string* msg = new std::string(ss.str()); |
| 660 | return msg; |
| 661 | } |
| 662 | |
[email protected] | 6d445d3 | 2010-09-30 19:10:03 | [diff] [blame] | 663 | // Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated |
| 664 | // in logging.cc. |
[email protected] | dc72da3 | 2011-10-24 20:20:30 | [diff] [blame] | 665 | extern template BASE_EXPORT std::string* MakeCheckOpString<int, int>( |
[email protected] | 6d445d3 | 2010-09-30 19:10:03 | [diff] [blame] | 666 | const int&, const int&, const char* names); |
[email protected] | dc72da3 | 2011-10-24 20:20:30 | [diff] [blame] | 667 | extern template BASE_EXPORT |
| 668 | std::string* MakeCheckOpString<unsigned long, unsigned long>( |
[email protected] | 6d445d3 | 2010-09-30 19:10:03 | [diff] [blame] | 669 | const unsigned long&, const unsigned long&, const char* names); |
[email protected] | dc72da3 | 2011-10-24 20:20:30 | [diff] [blame] | 670 | extern template BASE_EXPORT |
| 671 | std::string* MakeCheckOpString<unsigned long, unsigned int>( |
[email protected] | 6d445d3 | 2010-09-30 19:10:03 | [diff] [blame] | 672 | const unsigned long&, const unsigned int&, const char* names); |
[email protected] | dc72da3 | 2011-10-24 20:20:30 | [diff] [blame] | 673 | extern template BASE_EXPORT |
| 674 | std::string* MakeCheckOpString<unsigned int, unsigned long>( |
[email protected] | 6d445d3 | 2010-09-30 19:10:03 | [diff] [blame] | 675 | const unsigned int&, const unsigned long&, const char* names); |
[email protected] | dc72da3 | 2011-10-24 20:20:30 | [diff] [blame] | 676 | extern template BASE_EXPORT |
| 677 | std::string* MakeCheckOpString<std::string, std::string>( |
[email protected] | 6d445d3 | 2010-09-30 19:10:03 | [diff] [blame] | 678 | const std::string&, const std::string&, const char* name); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 679 | |
[email protected] | 7151260 | 2010-11-01 22:19:56 | [diff] [blame] | 680 | // Helper functions for CHECK_OP macro. |
| 681 | // The (int, int) specialization works around the issue that the compiler |
| 682 | // will not instantiate the template version of the function on values of |
| 683 | // unnamed enum type - see comment below. |
kmarshall | fe2f09f8 | 2017-04-20 21:05:26 | [diff] [blame] | 684 | // |
| 685 | // The checked condition is wrapped with ANALYZER_ASSUME_TRUE, which under |
| 686 | // static analysis builds, blocks analysis of the current path if the |
| 687 | // condition is false. |
kmarshall | 9db26fb | 2017-02-15 01:05:33 | [diff] [blame] | 688 | #define DEFINE_CHECK_OP_IMPL(name, op) \ |
| 689 | template <class t1, class t2> \ |
| 690 | inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \ |
| 691 | const char* names) { \ |
kmarshall | fe2f09f8 | 2017-04-20 21:05:26 | [diff] [blame] | 692 | if (ANALYZER_ASSUME_TRUE(v1 op v2)) \ |
kmarshall | 9db26fb | 2017-02-15 01:05:33 | [diff] [blame] | 693 | return NULL; \ |
| 694 | else \ |
| 695 | return ::logging::MakeCheckOpString(v1, v2, names); \ |
| 696 | } \ |
[email protected] | 7151260 | 2010-11-01 22:19:56 | [diff] [blame] | 697 | inline std::string* Check##name##Impl(int v1, int v2, const char* names) { \ |
kmarshall | fe2f09f8 | 2017-04-20 21:05:26 | [diff] [blame] | 698 | if (ANALYZER_ASSUME_TRUE(v1 op v2)) \ |
kmarshall | 9db26fb | 2017-02-15 01:05:33 | [diff] [blame] | 699 | return NULL; \ |
| 700 | else \ |
| 701 | return ::logging::MakeCheckOpString(v1, v2, names); \ |
[email protected] | 7151260 | 2010-11-01 22:19:56 | [diff] [blame] | 702 | } |
| 703 | DEFINE_CHECK_OP_IMPL(EQ, ==) |
| 704 | DEFINE_CHECK_OP_IMPL(NE, !=) |
| 705 | DEFINE_CHECK_OP_IMPL(LE, <=) |
| 706 | DEFINE_CHECK_OP_IMPL(LT, < ) |
| 707 | DEFINE_CHECK_OP_IMPL(GE, >=) |
| 708 | DEFINE_CHECK_OP_IMPL(GT, > ) |
| 709 | #undef DEFINE_CHECK_OP_IMPL |
[email protected] | e150c038 | 2010-03-02 00:41:12 | [diff] [blame] | 710 | |
| 711 | #define CHECK_EQ(val1, val2) CHECK_OP(EQ, ==, val1, val2) |
| 712 | #define CHECK_NE(val1, val2) CHECK_OP(NE, !=, val1, val2) |
| 713 | #define CHECK_LE(val1, val2) CHECK_OP(LE, <=, val1, val2) |
| 714 | #define CHECK_LT(val1, val2) CHECK_OP(LT, < , val1, val2) |
| 715 | #define CHECK_GE(val1, val2) CHECK_OP(GE, >=, val1, val2) |
| 716 | #define CHECK_GT(val1, val2) CHECK_OP(GT, > , val1, val2) |
| 717 | |
jam | 121900aa | 2016-04-19 00:07:34 | [diff] [blame] | 718 | #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
danakj | e649f57 | 2015-01-08 23:35:58 | [diff] [blame] | 719 | #define DCHECK_IS_ON() 0 |
[email protected] | 1a150551 | 2014-03-10 18:23:38 | [diff] [blame] | 720 | #else |
danakj | e649f57 | 2015-01-08 23:35:58 | [diff] [blame] | 721 | #define DCHECK_IS_ON() 1 |
[email protected] | e3cca33 | 2009-08-20 01:20:29 | [diff] [blame] | 722 | #endif |
| 723 | |
[email protected] | d15e56c | 2010-09-30 21:12:33 | [diff] [blame] | 724 | // Definitions for DLOG et al. |
| 725 | |
gab | 190f754 | 2016-08-01 20:03:41 | [diff] [blame] | 726 | #if DCHECK_IS_ON() |
[email protected] | d926c20 | 2010-10-01 02:58:24 | [diff] [blame] | 727 | |
[email protected] | 5e98780 | 2010-11-01 19:49:22 | [diff] [blame] | 728 | #define DLOG_IS_ON(severity) LOG_IS_ON(severity) |
[email protected] | d926c20 | 2010-10-01 02:58:24 | [diff] [blame] | 729 | #define DLOG_IF(severity, condition) LOG_IF(severity, condition) |
| 730 | #define DLOG_ASSERT(condition) LOG_ASSERT(condition) |
[email protected] | d926c20 | 2010-10-01 02:58:24 | [diff] [blame] | 731 | #define DPLOG_IF(severity, condition) PLOG_IF(severity, condition) |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 732 | #define DVLOG_IF(verboselevel, condition) VLOG_IF(verboselevel, condition) |
[email protected] | fb879b1a | 2011-03-06 18:16:31 | [diff] [blame] | 733 | #define DVPLOG_IF(verboselevel, condition) VPLOG_IF(verboselevel, condition) |
[email protected] | d926c20 | 2010-10-01 02:58:24 | [diff] [blame] | 734 | |
gab | 190f754 | 2016-08-01 20:03:41 | [diff] [blame] | 735 | #else // DCHECK_IS_ON() |
[email protected] | d926c20 | 2010-10-01 02:58:24 | [diff] [blame] | 736 | |
gab | 190f754 | 2016-08-01 20:03:41 | [diff] [blame] | 737 | // If !DCHECK_IS_ON(), we want to avoid emitting any references to |condition| |
| 738 | // (which may reference a variable defined only if DCHECK_IS_ON()). |
| 739 | // Contrast this with DCHECK et al., which has different behavior. |
[email protected] | d926c20 | 2010-10-01 02:58:24 | [diff] [blame] | 740 | |
[email protected] | 5e98780 | 2010-11-01 19:49:22 | [diff] [blame] | 741 | #define DLOG_IS_ON(severity) false |
[email protected] | ddb9b33 | 2011-12-02 07:31:09 | [diff] [blame] | 742 | #define DLOG_IF(severity, condition) EAT_STREAM_PARAMETERS |
| 743 | #define DLOG_ASSERT(condition) EAT_STREAM_PARAMETERS |
| 744 | #define DPLOG_IF(severity, condition) EAT_STREAM_PARAMETERS |
| 745 | #define DVLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS |
| 746 | #define DVPLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS |
[email protected] | d926c20 | 2010-10-01 02:58:24 | [diff] [blame] | 747 | |
gab | 190f754 | 2016-08-01 20:03:41 | [diff] [blame] | 748 | #endif // DCHECK_IS_ON() |
[email protected] | d926c20 | 2010-10-01 02:58:24 | [diff] [blame] | 749 | |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 750 | #define DLOG(severity) \ |
| 751 | LAZY_STREAM(LOG_STREAM(severity), DLOG_IS_ON(severity)) |
| 752 | |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 753 | #define DPLOG(severity) \ |
| 754 | LAZY_STREAM(PLOG_STREAM(severity), DLOG_IS_ON(severity)) |
| 755 | |
Ken MacKay | 70e886700 | 2019-01-16 00:22:15 | [diff] [blame] | 756 | #define DVLOG(verboselevel) DVLOG_IF(verboselevel, true) |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 757 | |
Ken MacKay | 70e886700 | 2019-01-16 00:22:15 | [diff] [blame] | 758 | #define DVPLOG(verboselevel) DVPLOG_IF(verboselevel, true) |
[email protected] | fb879b1a | 2011-03-06 18:16:31 | [diff] [blame] | 759 | |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 760 | // Definitions for DCHECK et al. |
[email protected] | d926c20 | 2010-10-01 02:58:24 | [diff] [blame] | 761 | |
danakj | e649f57 | 2015-01-08 23:35:58 | [diff] [blame] | 762 | #if DCHECK_IS_ON() |
[email protected] | e3cca33 | 2009-08-20 01:20:29 | [diff] [blame] | 763 | |
Tomas Popela | afffa97 | 2018-11-13 20:42:05 | [diff] [blame] | 764 | #if defined(DCHECK_IS_CONFIGURABLE) |
Wez | 289477f | 2017-08-24 20:51:30 | [diff] [blame] | 765 | BASE_EXPORT extern LogSeverity LOG_DCHECK; |
| 766 | #else |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 767 | const LogSeverity LOG_DCHECK = LOG_FATAL; |
Tomas Popela | afffa97 | 2018-11-13 20:42:05 | [diff] [blame] | 768 | #endif // defined(DCHECK_IS_CONFIGURABLE) |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 769 | |
danakj | e649f57 | 2015-01-08 23:35:58 | [diff] [blame] | 770 | #else // DCHECK_IS_ON() |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 771 | |
Sigurdur Asgeirsson | 7013e5f | 2017-09-29 17:42:58 | [diff] [blame] | 772 | // There may be users of LOG_DCHECK that are enabled independently |
| 773 | // of DCHECK_IS_ON(), so default to FATAL logging for those. |
| 774 | const LogSeverity LOG_DCHECK = LOG_FATAL; |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 775 | |
danakj | e649f57 | 2015-01-08 23:35:58 | [diff] [blame] | 776 | #endif // DCHECK_IS_ON() |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 777 | |
[email protected] | deba0ff | 2010-11-03 05:30:14 | [diff] [blame] | 778 | // DCHECK et al. make sure to reference |condition| regardless of |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 779 | // whether DCHECKs are enabled; this is so that we don't get unused |
| 780 | // variable warnings if the only use of a variable is in a DCHECK. |
| 781 | // This behavior is different from DLOG_IF et al. |
dcheng | fc670f47 | 2017-01-25 17:48:43 | [diff] [blame] | 782 | // |
| 783 | // Note that the definition of the DCHECK macros depends on whether or not |
| 784 | // DCHECK_IS_ON() is true. When DCHECK_IS_ON() is false, the macros use |
| 785 | // EAT_STREAM_PARAMETERS to avoid expressions that would create temporaries. |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 786 | |
dcheng | fc670f47 | 2017-01-25 17:48:43 | [diff] [blame] | 787 | #if DCHECK_IS_ON() |
| 788 | |
kmarshall | fe2f09f8 | 2017-04-20 21:05:26 | [diff] [blame] | 789 | #define DCHECK(condition) \ |
| 790 | LAZY_STREAM(LOG_STREAM(DCHECK), !ANALYZER_ASSUME_TRUE(condition)) \ |
dcheng | fc670f47 | 2017-01-25 17:48:43 | [diff] [blame] | 791 | << "Check failed: " #condition ". " |
kmarshall | fe2f09f8 | 2017-04-20 21:05:26 | [diff] [blame] | 792 | #define DPCHECK(condition) \ |
| 793 | LAZY_STREAM(PLOG_STREAM(DCHECK), !ANALYZER_ASSUME_TRUE(condition)) \ |
danakj | e649f57 | 2015-01-08 23:35:58 | [diff] [blame] | 794 | << "Check failed: " #condition ". " |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 795 | |
dcheng | fc670f47 | 2017-01-25 17:48:43 | [diff] [blame] | 796 | #else // DCHECK_IS_ON() |
| 797 | |
kmarshall | 08c892f7 | 2017-02-28 03:46:18 | [diff] [blame] | 798 | #define DCHECK(condition) EAT_STREAM_PARAMETERS << !(condition) |
| 799 | #define DPCHECK(condition) EAT_STREAM_PARAMETERS << !(condition) |
dcheng | fc670f47 | 2017-01-25 17:48:43 | [diff] [blame] | 800 | |
| 801 | #endif // DCHECK_IS_ON() |
[email protected] | d926c20 | 2010-10-01 02:58:24 | [diff] [blame] | 802 | |
| 803 | // Helper macro for binary operators. |
| 804 | // Don't use this macro directly in your code, use DCHECK_EQ et al below. |
erikwright | 6ad937b | 2015-07-22 20:05:52 | [diff] [blame] | 805 | // The 'switch' is used to prevent the 'else' from being ambiguous when the |
| 806 | // macro is used in an 'if' clause such as: |
| 807 | // if (a == 1) |
| 808 | // DCHECK_EQ(2, a); |
dcheng | fc670f47 | 2017-01-25 17:48:43 | [diff] [blame] | 809 | #if DCHECK_IS_ON() |
| 810 | |
tsniatowski | 612550f | 2016-07-21 18:26:20 | [diff] [blame] | 811 | #define DCHECK_OP(name, op, val1, val2) \ |
| 812 | switch (0) case 0: default: \ |
| 813 | if (::logging::CheckOpResult true_if_passed = \ |
tsniatowski | 612550f | 2016-07-21 18:26:20 | [diff] [blame] | 814 | ::logging::Check##name##Impl((val1), (val2), \ |
Wez | 6a592ee | 2018-05-25 20:29:07 | [diff] [blame] | 815 | #val1 " " #op " " #val2)) \ |
tsniatowski | 612550f | 2016-07-21 18:26:20 | [diff] [blame] | 816 | ; \ |
| 817 | else \ |
| 818 | ::logging::LogMessage(__FILE__, __LINE__, ::logging::LOG_DCHECK, \ |
| 819 | true_if_passed.message()).stream() |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 820 | |
dcheng | fc670f47 | 2017-01-25 17:48:43 | [diff] [blame] | 821 | #else // DCHECK_IS_ON() |
| 822 | |
| 823 | // When DCHECKs aren't enabled, DCHECK_OP still needs to reference operator<< |
| 824 | // overloads for |val1| and |val2| to avoid potential compiler warnings about |
| 825 | // unused functions. For the same reason, it also compares |val1| and |val2| |
| 826 | // using |op|. |
| 827 | // |
| 828 | // Note that the contract of DCHECK_EQ, etc is that arguments are only evaluated |
| 829 | // once. Even though |val1| and |val2| appear twice in this version of the macro |
| 830 | // expansion, this is OK, since the expression is never actually evaluated. |
| 831 | #define DCHECK_OP(name, op, val1, val2) \ |
| 832 | EAT_STREAM_PARAMETERS << (::logging::MakeCheckOpValueString( \ |
| 833 | ::logging::g_swallow_stream, val1), \ |
| 834 | ::logging::MakeCheckOpValueString( \ |
| 835 | ::logging::g_swallow_stream, val2), \ |
kmarshall | 08c892f7 | 2017-02-28 03:46:18 | [diff] [blame] | 836 | (val1)op(val2)) |
dcheng | fc670f47 | 2017-01-25 17:48:43 | [diff] [blame] | 837 | |
| 838 | #endif // DCHECK_IS_ON() |
| 839 | |
[email protected] | deba0ff | 2010-11-03 05:30:14 | [diff] [blame] | 840 | // Equality/Inequality checks - compare two values, and log a |
| 841 | // LOG_DCHECK message including the two values when the result is not |
| 842 | // as expected. The values must have operator<<(ostream, ...) |
| 843 | // defined. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 844 | // |
| 845 | // You may append to the error message like so: |
pwnall | 7ae42b46 | 2016-09-22 02:26:12 | [diff] [blame] | 846 | // DCHECK_NE(1, 2) << "The world must be ending!"; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 847 | // |
| 848 | // We are very careful to ensure that each argument is evaluated exactly |
| 849 | // once, and that anything which is legal to pass as a function argument is |
| 850 | // legal here. In particular, the arguments may be temporary expressions |
| 851 | // which will end up being destroyed at the end of the apparent statement, |
| 852 | // for example: |
| 853 | // DCHECK_EQ(string("abc")[1], 'b'); |
| 854 | // |
brucedawson | 93a60b8c | 2016-04-28 20:46:16 | [diff] [blame] | 855 | // WARNING: These don't compile correctly if one of the arguments is a pointer |
| 856 | // and the other is NULL. In new code, prefer nullptr instead. To |
| 857 | // work around this for C++98, simply static_cast NULL to the type of the |
| 858 | // desired pointer. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 859 | |
| 860 | #define DCHECK_EQ(val1, val2) DCHECK_OP(EQ, ==, val1, val2) |
| 861 | #define DCHECK_NE(val1, val2) DCHECK_OP(NE, !=, val1, val2) |
| 862 | #define DCHECK_LE(val1, val2) DCHECK_OP(LE, <=, val1, val2) |
| 863 | #define DCHECK_LT(val1, val2) DCHECK_OP(LT, < , val1, val2) |
| 864 | #define DCHECK_GE(val1, val2) DCHECK_OP(GE, >=, val1, val2) |
| 865 | #define DCHECK_GT(val1, val2) DCHECK_OP(GT, > , val1, val2) |
| 866 | |
Xiaohan Wang | ee536b21 | 2019-05-07 16:16:07 | [diff] [blame] | 867 | #if BUILDFLAG(ENABLE_LOG_ERROR_NOT_REACHED) |
tnagel | ff3f34a | 2015-05-24 12:59:14 | [diff] [blame] | 868 | // Implement logging of NOTREACHED() as a dedicated function to get function |
| 869 | // call overhead down to a minimum. |
| 870 | void LogErrorNotReached(const char* file, int line); |
| 871 | #define NOTREACHED() \ |
| 872 | true ? ::logging::LogErrorNotReached(__FILE__, __LINE__) \ |
| 873 | : EAT_STREAM_PARAMETERS |
[email protected] | 7c67fbe | 2013-09-26 07:55:21 | [diff] [blame] | 874 | #else |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 875 | #define NOTREACHED() DCHECK(false) |
[email protected] | 7c67fbe | 2013-09-26 07:55:21 | [diff] [blame] | 876 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 877 | |
| 878 | // Redefine the standard assert to use our nice log files |
| 879 | #undef assert |
| 880 | #define assert(x) DLOG_ASSERT(x) |
| 881 | |
| 882 | // This class more or less represents a particular log message. You |
| 883 | // create an instance of LogMessage and then stream stuff to it. |
| 884 | // When you finish streaming to it, ~LogMessage is called and the |
| 885 | // full message gets streamed to the appropriate destination. |
| 886 | // |
| 887 | // You shouldn't actually use LogMessage's constructor to log things, |
| 888 | // though. You should use the LOG() macro (and variants thereof) |
| 889 | // above. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 890 | class BASE_EXPORT LogMessage { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 891 | public: |
[email protected] | bf8ddf13a | 2014-06-18 15:02:22 | [diff] [blame] | 892 | // Used for LOG(severity). |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 893 | LogMessage(const char* file, int line, LogSeverity severity); |
| 894 | |
tnagel | 4a045d3f | 2015-07-12 14:19:28 | [diff] [blame] | 895 | // Used for CHECK(). Implied severity = LOG_FATAL. |
| 896 | LogMessage(const char* file, int line, const char* condition); |
| 897 | |
[email protected] | bf8ddf13a | 2014-06-18 15:02:22 | [diff] [blame] | 898 | // Used for CHECK_EQ(), etc. Takes ownership of the given string. |
| 899 | // Implied severity = LOG_FATAL. |
[email protected] | 9c7132e | 2011-02-08 07:39:08 | [diff] [blame] | 900 | LogMessage(const char* file, int line, std::string* result); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 901 | |
[email protected] | bf8ddf13a | 2014-06-18 15:02:22 | [diff] [blame] | 902 | // Used for DCHECK_EQ(), etc. Takes ownership of the given string. |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 903 | LogMessage(const char* file, int line, LogSeverity severity, |
[email protected] | 9c7132e | 2011-02-08 07:39:08 | [diff] [blame] | 904 | std::string* result); |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 905 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 906 | ~LogMessage(); |
| 907 | |
| 908 | std::ostream& stream() { return stream_; } |
| 909 | |
pastarmovj | 89f7ee1 | 2016-09-20 14:58:13 | [diff] [blame] | 910 | LogSeverity severity() { return severity_; } |
| 911 | std::string str() { return stream_.str(); } |
| 912 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 913 | private: |
| 914 | void Init(const char* file, int line); |
| 915 | |
| 916 | LogSeverity severity_; |
| 917 | std::ostringstream stream_; |
[email protected] | c8887392 | 2008-07-30 13:02:03 | [diff] [blame] | 918 | size_t message_start_; // Offset of the start of the message (past prefix |
| 919 | // info). |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 920 | // The file and line information passed in to the constructor. |
| 921 | const char* file_; |
| 922 | const int line_; |
| 923 | |
[email protected] | 3f85caa | 2009-04-14 16:52:11 | [diff] [blame] | 924 | // This is useful since the LogMessage class uses a lot of Win32 calls |
| 925 | // that will lose the value of GLE and the code that called the log function |
| 926 | // will have lost the thread error value when the log call returns. |
Etienne Pierre-Doray | d120ebf | 2018-09-14 23:38:21 | [diff] [blame] | 927 | base::internal::ScopedClearLastError last_error_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 928 | |
[email protected] | 39be424 | 2008-08-07 18:31:40 | [diff] [blame] | 929 | DISALLOW_COPY_AND_ASSIGN(LogMessage); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 930 | }; |
| 931 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 932 | // This class is used to explicitly ignore values in the conditional |
| 933 | // logging macros. This avoids compiler warnings like "value computed |
| 934 | // is not used" and "statement has no effect". |
[email protected] | 23bb71f | 2011-04-21 22:22:10 | [diff] [blame] | 935 | class LogMessageVoidify { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 936 | public: |
Chris Watkins | 091d629 | 2017-12-13 04:25:58 | [diff] [blame] | 937 | LogMessageVoidify() = default; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 938 | // This has to be an operator with a precedence lower than << but |
| 939 | // higher than ?: |
| 940 | void operator&(std::ostream&) { } |
| 941 | }; |
| 942 | |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 943 | #if defined(OS_WIN) |
| 944 | typedef unsigned long SystemErrorCode; |
Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 945 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 946 | typedef int SystemErrorCode; |
| 947 | #endif |
| 948 | |
| 949 | // Alias for ::GetLastError() on Windows and errno on POSIX. Avoids having to |
| 950 | // pull in windows.h just for GetLastError() and DWORD. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 951 | BASE_EXPORT SystemErrorCode GetLastSystemErrorCode(); |
[email protected] | c914d8a | 2014-04-23 01:11:01 | [diff] [blame] | 952 | BASE_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code); |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 953 | |
| 954 | #if defined(OS_WIN) |
| 955 | // Appends a formatted system message of the GetLastError() type. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 956 | class BASE_EXPORT Win32ErrorLogMessage { |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 957 | public: |
| 958 | Win32ErrorLogMessage(const char* file, |
| 959 | int line, |
| 960 | LogSeverity severity, |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 961 | SystemErrorCode err); |
| 962 | |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 963 | // Appends the error message before destructing the encapsulated class. |
| 964 | ~Win32ErrorLogMessage(); |
| 965 | |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 966 | std::ostream& stream() { return log_message_.stream(); } |
| 967 | |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 968 | private: |
| 969 | SystemErrorCode err_; |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 970 | LogMessage log_message_; |
| 971 | |
| 972 | DISALLOW_COPY_AND_ASSIGN(Win32ErrorLogMessage); |
| 973 | }; |
Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 974 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 975 | // Appends a formatted system message of the errno type |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 976 | class BASE_EXPORT ErrnoLogMessage { |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 977 | public: |
| 978 | ErrnoLogMessage(const char* file, |
| 979 | int line, |
| 980 | LogSeverity severity, |
| 981 | SystemErrorCode err); |
| 982 | |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 983 | // Appends the error message before destructing the encapsulated class. |
| 984 | ~ErrnoLogMessage(); |
| 985 | |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 986 | std::ostream& stream() { return log_message_.stream(); } |
| 987 | |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 988 | private: |
| 989 | SystemErrorCode err_; |
| 990 | LogMessage log_message_; |
| 991 | |
| 992 | DISALLOW_COPY_AND_ASSIGN(ErrnoLogMessage); |
| 993 | }; |
| 994 | #endif // OS_WIN |
| 995 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 996 | // Closes the log file explicitly if open. |
| 997 | // NOTE: Since the log file is opened as necessary by the action of logging |
| 998 | // statements, there's no guarantee that it will stay closed |
| 999 | // after this call. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 1000 | BASE_EXPORT void CloseLogFile(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1001 | |
Robbie McElrath | 8bf4984 | 2019-08-20 22:22:53 | [diff] [blame] | 1002 | #if defined(OS_CHROMEOS) |
| 1003 | // Returns a new file handle that will write to the same destination as the |
| 1004 | // currently open log file. Returns nullptr if logging to a file is disabled, |
| 1005 | // or if opening the file failed. This is intended to be used to initialize |
| 1006 | // logging in child processes that are unable to open files. |
| 1007 | BASE_EXPORT FILE* DuplicateLogFILE(); |
| 1008 | #endif |
| 1009 | |
[email protected] | e36ddc8 | 2009-12-08 04:22:50 | [diff] [blame] | 1010 | // Async signal safe logging mechanism. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 1011 | BASE_EXPORT void RawLog(int level, const char* message); |
[email protected] | e36ddc8 | 2009-12-08 04:22:50 | [diff] [blame] | 1012 | |
tsniatowski | 612550f | 2016-07-21 18:26:20 | [diff] [blame] | 1013 | #define RAW_LOG(level, message) \ |
| 1014 | ::logging::RawLog(::logging::LOG_##level, message) |
[email protected] | e36ddc8 | 2009-12-08 04:22:50 | [diff] [blame] | 1015 | |
tsniatowski | 612550f | 2016-07-21 18:26:20 | [diff] [blame] | 1016 | #define RAW_CHECK(condition) \ |
| 1017 | do { \ |
kmarshall | 08c892f7 | 2017-02-28 03:46:18 | [diff] [blame] | 1018 | if (!(condition)) \ |
tsniatowski | 612550f | 2016-07-21 18:26:20 | [diff] [blame] | 1019 | ::logging::RawLog(::logging::LOG_FATAL, \ |
| 1020 | "Check failed: " #condition "\n"); \ |
[email protected] | e36ddc8 | 2009-12-08 04:22:50 | [diff] [blame] | 1021 | } while (0) |
| 1022 | |
[email protected] | f01b88a | 2013-02-27 22:04:00 | [diff] [blame] | 1023 | #if defined(OS_WIN) |
ananta | 61762fb | 2015-09-18 01:00:09 | [diff] [blame] | 1024 | // Returns true if logging to file is enabled. |
| 1025 | BASE_EXPORT bool IsLoggingToFileEnabled(); |
| 1026 | |
[email protected] | f01b88a | 2013-02-27 22:04:00 | [diff] [blame] | 1027 | // Returns the default log file path. |
jdoerrie | 5c4dc4e | 2019-02-01 18:02:33 | [diff] [blame] | 1028 | BASE_EXPORT base::string16 GetLogFileFullPath(); |
[email protected] | f01b88a | 2013-02-27 22:04:00 | [diff] [blame] | 1029 | #endif |
| 1030 | |
[email protected] | 39be424 | 2008-08-07 18:31:40 | [diff] [blame] | 1031 | } // namespace logging |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1032 | |
[email protected] | 81411c6 | 2014-07-08 23:03:06 | [diff] [blame] | 1033 | // Note that "The behavior of a C++ program is undefined if it adds declarations |
| 1034 | // or definitions to namespace std or to a namespace within namespace std unless |
| 1035 | // otherwise specified." --C++11[namespace.std] |
| 1036 | // |
| 1037 | // We've checked that this particular definition has the intended behavior on |
| 1038 | // our implementations, but it's prone to breaking in the future, and please |
| 1039 | // don't imitate this in your own definitions without checking with some |
| 1040 | // standard library experts. |
| 1041 | namespace std { |
[email protected] | 46ce5b56 | 2010-06-16 18:39:53 | [diff] [blame] | 1042 | // These functions are provided as a convenience for logging, which is where we |
| 1043 | // use streams (it is against Google style to use streams in other places). It |
| 1044 | // is designed to allow you to emit non-ASCII Unicode strings to the log file, |
| 1045 | // which is normally ASCII. It is relatively slow, so try not to use it for |
| 1046 | // common cases. Non-ASCII characters will be converted to UTF-8 by these |
| 1047 | // operators. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 1048 | BASE_EXPORT std::ostream& operator<<(std::ostream& out, const wchar_t* wstr); |
[email protected] | 46ce5b56 | 2010-06-16 18:39:53 | [diff] [blame] | 1049 | inline std::ostream& operator<<(std::ostream& out, const std::wstring& wstr) { |
| 1050 | return out << wstr.c_str(); |
| 1051 | } |
[email protected] | 81411c6 | 2014-07-08 23:03:06 | [diff] [blame] | 1052 | } // namespace std |
[email protected] | 46ce5b56 | 2010-06-16 18:39:53 | [diff] [blame] | 1053 | |
Daniel Bratell | ff54119 | 2017-11-02 14:22:28 | [diff] [blame] | 1054 | // The NOTIMPLEMENTED() macro annotates codepaths which have not been |
| 1055 | // implemented yet. If output spam is a serious concern, |
| 1056 | // NOTIMPLEMENTED_LOG_ONCE can be used. |
[email protected] | 0dfc81b | 2008-08-25 03:44:40 | [diff] [blame] | 1057 | |
[email protected] | f6cda75 | 2008-10-30 23:54:26 | [diff] [blame] | 1058 | #if defined(COMPILER_GCC) |
| 1059 | // On Linux, with GCC, we can use __PRETTY_FUNCTION__ to get the demangled name |
| 1060 | // of the current function in the NOTIMPLEMENTED message. |
| 1061 | #define NOTIMPLEMENTED_MSG "Not implemented reached in " << __PRETTY_FUNCTION__ |
| 1062 | #else |
| 1063 | #define NOTIMPLEMENTED_MSG "NOT IMPLEMENTED" |
| 1064 | #endif |
| 1065 | |
Daniel Cheng | 5b0b301 | 2019-04-26 00:58:04 | [diff] [blame] | 1066 | #define NOTIMPLEMENTED() DLOG(ERROR) << NOTIMPLEMENTED_MSG |
| 1067 | #define NOTIMPLEMENTED_LOG_ONCE() \ |
| 1068 | do { \ |
| 1069 | static bool logged_once = false; \ |
| 1070 | DLOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG; \ |
| 1071 | logged_once = true; \ |
| 1072 | } while (0); \ |
Daniel Bratell | ff54119 | 2017-11-02 14:22:28 | [diff] [blame] | 1073 | EAT_STREAM_PARAMETERS |
[email protected] | 0dfc81b | 2008-08-25 03:44:40 | [diff] [blame] | 1074 | |
[email protected] | 39be424 | 2008-08-07 18:31:40 | [diff] [blame] | 1075 | #endif // BASE_LOGGING_H_ |