blob: 440fa5ebf331028bed8fa135b7c45531be62b5a4 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2012 The Chromium Authors
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
[email protected]39be4242008-08-07 18:31:405#ifndef BASE_LOGGING_H_
6#define BASE_LOGGING_H_
initial.commitd7cae122008-07-26 21:49:387
avi9b6f42932015-12-26 22:15:148#include <stddef.h>
9
[email protected]e7972d12011-06-18 11:53:1410#include <cassert>
Sharon Yang7cb919a2019-05-20 20:27:1511#include <cstdint>
initial.commitd7cae122008-07-26 21:49:3812#include <sstream>
avi9b6f42932015-12-26 22:15:1413#include <string>
David Benjaminb1ccd0cb2023-06-22 23:08:4514#include <string_view>
initial.commitd7cae122008-07-26 21:49:3815
[email protected]0bea7252011-08-05 15:34:0016#include "base/base_export.h"
danakjcb7c5292016-12-20 19:05:3517#include "base/compiler_specific.h"
Hans Wennborg944479f2020-06-25 21:39:2518#include "base/dcheck_is_on.h"
Dan McArdleb4b65e32024-04-15 19:09:0019#include "base/files/file_path.h"
Avi Drissman63e1f992023-01-13 18:54:4320#include "base/functional/callback_forward.h"
Lukasz Anforowicz191a4d32024-11-12 01:48:0821#include "base/logging/log_severity.h"
David Benjaminb1ccd0cb2023-06-22 23:08:4522#include "base/strings/utf_ostream_operators.h"
Eric Willigers026d7ea2021-12-07 21:44:5423#include "build/build_config.h"
initial.commitd7cae122008-07-26 21:49:3824
Georg Neisffe34f652021-12-27 21:42:3625#if BUILDFLAG(IS_CHROMEOS)
Robbie McElrath8bf49842019-08-20 22:22:5326#include <cstdio>
Lei Zhangc1646b32024-05-24 21:46:3127
28#include "base/memory/raw_ptr.h"
Robbie McElrath8bf49842019-08-20 22:22:5329#endif
30
Alex Gough9d2b795f2024-03-18 18:51:5331#if BUILDFLAG(IS_WIN)
32#include "base/win/windows_types.h"
33#endif
34
initial.commitd7cae122008-07-26 21:49:3835//
36// Optional message capabilities
37// -----------------------------
38// Assertion failed messages and fatal errors are displayed in a dialog box
39// before the application exits. However, running this UI creates a message
40// loop, which causes application messages to be processed and potentially
41// dispatched to existing application windows. Since the application is in a
42// bad state when this assertion dialog is displayed, these messages may not
43// get processed and hang the dialog, or the application might go crazy.
44//
45// Therefore, it can be beneficial to display the error dialog in a separate
46// process from the main application. When the logging system needs to display
47// a fatal error dialog box, it will look for a program called
48// "DebugMessage.exe" in the same directory as the application executable. It
49// will run this application with the message as the command line, and will
50// not include the name of the application as is traditional for easier
51// parsing.
52//
53// The code for DebugMessage.exe is only one line. In WinMain, do:
54// MessageBox(NULL, GetCommandLineW(), L"Fatal Error", 0);
55//
56// If DebugMessage.exe is not found, the logging code will use a normal
57// MessageBox, potentially causing the problems discussed above.
58
initial.commitd7cae122008-07-26 21:49:3859// Instructions
60// ------------
61//
62// Make a bunch of macros for logging. The way to log things is to stream
63// things to LOG(<a particular severity level>). E.g.,
64//
65// LOG(INFO) << "Found " << num_cookies << " cookies";
66//
67// You can also do conditional logging:
68//
69// LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
70//
initial.commitd7cae122008-07-26 21:49:3871// The CHECK(condition) macro is active in both debug and release builds and
72// effectively performs a LOG(FATAL) which terminates the process and
73// generates a crashdump unless a debugger is attached.
74//
75// There are also "debug mode" logging macros like the ones above:
76//
77// DLOG(INFO) << "Found cookies";
78//
79// DLOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
80//
81// All "debug mode" logging is compiled away to nothing for non-debug mode
82// compiles. LOG_IF and development flags also work well together
83// because the code can be compiled away sometimes.
84//
85// We also have
86//
87// LOG_ASSERT(assertion);
88// DLOG_ASSERT(assertion);
89//
90// which is syntactic sugar for {,D}LOG_IF(FATAL, assert fails) << assertion;
91//
[email protected]99b7c57f2010-09-29 19:26:3692// There are "verbose level" logging macros. They look like
93//
94// VLOG(1) << "I'm printed when you run the program with --v=1 or more";
95// VLOG(2) << "I'm printed when you run the program with --v=2 or more";
96//
97// These always log at the INFO log level (when they log at all).
Xiyuan Xiaa0559da2022-05-05 19:42:4598//
Xiyuan Xia28d809d2023-11-02 22:00:4299// The verbose logging can also be turned on module-by-module. For instance,
[email protected]b0d38d4c2010-10-29 00:39:48100// --vmodule=profile=2,icon_loader=1,browser_*=3,*/chromeos/*=4 --v=0
[email protected]99b7c57f2010-09-29 19:26:36101// will cause:
102// a. VLOG(2) and lower messages to be printed from profile.{h,cc}
103// b. VLOG(1) and lower messages to be printed from icon_loader.{h,cc}
104// c. VLOG(3) and lower messages to be printed from files prefixed with
105// "browser"
[email protected]e11de722010-11-01 20:50:55106// d. VLOG(4) and lower messages to be printed from files under a
[email protected]b0d38d4c2010-10-29 00:39:48107// "chromeos" directory.
[email protected]e11de722010-11-01 20:50:55108// e. VLOG(0) and lower messages to be printed from elsewhere
[email protected]99b7c57f2010-09-29 19:26:36109//
110// The wildcarding functionality shown by (c) supports both '*' (match
[email protected]b0d38d4c2010-10-29 00:39:48111// 0 or more characters) and '?' (match any single character)
112// wildcards. Any pattern containing a forward or backward slash will
113// be tested against the whole pathname and not just the module.
114// E.g., "*/foo/bar/*=2" would change the logging level for all code
115// in source files under a "foo/bar" directory.
[email protected]99b7c57f2010-09-29 19:26:36116//
Mason Freed14240d162020-08-12 13:06:34117// Note that for a Chromium binary built in release mode (is_debug = false) you
118// must pass "--enable-logging=stderr" in order to see the output of VLOG
119// statements.
120//
[email protected]99b7c57f2010-09-29 19:26:36121// There's also VLOG_IS_ON(n) "verbose level" condition macro. To be used as
122//
123// if (VLOG_IS_ON(2)) {
124// // do some logging preparation and logging
125// // that can't be accomplished with just VLOG(2) << ...;
126// }
127//
128// There is also a VLOG_IF "verbose level" condition macro for sample
129// cases, when some extra computation and preparation for logs is not
130// needed.
131//
132// VLOG_IF(1, (size > 1024))
133// << "I'm printed when size is more than 1024 and when you run the "
134// "program with --v=1 or more";
135//
initial.commitd7cae122008-07-26 21:49:38136// We also override the standard 'assert' to use 'DLOG_ASSERT'.
137//
[email protected]d8617a62009-10-09 23:52:20138// Lastly, there is:
139//
140// PLOG(ERROR) << "Couldn't do foo";
141// DPLOG(ERROR) << "Couldn't do foo";
142// PLOG_IF(ERROR, cond) << "Couldn't do foo";
143// DPLOG_IF(ERROR, cond) << "Couldn't do foo";
144// PCHECK(condition) << "Couldn't do foo";
145// DPCHECK(condition) << "Couldn't do foo";
146//
147// which append the last system error to the message in string form (taken from
148// GetLastError() on Windows and errno on POSIX).
149//
initial.commitd7cae122008-07-26 21:49:38150// The supported severity levels for macros that allow you to specify one
[email protected]f2c05492014-06-17 12:04:23151// are (in increasing order of severity) INFO, WARNING, ERROR, and FATAL.
initial.commitd7cae122008-07-26 21:49:38152//
153// Very important: logging a message at the FATAL severity level causes
154// the program to terminate (after the message is logged).
[email protected]fb62a532009-02-12 01:19:05155//
danakjf8e9c302021-01-27 21:37:23156// There is the special severity of DFATAL, which logs FATAL in DCHECK-enabled
157// builds, ERROR in normal mode.
Rob Schonberger45637212018-12-03 04:46:25158//
Yuta Hijikata9b7279a2020-08-26 16:10:54159// Output is formatted as per the following example, except on Chrome OS.
Rob Schonberger45637212018-12-03 04:46:25160// [3816:3877:0812/234555.406952:VERBOSE1:drm_device_handle.cc(90)] Succeeded
161// authenticating /dev/dri/card0 in 0 ms with 1 attempt(s)
162//
163// The colon separated fields inside the brackets are as follows:
164// 0. An optional Logfile prefix (not included in this example)
165// 1. Process ID
166// 2. Thread ID
167// 3. The date/time of the log message, in MMDD/HHMMSS.Milliseconds format
168// 4. The log level
169// 5. The filename and line number where the log was instantiated
170//
Yuta Hijikata9b7279a2020-08-26 16:10:54171// Output for Chrome OS can be switched to syslog-like format. See
Georg Neisffe34f652021-12-27 21:42:36172// InitWithSyslogPrefix() in logging_chromeos.cc for details.
Yuta Hijikata9b7279a2020-08-26 16:10:54173//
Rob Schonberger45637212018-12-03 04:46:25174// Note that the visibility can be changed by setting preferences in
175// SetLogItems()
Mason Freed14240d162020-08-12 13:06:34176//
177// Additional logging-related information can be found here:
John Palmerd29fc4362021-05-20 03:29:22178// https://chromium.googlesource.com/chromium/src/+/main/docs/linux/debugging.md#Logging
initial.commitd7cae122008-07-26 21:49:38179
180namespace logging {
181
Sharon Yang7cb919a2019-05-20 20:27:15182// A bitmask of potential logging destinations.
183using LoggingDestination = uint32_t;
184// Specifies where logs will be written. Multiple destinations can be specified
185// with bitwise OR.
186// Unless destination is LOG_NONE, all logs with severity ERROR and above will
187// be written to stderr in addition to the specified destination.
Alex Gough9d2b795f2024-03-18 18:51:53188// LOG_TO_FILE includes logging to externally-provided file handles.
Sharon Yang7cb919a2019-05-20 20:27:15189enum : uint32_t {
Xiaohan Wang38e4ebb2022-01-19 06:57:43190 LOG_NONE = 0,
191 LOG_TO_FILE = 1 << 0,
[email protected]5e3f7c22013-06-21 21:15:33192 LOG_TO_SYSTEM_DEBUG_LOG = 1 << 1,
Xiaohan Wang38e4ebb2022-01-19 06:57:43193 LOG_TO_STDERR = 1 << 2,
[email protected]5e3f7c22013-06-21 21:15:33194
Sharon Yang7cb919a2019-05-20 20:27:15195 LOG_TO_ALL = LOG_TO_FILE | LOG_TO_SYSTEM_DEBUG_LOG | LOG_TO_STDERR,
[email protected]5e3f7c22013-06-21 21:15:33196
Sharon Yang7cb919a2019-05-20 20:27:15197// On Windows, use a file next to the exe.
198// On POSIX platforms, where it may not even be possible to locate the
199// executable on disk, use stderr.
200// On Fuchsia, use the Fuchsia logging service.
Nico Weber6f2d26d2025-06-27 07:32:08201#if BUILDFLAG(IS_FUCHSIA)
[email protected]5e3f7c22013-06-21 21:15:33202 LOG_DEFAULT = LOG_TO_SYSTEM_DEBUG_LOG,
Xiaohan Wang38e4ebb2022-01-19 06:57:43203#elif BUILDFLAG(IS_WIN)
Sharon Yang7cb919a2019-05-20 20:27:15204 LOG_DEFAULT = LOG_TO_FILE,
Xiaohan Wang38e4ebb2022-01-19 06:57:43205#elif BUILDFLAG(IS_POSIX)
Sharon Yang7cb919a2019-05-20 20:27:15206 LOG_DEFAULT = LOG_TO_SYSTEM_DEBUG_LOG | LOG_TO_STDERR,
[email protected]5e3f7c22013-06-21 21:15:33207#endif
208};
initial.commitd7cae122008-07-26 21:49:38209
210// Indicates that the log file should be locked when being written to.
[email protected]5e3f7c22013-06-21 21:15:33211// Unless there is only one single-threaded process that is logging to
212// the log file, the file should be locked during writes to make each
[email protected]3ee50d12014-03-05 01:43:27213// log output atomic. Other writers will block.
initial.commitd7cae122008-07-26 21:49:38214//
215// All processes writing to the log file must have their locking set for it to
[email protected]5e3f7c22013-06-21 21:15:33216// work properly. Defaults to LOCK_LOG_FILE.
initial.commitd7cae122008-07-26 21:49:38217enum LogLockingState { LOCK_LOG_FILE, DONT_LOCK_LOG_FILE };
218
219// On startup, should we delete or append to an existing log file (if any)?
220// Defaults to APPEND_TO_OLD_LOG_FILE.
221enum OldFileDeletionState { DELETE_OLD_LOG_FILE, APPEND_TO_OLD_LOG_FILE };
222
Georg Neisffe34f652021-12-27 21:42:36223#if BUILDFLAG(IS_CHROMEOS)
Yuta Hijikata1fc8f6342020-09-01 03:25:56224// Defines the log message prefix format to use.
225// LOG_FORMAT_SYSLOG indicates syslog-like message prefixes.
226// LOG_FORMAT_CHROME indicates the normal Chrome format.
Yuta Hijikata9b7279a2020-08-26 16:10:54227enum class BASE_EXPORT LogFormat { LOG_FORMAT_CHROME, LOG_FORMAT_SYSLOG };
228#endif
229
[email protected]5e3f7c22013-06-21 21:15:33230struct BASE_EXPORT LoggingSettings {
Sharon Yang7cb919a2019-05-20 20:27:15231 // Equivalent to logging destination enum, but allows for multiple
232 // destinations.
Wez7e125622019-05-29 22:11:28233 uint32_t logging_dest = LOG_DEFAULT;
[email protected]5e3f7c22013-06-21 21:15:33234
Robbie McElrath8bf49842019-08-20 22:22:53235 // The four settings below have an effect only when LOG_TO_FILE is
[email protected]5e3f7c22013-06-21 21:15:33236 // set in |logging_dest|.
Dan McArdleb4b65e32024-04-15 19:09:00237 base::FilePath::StringType log_file_path;
Wez7e125622019-05-29 22:11:28238 LogLockingState lock_log = LOCK_LOG_FILE;
239 OldFileDeletionState delete_old = APPEND_TO_OLD_LOG_FILE;
Georg Neisffe34f652021-12-27 21:42:36240#if BUILDFLAG(IS_CHROMEOS)
Robbie McElrath8bf49842019-08-20 22:22:53241 // Contains an optional file that logs should be written to. If present,
242 // |log_file_path| will be ignored, and the logging system will take ownership
243 // of the FILE. If there's an error writing to this file, no fallback paths
244 // will be opened.
Bartek Nowierskif5eeeba2024-01-25 12:49:39245 raw_ptr<FILE> log_file = nullptr;
Yuta Hijikata1fc8f6342020-09-01 03:25:56246 // ChromeOS uses the syslog log format by default.
247 LogFormat log_format = LogFormat::LOG_FORMAT_SYSLOG;
Robbie McElrath8bf49842019-08-20 22:22:53248#endif
Alex Gough9d2b795f2024-03-18 18:51:53249#if BUILDFLAG(IS_WIN)
250 // Contains an optional file that logs should be written to. If present,
251 // `log_file_path` will be ignored, and the logging system will take ownership
252 // of the HANDLE. If there's an error writing to this file, no fallback paths
253 // will be opened.
254 HANDLE log_file = nullptr;
255#endif
[email protected]5e3f7c22013-06-21 21:15:33256};
[email protected]ff3d0c32010-08-23 19:57:46257
258// Define different names for the BaseInitLoggingImpl() function depending on
259// whether NDEBUG is defined or not so that we'll fail to link if someone tries
260// to compile logging.cc with NDEBUG but includes logging.h without defining it,
261// or vice versa.
weza245bd072017-06-18 23:26:34262#if defined(NDEBUG)
[email protected]ff3d0c32010-08-23 19:57:46263#define BaseInitLoggingImpl BaseInitLoggingImpl_built_with_NDEBUG
264#else
265#define BaseInitLoggingImpl BaseInitLoggingImpl_built_without_NDEBUG
266#endif
267
268// Implementation of the InitLogging() method declared below. We use a
269// more-specific name so we can #define it above without affecting other code
270// that has named stuff "InitLogging".
[email protected]5e3f7c22013-06-21 21:15:33271BASE_EXPORT bool BaseInitLoggingImpl(const LoggingSettings& settings);
[email protected]ff3d0c32010-08-23 19:57:46272
initial.commitd7cae122008-07-26 21:49:38273// Sets the log file name and other global logging state. Calling this function
274// is recommended, and is normally done at the beginning of application init.
275// If you don't call it, all the flags will be initialized to their default
276// values, and there is a race condition that may leak a critical section
277// object if two threads try to do the first log at the same time.
278// See the definition of the enums above for descriptions and default values.
279//
280// The default log file is initialized to "debug.log" in the application
281// directory. You probably don't want this, especially since the program
282// directory may not be writable on an enduser's system.
[email protected]064aa162011-12-03 00:30:08283//
284// This function may be called a second time to re-direct logging (e.g after
285// loging in to a user partition), however it should never be called more than
286// twice.
[email protected]5e3f7c22013-06-21 21:15:33287inline bool InitLogging(const LoggingSettings& settings) {
288 return BaseInitLoggingImpl(settings);
[email protected]ff3d0c32010-08-23 19:57:46289}
initial.commitd7cae122008-07-26 21:49:38290
291// Sets the log level. Anything at or above this level will be written to the
292// log file/displayed to the user (if applicable). Anything below this level
[email protected]162ac0f2010-11-04 15:50:49293// will be silently ignored. The log level defaults to 0 (everything is logged
294// up to level INFO) if this function is not called.
295// Note that log messages for VLOG(x) are logged at level -x, so setting
Fergal Dalycaf92ba2022-05-17 20:11:52296// the min log level to negative values enables verbose logging and conversely,
297// setting the VLOG default level will set this min level to a negative number,
298// effectively enabling all levels of logging.
[email protected]0bea7252011-08-05 15:34:00299BASE_EXPORT void SetMinLogLevel(int level);
initial.commitd7cae122008-07-26 21:49:38300
[email protected]8a2986ca2009-04-10 19:13:42301// Gets the current log level.
[email protected]0bea7252011-08-05 15:34:00302BASE_EXPORT int GetMinLogLevel();
initial.commitd7cae122008-07-26 21:49:38303
skobesc78c0ad72015-12-07 20:21:23304// Used by LOG_IS_ON to lazy-evaluate stream arguments.
305BASE_EXPORT bool ShouldCreateLogMessage(int severity);
306
[email protected]162ac0f2010-11-04 15:50:49307// Gets the VLOG default verbosity level.
[email protected]0bea7252011-08-05 15:34:00308BASE_EXPORT int GetVlogVerbosity();
[email protected]162ac0f2010-11-04 15:50:49309
Collin Baker0162bae2025-04-03 18:01:40310// Note that |N| is the size *with* the null terminator.
311BASE_EXPORT int GetVlogLevelHelper(const char* file_start, size_t N);
[email protected]2f4e9a62010-09-29 21:25:14312
tnagel270da922017-05-24 12:10:44313// Gets the current vlog level for the given file (usually taken from __FILE__).
[email protected]99b7c57f2010-09-29 19:26:36314template <size_t N>
315int GetVlogLevel(const char (&file)[N]) {
linxinan855543a02025-03-27 00:31:30316 // Disable runtime VLOG()s in official non-DCHECK builds. This saves ~135k on
317 // the android-binary-size bot in crrev.com/c/6344673. Parts of the code can,
318 // and do, override ENABLED_VLOG_LEVEL to collect logs in the wild. The rest
319 // is dead-code stripped.
320#if defined(OFFICIAL_BUILD) && !DCHECK_IS_ON() && BUILDFLAG(IS_ANDROID)
Peter Boströmf02cf0b2025-03-19 16:56:57321 return -1;
322#else
Collin Baker0162bae2025-04-03 18:01:40323 return GetVlogLevelHelper(file, N);
linxinan855543a02025-03-27 00:31:30324#endif // defined(OFFICIAL_BUILD) && !DCHECK_IS_ON() && BUILDFLAG(IS_ANDROID)
[email protected]99b7c57f2010-09-29 19:26:36325}
initial.commitd7cae122008-07-26 21:49:38326
327// Sets the common items you want to be prepended to each log message.
328// process and thread IDs default to off, the timestamp defaults to on.
329// If this function is not called, logging defaults to writing the timestamp
330// only.
Peter Kasting134ef9af2024-12-28 02:30:09331BASE_EXPORT void SetLogItems(bool enable_process_id,
332 bool enable_thread_id,
333 bool enable_timestamp,
334 bool enable_tickcount);
initial.commitd7cae122008-07-26 21:49:38335
James Cooka0536c32018-08-01 20:13:31336// Sets an optional prefix to add to each log message. |prefix| is not copied
337// and should be a raw string constant. |prefix| must only contain ASCII letters
338// to avoid confusion with PIDs and timestamps. Pass null to remove the prefix.
339// Logging defaults to no prefix.
340BASE_EXPORT void SetLogPrefix(const char* prefix);
341
[email protected]81e0a852010-08-17 00:38:12342// Sets whether or not you'd like to see fatal debug messages popped up in
343// a dialog box or not.
344// Dialogs are not shown by default.
[email protected]0bea7252011-08-05 15:34:00345BASE_EXPORT void SetShowErrorDialogs(bool enable_dialogs);
[email protected]81e0a852010-08-17 00:38:12346
Greg Thompson0f083542024-07-10 14:13:21347// Registers an abort hook with absl that will crash the process similarly to a
348// `CHECK` failure in case of a FATAL error in absl (e.g., any operation that
349// would throw an exception).
350BASE_EXPORT void RegisterAbslAbortHook();
351
initial.commitd7cae122008-07-26 21:49:38352// Sets the Log Assert Handler that will be used to notify of check failures.
alex-accc1bde62017-04-19 08:33:55353// Resets Log Assert Handler on object destruction.
[email protected]fb62a532009-02-12 01:19:05354// The default handler shows a dialog box and then terminate the process,
355// however clients can use this function to override with their own handling
356// (e.g. a silent one for Unit Tests)
alex-accc1bde62017-04-19 08:33:55357using LogAssertHandlerFunction =
Collin Baker0162bae2025-04-03 18:01:40358 base::RepeatingCallback<void(const char* file,
kylechar83fb51e52019-03-14 15:30:43359 int line,
Aquibuzzaman Md. Sayem42abceb72024-05-08 18:48:27360 std::string_view message,
361 std::string_view stack_trace)>;
alex-accc1bde62017-04-19 08:33:55362
363class BASE_EXPORT ScopedLogAssertHandler {
364 public:
365 explicit ScopedLogAssertHandler(LogAssertHandlerFunction handler);
David Bienvenub4b441e2020-09-23 05:49:57366 ScopedLogAssertHandler(const ScopedLogAssertHandler&) = delete;
367 ScopedLogAssertHandler& operator=(const ScopedLogAssertHandler&) = delete;
alex-accc1bde62017-04-19 08:33:55368 ~ScopedLogAssertHandler();
alex-accc1bde62017-04-19 08:33:55369};
[email protected]64e5cc02010-11-03 19:20:27370
[email protected]2b07b8412009-11-25 15:26:34371// Sets the Log Message Handler that gets passed every log message before
372// it's sent to other log destinations (if any).
373// Returns true to signal that it handled the message and the message
374// should not be sent to other log destinations.
[email protected]162ac0f2010-11-04 15:50:49375typedef bool (*LogMessageHandlerFunction)(int severity,
Collin Baker0162bae2025-04-03 18:01:40376 const char* file,
Peter Kasting134ef9af2024-12-28 02:30:09377 int line,
378 size_t message_start,
379 const std::string& str);
[email protected]0bea7252011-08-05 15:34:00380BASE_EXPORT void SetLogMessageHandler(LogMessageHandlerFunction handler);
381BASE_EXPORT LogMessageHandlerFunction GetLogMessageHandler();
[email protected]2b07b8412009-11-25 15:26:34382
initial.commitd7cae122008-07-26 21:49:38383// A few definitions of macros that don't generate much code. These are used
384// by LOG() and LOG_IF, etc. Since these are used all over our code, it's
385// better to have compact code for these operations.
Lei Zhang93dd42572020-10-23 18:45:53386#define COMPACT_GOOGLE_LOG_EX_INFO(ClassName, ...) \
387 ::logging::ClassName(__FILE__, __LINE__, ::logging::LOGGING_INFO, \
tsniatowski612550f2016-07-21 18:26:20388 ##__VA_ARGS__)
Lei Zhang93dd42572020-10-23 18:45:53389#define COMPACT_GOOGLE_LOG_EX_WARNING(ClassName, ...) \
390 ::logging::ClassName(__FILE__, __LINE__, ::logging::LOGGING_WARNING, \
391 ##__VA_ARGS__)
392#define COMPACT_GOOGLE_LOG_EX_ERROR(ClassName, ...) \
393 ::logging::ClassName(__FILE__, __LINE__, ::logging::LOGGING_ERROR, \
394 ##__VA_ARGS__)
Peter Boströmea64595a2024-01-12 14:57:48395#define COMPACT_GOOGLE_LOG_EX_FATAL(ClassName, ...) \
396 ::logging::ClassName##Fatal(__FILE__, __LINE__, ::logging::LOGGING_FATAL, \
397 ##__VA_ARGS__)
Lei Zhang93dd42572020-10-23 18:45:53398#define COMPACT_GOOGLE_LOG_EX_DFATAL(ClassName, ...) \
399 ::logging::ClassName(__FILE__, __LINE__, ::logging::LOGGING_DFATAL, \
400 ##__VA_ARGS__)
[email protected]d8617a62009-10-09 23:52:20401
Wez289477f2017-08-24 20:51:30402#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)
initial.commitd7cae122008-07-26 21:49:38407
Xiaohan Wang38e4ebb2022-01-19 06:57:43408#if BUILDFLAG(IS_WIN)
initial.commitd7cae122008-07-26 21:49:38409// wingdi.h defines ERROR to be 0. When we call LOG(ERROR), it gets
410// substituted with 0, and it expands to COMPACT_GOOGLE_LOG_0. To allow us
411// to keep using this syntax, we define this macro to do the same thing
412// as COMPACT_GOOGLE_LOG_ERROR, and also define ERROR the same way that
413// the Windows SDK does for consistency.
414#define ERROR 0
[email protected]d8617a62009-10-09 23:52:20415#define COMPACT_GOOGLE_LOG_EX_0(ClassName, ...) \
Peter Kasting134ef9af2024-12-28 02:30:09416 COMPACT_GOOGLE_LOG_EX_ERROR(ClassName, ##__VA_ARGS__)
[email protected]d8617a62009-10-09 23:52:20417#define COMPACT_GOOGLE_LOG_0 COMPACT_GOOGLE_LOG_ERROR
[email protected]521b0c42010-10-01 23:02:36418// Needed for LOG_IS_ON(ERROR).
Lei Zhang4d9e18572021-04-30 08:57:06419constexpr LogSeverity LOGGING_0 = LOGGING_ERROR;
[email protected]8d127302013-01-10 02:41:57420#endif
[email protected]521b0c42010-10-01 23:02:36421
[email protected]f2c05492014-06-17 12:04:23422// As special cases, we can assume that LOG_IS_ON(FATAL) always holds. Also,
423// LOG_IS_ON(DFATAL) always holds in debug mode. In particular, CHECK()s will
424// always fire if they fail.
Peter Boströmd9f666872024-01-17 00:00:20425// FATAL is always enabled and required to be resolved in compile time for
426// LOG(FATAL) to be properly understood as [[noreturn]].
427#define LOG_IS_ON(severity) \
428 (::logging::LOGGING_##severity == ::logging::LOGGING_FATAL || \
429 ::logging::ShouldCreateLogMessage(::logging::LOGGING_##severity))
[email protected]521b0c42010-10-01 23:02:36430
Xiyuan Xia28d809d2023-11-02 22:00:42431// Define a default ENABLED_VLOG_LEVEL if it is not defined. The macros allows
432// code to enable vlog level at build time without the need of --vmodule
433// switch at runtime. This is intended for VLOGs that needed from production
434// code without the cpu overhead to match vmodule patterns on every VLOG
435// instance.
Xiyuan Xiaa0559da2022-05-05 19:42:45436#if !defined(ENABLED_VLOG_LEVEL)
Xiyuan Xia28d809d2023-11-02 22:00:42437#define ENABLED_VLOG_LEVEL -1
Xiyuan Xiaa0559da2022-05-05 19:42:45438#endif // !defined(ENABLED_VLOG_LEVEL)
439
Ken MacKay70e8867002019-01-16 00:22:15440// We don't do any caching tricks with VLOG_IS_ON() like the
441// google-glog version since it increases binary size. This means
[email protected]521b0c42010-10-01 23:02:36442// that using the v-logging functions in conjunction with --vmodule
443// may be slow.
Xiyuan Xia28d809d2023-11-02 22:00:42444#define VLOG_IS_ON(verboselevel) \
445 ((verboselevel) <= (ENABLED_VLOG_LEVEL) || \
446 (verboselevel) <= ::logging::GetVlogLevel(__FILE__))
Xiyuan Xiaa0559da2022-05-05 19:42:45447
[email protected]521b0c42010-10-01 23:02:36448// Helper macro which avoids evaluating the arguments to a stream if
chcunninghamf6a96082015-02-07 01:58:37449// the condition doesn't hold. Condition is evaluated once and only once.
Peter Kasting134ef9af2024-12-28 02:30:09450#define LAZY_STREAM(stream, condition) \
451 !(condition) ? (void)0 : ::logging::LogMessageVoidify() & (stream)
initial.commitd7cae122008-07-26 21:49:38452
453// We use the preprocessor's merging operator, "##", so that, e.g.,
454// LOG(INFO) becomes the token COMPACT_GOOGLE_LOG_INFO. There's some funny
455// subtle difference between ostream member streaming functions (e.g.,
456// ostream::operator<<(int) and ostream non-member streaming functions
457// (e.g., ::operator<<(ostream&, string&): it turns out that it's
458// impossible to stream something like a string directly to an unnamed
459// ostream. We employ a neat hack by calling the stream() member
460// function of LogMessage which seems to avoid the problem.
Peter Kasting134ef9af2024-12-28 02:30:09461#define LOG_STREAM(severity) COMPACT_GOOGLE_LOG_##severity.stream()
initial.commitd7cae122008-07-26 21:49:38462
[email protected]521b0c42010-10-01 23:02:36463#define LOG(severity) LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity))
464#define LOG_IF(severity, condition) \
465 LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity) && (condition))
466
[email protected]162ac0f2010-11-04 15:50:49467// The VLOG macros log with negative verbosities.
468#define VLOG_STREAM(verbose_level) \
Artem Bolgar30e5d692020-12-12 01:15:58469 ::logging::LogMessage(__FILE__, __LINE__, -(verbose_level)).stream()
[email protected]162ac0f2010-11-04 15:50:49470
471#define VLOG(verbose_level) \
472 LAZY_STREAM(VLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level))
473
474#define VLOG_IF(verbose_level, condition) \
475 LAZY_STREAM(VLOG_STREAM(verbose_level), \
Peter Kasting134ef9af2024-12-28 02:30:09476 VLOG_IS_ON(verbose_level) && (condition))
[email protected]99b7c57f2010-09-29 19:26:36477
Peter Boström3ed27e82022-11-17 01:13:23478#if BUILDFLAG(IS_WIN)
Peter Kasting134ef9af2024-12-28 02:30:09479#define VPLOG_STREAM(verbose_level) \
Artem Bolgar30e5d692020-12-12 01:15:58480 ::logging::Win32ErrorLogMessage(__FILE__, __LINE__, -(verbose_level), \
Peter Kasting134ef9af2024-12-28 02:30:09481 ::logging::GetLastSystemErrorCode()) \
482 .stream()
Xiaohan Wang38e4ebb2022-01-19 06:57:43483#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
Peter Kasting134ef9af2024-12-28 02:30:09484#define VPLOG_STREAM(verbose_level) \
Artem Bolgar30e5d692020-12-12 01:15:58485 ::logging::ErrnoLogMessage(__FILE__, __LINE__, -(verbose_level), \
Peter Kasting134ef9af2024-12-28 02:30:09486 ::logging::GetLastSystemErrorCode()) \
487 .stream()
[email protected]fb879b1a2011-03-06 18:16:31488#endif
489
490#define VPLOG(verbose_level) \
491 LAZY_STREAM(VPLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level))
492
493#define VPLOG_IF(verbose_level, condition) \
494 LAZY_STREAM(VPLOG_STREAM(verbose_level), \
Peter Kasting134ef9af2024-12-28 02:30:09495 VLOG_IS_ON(verbose_level) && (condition))
[email protected]fb879b1a2011-03-06 18:16:31496
[email protected]99b7c57f2010-09-29 19:26:36497// TODO(akalin): Add more VLOG variants, e.g. VPLOG.
initial.commitd7cae122008-07-26 21:49:38498
kmarshallfe2f09f82017-04-20 21:05:26499#define LOG_ASSERT(condition) \
500 LOG_IF(FATAL, !(ANALYZER_ASSUME_TRUE(condition))) \
501 << "Assert failed: " #condition ". "
initial.commitd7cae122008-07-26 21:49:38502
Xiaohan Wang38e4ebb2022-01-19 06:57:43503#if BUILDFLAG(IS_WIN)
Peter Kasting134ef9af2024-12-28 02:30:09504#define PLOG_STREAM(severity) \
505 COMPACT_GOOGLE_LOG_EX_##severity(Win32ErrorLogMessage, \
506 ::logging::GetLastSystemErrorCode()) \
507 .stream()
Xiaohan Wang38e4ebb2022-01-19 06:57:43508#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
Peter Kasting134ef9af2024-12-28 02:30:09509#define PLOG_STREAM(severity) \
510 COMPACT_GOOGLE_LOG_EX_##severity(ErrnoLogMessage, \
511 ::logging::GetLastSystemErrorCode()) \
512 .stream()
[email protected]d8617a62009-10-09 23:52:20513#endif
514
Peter Kasting134ef9af2024-12-28 02:30:09515#define PLOG(severity) LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity))
[email protected]521b0c42010-10-01 23:02:36516
[email protected]d8617a62009-10-09 23:52:20517#define PLOG_IF(severity, condition) \
[email protected]521b0c42010-10-01 23:02:36518 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity) && (condition))
[email protected]d8617a62009-10-09 23:52:20519
scottmg3c957a52016-12-10 20:57:59520BASE_EXPORT extern std::ostream* g_swallow_stream;
521
522// Note that g_swallow_stream is used instead of an arbitrary LOG() stream to
523// avoid the creation of an object with a non-trivial destructor (LogMessage).
524// On MSVC x86 (checked on 2015 Update 3), this causes a few additional
525// pointless instructions to be emitted even at full optimization level, even
526// though the : arm of the ternary operator is clearly never executed. Using a
527// simpler object to be &'d with Voidify() avoids these extra instructions.
528// Using a simpler POD object with a templated operator<< also works to avoid
529// these instructions. However, this causes warnings on statically defined
530// implementations of operator<<(std::ostream, ...) in some .cc files, because
531// they become defined-but-unreferenced functions. A reinterpret_cast of 0 to an
532// ostream* also is not suitable, because some compilers warn of undefined
533// behavior.
534#define EAT_STREAM_PARAMETERS \
535 true ? (void)0 \
536 : ::logging::LogMessageVoidify() & (*::logging::g_swallow_stream)
[email protected]ddb9b332011-12-02 07:31:09537
[email protected]d15e56c2010-09-30 21:12:33538// Definitions for DLOG et al.
539
gab190f7542016-08-01 20:03:41540#if DCHECK_IS_ON()
[email protected]d926c202010-10-01 02:58:24541
Peter Boström69d78ca2024-01-22 22:07:31542// All of these definitions use DLOG_IS_ON() rather than define to their LOG()
543// equivalents, as DLOG(FATAL) and friends can't be understood as [[noreturn]]
544// but LOG(FATAL) is.
Peter Boströmd9f666872024-01-17 00:00:20545#define DLOG_IS_ON(severity) \
546 (::logging::ShouldCreateLogMessage(::logging::LOGGING_##severity))
547
Peter Boström69d78ca2024-01-22 22:07:31548#define DLOG(severity) LAZY_STREAM(LOG_STREAM(severity), DLOG_IS_ON(severity))
549#define DLOG_IF(severity, condition) \
550 LAZY_STREAM(LOG_STREAM(severity), DLOG_IS_ON(severity) && (condition))
551#define DPLOG(severity) LAZY_STREAM(PLOG_STREAM(severity), DLOG_IS_ON(severity))
552#define DPLOG_IF(severity, condition) \
553 LAZY_STREAM(PLOG_STREAM(severity), DLOG_IS_ON(severity) && (condition))
[email protected]521b0c42010-10-01 23:02:36554#define DVLOG_IF(verboselevel, condition) VLOG_IF(verboselevel, condition)
[email protected]fb879b1a2011-03-06 18:16:31555#define DVPLOG_IF(verboselevel, condition) VPLOG_IF(verboselevel, condition)
Peter Boström69d78ca2024-01-22 22:07:31556#define DLOG_ASSERT(condition) \
557 DLOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". "
[email protected]d926c202010-10-01 02:58:24558
gab190f7542016-08-01 20:03:41559#else // DCHECK_IS_ON()
[email protected]d926c202010-10-01 02:58:24560
gab190f7542016-08-01 20:03:41561// If !DCHECK_IS_ON(), we want to avoid emitting any references to |condition|
562// (which may reference a variable defined only if DCHECK_IS_ON()).
563// Contrast this with DCHECK et al., which has different behavior.
[email protected]d926c202010-10-01 02:58:24564
[email protected]5e987802010-11-01 19:49:22565#define DLOG_IS_ON(severity) false
Peter Boström69d78ca2024-01-22 22:07:31566#define DLOG(severity) EAT_STREAM_PARAMETERS
[email protected]ddb9b332011-12-02 07:31:09567#define DLOG_IF(severity, condition) EAT_STREAM_PARAMETERS
Peter Boström69d78ca2024-01-22 22:07:31568#define DPLOG(severity) EAT_STREAM_PARAMETERS
[email protected]ddb9b332011-12-02 07:31:09569#define DPLOG_IF(severity, condition) EAT_STREAM_PARAMETERS
570#define DVLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS
571#define DVPLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS
Peter Boström69d78ca2024-01-22 22:07:31572#define DLOG_ASSERT(condition) EAT_STREAM_PARAMETERS
[email protected]d926c202010-10-01 02:58:24573
gab190f7542016-08-01 20:03:41574#endif // DCHECK_IS_ON()
[email protected]d926c202010-10-01 02:58:24575
Ken MacKay70e8867002019-01-16 00:22:15576#define DVLOG(verboselevel) DVLOG_IF(verboselevel, true)
Ken MacKay70e8867002019-01-16 00:22:15577#define DVPLOG(verboselevel) DVPLOG_IF(verboselevel, true)
[email protected]fb879b1a2011-03-06 18:16:31578
[email protected]521b0c42010-10-01 23:02:36579// Definitions for DCHECK et al.
[email protected]d926c202010-10-01 02:58:24580
Peter Boström29c761792024-01-18 23:05:29581// TODO(pbos): Move this to check.h. Probably find a better name. Maybe this
582// means that we want LogSeverity in a separate file, but maybe we can just have
583// this as a bool DCHECK_IS_FATAL.
Wez02cedeba2022-07-26 12:48:38584#if BUILDFLAG(DCHECK_IS_CONFIGURABLE)
Lei Zhang93dd42572020-10-23 18:45:53585BASE_EXPORT extern LogSeverity LOGGING_DCHECK;
Wez289477f2017-08-24 20:51:30586#else
Lei Zhang4d9e18572021-04-30 08:57:06587constexpr LogSeverity LOGGING_DCHECK = LOGGING_FATAL;
Wez02cedeba2022-07-26 12:48:38588#endif // BUILDFLAG(DCHECK_IS_CONFIGURABLE)
[email protected]521b0c42010-10-01 23:02:36589
initial.commitd7cae122008-07-26 21:49:38590// Redefine the standard assert to use our nice log files
591#undef assert
592#define assert(x) DLOG_ASSERT(x)
593
594// This class more or less represents a particular log message. You
595// create an instance of LogMessage and then stream stuff to it.
596// When you finish streaming to it, ~LogMessage is called and the
597// full message gets streamed to the appropriate destination.
598//
599// You shouldn't actually use LogMessage's constructor to log things,
600// though. You should use the LOG() macro (and variants thereof)
601// above.
[email protected]0bea7252011-08-05 15:34:00602class BASE_EXPORT LogMessage {
initial.commitd7cae122008-07-26 21:49:38603 public:
initial.commitd7cae122008-07-26 21:49:38604 LogMessage(const char* file, int line, LogSeverity severity);
605
David Bienvenub4b441e2020-09-23 05:49:57606 LogMessage(const LogMessage&) = delete;
Collin Baker0162bae2025-04-03 18:01:40607 LogMessage& operator=(const LogMessage&) = delete;
Hans Wennborg12aea3e2020-04-14 15:29:00608 virtual ~LogMessage();
initial.commitd7cae122008-07-26 21:49:38609
610 std::ostream& stream() { return stream_; }
611
Peter Boström6c0094d12022-07-07 16:03:39612 LogSeverity severity() const { return severity_; }
613 std::string str() const { return stream_.str(); }
Collin Baker0162bae2025-04-03 18:01:40614 const char* file() const { return file_; }
Peter Boström1d3b6d82022-07-11 17:59:49615 int line() const { return line_; }
Peter Boström6c0094d12022-07-07 16:03:39616
Peter Boström37482962022-07-14 16:09:54617 // Gets file:line: message in a format suitable for crash reporting.
618 std::string BuildCrashString() const;
pastarmovj89f7ee12016-09-20 14:58:13619
Peter Boström835240922024-01-10 18:39:51620 protected:
621 void Flush();
622
initial.commitd7cae122008-07-26 21:49:38623 private:
Collin Baker0162bae2025-04-03 18:01:40624 void Init(const char* file, int line);
initial.commitd7cae122008-07-26 21:49:38625
Peter Boström4aa155cd2023-12-21 00:36:44626 void HandleFatal(size_t stack_start, const std::string& str_newline) const;
627
David Dorwin11e7c2c12021-04-10 17:01:09628 const LogSeverity severity_;
initial.commitd7cae122008-07-26 21:49:38629 std::ostringstream stream_;
[email protected]c88873922008-07-30 13:02:03630 size_t message_start_; // Offset of the start of the message (past prefix
631 // info).
[email protected]162ac0f2010-11-04 15:50:49632 // The file and line information passed in to the constructor.
Collin Baker0162bae2025-04-03 18:01:40633 const char* const file_;
[email protected]162ac0f2010-11-04 15:50:49634 const int line_;
635
Georg Neisffe34f652021-12-27 21:42:36636#if BUILDFLAG(IS_CHROMEOS)
Aquibuzzaman Md. Sayem42abceb72024-05-08 18:48:27637 void InitWithSyslogPrefix(std::string_view filename,
Yuta Hijikata9b7279a2020-08-26 16:10:54638 int line,
639 uint64_t tick_count,
640 const char* log_severity_name_c_str,
641 const char* log_prefix,
642 bool enable_process_id,
643 bool enable_thread_id,
644 bool enable_timestamp,
645 bool enable_tickcount);
646#endif
initial.commitd7cae122008-07-26 21:49:38647};
648
Peter Boströmea64595a2024-01-12 14:57:48649class BASE_EXPORT LogMessageFatal final : public LogMessage {
650 public:
651 using LogMessage::LogMessage;
652 [[noreturn]] ~LogMessageFatal() override;
653};
654
initial.commitd7cae122008-07-26 21:49:38655// This class is used to explicitly ignore values in the conditional
656// logging macros. This avoids compiler warnings like "value computed
657// is not used" and "statement has no effect".
[email protected]23bb71f2011-04-21 22:22:10658class LogMessageVoidify {
initial.commitd7cae122008-07-26 21:49:38659 public:
Chris Watkins091d6292017-12-13 04:25:58660 LogMessageVoidify() = default;
initial.commitd7cae122008-07-26 21:49:38661 // This has to be an operator with a precedence lower than << but
662 // higher than ?:
Peter Kasting134ef9af2024-12-28 02:30:09663 void operator&(std::ostream&) {}
initial.commitd7cae122008-07-26 21:49:38664};
665
Xiaohan Wang38e4ebb2022-01-19 06:57:43666#if BUILDFLAG(IS_WIN)
[email protected]d8617a62009-10-09 23:52:20667typedef unsigned long SystemErrorCode;
Xiaohan Wang38e4ebb2022-01-19 06:57:43668#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
[email protected]d8617a62009-10-09 23:52:20669typedef int SystemErrorCode;
670#endif
671
672// Alias for ::GetLastError() on Windows and errno on POSIX. Avoids having to
673// pull in windows.h just for GetLastError() and DWORD.
[email protected]0bea7252011-08-05 15:34:00674BASE_EXPORT SystemErrorCode GetLastSystemErrorCode();
[email protected]c914d8a2014-04-23 01:11:01675BASE_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code);
[email protected]d8617a62009-10-09 23:52:20676
Xiaohan Wang38e4ebb2022-01-19 06:57:43677#if BUILDFLAG(IS_WIN)
[email protected]d8617a62009-10-09 23:52:20678// Appends a formatted system message of the GetLastError() type.
Hans Wennborg12aea3e2020-04-14 15:29:00679class BASE_EXPORT Win32ErrorLogMessage : public LogMessage {
[email protected]d8617a62009-10-09 23:52:20680 public:
Collin Baker0162bae2025-04-03 18:01:40681 Win32ErrorLogMessage(const char* file,
[email protected]d8617a62009-10-09 23:52:20682 int line,
683 LogSeverity severity,
[email protected]d8617a62009-10-09 23:52:20684 SystemErrorCode err);
David Bienvenub4b441e2020-09-23 05:49:57685 Win32ErrorLogMessage(const Win32ErrorLogMessage&) = delete;
686 Win32ErrorLogMessage& operator=(const Win32ErrorLogMessage&) = delete;
[email protected]d8617a62009-10-09 23:52:20687 // Appends the error message before destructing the encapsulated class.
Hans Wennborg12aea3e2020-04-14 15:29:00688 ~Win32ErrorLogMessage() override;
[email protected]a502bbe72011-01-07 18:06:45689
Peter Boströmea64595a2024-01-12 14:57:48690 protected:
691 void AppendError();
692
[email protected]d8617a62009-10-09 23:52:20693 private:
694 SystemErrorCode err_;
[email protected]d8617a62009-10-09 23:52:20695};
Peter Boströmea64595a2024-01-12 14:57:48696
697class BASE_EXPORT Win32ErrorLogMessageFatal final
698 : public Win32ErrorLogMessage {
699 public:
700 using Win32ErrorLogMessage::Win32ErrorLogMessage;
701 [[noreturn]] ~Win32ErrorLogMessageFatal() override;
702};
703
Xiaohan Wang38e4ebb2022-01-19 06:57:43704#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
[email protected]d8617a62009-10-09 23:52:20705// Appends a formatted system message of the errno type
Hans Wennborg12aea3e2020-04-14 15:29:00706class BASE_EXPORT ErrnoLogMessage : public LogMessage {
[email protected]d8617a62009-10-09 23:52:20707 public:
Collin Baker0162bae2025-04-03 18:01:40708 ErrnoLogMessage(const char* file,
[email protected]d8617a62009-10-09 23:52:20709 int line,
710 LogSeverity severity,
711 SystemErrorCode err);
David Bienvenub4b441e2020-09-23 05:49:57712 ErrnoLogMessage(const ErrnoLogMessage&) = delete;
713 ErrnoLogMessage& operator=(const ErrnoLogMessage&) = delete;
[email protected]d8617a62009-10-09 23:52:20714 // Appends the error message before destructing the encapsulated class.
Hans Wennborg12aea3e2020-04-14 15:29:00715 ~ErrnoLogMessage() override;
[email protected]a502bbe72011-01-07 18:06:45716
Peter Boströmea64595a2024-01-12 14:57:48717 protected:
718 void AppendError();
719
[email protected]d8617a62009-10-09 23:52:20720 private:
721 SystemErrorCode err_;
[email protected]d8617a62009-10-09 23:52:20722};
Peter Boströmea64595a2024-01-12 14:57:48723
724class BASE_EXPORT ErrnoLogMessageFatal final : public ErrnoLogMessage {
725 public:
726 using ErrnoLogMessage::ErrnoLogMessage;
727 [[noreturn]] ~ErrnoLogMessageFatal() override;
728};
729
Xiaohan Wang38e4ebb2022-01-19 06:57:43730#endif // BUILDFLAG(IS_WIN)
[email protected]d8617a62009-10-09 23:52:20731
initial.commitd7cae122008-07-26 21:49:38732// Closes the log file explicitly if open.
733// NOTE: Since the log file is opened as necessary by the action of logging
734// statements, there's no guarantee that it will stay closed
735// after this call.
[email protected]0bea7252011-08-05 15:34:00736BASE_EXPORT void CloseLogFile();
initial.commitd7cae122008-07-26 21:49:38737
Georg Neisff37fb52025-02-05 09:05:26738#if BUILDFLAG(IS_CHROMEOS)
Robbie McElrath8bf49842019-08-20 22:22:53739// Returns a new file handle that will write to the same destination as the
740// currently open log file. Returns nullptr if logging to a file is disabled,
741// or if opening the file failed. This is intended to be used to initialize
742// logging in child processes that are unable to open files.
743BASE_EXPORT FILE* DuplicateLogFILE();
744#endif
745
[email protected]e36ddc82009-12-08 04:22:50746// Async signal safe logging mechanism.
[email protected]0bea7252011-08-05 15:34:00747BASE_EXPORT void RawLog(int level, const char* message);
[email protected]e36ddc82009-12-08 04:22:50748
tsniatowski612550f2016-07-21 18:26:20749#define RAW_LOG(level, message) \
Lei Zhang93dd42572020-10-23 18:45:53750 ::logging::RawLog(::logging::LOGGING_##level, message)
[email protected]e36ddc82009-12-08 04:22:50751
Xiaohan Wang38e4ebb2022-01-19 06:57:43752#if BUILDFLAG(IS_WIN)
ananta61762fb2015-09-18 01:00:09753// Returns true if logging to file is enabled.
754BASE_EXPORT bool IsLoggingToFileEnabled();
755
[email protected]f01b88a2013-02-27 22:04:00756// Returns the default log file path.
Jan Wilken Dörrieb630aca2019-12-04 10:59:11757BASE_EXPORT std::wstring GetLogFileFullPath();
Alex Gough9d2b795f2024-03-18 18:51:53758
759// Duplicates the log file handle to send into a child process.
760BASE_EXPORT HANDLE DuplicateLogFileHandle();
[email protected]f01b88a2013-02-27 22:04:00761#endif
762
[email protected]39be4242008-08-07 18:31:40763} // namespace logging
initial.commitd7cae122008-07-26 21:49:38764
[email protected]39be4242008-08-07 18:31:40765#endif // BASE_LOGGING_H_