[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 <sstream> |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 13 | #include <string> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 14 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 15 | #include "base/base_export.h" |
alex-ac | cc1bde6 | 2017-04-19 08:33:55 | [diff] [blame] | 16 | #include "base/callback_forward.h" |
danakj | cb7c529 | 2016-12-20 19:05:35 | [diff] [blame] | 17 | #include "base/compiler_specific.h" |
Hans Wennborg | 944479f | 2020-06-25 21:39:25 | [diff] [blame] | 18 | #include "base/dcheck_is_on.h" |
Etienne Pierre-Doray | d120ebf | 2018-09-14 23:38:21 | [diff] [blame] | 19 | #include "base/scoped_clear_last_error.h" |
alex-ac | cc1bde6 | 2017-04-19 08:33:55 | [diff] [blame] | 20 | #include "base/strings/string_piece_forward.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 21 | |
Robbie McElrath | 8bf4984 | 2019-08-20 22:22:53 | [diff] [blame] | 22 | #if defined(OS_CHROMEOS) |
| 23 | #include <cstdio> |
| 24 | #endif |
| 25 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 26 | // |
| 27 | // Optional message capabilities |
| 28 | // ----------------------------- |
| 29 | // Assertion failed messages and fatal errors are displayed in a dialog box |
| 30 | // before the application exits. However, running this UI creates a message |
| 31 | // loop, which causes application messages to be processed and potentially |
| 32 | // dispatched to existing application windows. Since the application is in a |
| 33 | // bad state when this assertion dialog is displayed, these messages may not |
| 34 | // get processed and hang the dialog, or the application might go crazy. |
| 35 | // |
| 36 | // Therefore, it can be beneficial to display the error dialog in a separate |
| 37 | // process from the main application. When the logging system needs to display |
| 38 | // a fatal error dialog box, it will look for a program called |
| 39 | // "DebugMessage.exe" in the same directory as the application executable. It |
| 40 | // will run this application with the message as the command line, and will |
| 41 | // not include the name of the application as is traditional for easier |
| 42 | // parsing. |
| 43 | // |
| 44 | // The code for DebugMessage.exe is only one line. In WinMain, do: |
| 45 | // MessageBox(NULL, GetCommandLineW(), L"Fatal Error", 0); |
| 46 | // |
| 47 | // If DebugMessage.exe is not found, the logging code will use a normal |
| 48 | // MessageBox, potentially causing the problems discussed above. |
| 49 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 50 | // Instructions |
| 51 | // ------------ |
| 52 | // |
| 53 | // Make a bunch of macros for logging. The way to log things is to stream |
| 54 | // things to LOG(<a particular severity level>). E.g., |
| 55 | // |
| 56 | // LOG(INFO) << "Found " << num_cookies << " cookies"; |
| 57 | // |
| 58 | // You can also do conditional logging: |
| 59 | // |
| 60 | // LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies"; |
| 61 | // |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 62 | // The CHECK(condition) macro is active in both debug and release builds and |
| 63 | // effectively performs a LOG(FATAL) which terminates the process and |
| 64 | // generates a crashdump unless a debugger is attached. |
| 65 | // |
| 66 | // There are also "debug mode" logging macros like the ones above: |
| 67 | // |
| 68 | // DLOG(INFO) << "Found cookies"; |
| 69 | // |
| 70 | // DLOG_IF(INFO, num_cookies > 10) << "Got lots of cookies"; |
| 71 | // |
| 72 | // All "debug mode" logging is compiled away to nothing for non-debug mode |
| 73 | // compiles. LOG_IF and development flags also work well together |
| 74 | // because the code can be compiled away sometimes. |
| 75 | // |
| 76 | // We also have |
| 77 | // |
| 78 | // LOG_ASSERT(assertion); |
| 79 | // DLOG_ASSERT(assertion); |
| 80 | // |
| 81 | // which is syntactic sugar for {,D}LOG_IF(FATAL, assert fails) << assertion; |
| 82 | // |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 83 | // There are "verbose level" logging macros. They look like |
| 84 | // |
| 85 | // VLOG(1) << "I'm printed when you run the program with --v=1 or more"; |
| 86 | // VLOG(2) << "I'm printed when you run the program with --v=2 or more"; |
| 87 | // |
| 88 | // These always log at the INFO log level (when they log at all). |
| 89 | // The verbose logging can also be turned on module-by-module. For instance, |
[email protected] | b0d38d4c | 2010-10-29 00:39:48 | [diff] [blame] | 90 | // --vmodule=profile=2,icon_loader=1,browser_*=3,*/chromeos/*=4 --v=0 |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 91 | // will cause: |
| 92 | // a. VLOG(2) and lower messages to be printed from profile.{h,cc} |
| 93 | // b. VLOG(1) and lower messages to be printed from icon_loader.{h,cc} |
| 94 | // c. VLOG(3) and lower messages to be printed from files prefixed with |
| 95 | // "browser" |
[email protected] | e11de72 | 2010-11-01 20:50:55 | [diff] [blame] | 96 | // d. VLOG(4) and lower messages to be printed from files under a |
[email protected] | b0d38d4c | 2010-10-29 00:39:48 | [diff] [blame] | 97 | // "chromeos" directory. |
[email protected] | e11de72 | 2010-11-01 20:50:55 | [diff] [blame] | 98 | // e. VLOG(0) and lower messages to be printed from elsewhere |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 99 | // |
| 100 | // The wildcarding functionality shown by (c) supports both '*' (match |
[email protected] | b0d38d4c | 2010-10-29 00:39:48 | [diff] [blame] | 101 | // 0 or more characters) and '?' (match any single character) |
| 102 | // wildcards. Any pattern containing a forward or backward slash will |
| 103 | // be tested against the whole pathname and not just the module. |
| 104 | // E.g., "*/foo/bar/*=2" would change the logging level for all code |
| 105 | // in source files under a "foo/bar" directory. |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 106 | // |
Mason Freed | 14240d16 | 2020-08-12 13:06:34 | [diff] [blame] | 107 | // Note that for a Chromium binary built in release mode (is_debug = false) you |
| 108 | // must pass "--enable-logging=stderr" in order to see the output of VLOG |
| 109 | // statements. |
| 110 | // |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 111 | // There's also VLOG_IS_ON(n) "verbose level" condition macro. To be used as |
| 112 | // |
| 113 | // if (VLOG_IS_ON(2)) { |
| 114 | // // do some logging preparation and logging |
| 115 | // // that can't be accomplished with just VLOG(2) << ...; |
| 116 | // } |
| 117 | // |
| 118 | // There is also a VLOG_IF "verbose level" condition macro for sample |
| 119 | // cases, when some extra computation and preparation for logs is not |
| 120 | // needed. |
| 121 | // |
| 122 | // VLOG_IF(1, (size > 1024)) |
| 123 | // << "I'm printed when size is more than 1024 and when you run the " |
| 124 | // "program with --v=1 or more"; |
| 125 | // |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 126 | // We also override the standard 'assert' to use 'DLOG_ASSERT'. |
| 127 | // |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 128 | // Lastly, there is: |
| 129 | // |
| 130 | // PLOG(ERROR) << "Couldn't do foo"; |
| 131 | // DPLOG(ERROR) << "Couldn't do foo"; |
| 132 | // PLOG_IF(ERROR, cond) << "Couldn't do foo"; |
| 133 | // DPLOG_IF(ERROR, cond) << "Couldn't do foo"; |
| 134 | // PCHECK(condition) << "Couldn't do foo"; |
| 135 | // DPCHECK(condition) << "Couldn't do foo"; |
| 136 | // |
| 137 | // which append the last system error to the message in string form (taken from |
| 138 | // GetLastError() on Windows and errno on POSIX). |
| 139 | // |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 140 | // The supported severity levels for macros that allow you to specify one |
[email protected] | f2c0549 | 2014-06-17 12:04:23 | [diff] [blame] | 141 | // are (in increasing order of severity) INFO, WARNING, ERROR, and FATAL. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 142 | // |
| 143 | // Very important: logging a message at the FATAL severity level causes |
| 144 | // the program to terminate (after the message is logged). |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 145 | // |
[email protected] | f2c0549 | 2014-06-17 12:04:23 | [diff] [blame] | 146 | // There is the special severity of DFATAL, which logs FATAL in debug mode, |
| 147 | // ERROR in normal mode. |
Rob Schonberger | 4563721 | 2018-12-03 04:46:25 | [diff] [blame] | 148 | // |
Yuta Hijikata | 9b7279a | 2020-08-26 16:10:54 | [diff] [blame] | 149 | // Output is formatted as per the following example, except on Chrome OS. |
Rob Schonberger | 4563721 | 2018-12-03 04:46:25 | [diff] [blame] | 150 | // [3816:3877:0812/234555.406952:VERBOSE1:drm_device_handle.cc(90)] Succeeded |
| 151 | // authenticating /dev/dri/card0 in 0 ms with 1 attempt(s) |
| 152 | // |
| 153 | // The colon separated fields inside the brackets are as follows: |
| 154 | // 0. An optional Logfile prefix (not included in this example) |
| 155 | // 1. Process ID |
| 156 | // 2. Thread ID |
| 157 | // 3. The date/time of the log message, in MMDD/HHMMSS.Milliseconds format |
| 158 | // 4. The log level |
| 159 | // 5. The filename and line number where the log was instantiated |
| 160 | // |
Yuta Hijikata | 9b7279a | 2020-08-26 16:10:54 | [diff] [blame] | 161 | // Output for Chrome OS can be switched to syslog-like format. See |
| 162 | // InitWithSyslogPrefix() in logging_chromeos.h for details. |
| 163 | // |
Rob Schonberger | 4563721 | 2018-12-03 04:46:25 | [diff] [blame] | 164 | // Note that the visibility can be changed by setting preferences in |
| 165 | // SetLogItems() |
Mason Freed | 14240d16 | 2020-08-12 13:06:34 | [diff] [blame] | 166 | // |
| 167 | // Additional logging-related information can be found here: |
| 168 | // https://chromium.googlesource.com/chromium/src/+/master/docs/linux/debugging.md#Logging |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 169 | |
| 170 | namespace logging { |
| 171 | |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 172 | // TODO(avi): do we want to do a unification of character types here? |
| 173 | #if defined(OS_WIN) |
Jan Wilken Dörrie | b630aca | 2019-12-04 10:59:11 | [diff] [blame] | 174 | typedef wchar_t PathChar; |
Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 175 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 176 | typedef char PathChar; |
| 177 | #endif |
| 178 | |
Sharon Yang | 7cb919a | 2019-05-20 20:27:15 | [diff] [blame] | 179 | // A bitmask of potential logging destinations. |
| 180 | using LoggingDestination = uint32_t; |
| 181 | // Specifies where logs will be written. Multiple destinations can be specified |
| 182 | // with bitwise OR. |
| 183 | // Unless destination is LOG_NONE, all logs with severity ERROR and above will |
| 184 | // be written to stderr in addition to the specified destination. |
| 185 | enum : uint32_t { |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 186 | LOG_NONE = 0, |
| 187 | LOG_TO_FILE = 1 << 0, |
| 188 | LOG_TO_SYSTEM_DEBUG_LOG = 1 << 1, |
Sharon Yang | 7cb919a | 2019-05-20 20:27:15 | [diff] [blame] | 189 | LOG_TO_STDERR = 1 << 2, |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 190 | |
Sharon Yang | 7cb919a | 2019-05-20 20:27:15 | [diff] [blame] | 191 | 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] | 192 | |
Sharon Yang | 7cb919a | 2019-05-20 20:27:15 | [diff] [blame] | 193 | // On Windows, use a file next to the exe. |
| 194 | // On POSIX platforms, where it may not even be possible to locate the |
| 195 | // executable on disk, use stderr. |
| 196 | // On Fuchsia, use the Fuchsia logging service. |
| 197 | #if defined(OS_FUCHSIA) || defined(OS_NACL) |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 198 | LOG_DEFAULT = LOG_TO_SYSTEM_DEBUG_LOG, |
Sharon Yang | 7cb919a | 2019-05-20 20:27:15 | [diff] [blame] | 199 | #elif defined(OS_WIN) |
| 200 | LOG_DEFAULT = LOG_TO_FILE, |
| 201 | #elif defined(OS_POSIX) |
| 202 | LOG_DEFAULT = LOG_TO_SYSTEM_DEBUG_LOG | LOG_TO_STDERR, |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 203 | #endif |
| 204 | }; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 205 | |
| 206 | // Indicates that the log file should be locked when being written to. |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 207 | // Unless there is only one single-threaded process that is logging to |
| 208 | // the log file, the file should be locked during writes to make each |
[email protected] | 3ee50d1 | 2014-03-05 01:43:27 | [diff] [blame] | 209 | // log output atomic. Other writers will block. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 210 | // |
| 211 | // 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] | 212 | // work properly. Defaults to LOCK_LOG_FILE. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 213 | enum LogLockingState { LOCK_LOG_FILE, DONT_LOCK_LOG_FILE }; |
| 214 | |
| 215 | // On startup, should we delete or append to an existing log file (if any)? |
| 216 | // Defaults to APPEND_TO_OLD_LOG_FILE. |
| 217 | enum OldFileDeletionState { DELETE_OLD_LOG_FILE, APPEND_TO_OLD_LOG_FILE }; |
| 218 | |
Yuta Hijikata | 9b7279a | 2020-08-26 16:10:54 | [diff] [blame] | 219 | #if defined(OS_CHROMEOS) |
Yuta Hijikata | 1fc8f634 | 2020-09-01 03:25:56 | [diff] [blame] | 220 | // Defines the log message prefix format to use. |
| 221 | // LOG_FORMAT_SYSLOG indicates syslog-like message prefixes. |
| 222 | // LOG_FORMAT_CHROME indicates the normal Chrome format. |
Yuta Hijikata | 9b7279a | 2020-08-26 16:10:54 | [diff] [blame] | 223 | enum class BASE_EXPORT LogFormat { LOG_FORMAT_CHROME, LOG_FORMAT_SYSLOG }; |
| 224 | #endif |
| 225 | |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 226 | struct BASE_EXPORT LoggingSettings { |
Sharon Yang | 7cb919a | 2019-05-20 20:27:15 | [diff] [blame] | 227 | // Equivalent to logging destination enum, but allows for multiple |
| 228 | // destinations. |
Wez | 7e12562 | 2019-05-29 22:11:28 | [diff] [blame] | 229 | uint32_t logging_dest = LOG_DEFAULT; |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 230 | |
Robbie McElrath | 8bf4984 | 2019-08-20 22:22:53 | [diff] [blame] | 231 | // The four settings below have an effect only when LOG_TO_FILE is |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 232 | // set in |logging_dest|. |
Robbie McElrath | 8bf4984 | 2019-08-20 22:22:53 | [diff] [blame] | 233 | const PathChar* log_file_path = nullptr; |
Wez | 7e12562 | 2019-05-29 22:11:28 | [diff] [blame] | 234 | LogLockingState lock_log = LOCK_LOG_FILE; |
| 235 | OldFileDeletionState delete_old = APPEND_TO_OLD_LOG_FILE; |
Robbie McElrath | 8bf4984 | 2019-08-20 22:22:53 | [diff] [blame] | 236 | #if defined(OS_CHROMEOS) |
| 237 | // Contains an optional file that logs should be written to. If present, |
| 238 | // |log_file_path| will be ignored, and the logging system will take ownership |
| 239 | // of the FILE. If there's an error writing to this file, no fallback paths |
| 240 | // will be opened. |
| 241 | FILE* log_file = nullptr; |
Yuta Hijikata | 1fc8f634 | 2020-09-01 03:25:56 | [diff] [blame] | 242 | // ChromeOS uses the syslog log format by default. |
| 243 | LogFormat log_format = LogFormat::LOG_FORMAT_SYSLOG; |
Robbie McElrath | 8bf4984 | 2019-08-20 22:22:53 | [diff] [blame] | 244 | #endif |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 245 | }; |
[email protected] | ff3d0c3 | 2010-08-23 19:57:46 | [diff] [blame] | 246 | |
| 247 | // Define different names for the BaseInitLoggingImpl() function depending on |
| 248 | // whether NDEBUG is defined or not so that we'll fail to link if someone tries |
| 249 | // to compile logging.cc with NDEBUG but includes logging.h without defining it, |
| 250 | // or vice versa. |
wez | a245bd07 | 2017-06-18 23:26:34 | [diff] [blame] | 251 | #if defined(NDEBUG) |
[email protected] | ff3d0c3 | 2010-08-23 19:57:46 | [diff] [blame] | 252 | #define BaseInitLoggingImpl BaseInitLoggingImpl_built_with_NDEBUG |
| 253 | #else |
| 254 | #define BaseInitLoggingImpl BaseInitLoggingImpl_built_without_NDEBUG |
| 255 | #endif |
| 256 | |
| 257 | // Implementation of the InitLogging() method declared below. We use a |
| 258 | // more-specific name so we can #define it above without affecting other code |
| 259 | // that has named stuff "InitLogging". |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 260 | BASE_EXPORT bool BaseInitLoggingImpl(const LoggingSettings& settings); |
[email protected] | ff3d0c3 | 2010-08-23 19:57:46 | [diff] [blame] | 261 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 262 | // Sets the log file name and other global logging state. Calling this function |
| 263 | // is recommended, and is normally done at the beginning of application init. |
| 264 | // If you don't call it, all the flags will be initialized to their default |
| 265 | // values, and there is a race condition that may leak a critical section |
| 266 | // object if two threads try to do the first log at the same time. |
| 267 | // See the definition of the enums above for descriptions and default values. |
| 268 | // |
| 269 | // The default log file is initialized to "debug.log" in the application |
| 270 | // directory. You probably don't want this, especially since the program |
| 271 | // directory may not be writable on an enduser's system. |
[email protected] | 064aa16 | 2011-12-03 00:30:08 | [diff] [blame] | 272 | // |
| 273 | // This function may be called a second time to re-direct logging (e.g after |
| 274 | // loging in to a user partition), however it should never be called more than |
| 275 | // twice. |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 276 | inline bool InitLogging(const LoggingSettings& settings) { |
| 277 | return BaseInitLoggingImpl(settings); |
[email protected] | ff3d0c3 | 2010-08-23 19:57:46 | [diff] [blame] | 278 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 279 | |
| 280 | // Sets the log level. Anything at or above this level will be written to the |
| 281 | // log file/displayed to the user (if applicable). Anything below this level |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 282 | // will be silently ignored. The log level defaults to 0 (everything is logged |
| 283 | // up to level INFO) if this function is not called. |
| 284 | // Note that log messages for VLOG(x) are logged at level -x, so setting |
| 285 | // the min log level to negative values enables verbose logging. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 286 | BASE_EXPORT void SetMinLogLevel(int level); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 287 | |
[email protected] | 8a2986ca | 2009-04-10 19:13:42 | [diff] [blame] | 288 | // Gets the current log level. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 289 | BASE_EXPORT int GetMinLogLevel(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 290 | |
skobes | c78c0ad7 | 2015-12-07 20:21:23 | [diff] [blame] | 291 | // Used by LOG_IS_ON to lazy-evaluate stream arguments. |
| 292 | BASE_EXPORT bool ShouldCreateLogMessage(int severity); |
| 293 | |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 294 | // Gets the VLOG default verbosity level. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 295 | BASE_EXPORT int GetVlogVerbosity(); |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 296 | |
[email protected] | 2f4e9a6 | 2010-09-29 21:25:14 | [diff] [blame] | 297 | // Note that |N| is the size *with* the null terminator. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 298 | BASE_EXPORT int GetVlogLevelHelper(const char* file_start, size_t N); |
[email protected] | 2f4e9a6 | 2010-09-29 21:25:14 | [diff] [blame] | 299 | |
tnagel | 270da92 | 2017-05-24 12:10:44 | [diff] [blame] | 300 | // Gets the current vlog level for the given file (usually taken from __FILE__). |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 301 | template <size_t N> |
| 302 | int GetVlogLevel(const char (&file)[N]) { |
| 303 | return GetVlogLevelHelper(file, N); |
| 304 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 305 | |
| 306 | // Sets the common items you want to be prepended to each log message. |
| 307 | // process and thread IDs default to off, the timestamp defaults to on. |
| 308 | // If this function is not called, logging defaults to writing the timestamp |
| 309 | // only. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 310 | BASE_EXPORT void SetLogItems(bool enable_process_id, bool enable_thread_id, |
| 311 | bool enable_timestamp, bool enable_tickcount); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 312 | |
James Cook | a0536c3 | 2018-08-01 20:13:31 | [diff] [blame] | 313 | // Sets an optional prefix to add to each log message. |prefix| is not copied |
| 314 | // and should be a raw string constant. |prefix| must only contain ASCII letters |
| 315 | // to avoid confusion with PIDs and timestamps. Pass null to remove the prefix. |
| 316 | // Logging defaults to no prefix. |
| 317 | BASE_EXPORT void SetLogPrefix(const char* prefix); |
| 318 | |
[email protected] | 81e0a85 | 2010-08-17 00:38:12 | [diff] [blame] | 319 | // Sets whether or not you'd like to see fatal debug messages popped up in |
| 320 | // a dialog box or not. |
| 321 | // Dialogs are not shown by default. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 322 | BASE_EXPORT void SetShowErrorDialogs(bool enable_dialogs); |
[email protected] | 81e0a85 | 2010-08-17 00:38:12 | [diff] [blame] | 323 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 324 | // 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] | 325 | // Resets Log Assert Handler on object destruction. |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 326 | // The default handler shows a dialog box and then terminate the process, |
| 327 | // however clients can use this function to override with their own handling |
| 328 | // (e.g. a silent one for Unit Tests) |
alex-ac | cc1bde6 | 2017-04-19 08:33:55 | [diff] [blame] | 329 | using LogAssertHandlerFunction = |
kylechar | 83fb51e5 | 2019-03-14 15:30:43 | [diff] [blame] | 330 | base::RepeatingCallback<void(const char* file, |
| 331 | int line, |
| 332 | const base::StringPiece message, |
| 333 | const base::StringPiece stack_trace)>; |
alex-ac | cc1bde6 | 2017-04-19 08:33:55 | [diff] [blame] | 334 | |
| 335 | class BASE_EXPORT ScopedLogAssertHandler { |
| 336 | public: |
| 337 | explicit ScopedLogAssertHandler(LogAssertHandlerFunction handler); |
David Bienvenu | b4b441e | 2020-09-23 05:49:57 | [diff] [blame] | 338 | ScopedLogAssertHandler(const ScopedLogAssertHandler&) = delete; |
| 339 | ScopedLogAssertHandler& operator=(const ScopedLogAssertHandler&) = delete; |
alex-ac | cc1bde6 | 2017-04-19 08:33:55 | [diff] [blame] | 340 | ~ScopedLogAssertHandler(); |
alex-ac | cc1bde6 | 2017-04-19 08:33:55 | [diff] [blame] | 341 | }; |
[email protected] | 64e5cc0 | 2010-11-03 19:20:27 | [diff] [blame] | 342 | |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 343 | // Sets the Log Message Handler that gets passed every log message before |
| 344 | // it's sent to other log destinations (if any). |
| 345 | // Returns true to signal that it handled the message and the message |
| 346 | // should not be sent to other log destinations. |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 347 | typedef bool (*LogMessageHandlerFunction)(int severity, |
| 348 | const char* file, int line, size_t message_start, const std::string& str); |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 349 | BASE_EXPORT void SetLogMessageHandler(LogMessageHandlerFunction handler); |
| 350 | BASE_EXPORT LogMessageHandlerFunction GetLogMessageHandler(); |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 351 | |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame^] | 352 | using LogSeverity = int; |
| 353 | const LogSeverity LOGGING_VERBOSE = -1; // This is level 1 verbosity |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 354 | // Note: the log severities are used to index into the array of names, |
| 355 | // see log_severity_names. |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame^] | 356 | const LogSeverity LOGGING_INFO = 0; |
| 357 | const LogSeverity LOGGING_WARNING = 1; |
| 358 | const LogSeverity LOGGING_ERROR = 2; |
| 359 | const LogSeverity LOGGING_FATAL = 3; |
| 360 | const LogSeverity LOGGING_NUM_SEVERITIES = 4; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 361 | |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame^] | 362 | // LOGGING_DFATAL is LOGGING_FATAL in debug mode, ERROR in normal mode |
wez | a245bd07 | 2017-06-18 23:26:34 | [diff] [blame] | 363 | #if defined(NDEBUG) |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame^] | 364 | const LogSeverity LOGGING_DFATAL = LOGGING_ERROR; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 365 | #else |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame^] | 366 | const LogSeverity LOGGING_DFATAL = LOGGING_FATAL; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 367 | #endif |
| 368 | |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame^] | 369 | // This block duplicates the above entries to facilitate incremental conversion |
| 370 | // from LOG_FOO to LOGGING_FOO. |
| 371 | // TODO(thestig): Convert existing users to LOGGING_FOO and remove this block. |
| 372 | const LogSeverity LOG_VERBOSE = LOGGING_VERBOSE; |
| 373 | const LogSeverity LOG_INFO = LOGGING_INFO; |
| 374 | const LogSeverity LOG_WARNING = LOGGING_WARNING; |
| 375 | const LogSeverity LOG_ERROR = LOGGING_ERROR; |
| 376 | const LogSeverity LOG_FATAL = LOGGING_FATAL; |
| 377 | const LogSeverity LOG_NUM_SEVERITIES = LOGGING_NUM_SEVERITIES; |
| 378 | const LogSeverity LOG_DFATAL = LOGGING_DFATAL; |
| 379 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 380 | // A few definitions of macros that don't generate much code. These are used |
| 381 | // by LOG() and LOG_IF, etc. Since these are used all over our code, it's |
| 382 | // better to have compact code for these operations. |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame^] | 383 | #define COMPACT_GOOGLE_LOG_EX_INFO(ClassName, ...) \ |
| 384 | ::logging::ClassName(__FILE__, __LINE__, ::logging::LOGGING_INFO, \ |
tsniatowski | 612550f | 2016-07-21 18:26:20 | [diff] [blame] | 385 | ##__VA_ARGS__) |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame^] | 386 | #define COMPACT_GOOGLE_LOG_EX_WARNING(ClassName, ...) \ |
| 387 | ::logging::ClassName(__FILE__, __LINE__, ::logging::LOGGING_WARNING, \ |
| 388 | ##__VA_ARGS__) |
| 389 | #define COMPACT_GOOGLE_LOG_EX_ERROR(ClassName, ...) \ |
| 390 | ::logging::ClassName(__FILE__, __LINE__, ::logging::LOGGING_ERROR, \ |
| 391 | ##__VA_ARGS__) |
| 392 | #define COMPACT_GOOGLE_LOG_EX_FATAL(ClassName, ...) \ |
| 393 | ::logging::ClassName(__FILE__, __LINE__, ::logging::LOGGING_FATAL, \ |
| 394 | ##__VA_ARGS__) |
| 395 | #define COMPACT_GOOGLE_LOG_EX_DFATAL(ClassName, ...) \ |
| 396 | ::logging::ClassName(__FILE__, __LINE__, ::logging::LOGGING_DFATAL, \ |
| 397 | ##__VA_ARGS__) |
| 398 | #define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \ |
| 399 | ::logging::ClassName(__FILE__, __LINE__, ::logging::LOGGING_DCHECK, \ |
| 400 | ##__VA_ARGS__) |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 401 | |
Wez | 289477f | 2017-08-24 20:51:30 | [diff] [blame] | 402 | #define COMPACT_GOOGLE_LOG_INFO COMPACT_GOOGLE_LOG_EX_INFO(LogMessage) |
| 403 | #define COMPACT_GOOGLE_LOG_WARNING COMPACT_GOOGLE_LOG_EX_WARNING(LogMessage) |
| 404 | #define COMPACT_GOOGLE_LOG_ERROR COMPACT_GOOGLE_LOG_EX_ERROR(LogMessage) |
| 405 | #define COMPACT_GOOGLE_LOG_FATAL COMPACT_GOOGLE_LOG_EX_FATAL(LogMessage) |
| 406 | #define COMPACT_GOOGLE_LOG_DFATAL COMPACT_GOOGLE_LOG_EX_DFATAL(LogMessage) |
| 407 | #define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_EX_DCHECK(LogMessage) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 408 | |
[email protected] | 8d12730 | 2013-01-10 02:41:57 | [diff] [blame] | 409 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 410 | // wingdi.h defines ERROR to be 0. When we call LOG(ERROR), it gets |
| 411 | // substituted with 0, and it expands to COMPACT_GOOGLE_LOG_0. To allow us |
| 412 | // to keep using this syntax, we define this macro to do the same thing |
| 413 | // as COMPACT_GOOGLE_LOG_ERROR, and also define ERROR the same way that |
| 414 | // the Windows SDK does for consistency. |
| 415 | #define ERROR 0 |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 416 | #define COMPACT_GOOGLE_LOG_EX_0(ClassName, ...) \ |
| 417 | COMPACT_GOOGLE_LOG_EX_ERROR(ClassName , ##__VA_ARGS__) |
| 418 | #define COMPACT_GOOGLE_LOG_0 COMPACT_GOOGLE_LOG_ERROR |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 419 | // Needed for LOG_IS_ON(ERROR). |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame^] | 420 | const LogSeverity LOGGING_0 = LOGGING_ERROR; |
[email protected] | 8d12730 | 2013-01-10 02:41:57 | [diff] [blame] | 421 | #endif |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 422 | |
[email protected] | f2c0549 | 2014-06-17 12:04:23 | [diff] [blame] | 423 | // As special cases, we can assume that LOG_IS_ON(FATAL) always holds. Also, |
| 424 | // LOG_IS_ON(DFATAL) always holds in debug mode. In particular, CHECK()s will |
| 425 | // always fire if they fail. |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 426 | #define LOG_IS_ON(severity) \ |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame^] | 427 | (::logging::ShouldCreateLogMessage(::logging::LOGGING_##severity)) |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 428 | |
Ken MacKay | 70e886700 | 2019-01-16 00:22:15 | [diff] [blame] | 429 | // We don't do any caching tricks with VLOG_IS_ON() like the |
| 430 | // google-glog version since it increases binary size. This means |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 431 | // that using the v-logging functions in conjunction with --vmodule |
| 432 | // may be slow. |
| 433 | #define VLOG_IS_ON(verboselevel) \ |
| 434 | ((verboselevel) <= ::logging::GetVlogLevel(__FILE__)) |
| 435 | |
| 436 | // Helper macro which avoids evaluating the arguments to a stream if |
chcunningham | f6a9608 | 2015-02-07 01:58:37 | [diff] [blame] | 437 | // the condition doesn't hold. Condition is evaluated once and only once. |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 438 | #define LAZY_STREAM(stream, condition) \ |
| 439 | !(condition) ? (void) 0 : ::logging::LogMessageVoidify() & (stream) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 440 | |
| 441 | // We use the preprocessor's merging operator, "##", so that, e.g., |
| 442 | // LOG(INFO) becomes the token COMPACT_GOOGLE_LOG_INFO. There's some funny |
| 443 | // subtle difference between ostream member streaming functions (e.g., |
| 444 | // ostream::operator<<(int) and ostream non-member streaming functions |
| 445 | // (e.g., ::operator<<(ostream&, string&): it turns out that it's |
| 446 | // impossible to stream something like a string directly to an unnamed |
| 447 | // ostream. We employ a neat hack by calling the stream() member |
| 448 | // function of LogMessage which seems to avoid the problem. |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 449 | #define LOG_STREAM(severity) COMPACT_GOOGLE_LOG_ ## severity.stream() |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 450 | |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 451 | #define LOG(severity) LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity)) |
| 452 | #define LOG_IF(severity, condition) \ |
| 453 | LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity) && (condition)) |
| 454 | |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 455 | // The VLOG macros log with negative verbosities. |
| 456 | #define VLOG_STREAM(verbose_level) \ |
tsniatowski | 612550f | 2016-07-21 18:26:20 | [diff] [blame] | 457 | ::logging::LogMessage(__FILE__, __LINE__, -verbose_level).stream() |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 458 | |
| 459 | #define VLOG(verbose_level) \ |
| 460 | LAZY_STREAM(VLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level)) |
| 461 | |
| 462 | #define VLOG_IF(verbose_level, condition) \ |
| 463 | LAZY_STREAM(VLOG_STREAM(verbose_level), \ |
| 464 | VLOG_IS_ON(verbose_level) && (condition)) |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 465 | |
[email protected] | fb879b1a | 2011-03-06 18:16:31 | [diff] [blame] | 466 | #if defined (OS_WIN) |
| 467 | #define VPLOG_STREAM(verbose_level) \ |
tsniatowski | 612550f | 2016-07-21 18:26:20 | [diff] [blame] | 468 | ::logging::Win32ErrorLogMessage(__FILE__, __LINE__, -verbose_level, \ |
[email protected] | fb879b1a | 2011-03-06 18:16:31 | [diff] [blame] | 469 | ::logging::GetLastSystemErrorCode()).stream() |
Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 470 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
[email protected] | fb879b1a | 2011-03-06 18:16:31 | [diff] [blame] | 471 | #define VPLOG_STREAM(verbose_level) \ |
tsniatowski | 612550f | 2016-07-21 18:26:20 | [diff] [blame] | 472 | ::logging::ErrnoLogMessage(__FILE__, __LINE__, -verbose_level, \ |
[email protected] | fb879b1a | 2011-03-06 18:16:31 | [diff] [blame] | 473 | ::logging::GetLastSystemErrorCode()).stream() |
| 474 | #endif |
| 475 | |
| 476 | #define VPLOG(verbose_level) \ |
| 477 | LAZY_STREAM(VPLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level)) |
| 478 | |
| 479 | #define VPLOG_IF(verbose_level, condition) \ |
| 480 | LAZY_STREAM(VPLOG_STREAM(verbose_level), \ |
| 481 | VLOG_IS_ON(verbose_level) && (condition)) |
| 482 | |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 483 | // TODO(akalin): Add more VLOG variants, e.g. VPLOG. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 484 | |
kmarshall | fe2f09f8 | 2017-04-20 21:05:26 | [diff] [blame] | 485 | #define LOG_ASSERT(condition) \ |
| 486 | LOG_IF(FATAL, !(ANALYZER_ASSUME_TRUE(condition))) \ |
| 487 | << "Assert failed: " #condition ". " |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 488 | |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 489 | #if defined(OS_WIN) |
[email protected] | c914d8a | 2014-04-23 01:11:01 | [diff] [blame] | 490 | #define PLOG_STREAM(severity) \ |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 491 | COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \ |
| 492 | ::logging::GetLastSystemErrorCode()).stream() |
Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 493 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
[email protected] | c914d8a | 2014-04-23 01:11:01 | [diff] [blame] | 494 | #define PLOG_STREAM(severity) \ |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 495 | COMPACT_GOOGLE_LOG_EX_ ## severity(ErrnoLogMessage, \ |
| 496 | ::logging::GetLastSystemErrorCode()).stream() |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 497 | #endif |
| 498 | |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 499 | #define PLOG(severity) \ |
| 500 | LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity)) |
| 501 | |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 502 | #define PLOG_IF(severity, condition) \ |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 503 | LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity) && (condition)) |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 504 | |
scottmg | 3c957a5 | 2016-12-10 20:57:59 | [diff] [blame] | 505 | BASE_EXPORT extern std::ostream* g_swallow_stream; |
| 506 | |
| 507 | // Note that g_swallow_stream is used instead of an arbitrary LOG() stream to |
| 508 | // avoid the creation of an object with a non-trivial destructor (LogMessage). |
| 509 | // On MSVC x86 (checked on 2015 Update 3), this causes a few additional |
| 510 | // pointless instructions to be emitted even at full optimization level, even |
| 511 | // though the : arm of the ternary operator is clearly never executed. Using a |
| 512 | // simpler object to be &'d with Voidify() avoids these extra instructions. |
| 513 | // Using a simpler POD object with a templated operator<< also works to avoid |
| 514 | // these instructions. However, this causes warnings on statically defined |
| 515 | // implementations of operator<<(std::ostream, ...) in some .cc files, because |
| 516 | // they become defined-but-unreferenced functions. A reinterpret_cast of 0 to an |
| 517 | // ostream* also is not suitable, because some compilers warn of undefined |
| 518 | // behavior. |
| 519 | #define EAT_STREAM_PARAMETERS \ |
| 520 | true ? (void)0 \ |
| 521 | : ::logging::LogMessageVoidify() & (*::logging::g_swallow_stream) |
[email protected] | ddb9b33 | 2011-12-02 07:31:09 | [diff] [blame] | 522 | |
[email protected] | d15e56c | 2010-09-30 21:12:33 | [diff] [blame] | 523 | // Definitions for DLOG et al. |
| 524 | |
gab | 190f754 | 2016-08-01 20:03:41 | [diff] [blame] | 525 | #if DCHECK_IS_ON() |
[email protected] | d926c20 | 2010-10-01 02:58:24 | [diff] [blame] | 526 | |
[email protected] | 5e98780 | 2010-11-01 19:49:22 | [diff] [blame] | 527 | #define DLOG_IS_ON(severity) LOG_IS_ON(severity) |
[email protected] | d926c20 | 2010-10-01 02:58:24 | [diff] [blame] | 528 | #define DLOG_IF(severity, condition) LOG_IF(severity, condition) |
| 529 | #define DLOG_ASSERT(condition) LOG_ASSERT(condition) |
[email protected] | d926c20 | 2010-10-01 02:58:24 | [diff] [blame] | 530 | #define DPLOG_IF(severity, condition) PLOG_IF(severity, condition) |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 531 | #define DVLOG_IF(verboselevel, condition) VLOG_IF(verboselevel, condition) |
[email protected] | fb879b1a | 2011-03-06 18:16:31 | [diff] [blame] | 532 | #define DVPLOG_IF(verboselevel, condition) VPLOG_IF(verboselevel, condition) |
[email protected] | d926c20 | 2010-10-01 02:58:24 | [diff] [blame] | 533 | |
gab | 190f754 | 2016-08-01 20:03:41 | [diff] [blame] | 534 | #else // DCHECK_IS_ON() |
[email protected] | d926c20 | 2010-10-01 02:58:24 | [diff] [blame] | 535 | |
gab | 190f754 | 2016-08-01 20:03:41 | [diff] [blame] | 536 | // If !DCHECK_IS_ON(), we want to avoid emitting any references to |condition| |
| 537 | // (which may reference a variable defined only if DCHECK_IS_ON()). |
| 538 | // Contrast this with DCHECK et al., which has different behavior. |
[email protected] | d926c20 | 2010-10-01 02:58:24 | [diff] [blame] | 539 | |
[email protected] | 5e98780 | 2010-11-01 19:49:22 | [diff] [blame] | 540 | #define DLOG_IS_ON(severity) false |
[email protected] | ddb9b33 | 2011-12-02 07:31:09 | [diff] [blame] | 541 | #define DLOG_IF(severity, condition) EAT_STREAM_PARAMETERS |
| 542 | #define DLOG_ASSERT(condition) EAT_STREAM_PARAMETERS |
| 543 | #define DPLOG_IF(severity, condition) EAT_STREAM_PARAMETERS |
| 544 | #define DVLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS |
| 545 | #define DVPLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS |
[email protected] | d926c20 | 2010-10-01 02:58:24 | [diff] [blame] | 546 | |
gab | 190f754 | 2016-08-01 20:03:41 | [diff] [blame] | 547 | #endif // DCHECK_IS_ON() |
[email protected] | d926c20 | 2010-10-01 02:58:24 | [diff] [blame] | 548 | |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 549 | #define DLOG(severity) \ |
| 550 | LAZY_STREAM(LOG_STREAM(severity), DLOG_IS_ON(severity)) |
| 551 | |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 552 | #define DPLOG(severity) \ |
| 553 | LAZY_STREAM(PLOG_STREAM(severity), DLOG_IS_ON(severity)) |
| 554 | |
Ken MacKay | 70e886700 | 2019-01-16 00:22:15 | [diff] [blame] | 555 | #define DVLOG(verboselevel) DVLOG_IF(verboselevel, true) |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 556 | |
Ken MacKay | 70e886700 | 2019-01-16 00:22:15 | [diff] [blame] | 557 | #define DVPLOG(verboselevel) DVPLOG_IF(verboselevel, true) |
[email protected] | fb879b1a | 2011-03-06 18:16:31 | [diff] [blame] | 558 | |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 559 | // Definitions for DCHECK et al. |
[email protected] | d926c20 | 2010-10-01 02:58:24 | [diff] [blame] | 560 | |
danakj | e649f57 | 2015-01-08 23:35:58 | [diff] [blame] | 561 | #if DCHECK_IS_ON() |
[email protected] | e3cca33 | 2009-08-20 01:20:29 | [diff] [blame] | 562 | |
Tomas Popela | afffa97 | 2018-11-13 20:42:05 | [diff] [blame] | 563 | #if defined(DCHECK_IS_CONFIGURABLE) |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame^] | 564 | BASE_EXPORT extern LogSeverity LOGGING_DCHECK; |
Wez | 289477f | 2017-08-24 20:51:30 | [diff] [blame] | 565 | #else |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame^] | 566 | const LogSeverity LOGGING_DCHECK = LOGGING_FATAL; |
Tomas Popela | afffa97 | 2018-11-13 20:42:05 | [diff] [blame] | 567 | #endif // defined(DCHECK_IS_CONFIGURABLE) |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 568 | |
danakj | e649f57 | 2015-01-08 23:35:58 | [diff] [blame] | 569 | #else // DCHECK_IS_ON() |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 570 | |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame^] | 571 | // There may be users of LOGGING_DCHECK that are enabled independently |
Sigurdur Asgeirsson | 7013e5f | 2017-09-29 17:42:58 | [diff] [blame] | 572 | // of DCHECK_IS_ON(), so default to FATAL logging for those. |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame^] | 573 | const LogSeverity LOGGING_DCHECK = LOGGING_FATAL; |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 574 | |
danakj | e649f57 | 2015-01-08 23:35:58 | [diff] [blame] | 575 | #endif // DCHECK_IS_ON() |
[email protected] | 521b0c4 | 2010-10-01 23:02:36 | [diff] [blame] | 576 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 577 | // Redefine the standard assert to use our nice log files |
| 578 | #undef assert |
| 579 | #define assert(x) DLOG_ASSERT(x) |
| 580 | |
| 581 | // This class more or less represents a particular log message. You |
| 582 | // create an instance of LogMessage and then stream stuff to it. |
| 583 | // When you finish streaming to it, ~LogMessage is called and the |
| 584 | // full message gets streamed to the appropriate destination. |
| 585 | // |
| 586 | // You shouldn't actually use LogMessage's constructor to log things, |
| 587 | // though. You should use the LOG() macro (and variants thereof) |
| 588 | // above. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 589 | class BASE_EXPORT LogMessage { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 590 | public: |
[email protected] | bf8ddf13a | 2014-06-18 15:02:22 | [diff] [blame] | 591 | // Used for LOG(severity). |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 592 | LogMessage(const char* file, int line, LogSeverity severity); |
| 593 | |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame^] | 594 | // Used for CHECK(). Implied severity = LOGGING_FATAL. |
tnagel | 4a045d3f | 2015-07-12 14:19:28 | [diff] [blame] | 595 | LogMessage(const char* file, int line, const char* condition); |
David Bienvenu | b4b441e | 2020-09-23 05:49:57 | [diff] [blame] | 596 | LogMessage(const LogMessage&) = delete; |
| 597 | LogMessage& operator=(const LogMessage&) = delete; |
Hans Wennborg | 12aea3e | 2020-04-14 15:29:00 | [diff] [blame] | 598 | virtual ~LogMessage(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 599 | |
| 600 | std::ostream& stream() { return stream_; } |
| 601 | |
pastarmovj | 89f7ee1 | 2016-09-20 14:58:13 | [diff] [blame] | 602 | LogSeverity severity() { return severity_; } |
| 603 | std::string str() { return stream_.str(); } |
| 604 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 605 | private: |
| 606 | void Init(const char* file, int line); |
| 607 | |
| 608 | LogSeverity severity_; |
| 609 | std::ostringstream stream_; |
[email protected] | c8887392 | 2008-07-30 13:02:03 | [diff] [blame] | 610 | size_t message_start_; // Offset of the start of the message (past prefix |
| 611 | // info). |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 612 | // The file and line information passed in to the constructor. |
| 613 | const char* file_; |
| 614 | const int line_; |
Kevin Marshall | 9873826d | 2019-10-14 20:09:35 | [diff] [blame] | 615 | const char* file_basename_; |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 616 | |
[email protected] | 3f85caa | 2009-04-14 16:52:11 | [diff] [blame] | 617 | // This is useful since the LogMessage class uses a lot of Win32 calls |
| 618 | // that will lose the value of GLE and the code that called the log function |
| 619 | // will have lost the thread error value when the log call returns. |
Joshua Peraza | b427af26 | 2020-04-13 21:54:42 | [diff] [blame] | 620 | base::ScopedClearLastError last_error_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 621 | |
Yuta Hijikata | 9b7279a | 2020-08-26 16:10:54 | [diff] [blame] | 622 | #if defined(OS_CHROMEOS) |
| 623 | void InitWithSyslogPrefix(base::StringPiece filename, |
| 624 | int line, |
| 625 | uint64_t tick_count, |
| 626 | const char* log_severity_name_c_str, |
| 627 | const char* log_prefix, |
| 628 | bool enable_process_id, |
| 629 | bool enable_thread_id, |
| 630 | bool enable_timestamp, |
| 631 | bool enable_tickcount); |
| 632 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 633 | }; |
| 634 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 635 | // This class is used to explicitly ignore values in the conditional |
| 636 | // logging macros. This avoids compiler warnings like "value computed |
| 637 | // is not used" and "statement has no effect". |
[email protected] | 23bb71f | 2011-04-21 22:22:10 | [diff] [blame] | 638 | class LogMessageVoidify { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 639 | public: |
Chris Watkins | 091d629 | 2017-12-13 04:25:58 | [diff] [blame] | 640 | LogMessageVoidify() = default; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 641 | // This has to be an operator with a precedence lower than << but |
| 642 | // higher than ?: |
| 643 | void operator&(std::ostream&) { } |
| 644 | }; |
| 645 | |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 646 | #if defined(OS_WIN) |
| 647 | typedef unsigned long SystemErrorCode; |
Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 648 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 649 | typedef int SystemErrorCode; |
| 650 | #endif |
| 651 | |
| 652 | // Alias for ::GetLastError() on Windows and errno on POSIX. Avoids having to |
| 653 | // pull in windows.h just for GetLastError() and DWORD. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 654 | BASE_EXPORT SystemErrorCode GetLastSystemErrorCode(); |
[email protected] | c914d8a | 2014-04-23 01:11:01 | [diff] [blame] | 655 | BASE_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code); |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 656 | |
| 657 | #if defined(OS_WIN) |
| 658 | // Appends a formatted system message of the GetLastError() type. |
Hans Wennborg | 12aea3e | 2020-04-14 15:29:00 | [diff] [blame] | 659 | class BASE_EXPORT Win32ErrorLogMessage : public LogMessage { |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 660 | public: |
| 661 | Win32ErrorLogMessage(const char* file, |
| 662 | int line, |
| 663 | LogSeverity severity, |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 664 | SystemErrorCode err); |
David Bienvenu | b4b441e | 2020-09-23 05:49:57 | [diff] [blame] | 665 | Win32ErrorLogMessage(const Win32ErrorLogMessage&) = delete; |
| 666 | Win32ErrorLogMessage& operator=(const Win32ErrorLogMessage&) = delete; |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 667 | // Appends the error message before destructing the encapsulated class. |
Hans Wennborg | 12aea3e | 2020-04-14 15:29:00 | [diff] [blame] | 668 | ~Win32ErrorLogMessage() override; |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 669 | |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 670 | private: |
| 671 | SystemErrorCode err_; |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 672 | }; |
Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 673 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 674 | // Appends a formatted system message of the errno type |
Hans Wennborg | 12aea3e | 2020-04-14 15:29:00 | [diff] [blame] | 675 | class BASE_EXPORT ErrnoLogMessage : public LogMessage { |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 676 | public: |
| 677 | ErrnoLogMessage(const char* file, |
| 678 | int line, |
| 679 | LogSeverity severity, |
| 680 | SystemErrorCode err); |
David Bienvenu | b4b441e | 2020-09-23 05:49:57 | [diff] [blame] | 681 | ErrnoLogMessage(const ErrnoLogMessage&) = delete; |
| 682 | ErrnoLogMessage& operator=(const ErrnoLogMessage&) = delete; |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 683 | // Appends the error message before destructing the encapsulated class. |
Hans Wennborg | 12aea3e | 2020-04-14 15:29:00 | [diff] [blame] | 684 | ~ErrnoLogMessage() override; |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 685 | |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 686 | private: |
| 687 | SystemErrorCode err_; |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 688 | }; |
| 689 | #endif // OS_WIN |
| 690 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 691 | // Closes the log file explicitly if open. |
| 692 | // NOTE: Since the log file is opened as necessary by the action of logging |
| 693 | // statements, there's no guarantee that it will stay closed |
| 694 | // after this call. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 695 | BASE_EXPORT void CloseLogFile(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 696 | |
Robbie McElrath | 8bf4984 | 2019-08-20 22:22:53 | [diff] [blame] | 697 | #if defined(OS_CHROMEOS) |
| 698 | // Returns a new file handle that will write to the same destination as the |
| 699 | // currently open log file. Returns nullptr if logging to a file is disabled, |
| 700 | // or if opening the file failed. This is intended to be used to initialize |
| 701 | // logging in child processes that are unable to open files. |
| 702 | BASE_EXPORT FILE* DuplicateLogFILE(); |
| 703 | #endif |
| 704 | |
[email protected] | e36ddc8 | 2009-12-08 04:22:50 | [diff] [blame] | 705 | // Async signal safe logging mechanism. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 706 | BASE_EXPORT void RawLog(int level, const char* message); |
[email protected] | e36ddc8 | 2009-12-08 04:22:50 | [diff] [blame] | 707 | |
tsniatowski | 612550f | 2016-07-21 18:26:20 | [diff] [blame] | 708 | #define RAW_LOG(level, message) \ |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame^] | 709 | ::logging::RawLog(::logging::LOGGING_##level, message) |
[email protected] | e36ddc8 | 2009-12-08 04:22:50 | [diff] [blame] | 710 | |
[email protected] | f01b88a | 2013-02-27 22:04:00 | [diff] [blame] | 711 | #if defined(OS_WIN) |
ananta | 61762fb | 2015-09-18 01:00:09 | [diff] [blame] | 712 | // Returns true if logging to file is enabled. |
| 713 | BASE_EXPORT bool IsLoggingToFileEnabled(); |
| 714 | |
[email protected] | f01b88a | 2013-02-27 22:04:00 | [diff] [blame] | 715 | // Returns the default log file path. |
Jan Wilken Dörrie | b630aca | 2019-12-04 10:59:11 | [diff] [blame] | 716 | BASE_EXPORT std::wstring GetLogFileFullPath(); |
[email protected] | f01b88a | 2013-02-27 22:04:00 | [diff] [blame] | 717 | #endif |
| 718 | |
[email protected] | 39be424 | 2008-08-07 18:31:40 | [diff] [blame] | 719 | } // namespace logging |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 720 | |
[email protected] | 81411c6 | 2014-07-08 23:03:06 | [diff] [blame] | 721 | // Note that "The behavior of a C++ program is undefined if it adds declarations |
| 722 | // or definitions to namespace std or to a namespace within namespace std unless |
| 723 | // otherwise specified." --C++11[namespace.std] |
| 724 | // |
| 725 | // We've checked that this particular definition has the intended behavior on |
| 726 | // our implementations, but it's prone to breaking in the future, and please |
| 727 | // don't imitate this in your own definitions without checking with some |
| 728 | // standard library experts. |
| 729 | namespace std { |
[email protected] | 46ce5b56 | 2010-06-16 18:39:53 | [diff] [blame] | 730 | // These functions are provided as a convenience for logging, which is where we |
| 731 | // use streams (it is against Google style to use streams in other places). It |
| 732 | // is designed to allow you to emit non-ASCII Unicode strings to the log file, |
| 733 | // which is normally ASCII. It is relatively slow, so try not to use it for |
| 734 | // common cases. Non-ASCII characters will be converted to UTF-8 by these |
| 735 | // operators. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 736 | BASE_EXPORT std::ostream& operator<<(std::ostream& out, const wchar_t* wstr); |
[email protected] | 46ce5b56 | 2010-06-16 18:39:53 | [diff] [blame] | 737 | inline std::ostream& operator<<(std::ostream& out, const std::wstring& wstr) { |
| 738 | return out << wstr.c_str(); |
| 739 | } |
[email protected] | 81411c6 | 2014-07-08 23:03:06 | [diff] [blame] | 740 | } // namespace std |
[email protected] | 46ce5b56 | 2010-06-16 18:39:53 | [diff] [blame] | 741 | |
[email protected] | 39be424 | 2008-08-07 18:31:40 | [diff] [blame] | 742 | #endif // BASE_LOGGING_H_ |