[go: nahoru, domu]

blob: 04b915cde2257df48858fb8f7856876c6273531d [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
brettw@google.com39be4242008-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
tzik@chromium.orge7972d12011-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
darin@chromium.org0bea7252011-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"
Avi Drissman63e1f992023-01-13 18:54:4319#include "base/functional/callback_forward.h"
Etienne Pierre-Dorayd120ebf2018-09-14 23:38:2120#include "base/scoped_clear_last_error.h"
Lei Zhangc5f765d52023-11-08 00:53:3321#include "base/strings/string_piece.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"
Yuta Hijikata000df18f2020-11-18 06:55:5824#include "build/chromeos_buildflags.h"
initial.commitd7cae122008-07-26 21:49:3825
Georg Neisffe34f652021-12-27 21:42:3626#if BUILDFLAG(IS_CHROMEOS)
Robbie McElrath8bf49842019-08-20 22:22:5327#include <cstdio>
28#endif
29
initial.commitd7cae122008-07-26 21:49:3830//
31// Optional message capabilities
32// -----------------------------
33// Assertion failed messages and fatal errors are displayed in a dialog box
34// before the application exits. However, running this UI creates a message
35// loop, which causes application messages to be processed and potentially
36// dispatched to existing application windows. Since the application is in a
37// bad state when this assertion dialog is displayed, these messages may not
38// get processed and hang the dialog, or the application might go crazy.
39//
40// Therefore, it can be beneficial to display the error dialog in a separate
41// process from the main application. When the logging system needs to display
42// a fatal error dialog box, it will look for a program called
43// "DebugMessage.exe" in the same directory as the application executable. It
44// will run this application with the message as the command line, and will
45// not include the name of the application as is traditional for easier
46// parsing.
47//
48// The code for DebugMessage.exe is only one line. In WinMain, do:
49// MessageBox(NULL, GetCommandLineW(), L"Fatal Error", 0);
50//
51// If DebugMessage.exe is not found, the logging code will use a normal
52// MessageBox, potentially causing the problems discussed above.
53
initial.commitd7cae122008-07-26 21:49:3854// Instructions
55// ------------
56//
57// Make a bunch of macros for logging. The way to log things is to stream
58// things to LOG(<a particular severity level>). E.g.,
59//
60// LOG(INFO) << "Found " << num_cookies << " cookies";
61//
62// You can also do conditional logging:
63//
64// LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
65//
initial.commitd7cae122008-07-26 21:49:3866// The CHECK(condition) macro is active in both debug and release builds and
67// effectively performs a LOG(FATAL) which terminates the process and
68// generates a crashdump unless a debugger is attached.
69//
70// There are also "debug mode" logging macros like the ones above:
71//
72// DLOG(INFO) << "Found cookies";
73//
74// DLOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
75//
76// All "debug mode" logging is compiled away to nothing for non-debug mode
77// compiles. LOG_IF and development flags also work well together
78// because the code can be compiled away sometimes.
79//
80// We also have
81//
82// LOG_ASSERT(assertion);
83// DLOG_ASSERT(assertion);
84//
85// which is syntactic sugar for {,D}LOG_IF(FATAL, assert fails) << assertion;
86//
akalin@chromium.org99b7c57f2010-09-29 19:26:3687// There are "verbose level" logging macros. They look like
88//
89// VLOG(1) << "I'm printed when you run the program with --v=1 or more";
90// VLOG(2) << "I'm printed when you run the program with --v=2 or more";
91//
92// These always log at the INFO log level (when they log at all).
Xiyuan Xiaa0559da2022-05-05 19:42:4593//
Xiyuan Xia28d809d2023-11-02 22:00:4294// The verbose logging can also be turned on module-by-module. For instance,
akalin@chromium.orgb0d38d42010-10-29 00:39:4895// --vmodule=profile=2,icon_loader=1,browser_*=3,*/chromeos/*=4 --v=0
akalin@chromium.org99b7c57f2010-09-29 19:26:3696// will cause:
97// a. VLOG(2) and lower messages to be printed from profile.{h,cc}
98// b. VLOG(1) and lower messages to be printed from icon_loader.{h,cc}
99// c. VLOG(3) and lower messages to be printed from files prefixed with
100// "browser"
akalin@chromium.orge11de722010-11-01 20:50:55101// d. VLOG(4) and lower messages to be printed from files under a
akalin@chromium.orgb0d38d42010-10-29 00:39:48102// "chromeos" directory.
akalin@chromium.orge11de722010-11-01 20:50:55103// e. VLOG(0) and lower messages to be printed from elsewhere
akalin@chromium.org99b7c57f2010-09-29 19:26:36104//
105// The wildcarding functionality shown by (c) supports both '*' (match
akalin@chromium.orgb0d38d42010-10-29 00:39:48106// 0 or more characters) and '?' (match any single character)
107// wildcards. Any pattern containing a forward or backward slash will
108// be tested against the whole pathname and not just the module.
109// E.g., "*/foo/bar/*=2" would change the logging level for all code
110// in source files under a "foo/bar" directory.
akalin@chromium.org99b7c57f2010-09-29 19:26:36111//
Mason Freed14240d162020-08-12 13:06:34112// Note that for a Chromium binary built in release mode (is_debug = false) you
113// must pass "--enable-logging=stderr" in order to see the output of VLOG
114// statements.
115//
akalin@chromium.org99b7c57f2010-09-29 19:26:36116// There's also VLOG_IS_ON(n) "verbose level" condition macro. To be used as
117//
118// if (VLOG_IS_ON(2)) {
119// // do some logging preparation and logging
120// // that can't be accomplished with just VLOG(2) << ...;
121// }
122//
123// There is also a VLOG_IF "verbose level" condition macro for sample
124// cases, when some extra computation and preparation for logs is not
125// needed.
126//
127// VLOG_IF(1, (size > 1024))
128// << "I'm printed when size is more than 1024 and when you run the "
129// "program with --v=1 or more";
130//
initial.commitd7cae122008-07-26 21:49:38131// We also override the standard 'assert' to use 'DLOG_ASSERT'.
132//
tschmelcher@chromium.orgd8617a62009-10-09 23:52:20133// Lastly, there is:
134//
135// PLOG(ERROR) << "Couldn't do foo";
136// DPLOG(ERROR) << "Couldn't do foo";
137// PLOG_IF(ERROR, cond) << "Couldn't do foo";
138// DPLOG_IF(ERROR, cond) << "Couldn't do foo";
139// PCHECK(condition) << "Couldn't do foo";
140// DPCHECK(condition) << "Couldn't do foo";
141//
142// which append the last system error to the message in string form (taken from
143// GetLastError() on Windows and errno on POSIX).
144//
initial.commitd7cae122008-07-26 21:49:38145// The supported severity levels for macros that allow you to specify one
viettrungluu@chromium.orgf2c05492014-06-17 12:04:23146// are (in increasing order of severity) INFO, WARNING, ERROR, and FATAL.
initial.commitd7cae122008-07-26 21:49:38147//
148// Very important: logging a message at the FATAL severity level causes
149// the program to terminate (after the message is logged).
huanr@chromium.orgfb62a532009-02-12 01:19:05150//
danakjf8e9c302021-01-27 21:37:23151// There is the special severity of DFATAL, which logs FATAL in DCHECK-enabled
152// builds, ERROR in normal mode.
Rob Schonberger45637212018-12-03 04:46:25153//
Yuta Hijikata9b7279a2020-08-26 16:10:54154// Output is formatted as per the following example, except on Chrome OS.
Rob Schonberger45637212018-12-03 04:46:25155// [3816:3877:0812/234555.406952:VERBOSE1:drm_device_handle.cc(90)] Succeeded
156// authenticating /dev/dri/card0 in 0 ms with 1 attempt(s)
157//
158// The colon separated fields inside the brackets are as follows:
159// 0. An optional Logfile prefix (not included in this example)
160// 1. Process ID
161// 2. Thread ID
162// 3. The date/time of the log message, in MMDD/HHMMSS.Milliseconds format
163// 4. The log level
164// 5. The filename and line number where the log was instantiated
165//
Yuta Hijikata9b7279a2020-08-26 16:10:54166// Output for Chrome OS can be switched to syslog-like format. See
Georg Neisffe34f652021-12-27 21:42:36167// InitWithSyslogPrefix() in logging_chromeos.cc for details.
Yuta Hijikata9b7279a2020-08-26 16:10:54168//
Rob Schonberger45637212018-12-03 04:46:25169// Note that the visibility can be changed by setting preferences in
170// SetLogItems()
Mason Freed14240d162020-08-12 13:06:34171//
172// Additional logging-related information can be found here:
John Palmerd29fc4362021-05-20 03:29:22173// https://chromium.googlesource.com/chromium/src/+/main/docs/linux/debugging.md#Logging
initial.commitd7cae122008-07-26 21:49:38174
175namespace logging {
176
akalin@chromium.org5e3f7c22013-06-21 21:15:33177// TODO(avi): do we want to do a unification of character types here?
Xiaohan Wang38e4ebb2022-01-19 06:57:43178#if BUILDFLAG(IS_WIN)
Jan Wilken Dörrieb630aca2019-12-04 10:59:11179typedef wchar_t PathChar;
Xiaohan Wang38e4ebb2022-01-19 06:57:43180#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
akalin@chromium.org5e3f7c22013-06-21 21:15:33181typedef char PathChar;
182#endif
183
Sharon Yang7cb919a2019-05-20 20:27:15184// A bitmask of potential logging destinations.
185using LoggingDestination = uint32_t;
186// Specifies where logs will be written. Multiple destinations can be specified
187// with bitwise OR.
188// Unless destination is LOG_NONE, all logs with severity ERROR and above will
189// be written to stderr in addition to the specified destination.
190enum : uint32_t {
Xiaohan Wang38e4ebb2022-01-19 06:57:43191 LOG_NONE = 0,
192 LOG_TO_FILE = 1 << 0,
akalin@chromium.org5e3f7c22013-06-21 21:15:33193 LOG_TO_SYSTEM_DEBUG_LOG = 1 << 1,
Xiaohan Wang38e4ebb2022-01-19 06:57:43194 LOG_TO_STDERR = 1 << 2,
akalin@chromium.org5e3f7c22013-06-21 21:15:33195
Sharon Yang7cb919a2019-05-20 20:27:15196 LOG_TO_ALL = LOG_TO_FILE | LOG_TO_SYSTEM_DEBUG_LOG | LOG_TO_STDERR,
akalin@chromium.org5e3f7c22013-06-21 21:15:33197
Sharon Yang7cb919a2019-05-20 20:27:15198// On Windows, use a file next to the exe.
199// On POSIX platforms, where it may not even be possible to locate the
200// executable on disk, use stderr.
201// On Fuchsia, use the Fuchsia logging service.
Xiaohan Wang38e4ebb2022-01-19 06:57:43202#if BUILDFLAG(IS_FUCHSIA) || BUILDFLAG(IS_NACL)
akalin@chromium.org5e3f7c22013-06-21 21:15:33203 LOG_DEFAULT = LOG_TO_SYSTEM_DEBUG_LOG,
Xiaohan Wang38e4ebb2022-01-19 06:57:43204#elif BUILDFLAG(IS_WIN)
Sharon Yang7cb919a2019-05-20 20:27:15205 LOG_DEFAULT = LOG_TO_FILE,
Xiaohan Wang38e4ebb2022-01-19 06:57:43206#elif BUILDFLAG(IS_POSIX)
Sharon Yang7cb919a2019-05-20 20:27:15207 LOG_DEFAULT = LOG_TO_SYSTEM_DEBUG_LOG | LOG_TO_STDERR,
akalin@chromium.org5e3f7c22013-06-21 21:15:33208#endif
209};
initial.commitd7cae122008-07-26 21:49:38210
211// Indicates that the log file should be locked when being written to.
akalin@chromium.org5e3f7c22013-06-21 21:15:33212// Unless there is only one single-threaded process that is logging to
213// the log file, the file should be locked during writes to make each
wangxianzhu@chromium.org3ee50d12014-03-05 01:43:27214// log output atomic. Other writers will block.
initial.commitd7cae122008-07-26 21:49:38215//
216// All processes writing to the log file must have their locking set for it to
akalin@chromium.org5e3f7c22013-06-21 21:15:33217// work properly. Defaults to LOCK_LOG_FILE.
initial.commitd7cae122008-07-26 21:49:38218enum LogLockingState { LOCK_LOG_FILE, DONT_LOCK_LOG_FILE };
219
220// On startup, should we delete or append to an existing log file (if any)?
221// Defaults to APPEND_TO_OLD_LOG_FILE.
222enum OldFileDeletionState { DELETE_OLD_LOG_FILE, APPEND_TO_OLD_LOG_FILE };
223
Georg Neisffe34f652021-12-27 21:42:36224#if BUILDFLAG(IS_CHROMEOS)
Yuta Hijikata1fc8f6342020-09-01 03:25:56225// Defines the log message prefix format to use.
226// LOG_FORMAT_SYSLOG indicates syslog-like message prefixes.
227// LOG_FORMAT_CHROME indicates the normal Chrome format.
Yuta Hijikata9b7279a2020-08-26 16:10:54228enum class BASE_EXPORT LogFormat { LOG_FORMAT_CHROME, LOG_FORMAT_SYSLOG };
229#endif
230
akalin@chromium.org5e3f7c22013-06-21 21:15:33231struct BASE_EXPORT LoggingSettings {
Sharon Yang7cb919a2019-05-20 20:27:15232 // Equivalent to logging destination enum, but allows for multiple
233 // destinations.
Wez7e125622019-05-29 22:11:28234 uint32_t logging_dest = LOG_DEFAULT;
akalin@chromium.org5e3f7c22013-06-21 21:15:33235
Robbie McElrath8bf49842019-08-20 22:22:53236 // The four settings below have an effect only when LOG_TO_FILE is
akalin@chromium.org5e3f7c22013-06-21 21:15:33237 // set in |logging_dest|.
Robbie McElrath8bf49842019-08-20 22:22:53238 const PathChar* log_file_path = nullptr;
Wez7e125622019-05-29 22:11:28239 LogLockingState lock_log = LOCK_LOG_FILE;
240 OldFileDeletionState delete_old = APPEND_TO_OLD_LOG_FILE;
Georg Neisffe34f652021-12-27 21:42:36241#if BUILDFLAG(IS_CHROMEOS)
Robbie McElrath8bf49842019-08-20 22:22:53242 // Contains an optional file that logs should be written to. If present,
243 // |log_file_path| will be ignored, and the logging system will take ownership
244 // of the FILE. If there's an error writing to this file, no fallback paths
245 // will be opened.
246 FILE* log_file = nullptr;
Yuta Hijikata1fc8f6342020-09-01 03:25:56247 // ChromeOS uses the syslog log format by default.
248 LogFormat log_format = LogFormat::LOG_FORMAT_SYSLOG;
Robbie McElrath8bf49842019-08-20 22:22:53249#endif
akalin@chromium.org5e3f7c22013-06-21 21:15:33250};
derat@chromium.orgff3d0c32010-08-23 19:57:46251
252// Define different names for the BaseInitLoggingImpl() function depending on
253// whether NDEBUG is defined or not so that we'll fail to link if someone tries
254// to compile logging.cc with NDEBUG but includes logging.h without defining it,
255// or vice versa.
weza245bd072017-06-18 23:26:34256#if defined(NDEBUG)
derat@chromium.orgff3d0c32010-08-23 19:57:46257#define BaseInitLoggingImpl BaseInitLoggingImpl_built_with_NDEBUG
258#else
259#define BaseInitLoggingImpl BaseInitLoggingImpl_built_without_NDEBUG
260#endif
261
262// Implementation of the InitLogging() method declared below. We use a
263// more-specific name so we can #define it above without affecting other code
264// that has named stuff "InitLogging".
akalin@chromium.org5e3f7c22013-06-21 21:15:33265BASE_EXPORT bool BaseInitLoggingImpl(const LoggingSettings& settings);
derat@chromium.orgff3d0c32010-08-23 19:57:46266
initial.commitd7cae122008-07-26 21:49:38267// Sets the log file name and other global logging state. Calling this function
268// is recommended, and is normally done at the beginning of application init.
269// If you don't call it, all the flags will be initialized to their default
270// values, and there is a race condition that may leak a critical section
271// object if two threads try to do the first log at the same time.
272// See the definition of the enums above for descriptions and default values.
273//
274// The default log file is initialized to "debug.log" in the application
275// directory. You probably don't want this, especially since the program
276// directory may not be writable on an enduser's system.
stevenjb@chromium.org064aa162011-12-03 00:30:08277//
278// This function may be called a second time to re-direct logging (e.g after
279// loging in to a user partition), however it should never be called more than
280// twice.
akalin@chromium.org5e3f7c22013-06-21 21:15:33281inline bool InitLogging(const LoggingSettings& settings) {
282 return BaseInitLoggingImpl(settings);
derat@chromium.orgff3d0c32010-08-23 19:57:46283}
initial.commitd7cae122008-07-26 21:49:38284
285// Sets the log level. Anything at or above this level will be written to the
286// log file/displayed to the user (if applicable). Anything below this level
siggi@chromium.org162ac0f2010-11-04 15:50:49287// will be silently ignored. The log level defaults to 0 (everything is logged
288// up to level INFO) if this function is not called.
289// Note that log messages for VLOG(x) are logged at level -x, so setting
Fergal Dalycaf92ba2022-05-17 20:11:52290// the min log level to negative values enables verbose logging and conversely,
291// setting the VLOG default level will set this min level to a negative number,
292// effectively enabling all levels of logging.
darin@chromium.org0bea7252011-08-05 15:34:00293BASE_EXPORT void SetMinLogLevel(int level);
initial.commitd7cae122008-07-26 21:49:38294
ericroman@google.com8a2986ca2009-04-10 19:13:42295// Gets the current log level.
darin@chromium.org0bea7252011-08-05 15:34:00296BASE_EXPORT int GetMinLogLevel();
initial.commitd7cae122008-07-26 21:49:38297
skobesc78c0ad72015-12-07 20:21:23298// Used by LOG_IS_ON to lazy-evaluate stream arguments.
299BASE_EXPORT bool ShouldCreateLogMessage(int severity);
300
siggi@chromium.org162ac0f2010-11-04 15:50:49301// Gets the VLOG default verbosity level.
darin@chromium.org0bea7252011-08-05 15:34:00302BASE_EXPORT int GetVlogVerbosity();
siggi@chromium.org162ac0f2010-11-04 15:50:49303
akalin@chromium.org2f4e9a62010-09-29 21:25:14304// Note that |N| is the size *with* the null terminator.
darin@chromium.org0bea7252011-08-05 15:34:00305BASE_EXPORT int GetVlogLevelHelper(const char* file_start, size_t N);
akalin@chromium.org2f4e9a62010-09-29 21:25:14306
tnagel270da922017-05-24 12:10:44307// Gets the current vlog level for the given file (usually taken from __FILE__).
akalin@chromium.org99b7c57f2010-09-29 19:26:36308template <size_t N>
309int GetVlogLevel(const char (&file)[N]) {
310 return GetVlogLevelHelper(file, N);
311}
initial.commitd7cae122008-07-26 21:49:38312
313// Sets the common items you want to be prepended to each log message.
314// process and thread IDs default to off, the timestamp defaults to on.
315// If this function is not called, logging defaults to writing the timestamp
316// only.
darin@chromium.org0bea7252011-08-05 15:34:00317BASE_EXPORT void SetLogItems(bool enable_process_id, bool enable_thread_id,
318 bool enable_timestamp, bool enable_tickcount);
initial.commitd7cae122008-07-26 21:49:38319
James Cooka0536c32018-08-01 20:13:31320// Sets an optional prefix to add to each log message. |prefix| is not copied
321// and should be a raw string constant. |prefix| must only contain ASCII letters
322// to avoid confusion with PIDs and timestamps. Pass null to remove the prefix.
323// Logging defaults to no prefix.
324BASE_EXPORT void SetLogPrefix(const char* prefix);
325
cmasone@google.com81e0a852010-08-17 00:38:12326// Sets whether or not you'd like to see fatal debug messages popped up in
327// a dialog box or not.
328// Dialogs are not shown by default.
darin@chromium.org0bea7252011-08-05 15:34:00329BASE_EXPORT void SetShowErrorDialogs(bool enable_dialogs);
cmasone@google.com81e0a852010-08-17 00:38:12330
initial.commitd7cae122008-07-26 21:49:38331// Sets the Log Assert Handler that will be used to notify of check failures.
alex-accc1bde62017-04-19 08:33:55332// Resets Log Assert Handler on object destruction.
huanr@chromium.orgfb62a532009-02-12 01:19:05333// The default handler shows a dialog box and then terminate the process,
334// however clients can use this function to override with their own handling
335// (e.g. a silent one for Unit Tests)
alex-accc1bde62017-04-19 08:33:55336using LogAssertHandlerFunction =
kylechar83fb51e52019-03-14 15:30:43337 base::RepeatingCallback<void(const char* file,
338 int line,
339 const base::StringPiece message,
340 const base::StringPiece stack_trace)>;
alex-accc1bde62017-04-19 08:33:55341
342class BASE_EXPORT ScopedLogAssertHandler {
343 public:
344 explicit ScopedLogAssertHandler(LogAssertHandlerFunction handler);
David Bienvenub4b441e2020-09-23 05:49:57345 ScopedLogAssertHandler(const ScopedLogAssertHandler&) = delete;
346 ScopedLogAssertHandler& operator=(const ScopedLogAssertHandler&) = delete;
alex-accc1bde62017-04-19 08:33:55347 ~ScopedLogAssertHandler();
alex-accc1bde62017-04-19 08:33:55348};
hansl@google.com64e5cc02010-11-03 19:20:27349
siggi@chromium.org2b07b8412009-11-25 15:26:34350// Sets the Log Message Handler that gets passed every log message before
351// it's sent to other log destinations (if any).
352// Returns true to signal that it handled the message and the message
353// should not be sent to other log destinations.
siggi@chromium.org162ac0f2010-11-04 15:50:49354typedef bool (*LogMessageHandlerFunction)(int severity,
355 const char* file, int line, size_t message_start, const std::string& str);
darin@chromium.org0bea7252011-08-05 15:34:00356BASE_EXPORT void SetLogMessageHandler(LogMessageHandlerFunction handler);
357BASE_EXPORT LogMessageHandlerFunction GetLogMessageHandler();
siggi@chromium.org2b07b8412009-11-25 15:26:34358
Lei Zhang93dd42572020-10-23 18:45:53359using LogSeverity = int;
Lei Zhang4d9e18572021-04-30 08:57:06360constexpr LogSeverity LOGGING_VERBOSE = -1; // This is level 1 verbosity
siggi@chromium.org162ac0f2010-11-04 15:50:49361// Note: the log severities are used to index into the array of names,
362// see log_severity_names.
Lei Zhang4d9e18572021-04-30 08:57:06363constexpr LogSeverity LOGGING_INFO = 0;
364constexpr LogSeverity LOGGING_WARNING = 1;
365constexpr LogSeverity LOGGING_ERROR = 2;
366constexpr LogSeverity LOGGING_FATAL = 3;
367constexpr LogSeverity LOGGING_NUM_SEVERITIES = 4;
initial.commitd7cae122008-07-26 21:49:38368
danakjf8e9c302021-01-27 21:37:23369// LOGGING_DFATAL is LOGGING_FATAL in DCHECK-enabled builds, ERROR in normal
370// mode.
371#if DCHECK_IS_ON()
Lei Zhang4d9e18572021-04-30 08:57:06372constexpr LogSeverity LOGGING_DFATAL = LOGGING_FATAL;
danakjf8e9c302021-01-27 21:37:23373#else
Lei Zhang4d9e18572021-04-30 08:57:06374constexpr LogSeverity LOGGING_DFATAL = LOGGING_ERROR;
initial.commitd7cae122008-07-26 21:49:38375#endif
376
Lei Zhang93dd42572020-10-23 18:45:53377// This block duplicates the above entries to facilitate incremental conversion
378// from LOG_FOO to LOGGING_FOO.
379// TODO(thestig): Convert existing users to LOGGING_FOO and remove this block.
Lei Zhang4d9e18572021-04-30 08:57:06380constexpr LogSeverity LOG_VERBOSE = LOGGING_VERBOSE;
381constexpr LogSeverity LOG_INFO = LOGGING_INFO;
382constexpr LogSeverity LOG_WARNING = LOGGING_WARNING;
383constexpr LogSeverity LOG_ERROR = LOGGING_ERROR;
384constexpr LogSeverity LOG_FATAL = LOGGING_FATAL;
385constexpr LogSeverity LOG_DFATAL = LOGGING_DFATAL;
Lei Zhang93dd42572020-10-23 18:45:53386
initial.commitd7cae122008-07-26 21:49:38387// A few definitions of macros that don't generate much code. These are used
388// by LOG() and LOG_IF, etc. Since these are used all over our code, it's
389// better to have compact code for these operations.
Lei Zhang93dd42572020-10-23 18:45:53390#define COMPACT_GOOGLE_LOG_EX_INFO(ClassName, ...) \
391 ::logging::ClassName(__FILE__, __LINE__, ::logging::LOGGING_INFO, \
tsniatowski612550f2016-07-21 18:26:20392 ##__VA_ARGS__)
Lei Zhang93dd42572020-10-23 18:45:53393#define COMPACT_GOOGLE_LOG_EX_WARNING(ClassName, ...) \
394 ::logging::ClassName(__FILE__, __LINE__, ::logging::LOGGING_WARNING, \
395 ##__VA_ARGS__)
396#define COMPACT_GOOGLE_LOG_EX_ERROR(ClassName, ...) \
397 ::logging::ClassName(__FILE__, __LINE__, ::logging::LOGGING_ERROR, \
398 ##__VA_ARGS__)
399#define COMPACT_GOOGLE_LOG_EX_FATAL(ClassName, ...) \
400 ::logging::ClassName(__FILE__, __LINE__, ::logging::LOGGING_FATAL, \
401 ##__VA_ARGS__)
402#define COMPACT_GOOGLE_LOG_EX_DFATAL(ClassName, ...) \
403 ::logging::ClassName(__FILE__, __LINE__, ::logging::LOGGING_DFATAL, \
404 ##__VA_ARGS__)
405#define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \
406 ::logging::ClassName(__FILE__, __LINE__, ::logging::LOGGING_DCHECK, \
407 ##__VA_ARGS__)
tschmelcher@chromium.orgd8617a62009-10-09 23:52:20408
Wez289477f2017-08-24 20:51:30409#define COMPACT_GOOGLE_LOG_INFO COMPACT_GOOGLE_LOG_EX_INFO(LogMessage)
410#define COMPACT_GOOGLE_LOG_WARNING COMPACT_GOOGLE_LOG_EX_WARNING(LogMessage)
411#define COMPACT_GOOGLE_LOG_ERROR COMPACT_GOOGLE_LOG_EX_ERROR(LogMessage)
412#define COMPACT_GOOGLE_LOG_FATAL COMPACT_GOOGLE_LOG_EX_FATAL(LogMessage)
413#define COMPACT_GOOGLE_LOG_DFATAL COMPACT_GOOGLE_LOG_EX_DFATAL(LogMessage)
414#define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_EX_DCHECK(LogMessage)
initial.commitd7cae122008-07-26 21:49:38415
Xiaohan Wang38e4ebb2022-01-19 06:57:43416#if BUILDFLAG(IS_WIN)
initial.commitd7cae122008-07-26 21:49:38417// wingdi.h defines ERROR to be 0. When we call LOG(ERROR), it gets
418// substituted with 0, and it expands to COMPACT_GOOGLE_LOG_0. To allow us
419// to keep using this syntax, we define this macro to do the same thing
420// as COMPACT_GOOGLE_LOG_ERROR, and also define ERROR the same way that
421// the Windows SDK does for consistency.
422#define ERROR 0
tschmelcher@chromium.orgd8617a62009-10-09 23:52:20423#define COMPACT_GOOGLE_LOG_EX_0(ClassName, ...) \
424 COMPACT_GOOGLE_LOG_EX_ERROR(ClassName , ##__VA_ARGS__)
425#define COMPACT_GOOGLE_LOG_0 COMPACT_GOOGLE_LOG_ERROR
akalin@chromium.org521b0c42010-10-01 23:02:36426// Needed for LOG_IS_ON(ERROR).
Lei Zhang4d9e18572021-04-30 08:57:06427constexpr LogSeverity LOGGING_0 = LOGGING_ERROR;
joth@chromium.org8d127302013-01-10 02:41:57428#endif
akalin@chromium.org521b0c42010-10-01 23:02:36429
viettrungluu@chromium.orgf2c05492014-06-17 12:04:23430// As special cases, we can assume that LOG_IS_ON(FATAL) always holds. Also,
431// LOG_IS_ON(DFATAL) always holds in debug mode. In particular, CHECK()s will
432// always fire if they fail.
akalin@chromium.org521b0c42010-10-01 23:02:36433#define LOG_IS_ON(severity) \
Lei Zhang93dd42572020-10-23 18:45:53434 (::logging::ShouldCreateLogMessage(::logging::LOGGING_##severity))
akalin@chromium.org521b0c42010-10-01 23:02:36435
Xiyuan Xia28d809d2023-11-02 22:00:42436// Define a default ENABLED_VLOG_LEVEL if it is not defined. The macros allows
437// code to enable vlog level at build time without the need of --vmodule
438// switch at runtime. This is intended for VLOGs that needed from production
439// code without the cpu overhead to match vmodule patterns on every VLOG
440// instance.
Xiyuan Xiaa0559da2022-05-05 19:42:45441#if !defined(ENABLED_VLOG_LEVEL)
Xiyuan Xia28d809d2023-11-02 22:00:42442#define ENABLED_VLOG_LEVEL -1
Xiyuan Xiaa0559da2022-05-05 19:42:45443#endif // !defined(ENABLED_VLOG_LEVEL)
444
Ken MacKay70e8867002019-01-16 00:22:15445// We don't do any caching tricks with VLOG_IS_ON() like the
446// google-glog version since it increases binary size. This means
akalin@chromium.org521b0c42010-10-01 23:02:36447// that using the v-logging functions in conjunction with --vmodule
448// may be slow.
Xiyuan Xia28d809d2023-11-02 22:00:42449#define VLOG_IS_ON(verboselevel) \
450 ((verboselevel) <= (ENABLED_VLOG_LEVEL) || \
451 (verboselevel) <= ::logging::GetVlogLevel(__FILE__))
Xiyuan Xiaa0559da2022-05-05 19:42:45452
akalin@chromium.org521b0c42010-10-01 23:02:36453// Helper macro which avoids evaluating the arguments to a stream if
chcunninghamf6a96082015-02-07 01:58:37454// the condition doesn't hold. Condition is evaluated once and only once.
akalin@chromium.org521b0c42010-10-01 23:02:36455#define LAZY_STREAM(stream, condition) \
456 !(condition) ? (void) 0 : ::logging::LogMessageVoidify() & (stream)
initial.commitd7cae122008-07-26 21:49:38457
458// We use the preprocessor's merging operator, "##", so that, e.g.,
459// LOG(INFO) becomes the token COMPACT_GOOGLE_LOG_INFO. There's some funny
460// subtle difference between ostream member streaming functions (e.g.,
461// ostream::operator<<(int) and ostream non-member streaming functions
462// (e.g., ::operator<<(ostream&, string&): it turns out that it's
463// impossible to stream something like a string directly to an unnamed
464// ostream. We employ a neat hack by calling the stream() member
465// function of LogMessage which seems to avoid the problem.
akalin@chromium.org521b0c42010-10-01 23:02:36466#define LOG_STREAM(severity) COMPACT_GOOGLE_LOG_ ## severity.stream()
initial.commitd7cae122008-07-26 21:49:38467
akalin@chromium.org521b0c42010-10-01 23:02:36468#define LOG(severity) LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity))
469#define LOG_IF(severity, condition) \
470 LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity) && (condition))
471
siggi@chromium.org162ac0f2010-11-04 15:50:49472// The VLOG macros log with negative verbosities.
473#define VLOG_STREAM(verbose_level) \
Artem Bolgar30e5d692020-12-12 01:15:58474 ::logging::LogMessage(__FILE__, __LINE__, -(verbose_level)).stream()
siggi@chromium.org162ac0f2010-11-04 15:50:49475
476#define VLOG(verbose_level) \
477 LAZY_STREAM(VLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level))
478
479#define VLOG_IF(verbose_level, condition) \
480 LAZY_STREAM(VLOG_STREAM(verbose_level), \
481 VLOG_IS_ON(verbose_level) && (condition))
akalin@chromium.org99b7c57f2010-09-29 19:26:36482
Peter Boström3ed27e82022-11-17 01:13:23483#if BUILDFLAG(IS_WIN)
sail@chromium.orgfb879b1a2011-03-06 18:16:31484#define VPLOG_STREAM(verbose_level) \
Artem Bolgar30e5d692020-12-12 01:15:58485 ::logging::Win32ErrorLogMessage(__FILE__, __LINE__, -(verbose_level), \
sail@chromium.orgfb879b1a2011-03-06 18:16:31486 ::logging::GetLastSystemErrorCode()).stream()
Xiaohan Wang38e4ebb2022-01-19 06:57:43487#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
sail@chromium.orgfb879b1a2011-03-06 18:16:31488#define VPLOG_STREAM(verbose_level) \
Artem Bolgar30e5d692020-12-12 01:15:58489 ::logging::ErrnoLogMessage(__FILE__, __LINE__, -(verbose_level), \
sail@chromium.orgfb879b1a2011-03-06 18:16:31490 ::logging::GetLastSystemErrorCode()).stream()
491#endif
492
493#define VPLOG(verbose_level) \
494 LAZY_STREAM(VPLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level))
495
496#define VPLOG_IF(verbose_level, condition) \
497 LAZY_STREAM(VPLOG_STREAM(verbose_level), \
498 VLOG_IS_ON(verbose_level) && (condition))
499
akalin@chromium.org99b7c57f2010-09-29 19:26:36500// TODO(akalin): Add more VLOG variants, e.g. VPLOG.
initial.commitd7cae122008-07-26 21:49:38501
kmarshallfe2f09f82017-04-20 21:05:26502#define LOG_ASSERT(condition) \
503 LOG_IF(FATAL, !(ANALYZER_ASSUME_TRUE(condition))) \
504 << "Assert failed: " #condition ". "
initial.commitd7cae122008-07-26 21:49:38505
Xiaohan Wang38e4ebb2022-01-19 06:57:43506#if BUILDFLAG(IS_WIN)
vitalybuka@chromium.orgc914d8a2014-04-23 01:11:01507#define PLOG_STREAM(severity) \
tschmelcher@chromium.orgd8617a62009-10-09 23:52:20508 COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \
509 ::logging::GetLastSystemErrorCode()).stream()
Xiaohan Wang38e4ebb2022-01-19 06:57:43510#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
vitalybuka@chromium.orgc914d8a2014-04-23 01:11:01511#define PLOG_STREAM(severity) \
tschmelcher@chromium.orgd8617a62009-10-09 23:52:20512 COMPACT_GOOGLE_LOG_EX_ ## severity(ErrnoLogMessage, \
513 ::logging::GetLastSystemErrorCode()).stream()
tschmelcher@chromium.orgd8617a62009-10-09 23:52:20514#endif
515
akalin@chromium.org521b0c42010-10-01 23:02:36516#define PLOG(severity) \
517 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity))
518
tschmelcher@chromium.orgd8617a62009-10-09 23:52:20519#define PLOG_IF(severity, condition) \
akalin@chromium.org521b0c42010-10-01 23:02:36520 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity) && (condition))
tschmelcher@chromium.orgd8617a62009-10-09 23:52:20521
scottmg3c957a52016-12-10 20:57:59522BASE_EXPORT extern std::ostream* g_swallow_stream;
523
524// Note that g_swallow_stream is used instead of an arbitrary LOG() stream to
525// avoid the creation of an object with a non-trivial destructor (LogMessage).
526// On MSVC x86 (checked on 2015 Update 3), this causes a few additional
527// pointless instructions to be emitted even at full optimization level, even
528// though the : arm of the ternary operator is clearly never executed. Using a
529// simpler object to be &'d with Voidify() avoids these extra instructions.
530// Using a simpler POD object with a templated operator<< also works to avoid
531// these instructions. However, this causes warnings on statically defined
532// implementations of operator<<(std::ostream, ...) in some .cc files, because
533// they become defined-but-unreferenced functions. A reinterpret_cast of 0 to an
534// ostream* also is not suitable, because some compilers warn of undefined
535// behavior.
536#define EAT_STREAM_PARAMETERS \
537 true ? (void)0 \
538 : ::logging::LogMessageVoidify() & (*::logging::g_swallow_stream)
akalin@chromium.orgddb9b332011-12-02 07:31:09539
akalin@chromium.orgd15e56c2010-09-30 21:12:33540// Definitions for DLOG et al.
541
gab190f7542016-08-01 20:03:41542#if DCHECK_IS_ON()
akalin@chromium.orgd926c202010-10-01 02:58:24543
akalin@chromium.org5e987802010-11-01 19:49:22544#define DLOG_IS_ON(severity) LOG_IS_ON(severity)
akalin@chromium.orgd926c202010-10-01 02:58:24545#define DLOG_IF(severity, condition) LOG_IF(severity, condition)
546#define DLOG_ASSERT(condition) LOG_ASSERT(condition)
akalin@chromium.orgd926c202010-10-01 02:58:24547#define DPLOG_IF(severity, condition) PLOG_IF(severity, condition)
akalin@chromium.org521b0c42010-10-01 23:02:36548#define DVLOG_IF(verboselevel, condition) VLOG_IF(verboselevel, condition)
sail@chromium.orgfb879b1a2011-03-06 18:16:31549#define DVPLOG_IF(verboselevel, condition) VPLOG_IF(verboselevel, condition)
akalin@chromium.orgd926c202010-10-01 02:58:24550
gab190f7542016-08-01 20:03:41551#else // DCHECK_IS_ON()
akalin@chromium.orgd926c202010-10-01 02:58:24552
gab190f7542016-08-01 20:03:41553// If !DCHECK_IS_ON(), we want to avoid emitting any references to |condition|
554// (which may reference a variable defined only if DCHECK_IS_ON()).
555// Contrast this with DCHECK et al., which has different behavior.
akalin@chromium.orgd926c202010-10-01 02:58:24556
akalin@chromium.org5e987802010-11-01 19:49:22557#define DLOG_IS_ON(severity) false
akalin@chromium.orgddb9b332011-12-02 07:31:09558#define DLOG_IF(severity, condition) EAT_STREAM_PARAMETERS
559#define DLOG_ASSERT(condition) EAT_STREAM_PARAMETERS
560#define DPLOG_IF(severity, condition) EAT_STREAM_PARAMETERS
561#define DVLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS
562#define DVPLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS
akalin@chromium.orgd926c202010-10-01 02:58:24563
gab190f7542016-08-01 20:03:41564#endif // DCHECK_IS_ON()
akalin@chromium.orgd926c202010-10-01 02:58:24565
akalin@chromium.org521b0c42010-10-01 23:02:36566#define DLOG(severity) \
567 LAZY_STREAM(LOG_STREAM(severity), DLOG_IS_ON(severity))
568
akalin@chromium.org521b0c42010-10-01 23:02:36569#define DPLOG(severity) \
570 LAZY_STREAM(PLOG_STREAM(severity), DLOG_IS_ON(severity))
571
Ken MacKay70e8867002019-01-16 00:22:15572#define DVLOG(verboselevel) DVLOG_IF(verboselevel, true)
akalin@chromium.org521b0c42010-10-01 23:02:36573
Ken MacKay70e8867002019-01-16 00:22:15574#define DVPLOG(verboselevel) DVPLOG_IF(verboselevel, true)
sail@chromium.orgfb879b1a2011-03-06 18:16:31575
akalin@chromium.org521b0c42010-10-01 23:02:36576// Definitions for DCHECK et al.
akalin@chromium.orgd926c202010-10-01 02:58:24577
Wez02cedeba2022-07-26 12:48:38578#if BUILDFLAG(DCHECK_IS_CONFIGURABLE)
Lei Zhang93dd42572020-10-23 18:45:53579BASE_EXPORT extern LogSeverity LOGGING_DCHECK;
Wez289477f2017-08-24 20:51:30580#else
Lei Zhang4d9e18572021-04-30 08:57:06581constexpr LogSeverity LOGGING_DCHECK = LOGGING_FATAL;
Wez02cedeba2022-07-26 12:48:38582#endif // BUILDFLAG(DCHECK_IS_CONFIGURABLE)
akalin@chromium.org521b0c42010-10-01 23:02:36583
initial.commitd7cae122008-07-26 21:49:38584// Redefine the standard assert to use our nice log files
585#undef assert
586#define assert(x) DLOG_ASSERT(x)
587
588// This class more or less represents a particular log message. You
589// create an instance of LogMessage and then stream stuff to it.
590// When you finish streaming to it, ~LogMessage is called and the
591// full message gets streamed to the appropriate destination.
592//
593// You shouldn't actually use LogMessage's constructor to log things,
594// though. You should use the LOG() macro (and variants thereof)
595// above.
darin@chromium.org0bea7252011-08-05 15:34:00596class BASE_EXPORT LogMessage {
initial.commitd7cae122008-07-26 21:49:38597 public:
viettrungluu@chromium.orgbf8ddf13a2014-06-18 15:02:22598 // Used for LOG(severity).
initial.commitd7cae122008-07-26 21:49:38599 LogMessage(const char* file, int line, LogSeverity severity);
600
Lei Zhang93dd42572020-10-23 18:45:53601 // Used for CHECK(). Implied severity = LOGGING_FATAL.
tnagel4a045d3f2015-07-12 14:19:28602 LogMessage(const char* file, int line, const char* condition);
David Bienvenub4b441e2020-09-23 05:49:57603 LogMessage(const LogMessage&) = delete;
604 LogMessage& operator=(const LogMessage&) = delete;
Hans Wennborg12aea3e2020-04-14 15:29:00605 virtual ~LogMessage();
initial.commitd7cae122008-07-26 21:49:38606
607 std::ostream& stream() { return stream_; }
608
Peter Boström6c0094d12022-07-07 16:03:39609 LogSeverity severity() const { return severity_; }
610 std::string str() const { return stream_.str(); }
Peter Boström1d3b6d82022-07-11 17:59:49611 const char* file() const { return file_; }
612 int line() const { return line_; }
Peter Boström6c0094d12022-07-07 16:03:39613
Peter Boström37482962022-07-14 16:09:54614 // Gets file:line: message in a format suitable for crash reporting.
615 std::string BuildCrashString() const;
pastarmovj89f7ee12016-09-20 14:58:13616
initial.commitd7cae122008-07-26 21:49:38617 private:
618 void Init(const char* file, int line);
619
David Dorwin11e7c2c12021-04-10 17:01:09620 const LogSeverity severity_;
initial.commitd7cae122008-07-26 21:49:38621 std::ostringstream stream_;
maruel@google.comc88873922008-07-30 13:02:03622 size_t message_start_; // Offset of the start of the message (past prefix
623 // info).
siggi@chromium.org162ac0f2010-11-04 15:50:49624 // The file and line information passed in to the constructor.
David Dorwin11e7c2c12021-04-10 17:01:09625 const char* const file_;
siggi@chromium.org162ac0f2010-11-04 15:50:49626 const int line_;
627
tommi@google.com3f85caa2009-04-14 16:52:11628 // This is useful since the LogMessage class uses a lot of Win32 calls
629 // that will lose the value of GLE and the code that called the log function
630 // will have lost the thread error value when the log call returns.
Joshua Perazab427af262020-04-13 21:54:42631 base::ScopedClearLastError last_error_;
initial.commitd7cae122008-07-26 21:49:38632
Georg Neisffe34f652021-12-27 21:42:36633#if BUILDFLAG(IS_CHROMEOS)
Yuta Hijikata9b7279a2020-08-26 16:10:54634 void InitWithSyslogPrefix(base::StringPiece filename,
635 int line,
636 uint64_t tick_count,
637 const char* log_severity_name_c_str,
638 const char* log_prefix,
639 bool enable_process_id,
640 bool enable_thread_id,
641 bool enable_timestamp,
642 bool enable_tickcount);
643#endif
initial.commitd7cae122008-07-26 21:49:38644};
645
initial.commitd7cae122008-07-26 21:49:38646// This class is used to explicitly ignore values in the conditional
647// logging macros. This avoids compiler warnings like "value computed
648// is not used" and "statement has no effect".
rvargas@google.com23bb71f2011-04-21 22:22:10649class LogMessageVoidify {
initial.commitd7cae122008-07-26 21:49:38650 public:
Chris Watkins091d6292017-12-13 04:25:58651 LogMessageVoidify() = default;
initial.commitd7cae122008-07-26 21:49:38652 // This has to be an operator with a precedence lower than << but
653 // higher than ?:
654 void operator&(std::ostream&) { }
655};
656
Xiaohan Wang38e4ebb2022-01-19 06:57:43657#if BUILDFLAG(IS_WIN)
tschmelcher@chromium.orgd8617a62009-10-09 23:52:20658typedef unsigned long SystemErrorCode;
Xiaohan Wang38e4ebb2022-01-19 06:57:43659#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
tschmelcher@chromium.orgd8617a62009-10-09 23:52:20660typedef int SystemErrorCode;
661#endif
662
663// Alias for ::GetLastError() on Windows and errno on POSIX. Avoids having to
664// pull in windows.h just for GetLastError() and DWORD.
darin@chromium.org0bea7252011-08-05 15:34:00665BASE_EXPORT SystemErrorCode GetLastSystemErrorCode();
vitalybuka@chromium.orgc914d8a2014-04-23 01:11:01666BASE_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code);
tschmelcher@chromium.orgd8617a62009-10-09 23:52:20667
Xiaohan Wang38e4ebb2022-01-19 06:57:43668#if BUILDFLAG(IS_WIN)
tschmelcher@chromium.orgd8617a62009-10-09 23:52:20669// Appends a formatted system message of the GetLastError() type.
Hans Wennborg12aea3e2020-04-14 15:29:00670class BASE_EXPORT Win32ErrorLogMessage : public LogMessage {
tschmelcher@chromium.orgd8617a62009-10-09 23:52:20671 public:
672 Win32ErrorLogMessage(const char* file,
673 int line,
674 LogSeverity severity,
tschmelcher@chromium.orgd8617a62009-10-09 23:52:20675 SystemErrorCode err);
David Bienvenub4b441e2020-09-23 05:49:57676 Win32ErrorLogMessage(const Win32ErrorLogMessage&) = delete;
677 Win32ErrorLogMessage& operator=(const Win32ErrorLogMessage&) = delete;
tschmelcher@chromium.orgd8617a62009-10-09 23:52:20678 // Appends the error message before destructing the encapsulated class.
Hans Wennborg12aea3e2020-04-14 15:29:00679 ~Win32ErrorLogMessage() override;
erg@google.coma502bbe72011-01-07 18:06:45680
tschmelcher@chromium.orgd8617a62009-10-09 23:52:20681 private:
682 SystemErrorCode err_;
tschmelcher@chromium.orgd8617a62009-10-09 23:52:20683};
Xiaohan Wang38e4ebb2022-01-19 06:57:43684#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
tschmelcher@chromium.orgd8617a62009-10-09 23:52:20685// Appends a formatted system message of the errno type
Hans Wennborg12aea3e2020-04-14 15:29:00686class BASE_EXPORT ErrnoLogMessage : public LogMessage {
tschmelcher@chromium.orgd8617a62009-10-09 23:52:20687 public:
688 ErrnoLogMessage(const char* file,
689 int line,
690 LogSeverity severity,
691 SystemErrorCode err);
David Bienvenub4b441e2020-09-23 05:49:57692 ErrnoLogMessage(const ErrnoLogMessage&) = delete;
693 ErrnoLogMessage& operator=(const ErrnoLogMessage&) = delete;
tschmelcher@chromium.orgd8617a62009-10-09 23:52:20694 // Appends the error message before destructing the encapsulated class.
Hans Wennborg12aea3e2020-04-14 15:29:00695 ~ErrnoLogMessage() override;
erg@google.coma502bbe72011-01-07 18:06:45696
tschmelcher@chromium.orgd8617a62009-10-09 23:52:20697 private:
698 SystemErrorCode err_;
tschmelcher@chromium.orgd8617a62009-10-09 23:52:20699};
Xiaohan Wang38e4ebb2022-01-19 06:57:43700#endif // BUILDFLAG(IS_WIN)
tschmelcher@chromium.orgd8617a62009-10-09 23:52:20701
initial.commitd7cae122008-07-26 21:49:38702// Closes the log file explicitly if open.
703// NOTE: Since the log file is opened as necessary by the action of logging
704// statements, there's no guarantee that it will stay closed
705// after this call.
darin@chromium.org0bea7252011-08-05 15:34:00706BASE_EXPORT void CloseLogFile();
initial.commitd7cae122008-07-26 21:49:38707
Yuta Hijikata000df18f2020-11-18 06:55:58708#if BUILDFLAG(IS_CHROMEOS_ASH)
Robbie McElrath8bf49842019-08-20 22:22:53709// Returns a new file handle that will write to the same destination as the
710// currently open log file. Returns nullptr if logging to a file is disabled,
711// or if opening the file failed. This is intended to be used to initialize
712// logging in child processes that are unable to open files.
713BASE_EXPORT FILE* DuplicateLogFILE();
714#endif
715
willchan@chromium.orge36ddc82009-12-08 04:22:50716// Async signal safe logging mechanism.
darin@chromium.org0bea7252011-08-05 15:34:00717BASE_EXPORT void RawLog(int level, const char* message);
willchan@chromium.orge36ddc82009-12-08 04:22:50718
tsniatowski612550f2016-07-21 18:26:20719#define RAW_LOG(level, message) \
Lei Zhang93dd42572020-10-23 18:45:53720 ::logging::RawLog(::logging::LOGGING_##level, message)
willchan@chromium.orge36ddc82009-12-08 04:22:50721
Xiaohan Wang38e4ebb2022-01-19 06:57:43722#if BUILDFLAG(IS_WIN)
ananta61762fb2015-09-18 01:00:09723// Returns true if logging to file is enabled.
724BASE_EXPORT bool IsLoggingToFileEnabled();
725
ananta@chromium.orgf01b88a2013-02-27 22:04:00726// Returns the default log file path.
Jan Wilken Dörrieb630aca2019-12-04 10:59:11727BASE_EXPORT std::wstring GetLogFileFullPath();
ananta@chromium.orgf01b88a2013-02-27 22:04:00728#endif
729
brettw@google.com39be4242008-08-07 18:31:40730} // namespace logging
initial.commitd7cae122008-07-26 21:49:38731
brettw@google.com39be4242008-08-07 18:31:40732#endif // BASE_LOGGING_H_