blob: eb605f6685e1ec02d49adb9c647ab1ec5b66effa [file] [log] [blame]
[email protected]34a907732012-01-20 06:33:271// Copyright (c) 2012 The Chromium Authors. All rights reserved.
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>
initial.commitd7cae122008-07-26 21:49:3814
[email protected]0bea7252011-08-05 15:34:0015#include "base/base_export.h"
alex-accc1bde62017-04-19 08:33:5516#include "base/callback_forward.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"
Etienne Pierre-Dorayd120ebf2018-09-14 23:38:2119#include "base/scoped_clear_last_error.h"
alex-accc1bde62017-04-19 08:33:5520#include "base/strings/string_piece_forward.h"
Yuta Hijikata000df18f2020-11-18 06:55:5821#include "build/chromeos_buildflags.h"
initial.commitd7cae122008-07-26 21:49:3822
Yuta Hijikata000df18f2020-11-18 06:55:5823#if BUILDFLAG(IS_CHROMEOS_ASH)
Robbie McElrath8bf49842019-08-20 22:22:5324#include <cstdio>
25#endif
26
initial.commitd7cae122008-07-26 21:49:3827//
28// Optional message capabilities
29// -----------------------------
30// Assertion failed messages and fatal errors are displayed in a dialog box
31// before the application exits. However, running this UI creates a message
32// loop, which causes application messages to be processed and potentially
33// dispatched to existing application windows. Since the application is in a
34// bad state when this assertion dialog is displayed, these messages may not
35// get processed and hang the dialog, or the application might go crazy.
36//
37// Therefore, it can be beneficial to display the error dialog in a separate
38// process from the main application. When the logging system needs to display
39// a fatal error dialog box, it will look for a program called
40// "DebugMessage.exe" in the same directory as the application executable. It
41// will run this application with the message as the command line, and will
42// not include the name of the application as is traditional for easier
43// parsing.
44//
45// The code for DebugMessage.exe is only one line. In WinMain, do:
46// MessageBox(NULL, GetCommandLineW(), L"Fatal Error", 0);
47//
48// If DebugMessage.exe is not found, the logging code will use a normal
49// MessageBox, potentially causing the problems discussed above.
50
initial.commitd7cae122008-07-26 21:49:3851// Instructions
52// ------------
53//
54// Make a bunch of macros for logging. The way to log things is to stream
55// things to LOG(<a particular severity level>). E.g.,
56//
57// LOG(INFO) << "Found " << num_cookies << " cookies";
58//
59// You can also do conditional logging:
60//
61// LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
62//
initial.commitd7cae122008-07-26 21:49:3863// The CHECK(condition) macro is active in both debug and release builds and
64// effectively performs a LOG(FATAL) which terminates the process and
65// generates a crashdump unless a debugger is attached.
66//
67// There are also "debug mode" logging macros like the ones above:
68//
69// DLOG(INFO) << "Found cookies";
70//
71// DLOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
72//
73// All "debug mode" logging is compiled away to nothing for non-debug mode
74// compiles. LOG_IF and development flags also work well together
75// because the code can be compiled away sometimes.
76//
77// We also have
78//
79// LOG_ASSERT(assertion);
80// DLOG_ASSERT(assertion);
81//
82// which is syntactic sugar for {,D}LOG_IF(FATAL, assert fails) << assertion;
83//
[email protected]99b7c57f2010-09-29 19:26:3684// There are "verbose level" logging macros. They look like
85//
86// VLOG(1) << "I'm printed when you run the program with --v=1 or more";
87// VLOG(2) << "I'm printed when you run the program with --v=2 or more";
88//
89// These always log at the INFO log level (when they log at all).
90// The verbose logging can also be turned on module-by-module. For instance,
[email protected]b0d38d4c2010-10-29 00:39:4891// --vmodule=profile=2,icon_loader=1,browser_*=3,*/chromeos/*=4 --v=0
[email protected]99b7c57f2010-09-29 19:26:3692// will cause:
93// a. VLOG(2) and lower messages to be printed from profile.{h,cc}
94// b. VLOG(1) and lower messages to be printed from icon_loader.{h,cc}
95// c. VLOG(3) and lower messages to be printed from files prefixed with
96// "browser"
[email protected]e11de722010-11-01 20:50:5597// d. VLOG(4) and lower messages to be printed from files under a
[email protected]b0d38d4c2010-10-29 00:39:4898// "chromeos" directory.
[email protected]e11de722010-11-01 20:50:5599// e. VLOG(0) and lower messages to be printed from elsewhere
[email protected]99b7c57f2010-09-29 19:26:36100//
101// The wildcarding functionality shown by (c) supports both '*' (match
[email protected]b0d38d4c2010-10-29 00:39:48102// 0 or more characters) and '?' (match any single character)
103// wildcards. Any pattern containing a forward or backward slash will
104// be tested against the whole pathname and not just the module.
105// E.g., "*/foo/bar/*=2" would change the logging level for all code
106// in source files under a "foo/bar" directory.
[email protected]99b7c57f2010-09-29 19:26:36107//
Mason Freed14240d162020-08-12 13:06:34108// Note that for a Chromium binary built in release mode (is_debug = false) you
109// must pass "--enable-logging=stderr" in order to see the output of VLOG
110// statements.
111//
[email protected]99b7c57f2010-09-29 19:26:36112// There's also VLOG_IS_ON(n) "verbose level" condition macro. To be used as
113//
114// if (VLOG_IS_ON(2)) {
115// // do some logging preparation and logging
116// // that can't be accomplished with just VLOG(2) << ...;
117// }
118//
119// There is also a VLOG_IF "verbose level" condition macro for sample
120// cases, when some extra computation and preparation for logs is not
121// needed.
122//
123// VLOG_IF(1, (size > 1024))
124// << "I'm printed when size is more than 1024 and when you run the "
125// "program with --v=1 or more";
126//
initial.commitd7cae122008-07-26 21:49:38127// We also override the standard 'assert' to use 'DLOG_ASSERT'.
128//
[email protected]d8617a62009-10-09 23:52:20129// Lastly, there is:
130//
131// PLOG(ERROR) << "Couldn't do foo";
132// DPLOG(ERROR) << "Couldn't do foo";
133// PLOG_IF(ERROR, cond) << "Couldn't do foo";
134// DPLOG_IF(ERROR, cond) << "Couldn't do foo";
135// PCHECK(condition) << "Couldn't do foo";
136// DPCHECK(condition) << "Couldn't do foo";
137//
138// which append the last system error to the message in string form (taken from
139// GetLastError() on Windows and errno on POSIX).
140//
initial.commitd7cae122008-07-26 21:49:38141// The supported severity levels for macros that allow you to specify one
[email protected]f2c05492014-06-17 12:04:23142// are (in increasing order of severity) INFO, WARNING, ERROR, and FATAL.
initial.commitd7cae122008-07-26 21:49:38143//
144// Very important: logging a message at the FATAL severity level causes
145// the program to terminate (after the message is logged).
[email protected]fb62a532009-02-12 01:19:05146//
danakjf8e9c302021-01-27 21:37:23147// There is the special severity of DFATAL, which logs FATAL in DCHECK-enabled
148// builds, ERROR in normal mode.
Rob Schonberger45637212018-12-03 04:46:25149//
Yuta Hijikata9b7279a2020-08-26 16:10:54150// Output is formatted as per the following example, except on Chrome OS.
Rob Schonberger45637212018-12-03 04:46:25151// [3816:3877:0812/234555.406952:VERBOSE1:drm_device_handle.cc(90)] Succeeded
152// authenticating /dev/dri/card0 in 0 ms with 1 attempt(s)
153//
154// The colon separated fields inside the brackets are as follows:
155// 0. An optional Logfile prefix (not included in this example)
156// 1. Process ID
157// 2. Thread ID
158// 3. The date/time of the log message, in MMDD/HHMMSS.Milliseconds format
159// 4. The log level
160// 5. The filename and line number where the log was instantiated
161//
Yuta Hijikata9b7279a2020-08-26 16:10:54162// Output for Chrome OS can be switched to syslog-like format. See
163// InitWithSyslogPrefix() in logging_chromeos.h for details.
164//
Rob Schonberger45637212018-12-03 04:46:25165// Note that the visibility can be changed by setting preferences in
166// SetLogItems()
Mason Freed14240d162020-08-12 13:06:34167//
168// Additional logging-related information can be found here:
169// https://chromium.googlesource.com/chromium/src/+/master/docs/linux/debugging.md#Logging
initial.commitd7cae122008-07-26 21:49:38170
171namespace logging {
172
[email protected]5e3f7c22013-06-21 21:15:33173// TODO(avi): do we want to do a unification of character types here?
174#if defined(OS_WIN)
Jan Wilken Dörrieb630aca2019-12-04 10:59:11175typedef wchar_t PathChar;
Fabrice de Gans-Riberi306871de2018-05-16 19:38:39176#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
[email protected]5e3f7c22013-06-21 21:15:33177typedef char PathChar;
178#endif
179
Sharon Yang7cb919a2019-05-20 20:27:15180// A bitmask of potential logging destinations.
181using LoggingDestination = uint32_t;
182// Specifies where logs will be written. Multiple destinations can be specified
183// with bitwise OR.
184// Unless destination is LOG_NONE, all logs with severity ERROR and above will
185// be written to stderr in addition to the specified destination.
186enum : uint32_t {
[email protected]5e3f7c22013-06-21 21:15:33187 LOG_NONE = 0,
188 LOG_TO_FILE = 1 << 0,
189 LOG_TO_SYSTEM_DEBUG_LOG = 1 << 1,
Sharon Yang7cb919a2019-05-20 20:27:15190 LOG_TO_STDERR = 1 << 2,
[email protected]5e3f7c22013-06-21 21:15:33191
Sharon Yang7cb919a2019-05-20 20:27:15192 LOG_TO_ALL = LOG_TO_FILE | LOG_TO_SYSTEM_DEBUG_LOG | LOG_TO_STDERR,
[email protected]5e3f7c22013-06-21 21:15:33193
Sharon Yang7cb919a2019-05-20 20:27:15194// On Windows, use a file next to the exe.
195// On POSIX platforms, where it may not even be possible to locate the
196// executable on disk, use stderr.
197// On Fuchsia, use the Fuchsia logging service.
198#if defined(OS_FUCHSIA) || defined(OS_NACL)
[email protected]5e3f7c22013-06-21 21:15:33199 LOG_DEFAULT = LOG_TO_SYSTEM_DEBUG_LOG,
Sharon Yang7cb919a2019-05-20 20:27:15200#elif defined(OS_WIN)
201 LOG_DEFAULT = LOG_TO_FILE,
202#elif defined(OS_POSIX)
203 LOG_DEFAULT = LOG_TO_SYSTEM_DEBUG_LOG | LOG_TO_STDERR,
[email protected]5e3f7c22013-06-21 21:15:33204#endif
205};
initial.commitd7cae122008-07-26 21:49:38206
207// Indicates that the log file should be locked when being written to.
[email protected]5e3f7c22013-06-21 21:15:33208// Unless there is only one single-threaded process that is logging to
209// the log file, the file should be locked during writes to make each
[email protected]3ee50d12014-03-05 01:43:27210// log output atomic. Other writers will block.
initial.commitd7cae122008-07-26 21:49:38211//
212// All processes writing to the log file must have their locking set for it to
[email protected]5e3f7c22013-06-21 21:15:33213// work properly. Defaults to LOCK_LOG_FILE.
initial.commitd7cae122008-07-26 21:49:38214enum LogLockingState { LOCK_LOG_FILE, DONT_LOCK_LOG_FILE };
215
216// On startup, should we delete or append to an existing log file (if any)?
217// Defaults to APPEND_TO_OLD_LOG_FILE.
218enum OldFileDeletionState { DELETE_OLD_LOG_FILE, APPEND_TO_OLD_LOG_FILE };
219
Yuta Hijikata000df18f2020-11-18 06:55:58220#if BUILDFLAG(IS_CHROMEOS_ASH)
Yuta Hijikata1fc8f6342020-09-01 03:25:56221// Defines the log message prefix format to use.
222// LOG_FORMAT_SYSLOG indicates syslog-like message prefixes.
223// LOG_FORMAT_CHROME indicates the normal Chrome format.
Yuta Hijikata9b7279a2020-08-26 16:10:54224enum class BASE_EXPORT LogFormat { LOG_FORMAT_CHROME, LOG_FORMAT_SYSLOG };
225#endif
226
[email protected]5e3f7c22013-06-21 21:15:33227struct BASE_EXPORT LoggingSettings {
Sharon Yang7cb919a2019-05-20 20:27:15228 // Equivalent to logging destination enum, but allows for multiple
229 // destinations.
Wez7e125622019-05-29 22:11:28230 uint32_t logging_dest = LOG_DEFAULT;
[email protected]5e3f7c22013-06-21 21:15:33231
Robbie McElrath8bf49842019-08-20 22:22:53232 // The four settings below have an effect only when LOG_TO_FILE is
[email protected]5e3f7c22013-06-21 21:15:33233 // set in |logging_dest|.
Robbie McElrath8bf49842019-08-20 22:22:53234 const PathChar* log_file_path = nullptr;
Wez7e125622019-05-29 22:11:28235 LogLockingState lock_log = LOCK_LOG_FILE;
236 OldFileDeletionState delete_old = APPEND_TO_OLD_LOG_FILE;
Yuta Hijikata000df18f2020-11-18 06:55:58237#if BUILDFLAG(IS_CHROMEOS_ASH)
Robbie McElrath8bf49842019-08-20 22:22:53238 // Contains an optional file that logs should be written to. If present,
239 // |log_file_path| will be ignored, and the logging system will take ownership
240 // of the FILE. If there's an error writing to this file, no fallback paths
241 // will be opened.
242 FILE* log_file = nullptr;
Yuta Hijikata1fc8f6342020-09-01 03:25:56243 // ChromeOS uses the syslog log format by default.
244 LogFormat log_format = LogFormat::LOG_FORMAT_SYSLOG;
Robbie McElrath8bf49842019-08-20 22:22:53245#endif
[email protected]5e3f7c22013-06-21 21:15:33246};
[email protected]ff3d0c32010-08-23 19:57:46247
248// Define different names for the BaseInitLoggingImpl() function depending on
249// whether NDEBUG is defined or not so that we'll fail to link if someone tries
250// to compile logging.cc with NDEBUG but includes logging.h without defining it,
251// or vice versa.
weza245bd072017-06-18 23:26:34252#if defined(NDEBUG)
[email protected]ff3d0c32010-08-23 19:57:46253#define BaseInitLoggingImpl BaseInitLoggingImpl_built_with_NDEBUG
254#else
255#define BaseInitLoggingImpl BaseInitLoggingImpl_built_without_NDEBUG
256#endif
257
258// Implementation of the InitLogging() method declared below. We use a
259// more-specific name so we can #define it above without affecting other code
260// that has named stuff "InitLogging".
[email protected]5e3f7c22013-06-21 21:15:33261BASE_EXPORT bool BaseInitLoggingImpl(const LoggingSettings& settings);
[email protected]ff3d0c32010-08-23 19:57:46262
initial.commitd7cae122008-07-26 21:49:38263// Sets the log file name and other global logging state. Calling this function
264// is recommended, and is normally done at the beginning of application init.
265// If you don't call it, all the flags will be initialized to their default
266// values, and there is a race condition that may leak a critical section
267// object if two threads try to do the first log at the same time.
268// See the definition of the enums above for descriptions and default values.
269//
270// The default log file is initialized to "debug.log" in the application
271// directory. You probably don't want this, especially since the program
272// directory may not be writable on an enduser's system.
[email protected]064aa162011-12-03 00:30:08273//
274// This function may be called a second time to re-direct logging (e.g after
275// loging in to a user partition), however it should never be called more than
276// twice.
[email protected]5e3f7c22013-06-21 21:15:33277inline bool InitLogging(const LoggingSettings& settings) {
278 return BaseInitLoggingImpl(settings);
[email protected]ff3d0c32010-08-23 19:57:46279}
initial.commitd7cae122008-07-26 21:49:38280
281// Sets the log level. Anything at or above this level will be written to the
282// log file/displayed to the user (if applicable). Anything below this level
[email protected]162ac0f2010-11-04 15:50:49283// will be silently ignored. The log level defaults to 0 (everything is logged
284// up to level INFO) if this function is not called.
285// Note that log messages for VLOG(x) are logged at level -x, so setting
286// the min log level to negative values enables verbose logging.
[email protected]0bea7252011-08-05 15:34:00287BASE_EXPORT void SetMinLogLevel(int level);
initial.commitd7cae122008-07-26 21:49:38288
[email protected]8a2986ca2009-04-10 19:13:42289// Gets the current log level.
[email protected]0bea7252011-08-05 15:34:00290BASE_EXPORT int GetMinLogLevel();
initial.commitd7cae122008-07-26 21:49:38291
skobesc78c0ad72015-12-07 20:21:23292// Used by LOG_IS_ON to lazy-evaluate stream arguments.
293BASE_EXPORT bool ShouldCreateLogMessage(int severity);
294
[email protected]162ac0f2010-11-04 15:50:49295// Gets the VLOG default verbosity level.
[email protected]0bea7252011-08-05 15:34:00296BASE_EXPORT int GetVlogVerbosity();
[email protected]162ac0f2010-11-04 15:50:49297
[email protected]2f4e9a62010-09-29 21:25:14298// Note that |N| is the size *with* the null terminator.
[email protected]0bea7252011-08-05 15:34:00299BASE_EXPORT int GetVlogLevelHelper(const char* file_start, size_t N);
[email protected]2f4e9a62010-09-29 21:25:14300
tnagel270da922017-05-24 12:10:44301// Gets the current vlog level for the given file (usually taken from __FILE__).
[email protected]99b7c57f2010-09-29 19:26:36302template <size_t N>
303int GetVlogLevel(const char (&file)[N]) {
304 return GetVlogLevelHelper(file, N);
305}
initial.commitd7cae122008-07-26 21:49:38306
307// Sets the common items you want to be prepended to each log message.
308// process and thread IDs default to off, the timestamp defaults to on.
309// If this function is not called, logging defaults to writing the timestamp
310// only.
[email protected]0bea7252011-08-05 15:34:00311BASE_EXPORT void SetLogItems(bool enable_process_id, bool enable_thread_id,
312 bool enable_timestamp, bool enable_tickcount);
initial.commitd7cae122008-07-26 21:49:38313
James Cooka0536c32018-08-01 20:13:31314// Sets an optional prefix to add to each log message. |prefix| is not copied
315// and should be a raw string constant. |prefix| must only contain ASCII letters
316// to avoid confusion with PIDs and timestamps. Pass null to remove the prefix.
317// Logging defaults to no prefix.
318BASE_EXPORT void SetLogPrefix(const char* prefix);
319
[email protected]81e0a852010-08-17 00:38:12320// Sets whether or not you'd like to see fatal debug messages popped up in
321// a dialog box or not.
322// Dialogs are not shown by default.
[email protected]0bea7252011-08-05 15:34:00323BASE_EXPORT void SetShowErrorDialogs(bool enable_dialogs);
[email protected]81e0a852010-08-17 00:38:12324
initial.commitd7cae122008-07-26 21:49:38325// Sets the Log Assert Handler that will be used to notify of check failures.
alex-accc1bde62017-04-19 08:33:55326// Resets Log Assert Handler on object destruction.
[email protected]fb62a532009-02-12 01:19:05327// The default handler shows a dialog box and then terminate the process,
328// however clients can use this function to override with their own handling
329// (e.g. a silent one for Unit Tests)
alex-accc1bde62017-04-19 08:33:55330using LogAssertHandlerFunction =
kylechar83fb51e52019-03-14 15:30:43331 base::RepeatingCallback<void(const char* file,
332 int line,
333 const base::StringPiece message,
334 const base::StringPiece stack_trace)>;
alex-accc1bde62017-04-19 08:33:55335
336class BASE_EXPORT ScopedLogAssertHandler {
337 public:
338 explicit ScopedLogAssertHandler(LogAssertHandlerFunction handler);
David Bienvenub4b441e2020-09-23 05:49:57339 ScopedLogAssertHandler(const ScopedLogAssertHandler&) = delete;
340 ScopedLogAssertHandler& operator=(const ScopedLogAssertHandler&) = delete;
alex-accc1bde62017-04-19 08:33:55341 ~ScopedLogAssertHandler();
alex-accc1bde62017-04-19 08:33:55342};
[email protected]64e5cc02010-11-03 19:20:27343
[email protected]2b07b8412009-11-25 15:26:34344// Sets the Log Message Handler that gets passed every log message before
345// it's sent to other log destinations (if any).
346// Returns true to signal that it handled the message and the message
347// should not be sent to other log destinations.
[email protected]162ac0f2010-11-04 15:50:49348typedef bool (*LogMessageHandlerFunction)(int severity,
349 const char* file, int line, size_t message_start, const std::string& str);
[email protected]0bea7252011-08-05 15:34:00350BASE_EXPORT void SetLogMessageHandler(LogMessageHandlerFunction handler);
351BASE_EXPORT LogMessageHandlerFunction GetLogMessageHandler();
[email protected]2b07b8412009-11-25 15:26:34352
Lei Zhang93dd42572020-10-23 18:45:53353using LogSeverity = int;
Lei Zhang4d9e18572021-04-30 08:57:06354constexpr LogSeverity LOGGING_VERBOSE = -1; // This is level 1 verbosity
[email protected]162ac0f2010-11-04 15:50:49355// Note: the log severities are used to index into the array of names,
356// see log_severity_names.
Lei Zhang4d9e18572021-04-30 08:57:06357constexpr LogSeverity LOGGING_INFO = 0;
358constexpr LogSeverity LOGGING_WARNING = 1;
359constexpr LogSeverity LOGGING_ERROR = 2;
360constexpr LogSeverity LOGGING_FATAL = 3;
361constexpr LogSeverity LOGGING_NUM_SEVERITIES = 4;
initial.commitd7cae122008-07-26 21:49:38362
danakjf8e9c302021-01-27 21:37:23363// LOGGING_DFATAL is LOGGING_FATAL in DCHECK-enabled builds, ERROR in normal
364// mode.
365#if DCHECK_IS_ON()
Lei Zhang4d9e18572021-04-30 08:57:06366constexpr LogSeverity LOGGING_DFATAL = LOGGING_FATAL;
danakjf8e9c302021-01-27 21:37:23367#else
Lei Zhang4d9e18572021-04-30 08:57:06368constexpr LogSeverity LOGGING_DFATAL = LOGGING_ERROR;
initial.commitd7cae122008-07-26 21:49:38369#endif
370
Lei Zhang93dd42572020-10-23 18:45:53371// This block duplicates the above entries to facilitate incremental conversion
372// from LOG_FOO to LOGGING_FOO.
373// TODO(thestig): Convert existing users to LOGGING_FOO and remove this block.
Lei Zhang4d9e18572021-04-30 08:57:06374constexpr LogSeverity LOG_VERBOSE = LOGGING_VERBOSE;
375constexpr LogSeverity LOG_INFO = LOGGING_INFO;
376constexpr LogSeverity LOG_WARNING = LOGGING_WARNING;
377constexpr LogSeverity LOG_ERROR = LOGGING_ERROR;
378constexpr LogSeverity LOG_FATAL = LOGGING_FATAL;
379constexpr LogSeverity LOG_DFATAL = LOGGING_DFATAL;
Lei Zhang93dd42572020-10-23 18:45:53380
initial.commitd7cae122008-07-26 21:49:38381// A few definitions of macros that don't generate much code. These are used
382// by LOG() and LOG_IF, etc. Since these are used all over our code, it's
383// better to have compact code for these operations.
Lei Zhang93dd42572020-10-23 18:45:53384#define COMPACT_GOOGLE_LOG_EX_INFO(ClassName, ...) \
385 ::logging::ClassName(__FILE__, __LINE__, ::logging::LOGGING_INFO, \
tsniatowski612550f2016-07-21 18:26:20386 ##__VA_ARGS__)
Lei Zhang93dd42572020-10-23 18:45:53387#define COMPACT_GOOGLE_LOG_EX_WARNING(ClassName, ...) \
388 ::logging::ClassName(__FILE__, __LINE__, ::logging::LOGGING_WARNING, \
389 ##__VA_ARGS__)
390#define COMPACT_GOOGLE_LOG_EX_ERROR(ClassName, ...) \
391 ::logging::ClassName(__FILE__, __LINE__, ::logging::LOGGING_ERROR, \
392 ##__VA_ARGS__)
393#define COMPACT_GOOGLE_LOG_EX_FATAL(ClassName, ...) \
394 ::logging::ClassName(__FILE__, __LINE__, ::logging::LOGGING_FATAL, \
395 ##__VA_ARGS__)
396#define COMPACT_GOOGLE_LOG_EX_DFATAL(ClassName, ...) \
397 ::logging::ClassName(__FILE__, __LINE__, ::logging::LOGGING_DFATAL, \
398 ##__VA_ARGS__)
399#define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \
400 ::logging::ClassName(__FILE__, __LINE__, ::logging::LOGGING_DCHECK, \
401 ##__VA_ARGS__)
[email protected]d8617a62009-10-09 23:52:20402
Wez289477f2017-08-24 20:51:30403#define COMPACT_GOOGLE_LOG_INFO COMPACT_GOOGLE_LOG_EX_INFO(LogMessage)
404#define COMPACT_GOOGLE_LOG_WARNING COMPACT_GOOGLE_LOG_EX_WARNING(LogMessage)
405#define COMPACT_GOOGLE_LOG_ERROR COMPACT_GOOGLE_LOG_EX_ERROR(LogMessage)
406#define COMPACT_GOOGLE_LOG_FATAL COMPACT_GOOGLE_LOG_EX_FATAL(LogMessage)
407#define COMPACT_GOOGLE_LOG_DFATAL COMPACT_GOOGLE_LOG_EX_DFATAL(LogMessage)
408#define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_EX_DCHECK(LogMessage)
initial.commitd7cae122008-07-26 21:49:38409
[email protected]8d127302013-01-10 02:41:57410#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38411// wingdi.h defines ERROR to be 0. When we call LOG(ERROR), it gets
412// substituted with 0, and it expands to COMPACT_GOOGLE_LOG_0. To allow us
413// to keep using this syntax, we define this macro to do the same thing
414// as COMPACT_GOOGLE_LOG_ERROR, and also define ERROR the same way that
415// the Windows SDK does for consistency.
416#define ERROR 0
[email protected]d8617a62009-10-09 23:52:20417#define COMPACT_GOOGLE_LOG_EX_0(ClassName, ...) \
418 COMPACT_GOOGLE_LOG_EX_ERROR(ClassName , ##__VA_ARGS__)
419#define COMPACT_GOOGLE_LOG_0 COMPACT_GOOGLE_LOG_ERROR
[email protected]521b0c42010-10-01 23:02:36420// Needed for LOG_IS_ON(ERROR).
Lei Zhang4d9e18572021-04-30 08:57:06421constexpr LogSeverity LOGGING_0 = LOGGING_ERROR;
[email protected]8d127302013-01-10 02:41:57422#endif
[email protected]521b0c42010-10-01 23:02:36423
[email protected]f2c05492014-06-17 12:04:23424// As special cases, we can assume that LOG_IS_ON(FATAL) always holds. Also,
425// LOG_IS_ON(DFATAL) always holds in debug mode. In particular, CHECK()s will
426// always fire if they fail.
[email protected]521b0c42010-10-01 23:02:36427#define LOG_IS_ON(severity) \
Lei Zhang93dd42572020-10-23 18:45:53428 (::logging::ShouldCreateLogMessage(::logging::LOGGING_##severity))
[email protected]521b0c42010-10-01 23:02:36429
Ken MacKay70e8867002019-01-16 00:22:15430// We don't do any caching tricks with VLOG_IS_ON() like the
431// google-glog version since it increases binary size. This means
[email protected]521b0c42010-10-01 23:02:36432// that using the v-logging functions in conjunction with --vmodule
433// may be slow.
434#define VLOG_IS_ON(verboselevel) \
435 ((verboselevel) <= ::logging::GetVlogLevel(__FILE__))
436
437// Helper macro which avoids evaluating the arguments to a stream if
chcunninghamf6a96082015-02-07 01:58:37438// the condition doesn't hold. Condition is evaluated once and only once.
[email protected]521b0c42010-10-01 23:02:36439#define LAZY_STREAM(stream, condition) \
440 !(condition) ? (void) 0 : ::logging::LogMessageVoidify() & (stream)
initial.commitd7cae122008-07-26 21:49:38441
442// We use the preprocessor's merging operator, "##", so that, e.g.,
443// LOG(INFO) becomes the token COMPACT_GOOGLE_LOG_INFO. There's some funny
444// subtle difference between ostream member streaming functions (e.g.,
445// ostream::operator<<(int) and ostream non-member streaming functions
446// (e.g., ::operator<<(ostream&, string&): it turns out that it's
447// impossible to stream something like a string directly to an unnamed
448// ostream. We employ a neat hack by calling the stream() member
449// function of LogMessage which seems to avoid the problem.
[email protected]521b0c42010-10-01 23:02:36450#define LOG_STREAM(severity) COMPACT_GOOGLE_LOG_ ## severity.stream()
initial.commitd7cae122008-07-26 21:49:38451
[email protected]521b0c42010-10-01 23:02:36452#define LOG(severity) LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity))
453#define LOG_IF(severity, condition) \
454 LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity) && (condition))
455
[email protected]162ac0f2010-11-04 15:50:49456// The VLOG macros log with negative verbosities.
457#define VLOG_STREAM(verbose_level) \
Artem Bolgar30e5d692020-12-12 01:15:58458 ::logging::LogMessage(__FILE__, __LINE__, -(verbose_level)).stream()
[email protected]162ac0f2010-11-04 15:50:49459
460#define VLOG(verbose_level) \
461 LAZY_STREAM(VLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level))
462
463#define VLOG_IF(verbose_level, condition) \
464 LAZY_STREAM(VLOG_STREAM(verbose_level), \
465 VLOG_IS_ON(verbose_level) && (condition))
[email protected]99b7c57f2010-09-29 19:26:36466
[email protected]fb879b1a2011-03-06 18:16:31467#if defined (OS_WIN)
468#define VPLOG_STREAM(verbose_level) \
Artem Bolgar30e5d692020-12-12 01:15:58469 ::logging::Win32ErrorLogMessage(__FILE__, __LINE__, -(verbose_level), \
[email protected]fb879b1a2011-03-06 18:16:31470 ::logging::GetLastSystemErrorCode()).stream()
Fabrice de Gans-Riberi306871de2018-05-16 19:38:39471#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
[email protected]fb879b1a2011-03-06 18:16:31472#define VPLOG_STREAM(verbose_level) \
Artem Bolgar30e5d692020-12-12 01:15:58473 ::logging::ErrnoLogMessage(__FILE__, __LINE__, -(verbose_level), \
[email protected]fb879b1a2011-03-06 18:16:31474 ::logging::GetLastSystemErrorCode()).stream()
475#endif
476
477#define VPLOG(verbose_level) \
478 LAZY_STREAM(VPLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level))
479
480#define VPLOG_IF(verbose_level, condition) \
481 LAZY_STREAM(VPLOG_STREAM(verbose_level), \
482 VLOG_IS_ON(verbose_level) && (condition))
483
[email protected]99b7c57f2010-09-29 19:26:36484// TODO(akalin): Add more VLOG variants, e.g. VPLOG.
initial.commitd7cae122008-07-26 21:49:38485
kmarshallfe2f09f82017-04-20 21:05:26486#define LOG_ASSERT(condition) \
487 LOG_IF(FATAL, !(ANALYZER_ASSUME_TRUE(condition))) \
488 << "Assert failed: " #condition ". "
initial.commitd7cae122008-07-26 21:49:38489
[email protected]d8617a62009-10-09 23:52:20490#if defined(OS_WIN)
[email protected]c914d8a2014-04-23 01:11:01491#define PLOG_STREAM(severity) \
[email protected]d8617a62009-10-09 23:52:20492 COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \
493 ::logging::GetLastSystemErrorCode()).stream()
Fabrice de Gans-Riberi306871de2018-05-16 19:38:39494#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
[email protected]c914d8a2014-04-23 01:11:01495#define PLOG_STREAM(severity) \
[email protected]d8617a62009-10-09 23:52:20496 COMPACT_GOOGLE_LOG_EX_ ## severity(ErrnoLogMessage, \
497 ::logging::GetLastSystemErrorCode()).stream()
[email protected]d8617a62009-10-09 23:52:20498#endif
499
[email protected]521b0c42010-10-01 23:02:36500#define PLOG(severity) \
501 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity))
502
[email protected]d8617a62009-10-09 23:52:20503#define PLOG_IF(severity, condition) \
[email protected]521b0c42010-10-01 23:02:36504 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity) && (condition))
[email protected]d8617a62009-10-09 23:52:20505
scottmg3c957a52016-12-10 20:57:59506BASE_EXPORT extern std::ostream* g_swallow_stream;
507
508// Note that g_swallow_stream is used instead of an arbitrary LOG() stream to
509// avoid the creation of an object with a non-trivial destructor (LogMessage).
510// On MSVC x86 (checked on 2015 Update 3), this causes a few additional
511// pointless instructions to be emitted even at full optimization level, even
512// though the : arm of the ternary operator is clearly never executed. Using a
513// simpler object to be &'d with Voidify() avoids these extra instructions.
514// Using a simpler POD object with a templated operator<< also works to avoid
515// these instructions. However, this causes warnings on statically defined
516// implementations of operator<<(std::ostream, ...) in some .cc files, because
517// they become defined-but-unreferenced functions. A reinterpret_cast of 0 to an
518// ostream* also is not suitable, because some compilers warn of undefined
519// behavior.
520#define EAT_STREAM_PARAMETERS \
521 true ? (void)0 \
522 : ::logging::LogMessageVoidify() & (*::logging::g_swallow_stream)
[email protected]ddb9b332011-12-02 07:31:09523
[email protected]d15e56c2010-09-30 21:12:33524// Definitions for DLOG et al.
525
gab190f7542016-08-01 20:03:41526#if DCHECK_IS_ON()
[email protected]d926c202010-10-01 02:58:24527
[email protected]5e987802010-11-01 19:49:22528#define DLOG_IS_ON(severity) LOG_IS_ON(severity)
[email protected]d926c202010-10-01 02:58:24529#define DLOG_IF(severity, condition) LOG_IF(severity, condition)
530#define DLOG_ASSERT(condition) LOG_ASSERT(condition)
[email protected]d926c202010-10-01 02:58:24531#define DPLOG_IF(severity, condition) PLOG_IF(severity, condition)
[email protected]521b0c42010-10-01 23:02:36532#define DVLOG_IF(verboselevel, condition) VLOG_IF(verboselevel, condition)
[email protected]fb879b1a2011-03-06 18:16:31533#define DVPLOG_IF(verboselevel, condition) VPLOG_IF(verboselevel, condition)
[email protected]d926c202010-10-01 02:58:24534
gab190f7542016-08-01 20:03:41535#else // DCHECK_IS_ON()
[email protected]d926c202010-10-01 02:58:24536
gab190f7542016-08-01 20:03:41537// If !DCHECK_IS_ON(), we want to avoid emitting any references to |condition|
538// (which may reference a variable defined only if DCHECK_IS_ON()).
539// Contrast this with DCHECK et al., which has different behavior.
[email protected]d926c202010-10-01 02:58:24540
[email protected]5e987802010-11-01 19:49:22541#define DLOG_IS_ON(severity) false
[email protected]ddb9b332011-12-02 07:31:09542#define DLOG_IF(severity, condition) EAT_STREAM_PARAMETERS
543#define DLOG_ASSERT(condition) EAT_STREAM_PARAMETERS
544#define DPLOG_IF(severity, condition) EAT_STREAM_PARAMETERS
545#define DVLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS
546#define DVPLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS
[email protected]d926c202010-10-01 02:58:24547
gab190f7542016-08-01 20:03:41548#endif // DCHECK_IS_ON()
[email protected]d926c202010-10-01 02:58:24549
[email protected]521b0c42010-10-01 23:02:36550#define DLOG(severity) \
551 LAZY_STREAM(LOG_STREAM(severity), DLOG_IS_ON(severity))
552
[email protected]521b0c42010-10-01 23:02:36553#define DPLOG(severity) \
554 LAZY_STREAM(PLOG_STREAM(severity), DLOG_IS_ON(severity))
555
Ken MacKay70e8867002019-01-16 00:22:15556#define DVLOG(verboselevel) DVLOG_IF(verboselevel, true)
[email protected]521b0c42010-10-01 23:02:36557
Ken MacKay70e8867002019-01-16 00:22:15558#define DVPLOG(verboselevel) DVPLOG_IF(verboselevel, true)
[email protected]fb879b1a2011-03-06 18:16:31559
[email protected]521b0c42010-10-01 23:02:36560// Definitions for DCHECK et al.
[email protected]d926c202010-10-01 02:58:24561
danakje649f572015-01-08 23:35:58562#if DCHECK_IS_ON()
[email protected]e3cca332009-08-20 01:20:29563
Tomas Popelaafffa972018-11-13 20:42:05564#if defined(DCHECK_IS_CONFIGURABLE)
Lei Zhang93dd42572020-10-23 18:45:53565BASE_EXPORT extern LogSeverity LOGGING_DCHECK;
Wez289477f2017-08-24 20:51:30566#else
Lei Zhang4d9e18572021-04-30 08:57:06567constexpr LogSeverity LOGGING_DCHECK = LOGGING_FATAL;
Tomas Popelaafffa972018-11-13 20:42:05568#endif // defined(DCHECK_IS_CONFIGURABLE)
[email protected]521b0c42010-10-01 23:02:36569
danakje649f572015-01-08 23:35:58570#else // DCHECK_IS_ON()
[email protected]521b0c42010-10-01 23:02:36571
Lei Zhang93dd42572020-10-23 18:45:53572// There may be users of LOGGING_DCHECK that are enabled independently
Sigurdur Asgeirsson7013e5f2017-09-29 17:42:58573// of DCHECK_IS_ON(), so default to FATAL logging for those.
Lei Zhang4d9e18572021-04-30 08:57:06574constexpr LogSeverity LOGGING_DCHECK = LOGGING_FATAL;
[email protected]521b0c42010-10-01 23:02:36575
danakje649f572015-01-08 23:35:58576#endif // DCHECK_IS_ON()
[email protected]521b0c42010-10-01 23:02:36577
initial.commitd7cae122008-07-26 21:49:38578// Redefine the standard assert to use our nice log files
579#undef assert
580#define assert(x) DLOG_ASSERT(x)
581
582// This class more or less represents a particular log message. You
583// create an instance of LogMessage and then stream stuff to it.
584// When you finish streaming to it, ~LogMessage is called and the
585// full message gets streamed to the appropriate destination.
586//
587// You shouldn't actually use LogMessage's constructor to log things,
588// though. You should use the LOG() macro (and variants thereof)
589// above.
[email protected]0bea7252011-08-05 15:34:00590class BASE_EXPORT LogMessage {
initial.commitd7cae122008-07-26 21:49:38591 public:
[email protected]bf8ddf13a2014-06-18 15:02:22592 // Used for LOG(severity).
initial.commitd7cae122008-07-26 21:49:38593 LogMessage(const char* file, int line, LogSeverity severity);
594
Lei Zhang93dd42572020-10-23 18:45:53595 // Used for CHECK(). Implied severity = LOGGING_FATAL.
tnagel4a045d3f2015-07-12 14:19:28596 LogMessage(const char* file, int line, const char* condition);
David Bienvenub4b441e2020-09-23 05:49:57597 LogMessage(const LogMessage&) = delete;
598 LogMessage& operator=(const LogMessage&) = delete;
Hans Wennborg12aea3e2020-04-14 15:29:00599 virtual ~LogMessage();
initial.commitd7cae122008-07-26 21:49:38600
601 std::ostream& stream() { return stream_; }
602
pastarmovj89f7ee12016-09-20 14:58:13603 LogSeverity severity() { return severity_; }
604 std::string str() { return stream_.str(); }
605
initial.commitd7cae122008-07-26 21:49:38606 private:
607 void Init(const char* file, int line);
608
David Dorwin11e7c2c12021-04-10 17:01:09609 const LogSeverity severity_;
initial.commitd7cae122008-07-26 21:49:38610 std::ostringstream stream_;
[email protected]c88873922008-07-30 13:02:03611 size_t message_start_; // Offset of the start of the message (past prefix
612 // info).
[email protected]162ac0f2010-11-04 15:50:49613 // The file and line information passed in to the constructor.
David Dorwin11e7c2c12021-04-10 17:01:09614 const char* const file_;
[email protected]162ac0f2010-11-04 15:50:49615 const int line_;
616
[email protected]3f85caa2009-04-14 16:52:11617 // 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 Perazab427af262020-04-13 21:54:42620 base::ScopedClearLastError last_error_;
initial.commitd7cae122008-07-26 21:49:38621
Yuta Hijikata000df18f2020-11-18 06:55:58622#if BUILDFLAG(IS_CHROMEOS_ASH)
Yuta Hijikata9b7279a2020-08-26 16:10:54623 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.commitd7cae122008-07-26 21:49:38633};
634
initial.commitd7cae122008-07-26 21:49:38635// 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]23bb71f2011-04-21 22:22:10638class LogMessageVoidify {
initial.commitd7cae122008-07-26 21:49:38639 public:
Chris Watkins091d6292017-12-13 04:25:58640 LogMessageVoidify() = default;
initial.commitd7cae122008-07-26 21:49:38641 // This has to be an operator with a precedence lower than << but
642 // higher than ?:
643 void operator&(std::ostream&) { }
644};
645
[email protected]d8617a62009-10-09 23:52:20646#if defined(OS_WIN)
647typedef unsigned long SystemErrorCode;
Fabrice de Gans-Riberi306871de2018-05-16 19:38:39648#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
[email protected]d8617a62009-10-09 23:52:20649typedef 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]0bea7252011-08-05 15:34:00654BASE_EXPORT SystemErrorCode GetLastSystemErrorCode();
[email protected]c914d8a2014-04-23 01:11:01655BASE_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code);
[email protected]d8617a62009-10-09 23:52:20656
657#if defined(OS_WIN)
658// Appends a formatted system message of the GetLastError() type.
Hans Wennborg12aea3e2020-04-14 15:29:00659class BASE_EXPORT Win32ErrorLogMessage : public LogMessage {
[email protected]d8617a62009-10-09 23:52:20660 public:
661 Win32ErrorLogMessage(const char* file,
662 int line,
663 LogSeverity severity,
[email protected]d8617a62009-10-09 23:52:20664 SystemErrorCode err);
David Bienvenub4b441e2020-09-23 05:49:57665 Win32ErrorLogMessage(const Win32ErrorLogMessage&) = delete;
666 Win32ErrorLogMessage& operator=(const Win32ErrorLogMessage&) = delete;
[email protected]d8617a62009-10-09 23:52:20667 // Appends the error message before destructing the encapsulated class.
Hans Wennborg12aea3e2020-04-14 15:29:00668 ~Win32ErrorLogMessage() override;
[email protected]a502bbe72011-01-07 18:06:45669
[email protected]d8617a62009-10-09 23:52:20670 private:
671 SystemErrorCode err_;
[email protected]d8617a62009-10-09 23:52:20672};
Fabrice de Gans-Riberi306871de2018-05-16 19:38:39673#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
[email protected]d8617a62009-10-09 23:52:20674// Appends a formatted system message of the errno type
Hans Wennborg12aea3e2020-04-14 15:29:00675class BASE_EXPORT ErrnoLogMessage : public LogMessage {
[email protected]d8617a62009-10-09 23:52:20676 public:
677 ErrnoLogMessage(const char* file,
678 int line,
679 LogSeverity severity,
680 SystemErrorCode err);
David Bienvenub4b441e2020-09-23 05:49:57681 ErrnoLogMessage(const ErrnoLogMessage&) = delete;
682 ErrnoLogMessage& operator=(const ErrnoLogMessage&) = delete;
[email protected]d8617a62009-10-09 23:52:20683 // Appends the error message before destructing the encapsulated class.
Hans Wennborg12aea3e2020-04-14 15:29:00684 ~ErrnoLogMessage() override;
[email protected]a502bbe72011-01-07 18:06:45685
[email protected]d8617a62009-10-09 23:52:20686 private:
687 SystemErrorCode err_;
[email protected]d8617a62009-10-09 23:52:20688};
689#endif // OS_WIN
690
initial.commitd7cae122008-07-26 21:49:38691// 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]0bea7252011-08-05 15:34:00695BASE_EXPORT void CloseLogFile();
initial.commitd7cae122008-07-26 21:49:38696
Yuta Hijikata000df18f2020-11-18 06:55:58697#if BUILDFLAG(IS_CHROMEOS_ASH)
Robbie McElrath8bf49842019-08-20 22:22:53698// 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.
702BASE_EXPORT FILE* DuplicateLogFILE();
703#endif
704
[email protected]e36ddc82009-12-08 04:22:50705// Async signal safe logging mechanism.
[email protected]0bea7252011-08-05 15:34:00706BASE_EXPORT void RawLog(int level, const char* message);
[email protected]e36ddc82009-12-08 04:22:50707
tsniatowski612550f2016-07-21 18:26:20708#define RAW_LOG(level, message) \
Lei Zhang93dd42572020-10-23 18:45:53709 ::logging::RawLog(::logging::LOGGING_##level, message)
[email protected]e36ddc82009-12-08 04:22:50710
[email protected]f01b88a2013-02-27 22:04:00711#if defined(OS_WIN)
ananta61762fb2015-09-18 01:00:09712// Returns true if logging to file is enabled.
713BASE_EXPORT bool IsLoggingToFileEnabled();
714
[email protected]f01b88a2013-02-27 22:04:00715// Returns the default log file path.
Jan Wilken Dörrieb630aca2019-12-04 10:59:11716BASE_EXPORT std::wstring GetLogFileFullPath();
[email protected]f01b88a2013-02-27 22:04:00717#endif
718
[email protected]39be4242008-08-07 18:31:40719} // namespace logging
initial.commitd7cae122008-07-26 21:49:38720
[email protected]81411c62014-07-08 23:03:06721// 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.
729namespace std {
[email protected]46ce5b562010-06-16 18:39:53730// 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]0bea7252011-08-05 15:34:00736BASE_EXPORT std::ostream& operator<<(std::ostream& out, const wchar_t* wstr);
Jan Wilken Dörrie4a498d8c2021-01-20 10:19:39737BASE_EXPORT std::ostream& operator<<(std::ostream& out,
738 const std::wstring& wstr);
739
740BASE_EXPORT std::ostream& operator<<(std::ostream& out, const char16_t* str16);
741BASE_EXPORT std::ostream& operator<<(std::ostream& out,
742 const std::u16string& str16);
[email protected]81411c62014-07-08 23:03:06743} // namespace std
[email protected]46ce5b562010-06-16 18:39:53744
[email protected]39be4242008-08-07 18:31:40745#endif // BASE_LOGGING_H_