blob: e3f0cefaa073ea0571d60b3500879d1a9d93e963 [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>
initial.commitd7cae122008-07-26 21:49:3811#include <cstring>
12#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"
[email protected]ddb9b332011-12-02 07:31:0916#include "base/debug/debugger.h"
avi9b6f42932015-12-26 22:15:1417#include "base/macros.h"
[email protected]90509cb2011-03-25 18:46:3818#include "build/build_config.h"
initial.commitd7cae122008-07-26 21:49:3819
20//
21// Optional message capabilities
22// -----------------------------
23// Assertion failed messages and fatal errors are displayed in a dialog box
24// before the application exits. However, running this UI creates a message
25// loop, which causes application messages to be processed and potentially
26// dispatched to existing application windows. Since the application is in a
27// bad state when this assertion dialog is displayed, these messages may not
28// get processed and hang the dialog, or the application might go crazy.
29//
30// Therefore, it can be beneficial to display the error dialog in a separate
31// process from the main application. When the logging system needs to display
32// a fatal error dialog box, it will look for a program called
33// "DebugMessage.exe" in the same directory as the application executable. It
34// will run this application with the message as the command line, and will
35// not include the name of the application as is traditional for easier
36// parsing.
37//
38// The code for DebugMessage.exe is only one line. In WinMain, do:
39// MessageBox(NULL, GetCommandLineW(), L"Fatal Error", 0);
40//
41// If DebugMessage.exe is not found, the logging code will use a normal
42// MessageBox, potentially causing the problems discussed above.
43
44
45// Instructions
46// ------------
47//
48// Make a bunch of macros for logging. The way to log things is to stream
49// things to LOG(<a particular severity level>). E.g.,
50//
51// LOG(INFO) << "Found " << num_cookies << " cookies";
52//
53// You can also do conditional logging:
54//
55// LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
56//
initial.commitd7cae122008-07-26 21:49:3857// The CHECK(condition) macro is active in both debug and release builds and
58// effectively performs a LOG(FATAL) which terminates the process and
59// generates a crashdump unless a debugger is attached.
60//
61// There are also "debug mode" logging macros like the ones above:
62//
63// DLOG(INFO) << "Found cookies";
64//
65// DLOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
66//
67// All "debug mode" logging is compiled away to nothing for non-debug mode
68// compiles. LOG_IF and development flags also work well together
69// because the code can be compiled away sometimes.
70//
71// We also have
72//
73// LOG_ASSERT(assertion);
74// DLOG_ASSERT(assertion);
75//
76// which is syntactic sugar for {,D}LOG_IF(FATAL, assert fails) << assertion;
77//
[email protected]99b7c57f2010-09-29 19:26:3678// There are "verbose level" logging macros. They look like
79//
80// VLOG(1) << "I'm printed when you run the program with --v=1 or more";
81// VLOG(2) << "I'm printed when you run the program with --v=2 or more";
82//
83// These always log at the INFO log level (when they log at all).
84// The verbose logging can also be turned on module-by-module. For instance,
[email protected]b0d38d4c2010-10-29 00:39:4885// --vmodule=profile=2,icon_loader=1,browser_*=3,*/chromeos/*=4 --v=0
[email protected]99b7c57f2010-09-29 19:26:3686// will cause:
87// a. VLOG(2) and lower messages to be printed from profile.{h,cc}
88// b. VLOG(1) and lower messages to be printed from icon_loader.{h,cc}
89// c. VLOG(3) and lower messages to be printed from files prefixed with
90// "browser"
[email protected]e11de722010-11-01 20:50:5591// d. VLOG(4) and lower messages to be printed from files under a
[email protected]b0d38d4c2010-10-29 00:39:4892// "chromeos" directory.
[email protected]e11de722010-11-01 20:50:5593// e. VLOG(0) and lower messages to be printed from elsewhere
[email protected]99b7c57f2010-09-29 19:26:3694//
95// The wildcarding functionality shown by (c) supports both '*' (match
[email protected]b0d38d4c2010-10-29 00:39:4896// 0 or more characters) and '?' (match any single character)
97// wildcards. Any pattern containing a forward or backward slash will
98// be tested against the whole pathname and not just the module.
99// E.g., "*/foo/bar/*=2" would change the logging level for all code
100// in source files under a "foo/bar" directory.
[email protected]99b7c57f2010-09-29 19:26:36101//
102// There's also VLOG_IS_ON(n) "verbose level" condition macro. To be used as
103//
104// if (VLOG_IS_ON(2)) {
105// // do some logging preparation and logging
106// // that can't be accomplished with just VLOG(2) << ...;
107// }
108//
109// There is also a VLOG_IF "verbose level" condition macro for sample
110// cases, when some extra computation and preparation for logs is not
111// needed.
112//
113// VLOG_IF(1, (size > 1024))
114// << "I'm printed when size is more than 1024 and when you run the "
115// "program with --v=1 or more";
116//
initial.commitd7cae122008-07-26 21:49:38117// We also override the standard 'assert' to use 'DLOG_ASSERT'.
118//
[email protected]d8617a62009-10-09 23:52:20119// Lastly, there is:
120//
121// PLOG(ERROR) << "Couldn't do foo";
122// DPLOG(ERROR) << "Couldn't do foo";
123// PLOG_IF(ERROR, cond) << "Couldn't do foo";
124// DPLOG_IF(ERROR, cond) << "Couldn't do foo";
125// PCHECK(condition) << "Couldn't do foo";
126// DPCHECK(condition) << "Couldn't do foo";
127//
128// which append the last system error to the message in string form (taken from
129// GetLastError() on Windows and errno on POSIX).
130//
initial.commitd7cae122008-07-26 21:49:38131// The supported severity levels for macros that allow you to specify one
[email protected]f2c05492014-06-17 12:04:23132// are (in increasing order of severity) INFO, WARNING, ERROR, and FATAL.
initial.commitd7cae122008-07-26 21:49:38133//
134// Very important: logging a message at the FATAL severity level causes
135// the program to terminate (after the message is logged).
[email protected]fb62a532009-02-12 01:19:05136//
[email protected]f2c05492014-06-17 12:04:23137// There is the special severity of DFATAL, which logs FATAL in debug mode,
138// ERROR in normal mode.
initial.commitd7cae122008-07-26 21:49:38139
140namespace logging {
141
[email protected]5e3f7c22013-06-21 21:15:33142// TODO(avi): do we want to do a unification of character types here?
143#if defined(OS_WIN)
144typedef wchar_t PathChar;
145#else
146typedef char PathChar;
147#endif
148
149// Where to record logging output? A flat file and/or system debug log
150// via OutputDebugString.
151enum LoggingDestination {
152 LOG_NONE = 0,
153 LOG_TO_FILE = 1 << 0,
154 LOG_TO_SYSTEM_DEBUG_LOG = 1 << 1,
155
156 LOG_TO_ALL = LOG_TO_FILE | LOG_TO_SYSTEM_DEBUG_LOG,
157
158 // On Windows, use a file next to the exe; on POSIX platforms, where
159 // it may not even be possible to locate the executable on disk, use
160 // stderr.
161#if defined(OS_WIN)
162 LOG_DEFAULT = LOG_TO_FILE,
163#elif defined(OS_POSIX)
164 LOG_DEFAULT = LOG_TO_SYSTEM_DEBUG_LOG,
165#endif
166};
initial.commitd7cae122008-07-26 21:49:38167
168// Indicates that the log file should be locked when being written to.
[email protected]5e3f7c22013-06-21 21:15:33169// Unless there is only one single-threaded process that is logging to
170// the log file, the file should be locked during writes to make each
[email protected]3ee50d12014-03-05 01:43:27171// log output atomic. Other writers will block.
initial.commitd7cae122008-07-26 21:49:38172//
173// All processes writing to the log file must have their locking set for it to
[email protected]5e3f7c22013-06-21 21:15:33174// work properly. Defaults to LOCK_LOG_FILE.
initial.commitd7cae122008-07-26 21:49:38175enum LogLockingState { LOCK_LOG_FILE, DONT_LOCK_LOG_FILE };
176
177// On startup, should we delete or append to an existing log file (if any)?
178// Defaults to APPEND_TO_OLD_LOG_FILE.
179enum OldFileDeletionState { DELETE_OLD_LOG_FILE, APPEND_TO_OLD_LOG_FILE };
180
[email protected]5e3f7c22013-06-21 21:15:33181struct BASE_EXPORT LoggingSettings {
182 // The defaults values are:
183 //
184 // logging_dest: LOG_DEFAULT
185 // log_file: NULL
186 // lock_log: LOCK_LOG_FILE
187 // delete_old: APPEND_TO_OLD_LOG_FILE
[email protected]5e3f7c22013-06-21 21:15:33188 LoggingSettings();
189
190 LoggingDestination logging_dest;
191
192 // The three settings below have an effect only when LOG_TO_FILE is
193 // set in |logging_dest|.
194 const PathChar* log_file;
195 LogLockingState lock_log;
196 OldFileDeletionState delete_old;
[email protected]5e3f7c22013-06-21 21:15:33197};
[email protected]ff3d0c32010-08-23 19:57:46198
199// Define different names for the BaseInitLoggingImpl() function depending on
200// whether NDEBUG is defined or not so that we'll fail to link if someone tries
201// to compile logging.cc with NDEBUG but includes logging.h without defining it,
202// or vice versa.
203#if NDEBUG
204#define BaseInitLoggingImpl BaseInitLoggingImpl_built_with_NDEBUG
205#else
206#define BaseInitLoggingImpl BaseInitLoggingImpl_built_without_NDEBUG
207#endif
208
209// Implementation of the InitLogging() method declared below. We use a
210// more-specific name so we can #define it above without affecting other code
211// that has named stuff "InitLogging".
[email protected]5e3f7c22013-06-21 21:15:33212BASE_EXPORT bool BaseInitLoggingImpl(const LoggingSettings& settings);
[email protected]ff3d0c32010-08-23 19:57:46213
initial.commitd7cae122008-07-26 21:49:38214// Sets the log file name and other global logging state. Calling this function
215// is recommended, and is normally done at the beginning of application init.
216// If you don't call it, all the flags will be initialized to their default
217// values, and there is a race condition that may leak a critical section
218// object if two threads try to do the first log at the same time.
219// See the definition of the enums above for descriptions and default values.
220//
221// The default log file is initialized to "debug.log" in the application
222// directory. You probably don't want this, especially since the program
223// directory may not be writable on an enduser's system.
[email protected]064aa162011-12-03 00:30:08224//
225// This function may be called a second time to re-direct logging (e.g after
226// loging in to a user partition), however it should never be called more than
227// twice.
[email protected]5e3f7c22013-06-21 21:15:33228inline bool InitLogging(const LoggingSettings& settings) {
229 return BaseInitLoggingImpl(settings);
[email protected]ff3d0c32010-08-23 19:57:46230}
initial.commitd7cae122008-07-26 21:49:38231
232// Sets the log level. Anything at or above this level will be written to the
233// log file/displayed to the user (if applicable). Anything below this level
[email protected]162ac0f2010-11-04 15:50:49234// will be silently ignored. The log level defaults to 0 (everything is logged
235// up to level INFO) if this function is not called.
236// Note that log messages for VLOG(x) are logged at level -x, so setting
237// the min log level to negative values enables verbose logging.
[email protected]0bea7252011-08-05 15:34:00238BASE_EXPORT void SetMinLogLevel(int level);
initial.commitd7cae122008-07-26 21:49:38239
[email protected]8a2986ca2009-04-10 19:13:42240// Gets the current log level.
[email protected]0bea7252011-08-05 15:34:00241BASE_EXPORT int GetMinLogLevel();
initial.commitd7cae122008-07-26 21:49:38242
skobesc78c0ad72015-12-07 20:21:23243// Used by LOG_IS_ON to lazy-evaluate stream arguments.
244BASE_EXPORT bool ShouldCreateLogMessage(int severity);
245
[email protected]162ac0f2010-11-04 15:50:49246// Gets the VLOG default verbosity level.
[email protected]0bea7252011-08-05 15:34:00247BASE_EXPORT int GetVlogVerbosity();
[email protected]162ac0f2010-11-04 15:50:49248
[email protected]99b7c57f2010-09-29 19:26:36249// Gets the current vlog level for the given file (usually taken from
250// __FILE__).
[email protected]2f4e9a62010-09-29 21:25:14251
252// Note that |N| is the size *with* the null terminator.
[email protected]0bea7252011-08-05 15:34:00253BASE_EXPORT int GetVlogLevelHelper(const char* file_start, size_t N);
[email protected]2f4e9a62010-09-29 21:25:14254
[email protected]99b7c57f2010-09-29 19:26:36255template <size_t N>
256int GetVlogLevel(const char (&file)[N]) {
257 return GetVlogLevelHelper(file, N);
258}
initial.commitd7cae122008-07-26 21:49:38259
260// Sets the common items you want to be prepended to each log message.
261// process and thread IDs default to off, the timestamp defaults to on.
262// If this function is not called, logging defaults to writing the timestamp
263// only.
[email protected]0bea7252011-08-05 15:34:00264BASE_EXPORT void SetLogItems(bool enable_process_id, bool enable_thread_id,
265 bool enable_timestamp, bool enable_tickcount);
initial.commitd7cae122008-07-26 21:49:38266
[email protected]81e0a852010-08-17 00:38:12267// Sets whether or not you'd like to see fatal debug messages popped up in
268// a dialog box or not.
269// Dialogs are not shown by default.
[email protected]0bea7252011-08-05 15:34:00270BASE_EXPORT void SetShowErrorDialogs(bool enable_dialogs);
[email protected]81e0a852010-08-17 00:38:12271
initial.commitd7cae122008-07-26 21:49:38272// Sets the Log Assert Handler that will be used to notify of check failures.
[email protected]fb62a532009-02-12 01:19:05273// The default handler shows a dialog box and then terminate the process,
274// however clients can use this function to override with their own handling
275// (e.g. a silent one for Unit Tests)
initial.commitd7cae122008-07-26 21:49:38276typedef void (*LogAssertHandlerFunction)(const std::string& str);
[email protected]0bea7252011-08-05 15:34:00277BASE_EXPORT void SetLogAssertHandler(LogAssertHandlerFunction handler);
[email protected]64e5cc02010-11-03 19:20:27278
[email protected]2b07b8412009-11-25 15:26:34279// Sets the Log Message Handler that gets passed every log message before
280// it's sent to other log destinations (if any).
281// Returns true to signal that it handled the message and the message
282// should not be sent to other log destinations.
[email protected]162ac0f2010-11-04 15:50:49283typedef bool (*LogMessageHandlerFunction)(int severity,
284 const char* file, int line, size_t message_start, const std::string& str);
[email protected]0bea7252011-08-05 15:34:00285BASE_EXPORT void SetLogMessageHandler(LogMessageHandlerFunction handler);
286BASE_EXPORT LogMessageHandlerFunction GetLogMessageHandler();
[email protected]2b07b8412009-11-25 15:26:34287
initial.commitd7cae122008-07-26 21:49:38288typedef int LogSeverity;
[email protected]162ac0f2010-11-04 15:50:49289const LogSeverity LOG_VERBOSE = -1; // This is level 1 verbosity
290// Note: the log severities are used to index into the array of names,
291// see log_severity_names.
initial.commitd7cae122008-07-26 21:49:38292const LogSeverity LOG_INFO = 0;
293const LogSeverity LOG_WARNING = 1;
294const LogSeverity LOG_ERROR = 2;
[email protected]f2c05492014-06-17 12:04:23295const LogSeverity LOG_FATAL = 3;
296const LogSeverity LOG_NUM_SEVERITIES = 4;
initial.commitd7cae122008-07-26 21:49:38297
[email protected]521b0c42010-10-01 23:02:36298// LOG_DFATAL is LOG_FATAL in debug mode, ERROR in normal mode
initial.commitd7cae122008-07-26 21:49:38299#ifdef NDEBUG
[email protected]521b0c42010-10-01 23:02:36300const LogSeverity LOG_DFATAL = LOG_ERROR;
initial.commitd7cae122008-07-26 21:49:38301#else
[email protected]521b0c42010-10-01 23:02:36302const LogSeverity LOG_DFATAL = LOG_FATAL;
initial.commitd7cae122008-07-26 21:49:38303#endif
304
305// A few definitions of macros that don't generate much code. These are used
306// by LOG() and LOG_IF, etc. Since these are used all over our code, it's
307// better to have compact code for these operations.
[email protected]d8617a62009-10-09 23:52:20308#define COMPACT_GOOGLE_LOG_EX_INFO(ClassName, ...) \
309 logging::ClassName(__FILE__, __LINE__, logging::LOG_INFO , ##__VA_ARGS__)
310#define COMPACT_GOOGLE_LOG_EX_WARNING(ClassName, ...) \
311 logging::ClassName(__FILE__, __LINE__, logging::LOG_WARNING , ##__VA_ARGS__)
312#define COMPACT_GOOGLE_LOG_EX_ERROR(ClassName, ...) \
313 logging::ClassName(__FILE__, __LINE__, logging::LOG_ERROR , ##__VA_ARGS__)
[email protected]d8617a62009-10-09 23:52:20314#define COMPACT_GOOGLE_LOG_EX_FATAL(ClassName, ...) \
315 logging::ClassName(__FILE__, __LINE__, logging::LOG_FATAL , ##__VA_ARGS__)
316#define COMPACT_GOOGLE_LOG_EX_DFATAL(ClassName, ...) \
[email protected]521b0c42010-10-01 23:02:36317 logging::ClassName(__FILE__, __LINE__, logging::LOG_DFATAL , ##__VA_ARGS__)
[email protected]d8617a62009-10-09 23:52:20318
initial.commitd7cae122008-07-26 21:49:38319#define COMPACT_GOOGLE_LOG_INFO \
[email protected]d8617a62009-10-09 23:52:20320 COMPACT_GOOGLE_LOG_EX_INFO(LogMessage)
initial.commitd7cae122008-07-26 21:49:38321#define COMPACT_GOOGLE_LOG_WARNING \
[email protected]d8617a62009-10-09 23:52:20322 COMPACT_GOOGLE_LOG_EX_WARNING(LogMessage)
initial.commitd7cae122008-07-26 21:49:38323#define COMPACT_GOOGLE_LOG_ERROR \
[email protected]d8617a62009-10-09 23:52:20324 COMPACT_GOOGLE_LOG_EX_ERROR(LogMessage)
initial.commitd7cae122008-07-26 21:49:38325#define COMPACT_GOOGLE_LOG_FATAL \
[email protected]d8617a62009-10-09 23:52:20326 COMPACT_GOOGLE_LOG_EX_FATAL(LogMessage)
initial.commitd7cae122008-07-26 21:49:38327#define COMPACT_GOOGLE_LOG_DFATAL \
[email protected]d8617a62009-10-09 23:52:20328 COMPACT_GOOGLE_LOG_EX_DFATAL(LogMessage)
initial.commitd7cae122008-07-26 21:49:38329
[email protected]8d127302013-01-10 02:41:57330#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38331// wingdi.h defines ERROR to be 0. When we call LOG(ERROR), it gets
332// substituted with 0, and it expands to COMPACT_GOOGLE_LOG_0. To allow us
333// to keep using this syntax, we define this macro to do the same thing
334// as COMPACT_GOOGLE_LOG_ERROR, and also define ERROR the same way that
335// the Windows SDK does for consistency.
336#define ERROR 0
[email protected]d8617a62009-10-09 23:52:20337#define COMPACT_GOOGLE_LOG_EX_0(ClassName, ...) \
338 COMPACT_GOOGLE_LOG_EX_ERROR(ClassName , ##__VA_ARGS__)
339#define COMPACT_GOOGLE_LOG_0 COMPACT_GOOGLE_LOG_ERROR
[email protected]521b0c42010-10-01 23:02:36340// Needed for LOG_IS_ON(ERROR).
341const LogSeverity LOG_0 = LOG_ERROR;
[email protected]8d127302013-01-10 02:41:57342#endif
[email protected]521b0c42010-10-01 23:02:36343
[email protected]f2c05492014-06-17 12:04:23344// As special cases, we can assume that LOG_IS_ON(FATAL) always holds. Also,
345// LOG_IS_ON(DFATAL) always holds in debug mode. In particular, CHECK()s will
346// always fire if they fail.
[email protected]521b0c42010-10-01 23:02:36347#define LOG_IS_ON(severity) \
skobesc78c0ad72015-12-07 20:21:23348 (::logging::ShouldCreateLogMessage(::logging::LOG_##severity))
[email protected]521b0c42010-10-01 23:02:36349
350// We can't do any caching tricks with VLOG_IS_ON() like the
351// google-glog version since it requires GCC extensions. This means
352// that using the v-logging functions in conjunction with --vmodule
353// may be slow.
354#define VLOG_IS_ON(verboselevel) \
355 ((verboselevel) <= ::logging::GetVlogLevel(__FILE__))
356
357// Helper macro which avoids evaluating the arguments to a stream if
chcunninghamf6a96082015-02-07 01:58:37358// the condition doesn't hold. Condition is evaluated once and only once.
[email protected]521b0c42010-10-01 23:02:36359#define LAZY_STREAM(stream, condition) \
360 !(condition) ? (void) 0 : ::logging::LogMessageVoidify() & (stream)
initial.commitd7cae122008-07-26 21:49:38361
362// We use the preprocessor's merging operator, "##", so that, e.g.,
363// LOG(INFO) becomes the token COMPACT_GOOGLE_LOG_INFO. There's some funny
364// subtle difference between ostream member streaming functions (e.g.,
365// ostream::operator<<(int) and ostream non-member streaming functions
366// (e.g., ::operator<<(ostream&, string&): it turns out that it's
367// impossible to stream something like a string directly to an unnamed
368// ostream. We employ a neat hack by calling the stream() member
369// function of LogMessage which seems to avoid the problem.
[email protected]521b0c42010-10-01 23:02:36370#define LOG_STREAM(severity) COMPACT_GOOGLE_LOG_ ## severity.stream()
initial.commitd7cae122008-07-26 21:49:38371
[email protected]521b0c42010-10-01 23:02:36372#define LOG(severity) LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity))
373#define LOG_IF(severity, condition) \
374 LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity) && (condition))
375
[email protected]162ac0f2010-11-04 15:50:49376// The VLOG macros log with negative verbosities.
377#define VLOG_STREAM(verbose_level) \
378 logging::LogMessage(__FILE__, __LINE__, -verbose_level).stream()
379
380#define VLOG(verbose_level) \
381 LAZY_STREAM(VLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level))
382
383#define VLOG_IF(verbose_level, condition) \
384 LAZY_STREAM(VLOG_STREAM(verbose_level), \
385 VLOG_IS_ON(verbose_level) && (condition))
[email protected]99b7c57f2010-09-29 19:26:36386
[email protected]fb879b1a2011-03-06 18:16:31387#if defined (OS_WIN)
388#define VPLOG_STREAM(verbose_level) \
389 logging::Win32ErrorLogMessage(__FILE__, __LINE__, -verbose_level, \
390 ::logging::GetLastSystemErrorCode()).stream()
391#elif defined(OS_POSIX)
392#define VPLOG_STREAM(verbose_level) \
393 logging::ErrnoLogMessage(__FILE__, __LINE__, -verbose_level, \
394 ::logging::GetLastSystemErrorCode()).stream()
395#endif
396
397#define VPLOG(verbose_level) \
398 LAZY_STREAM(VPLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level))
399
400#define VPLOG_IF(verbose_level, condition) \
401 LAZY_STREAM(VPLOG_STREAM(verbose_level), \
402 VLOG_IS_ON(verbose_level) && (condition))
403
[email protected]99b7c57f2010-09-29 19:26:36404// TODO(akalin): Add more VLOG variants, e.g. VPLOG.
initial.commitd7cae122008-07-26 21:49:38405
initial.commitd7cae122008-07-26 21:49:38406#define LOG_ASSERT(condition) \
407 LOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". "
initial.commitd7cae122008-07-26 21:49:38408
[email protected]d8617a62009-10-09 23:52:20409#if defined(OS_WIN)
[email protected]c914d8a2014-04-23 01:11:01410#define PLOG_STREAM(severity) \
[email protected]d8617a62009-10-09 23:52:20411 COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \
412 ::logging::GetLastSystemErrorCode()).stream()
[email protected]d8617a62009-10-09 23:52:20413#elif defined(OS_POSIX)
[email protected]c914d8a2014-04-23 01:11:01414#define PLOG_STREAM(severity) \
[email protected]d8617a62009-10-09 23:52:20415 COMPACT_GOOGLE_LOG_EX_ ## severity(ErrnoLogMessage, \
416 ::logging::GetLastSystemErrorCode()).stream()
[email protected]d8617a62009-10-09 23:52:20417#endif
418
[email protected]521b0c42010-10-01 23:02:36419#define PLOG(severity) \
420 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity))
421
[email protected]d8617a62009-10-09 23:52:20422#define PLOG_IF(severity, condition) \
[email protected]521b0c42010-10-01 23:02:36423 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity) && (condition))
[email protected]d8617a62009-10-09 23:52:20424
[email protected]ddb9b332011-12-02 07:31:09425// The actual stream used isn't important.
426#define EAT_STREAM_PARAMETERS \
427 true ? (void) 0 : ::logging::LogMessageVoidify() & LOG_STREAM(FATAL)
428
erikwright6ad937b2015-07-22 20:05:52429// Captures the result of a CHECK_EQ (for example) and facilitates testing as a
430// boolean.
431class CheckOpResult {
432 public:
wezf01a9b72016-03-19 01:18:07433 // |message| must be non-null if and only if the check failed.
erikwright6ad937b2015-07-22 20:05:52434 CheckOpResult(std::string* message) : message_(message) {}
435 // Returns true if the check succeeded.
436 operator bool() const { return !message_; }
437 // Returns the message.
438 std::string* message() { return message_; }
439
440 private:
441 std::string* message_;
442};
443
initial.commitd7cae122008-07-26 21:49:38444// CHECK dies with a fatal error if condition is not true. It is *not*
445// controlled by NDEBUG, so the check will be executed regardless of
446// compilation mode.
[email protected]521b0c42010-10-01 23:02:36447//
448// We make sure CHECK et al. always evaluates their arguments, as
449// doing CHECK(FunctionWithSideEffect()) is a common idiom.
[email protected]ddb9b332011-12-02 07:31:09450
[email protected]84921ef2014-07-08 22:52:33451#if defined(OFFICIAL_BUILD) && defined(NDEBUG) && !defined(OS_ANDROID)
[email protected]ddb9b332011-12-02 07:31:09452
453// Make all CHECK functions discard their log strings to reduce code
tnagelbe15c56b702015-07-12 14:17:45454// bloat for official release builds (except Android).
[email protected]ddb9b332011-12-02 07:31:09455
456// TODO(akalin): This would be more valuable if there were some way to
457// remove BreakDebugger() from the backtrace, perhaps by turning it
458// into a macro (like __debugbreak() on Windows).
459#define CHECK(condition) \
460 !(condition) ? ::base::debug::BreakDebugger() : EAT_STREAM_PARAMETERS
461
462#define PCHECK(condition) CHECK(condition)
463
464#define CHECK_OP(name, op, val1, val2) CHECK((val1) op (val2))
465
466#else
467
brucedawson9d160252014-10-23 20:14:14468#if defined(_PREFAST_) && defined(OS_WIN)
469// Use __analysis_assume to tell the VC++ static analysis engine that
470// assert conditions are true, to suppress warnings. The LAZY_STREAM
471// parameter doesn't reference 'condition' in /analyze builds because
472// this evaluation confuses /analyze. The !! before condition is because
473// __analysis_assume gets confused on some conditions:
474// http://randomascii.wordpress.com/2011/09/13/analyze-for-visual-studio-the-ugly-part-5/
475
476#define CHECK(condition) \
477 __analysis_assume(!!(condition)), \
478 LAZY_STREAM(LOG_STREAM(FATAL), false) \
479 << "Check failed: " #condition ". "
480
481#define PCHECK(condition) \
482 __analysis_assume(!!(condition)), \
483 LAZY_STREAM(PLOG_STREAM(FATAL), false) \
484 << "Check failed: " #condition ". "
485
486#else // _PREFAST_
487
tnagel4a045d3f2015-07-12 14:19:28488// Do as much work as possible out of line to reduce inline code size.
489#define CHECK(condition) \
490 LAZY_STREAM(logging::LogMessage(__FILE__, __LINE__, #condition).stream(), \
491 !(condition))
initial.commitd7cae122008-07-26 21:49:38492
brucedawson9d160252014-10-23 20:14:14493#define PCHECK(condition) \
[email protected]521b0c42010-10-01 23:02:36494 LAZY_STREAM(PLOG_STREAM(FATAL), !(condition)) \
495 << "Check failed: " #condition ". "
[email protected]d8617a62009-10-09 23:52:20496
brucedawson9d160252014-10-23 20:14:14497#endif // _PREFAST_
498
[email protected]ddb9b332011-12-02 07:31:09499// Helper macro for binary operators.
500// Don't use this macro directly in your code, use CHECK_EQ et al below.
erikwright6ad937b2015-07-22 20:05:52501// The 'switch' is used to prevent the 'else' from being ambiguous when the
502// macro is used in an 'if' clause such as:
503// if (a == 1)
504// CHECK_EQ(2, a);
505#define CHECK_OP(name, op, val1, val2) \
506 switch (0) case 0: default: \
507 if (logging::CheckOpResult true_if_passed = \
508 logging::Check##name##Impl((val1), (val2), \
509 #val1 " " #op " " #val2)) \
510 ; \
511 else \
512 logging::LogMessage(__FILE__, __LINE__, true_if_passed.message()).stream()
[email protected]ddb9b332011-12-02 07:31:09513
514#endif
515
brucedawson93a60b8c2016-04-28 20:46:16516// This formats a value for a failing CHECK_XX statement. Ordinarily,
517// it uses the definition for operator<<, with a few special cases below.
518template <typename T>
519inline void MakeCheckOpValueString(std::ostream* os, const T& v) {
520 (*os) << v;
521}
522
523// We need an explicit specialization for std::nullptr_t.
524template <>
525BASE_EXPORT void MakeCheckOpValueString(std::ostream* os,
526 const std::nullptr_t& p);
527
initial.commitd7cae122008-07-26 21:49:38528// Build the error message string. This is separate from the "Impl"
529// function template because it is not performance critical and so can
[email protected]9c7132e2011-02-08 07:39:08530// be out of line, while the "Impl" code should be inline. Caller
531// takes ownership of the returned string.
initial.commitd7cae122008-07-26 21:49:38532template<class t1, class t2>
533std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) {
534 std::ostringstream ss;
brucedawson93a60b8c2016-04-28 20:46:16535 ss << names << " (";
536 MakeCheckOpValueString(&ss, v1);
537 ss << " vs. ";
538 MakeCheckOpValueString(&ss, v2);
539 ss << ")";
initial.commitd7cae122008-07-26 21:49:38540 std::string* msg = new std::string(ss.str());
541 return msg;
542}
543
[email protected]6d445d32010-09-30 19:10:03544// Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated
545// in logging.cc.
[email protected]dc72da32011-10-24 20:20:30546extern template BASE_EXPORT std::string* MakeCheckOpString<int, int>(
[email protected]6d445d32010-09-30 19:10:03547 const int&, const int&, const char* names);
[email protected]dc72da32011-10-24 20:20:30548extern template BASE_EXPORT
549std::string* MakeCheckOpString<unsigned long, unsigned long>(
[email protected]6d445d32010-09-30 19:10:03550 const unsigned long&, const unsigned long&, const char* names);
[email protected]dc72da32011-10-24 20:20:30551extern template BASE_EXPORT
552std::string* MakeCheckOpString<unsigned long, unsigned int>(
[email protected]6d445d32010-09-30 19:10:03553 const unsigned long&, const unsigned int&, const char* names);
[email protected]dc72da32011-10-24 20:20:30554extern template BASE_EXPORT
555std::string* MakeCheckOpString<unsigned int, unsigned long>(
[email protected]6d445d32010-09-30 19:10:03556 const unsigned int&, const unsigned long&, const char* names);
[email protected]dc72da32011-10-24 20:20:30557extern template BASE_EXPORT
558std::string* MakeCheckOpString<std::string, std::string>(
[email protected]6d445d32010-09-30 19:10:03559 const std::string&, const std::string&, const char* name);
initial.commitd7cae122008-07-26 21:49:38560
[email protected]71512602010-11-01 22:19:56561// Helper functions for CHECK_OP macro.
562// The (int, int) specialization works around the issue that the compiler
563// will not instantiate the template version of the function on values of
564// unnamed enum type - see comment below.
565#define DEFINE_CHECK_OP_IMPL(name, op) \
566 template <class t1, class t2> \
567 inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \
568 const char* names) { \
569 if (v1 op v2) return NULL; \
570 else return MakeCheckOpString(v1, v2, names); \
571 } \
572 inline std::string* Check##name##Impl(int v1, int v2, const char* names) { \
573 if (v1 op v2) return NULL; \
574 else return MakeCheckOpString(v1, v2, names); \
575 }
576DEFINE_CHECK_OP_IMPL(EQ, ==)
577DEFINE_CHECK_OP_IMPL(NE, !=)
578DEFINE_CHECK_OP_IMPL(LE, <=)
579DEFINE_CHECK_OP_IMPL(LT, < )
580DEFINE_CHECK_OP_IMPL(GE, >=)
581DEFINE_CHECK_OP_IMPL(GT, > )
582#undef DEFINE_CHECK_OP_IMPL
[email protected]e150c0382010-03-02 00:41:12583
584#define CHECK_EQ(val1, val2) CHECK_OP(EQ, ==, val1, val2)
585#define CHECK_NE(val1, val2) CHECK_OP(NE, !=, val1, val2)
586#define CHECK_LE(val1, val2) CHECK_OP(LE, <=, val1, val2)
587#define CHECK_LT(val1, val2) CHECK_OP(LT, < , val1, val2)
588#define CHECK_GE(val1, val2) CHECK_OP(GE, >=, val1, val2)
589#define CHECK_GT(val1, val2) CHECK_OP(GT, > , val1, val2)
590
jam121900aa2016-04-19 00:07:34591#if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
[email protected]d15e56c2010-09-30 21:12:33592#define ENABLE_DLOG 0
[email protected]d15e56c2010-09-30 21:12:33593#else
[email protected]d15e56c2010-09-30 21:12:33594#define ENABLE_DLOG 1
[email protected]1a1505512014-03-10 18:23:38595#endif
596
597#if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
danakje649f572015-01-08 23:35:58598#define DCHECK_IS_ON() 0
[email protected]1a1505512014-03-10 18:23:38599#else
danakje649f572015-01-08 23:35:58600#define DCHECK_IS_ON() 1
[email protected]e3cca332009-08-20 01:20:29601#endif
602
[email protected]d15e56c2010-09-30 21:12:33603// Definitions for DLOG et al.
604
[email protected]d926c202010-10-01 02:58:24605#if ENABLE_DLOG
606
[email protected]5e987802010-11-01 19:49:22607#define DLOG_IS_ON(severity) LOG_IS_ON(severity)
[email protected]d926c202010-10-01 02:58:24608#define DLOG_IF(severity, condition) LOG_IF(severity, condition)
609#define DLOG_ASSERT(condition) LOG_ASSERT(condition)
[email protected]d926c202010-10-01 02:58:24610#define DPLOG_IF(severity, condition) PLOG_IF(severity, condition)
[email protected]521b0c42010-10-01 23:02:36611#define DVLOG_IF(verboselevel, condition) VLOG_IF(verboselevel, condition)
[email protected]fb879b1a2011-03-06 18:16:31612#define DVPLOG_IF(verboselevel, condition) VPLOG_IF(verboselevel, condition)
[email protected]d926c202010-10-01 02:58:24613
614#else // ENABLE_DLOG
615
[email protected]521b0c42010-10-01 23:02:36616// If ENABLE_DLOG is off, we want to avoid emitting any references to
617// |condition| (which may reference a variable defined only if NDEBUG
618// is not defined). Contrast this with DCHECK et al., which has
619// different behavior.
[email protected]d926c202010-10-01 02:58:24620
[email protected]5e987802010-11-01 19:49:22621#define DLOG_IS_ON(severity) false
[email protected]ddb9b332011-12-02 07:31:09622#define DLOG_IF(severity, condition) EAT_STREAM_PARAMETERS
623#define DLOG_ASSERT(condition) EAT_STREAM_PARAMETERS
624#define DPLOG_IF(severity, condition) EAT_STREAM_PARAMETERS
625#define DVLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS
626#define DVPLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS
[email protected]d926c202010-10-01 02:58:24627
628#endif // ENABLE_DLOG
629
[email protected]d15e56c2010-09-30 21:12:33630// DEBUG_MODE is for uses like
631// if (DEBUG_MODE) foo.CheckThatFoo();
632// instead of
633// #ifndef NDEBUG
634// foo.CheckThatFoo();
635// #endif
636//
637// We tie its state to ENABLE_DLOG.
638enum { DEBUG_MODE = ENABLE_DLOG };
639
640#undef ENABLE_DLOG
641
[email protected]521b0c42010-10-01 23:02:36642#define DLOG(severity) \
643 LAZY_STREAM(LOG_STREAM(severity), DLOG_IS_ON(severity))
644
[email protected]521b0c42010-10-01 23:02:36645#define DPLOG(severity) \
646 LAZY_STREAM(PLOG_STREAM(severity), DLOG_IS_ON(severity))
647
[email protected]c3ab11c2011-10-25 06:28:45648#define DVLOG(verboselevel) DVLOG_IF(verboselevel, VLOG_IS_ON(verboselevel))
[email protected]521b0c42010-10-01 23:02:36649
[email protected]fb879b1a2011-03-06 18:16:31650#define DVPLOG(verboselevel) DVPLOG_IF(verboselevel, VLOG_IS_ON(verboselevel))
651
[email protected]521b0c42010-10-01 23:02:36652// Definitions for DCHECK et al.
[email protected]d926c202010-10-01 02:58:24653
danakje649f572015-01-08 23:35:58654#if DCHECK_IS_ON()
[email protected]e3cca332009-08-20 01:20:29655
[email protected]deba0ff2010-11-03 05:30:14656#define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \
657 COMPACT_GOOGLE_LOG_EX_FATAL(ClassName , ##__VA_ARGS__)
658#define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_FATAL
[email protected]521b0c42010-10-01 23:02:36659const LogSeverity LOG_DCHECK = LOG_FATAL;
660
danakje649f572015-01-08 23:35:58661#else // DCHECK_IS_ON()
[email protected]521b0c42010-10-01 23:02:36662
[email protected]c02cb8012014-03-14 18:39:53663// These are just dummy values.
[email protected]deba0ff2010-11-03 05:30:14664#define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \
665 COMPACT_GOOGLE_LOG_EX_INFO(ClassName , ##__VA_ARGS__)
666#define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_INFO
667const LogSeverity LOG_DCHECK = LOG_INFO;
[email protected]521b0c42010-10-01 23:02:36668
danakje649f572015-01-08 23:35:58669#endif // DCHECK_IS_ON()
[email protected]521b0c42010-10-01 23:02:36670
[email protected]deba0ff2010-11-03 05:30:14671// DCHECK et al. make sure to reference |condition| regardless of
[email protected]521b0c42010-10-01 23:02:36672// whether DCHECKs are enabled; this is so that we don't get unused
673// variable warnings if the only use of a variable is in a DCHECK.
674// This behavior is different from DLOG_IF et al.
675
brucedawson9d160252014-10-23 20:14:14676#if defined(_PREFAST_) && defined(OS_WIN)
677// See comments on the previous use of __analysis_assume.
678
679#define DCHECK(condition) \
680 __analysis_assume(!!(condition)), \
681 LAZY_STREAM(LOG_STREAM(DCHECK), false) \
682 << "Check failed: " #condition ". "
683
684#define DPCHECK(condition) \
685 __analysis_assume(!!(condition)), \
686 LAZY_STREAM(PLOG_STREAM(DCHECK), false) \
687 << "Check failed: " #condition ". "
688
Scott Graham0c6faeb2016-04-07 19:19:51689#else // _PREFAST_
brucedawson9d160252014-10-23 20:14:14690
piman2ac89dd2015-05-05 20:25:56691#define DCHECK(condition) \
692 LAZY_STREAM(LOG_STREAM(DCHECK), DCHECK_IS_ON() ? !(condition) : false) \
danakje649f572015-01-08 23:35:58693 << "Check failed: " #condition ". "
[email protected]521b0c42010-10-01 23:02:36694
piman2ac89dd2015-05-05 20:25:56695#define DPCHECK(condition) \
696 LAZY_STREAM(PLOG_STREAM(DCHECK), DCHECK_IS_ON() ? !(condition) : false) \
danakje649f572015-01-08 23:35:58697 << "Check failed: " #condition ". "
[email protected]d926c202010-10-01 02:58:24698
Scott Graham0c6faeb2016-04-07 19:19:51699#endif // _PREFAST_
brucedawson9d160252014-10-23 20:14:14700
[email protected]d926c202010-10-01 02:58:24701// Helper macro for binary operators.
702// Don't use this macro directly in your code, use DCHECK_EQ et al below.
erikwright6ad937b2015-07-22 20:05:52703// The 'switch' is used to prevent the 'else' from being ambiguous when the
704// macro is used in an 'if' clause such as:
705// if (a == 1)
706// DCHECK_EQ(2, a);
707#define DCHECK_OP(name, op, val1, val2) \
708 switch (0) case 0: default: \
709 if (logging::CheckOpResult true_if_passed = \
710 DCHECK_IS_ON() ? \
711 logging::Check##name##Impl((val1), (val2), \
712 #val1 " " #op " " #val2) : nullptr) \
713 ; \
714 else \
715 logging::LogMessage(__FILE__, __LINE__, ::logging::LOG_DCHECK, \
716 true_if_passed.message()).stream()
initial.commitd7cae122008-07-26 21:49:38717
[email protected]deba0ff2010-11-03 05:30:14718// Equality/Inequality checks - compare two values, and log a
719// LOG_DCHECK message including the two values when the result is not
720// as expected. The values must have operator<<(ostream, ...)
721// defined.
initial.commitd7cae122008-07-26 21:49:38722//
723// You may append to the error message like so:
724// DCHECK_NE(1, 2) << ": The world must be ending!";
725//
726// We are very careful to ensure that each argument is evaluated exactly
727// once, and that anything which is legal to pass as a function argument is
728// legal here. In particular, the arguments may be temporary expressions
729// which will end up being destroyed at the end of the apparent statement,
730// for example:
731// DCHECK_EQ(string("abc")[1], 'b');
732//
brucedawson93a60b8c2016-04-28 20:46:16733// WARNING: These don't compile correctly if one of the arguments is a pointer
734// and the other is NULL. In new code, prefer nullptr instead. To
735// work around this for C++98, simply static_cast NULL to the type of the
736// desired pointer.
initial.commitd7cae122008-07-26 21:49:38737
738#define DCHECK_EQ(val1, val2) DCHECK_OP(EQ, ==, val1, val2)
739#define DCHECK_NE(val1, val2) DCHECK_OP(NE, !=, val1, val2)
740#define DCHECK_LE(val1, val2) DCHECK_OP(LE, <=, val1, val2)
741#define DCHECK_LT(val1, val2) DCHECK_OP(LT, < , val1, val2)
742#define DCHECK_GE(val1, val2) DCHECK_OP(GE, >=, val1, val2)
743#define DCHECK_GT(val1, val2) DCHECK_OP(GT, > , val1, val2)
744
danakje649f572015-01-08 23:35:58745#if !DCHECK_IS_ON() && defined(OS_CHROMEOS)
tnagelff3f34a2015-05-24 12:59:14746// Implement logging of NOTREACHED() as a dedicated function to get function
747// call overhead down to a minimum.
748void LogErrorNotReached(const char* file, int line);
749#define NOTREACHED() \
750 true ? ::logging::LogErrorNotReached(__FILE__, __LINE__) \
751 : EAT_STREAM_PARAMETERS
[email protected]7c67fbe2013-09-26 07:55:21752#else
initial.commitd7cae122008-07-26 21:49:38753#define NOTREACHED() DCHECK(false)
[email protected]7c67fbe2013-09-26 07:55:21754#endif
initial.commitd7cae122008-07-26 21:49:38755
756// Redefine the standard assert to use our nice log files
757#undef assert
758#define assert(x) DLOG_ASSERT(x)
759
760// This class more or less represents a particular log message. You
761// create an instance of LogMessage and then stream stuff to it.
762// When you finish streaming to it, ~LogMessage is called and the
763// full message gets streamed to the appropriate destination.
764//
765// You shouldn't actually use LogMessage's constructor to log things,
766// though. You should use the LOG() macro (and variants thereof)
767// above.
[email protected]0bea7252011-08-05 15:34:00768class BASE_EXPORT LogMessage {
initial.commitd7cae122008-07-26 21:49:38769 public:
[email protected]bf8ddf13a2014-06-18 15:02:22770 // Used for LOG(severity).
initial.commitd7cae122008-07-26 21:49:38771 LogMessage(const char* file, int line, LogSeverity severity);
772
tnagel4a045d3f2015-07-12 14:19:28773 // Used for CHECK(). Implied severity = LOG_FATAL.
774 LogMessage(const char* file, int line, const char* condition);
775
[email protected]bf8ddf13a2014-06-18 15:02:22776 // Used for CHECK_EQ(), etc. Takes ownership of the given string.
777 // Implied severity = LOG_FATAL.
[email protected]9c7132e2011-02-08 07:39:08778 LogMessage(const char* file, int line, std::string* result);
initial.commitd7cae122008-07-26 21:49:38779
[email protected]bf8ddf13a2014-06-18 15:02:22780 // Used for DCHECK_EQ(), etc. Takes ownership of the given string.
[email protected]fb62a532009-02-12 01:19:05781 LogMessage(const char* file, int line, LogSeverity severity,
[email protected]9c7132e2011-02-08 07:39:08782 std::string* result);
[email protected]fb62a532009-02-12 01:19:05783
initial.commitd7cae122008-07-26 21:49:38784 ~LogMessage();
785
786 std::ostream& stream() { return stream_; }
787
788 private:
789 void Init(const char* file, int line);
790
791 LogSeverity severity_;
792 std::ostringstream stream_;
[email protected]c88873922008-07-30 13:02:03793 size_t message_start_; // Offset of the start of the message (past prefix
794 // info).
[email protected]162ac0f2010-11-04 15:50:49795 // The file and line information passed in to the constructor.
796 const char* file_;
797 const int line_;
798
[email protected]3f85caa2009-04-14 16:52:11799#if defined(OS_WIN)
800 // Stores the current value of GetLastError in the constructor and restores
801 // it in the destructor by calling SetLastError.
802 // This is useful since the LogMessage class uses a lot of Win32 calls
803 // that will lose the value of GLE and the code that called the log function
804 // will have lost the thread error value when the log call returns.
805 class SaveLastError {
806 public:
807 SaveLastError();
808 ~SaveLastError();
809
810 unsigned long get_error() const { return last_error_; }
811
812 protected:
813 unsigned long last_error_;
814 };
815
816 SaveLastError last_error_;
817#endif
initial.commitd7cae122008-07-26 21:49:38818
[email protected]39be4242008-08-07 18:31:40819 DISALLOW_COPY_AND_ASSIGN(LogMessage);
initial.commitd7cae122008-07-26 21:49:38820};
821
initial.commitd7cae122008-07-26 21:49:38822// This class is used to explicitly ignore values in the conditional
823// logging macros. This avoids compiler warnings like "value computed
824// is not used" and "statement has no effect".
[email protected]23bb71f2011-04-21 22:22:10825class LogMessageVoidify {
initial.commitd7cae122008-07-26 21:49:38826 public:
827 LogMessageVoidify() { }
828 // This has to be an operator with a precedence lower than << but
829 // higher than ?:
830 void operator&(std::ostream&) { }
831};
832
[email protected]d8617a62009-10-09 23:52:20833#if defined(OS_WIN)
834typedef unsigned long SystemErrorCode;
835#elif defined(OS_POSIX)
836typedef int SystemErrorCode;
837#endif
838
839// Alias for ::GetLastError() on Windows and errno on POSIX. Avoids having to
840// pull in windows.h just for GetLastError() and DWORD.
[email protected]0bea7252011-08-05 15:34:00841BASE_EXPORT SystemErrorCode GetLastSystemErrorCode();
[email protected]c914d8a2014-04-23 01:11:01842BASE_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code);
[email protected]d8617a62009-10-09 23:52:20843
844#if defined(OS_WIN)
845// Appends a formatted system message of the GetLastError() type.
[email protected]0bea7252011-08-05 15:34:00846class BASE_EXPORT Win32ErrorLogMessage {
[email protected]d8617a62009-10-09 23:52:20847 public:
848 Win32ErrorLogMessage(const char* file,
849 int line,
850 LogSeverity severity,
[email protected]d8617a62009-10-09 23:52:20851 SystemErrorCode err);
852
[email protected]d8617a62009-10-09 23:52:20853 // Appends the error message before destructing the encapsulated class.
854 ~Win32ErrorLogMessage();
855
[email protected]a502bbe72011-01-07 18:06:45856 std::ostream& stream() { return log_message_.stream(); }
857
[email protected]d8617a62009-10-09 23:52:20858 private:
859 SystemErrorCode err_;
[email protected]d8617a62009-10-09 23:52:20860 LogMessage log_message_;
861
862 DISALLOW_COPY_AND_ASSIGN(Win32ErrorLogMessage);
863};
864#elif defined(OS_POSIX)
865// Appends a formatted system message of the errno type
[email protected]0bea7252011-08-05 15:34:00866class BASE_EXPORT ErrnoLogMessage {
[email protected]d8617a62009-10-09 23:52:20867 public:
868 ErrnoLogMessage(const char* file,
869 int line,
870 LogSeverity severity,
871 SystemErrorCode err);
872
[email protected]d8617a62009-10-09 23:52:20873 // Appends the error message before destructing the encapsulated class.
874 ~ErrnoLogMessage();
875
[email protected]a502bbe72011-01-07 18:06:45876 std::ostream& stream() { return log_message_.stream(); }
877
[email protected]d8617a62009-10-09 23:52:20878 private:
879 SystemErrorCode err_;
880 LogMessage log_message_;
881
882 DISALLOW_COPY_AND_ASSIGN(ErrnoLogMessage);
883};
884#endif // OS_WIN
885
initial.commitd7cae122008-07-26 21:49:38886// Closes the log file explicitly if open.
887// NOTE: Since the log file is opened as necessary by the action of logging
888// statements, there's no guarantee that it will stay closed
889// after this call.
[email protected]0bea7252011-08-05 15:34:00890BASE_EXPORT void CloseLogFile();
initial.commitd7cae122008-07-26 21:49:38891
[email protected]e36ddc82009-12-08 04:22:50892// Async signal safe logging mechanism.
[email protected]0bea7252011-08-05 15:34:00893BASE_EXPORT void RawLog(int level, const char* message);
[email protected]e36ddc82009-12-08 04:22:50894
895#define RAW_LOG(level, message) logging::RawLog(logging::LOG_ ## level, message)
896
897#define RAW_CHECK(condition) \
898 do { \
899 if (!(condition)) \
900 logging::RawLog(logging::LOG_FATAL, "Check failed: " #condition "\n"); \
901 } while (0)
902
[email protected]f01b88a2013-02-27 22:04:00903#if defined(OS_WIN)
ananta61762fb2015-09-18 01:00:09904// Returns true if logging to file is enabled.
905BASE_EXPORT bool IsLoggingToFileEnabled();
906
[email protected]f01b88a2013-02-27 22:04:00907// Returns the default log file path.
908BASE_EXPORT std::wstring GetLogFileFullPath();
909#endif
910
[email protected]39be4242008-08-07 18:31:40911} // namespace logging
initial.commitd7cae122008-07-26 21:49:38912
[email protected]81411c62014-07-08 23:03:06913// Note that "The behavior of a C++ program is undefined if it adds declarations
914// or definitions to namespace std or to a namespace within namespace std unless
915// otherwise specified." --C++11[namespace.std]
916//
917// We've checked that this particular definition has the intended behavior on
918// our implementations, but it's prone to breaking in the future, and please
919// don't imitate this in your own definitions without checking with some
920// standard library experts.
921namespace std {
[email protected]46ce5b562010-06-16 18:39:53922// These functions are provided as a convenience for logging, which is where we
923// use streams (it is against Google style to use streams in other places). It
924// is designed to allow you to emit non-ASCII Unicode strings to the log file,
925// which is normally ASCII. It is relatively slow, so try not to use it for
926// common cases. Non-ASCII characters will be converted to UTF-8 by these
927// operators.
[email protected]0bea7252011-08-05 15:34:00928BASE_EXPORT std::ostream& operator<<(std::ostream& out, const wchar_t* wstr);
[email protected]46ce5b562010-06-16 18:39:53929inline std::ostream& operator<<(std::ostream& out, const std::wstring& wstr) {
930 return out << wstr.c_str();
931}
[email protected]81411c62014-07-08 23:03:06932} // namespace std
[email protected]46ce5b562010-06-16 18:39:53933
[email protected]0dfc81b2008-08-25 03:44:40934// The NOTIMPLEMENTED() macro annotates codepaths which have
935// not been implemented yet.
936//
937// The implementation of this macro is controlled by NOTIMPLEMENTED_POLICY:
938// 0 -- Do nothing (stripped by compiler)
939// 1 -- Warn at compile time
940// 2 -- Fail at compile time
941// 3 -- Fail at runtime (DCHECK)
942// 4 -- [default] LOG(ERROR) at runtime
943// 5 -- LOG(ERROR) at runtime, only once per call-site
944
945#ifndef NOTIMPLEMENTED_POLICY
[email protected]f5c7758a2012-07-25 16:17:57946#if defined(OS_ANDROID) && defined(OFFICIAL_BUILD)
947#define NOTIMPLEMENTED_POLICY 0
948#else
[email protected]0dfc81b2008-08-25 03:44:40949// Select default policy: LOG(ERROR)
950#define NOTIMPLEMENTED_POLICY 4
951#endif
[email protected]f5c7758a2012-07-25 16:17:57952#endif
[email protected]0dfc81b2008-08-25 03:44:40953
[email protected]f6cda752008-10-30 23:54:26954#if defined(COMPILER_GCC)
955// On Linux, with GCC, we can use __PRETTY_FUNCTION__ to get the demangled name
956// of the current function in the NOTIMPLEMENTED message.
957#define NOTIMPLEMENTED_MSG "Not implemented reached in " << __PRETTY_FUNCTION__
958#else
959#define NOTIMPLEMENTED_MSG "NOT IMPLEMENTED"
960#endif
961
[email protected]0dfc81b2008-08-25 03:44:40962#if NOTIMPLEMENTED_POLICY == 0
[email protected]38227292012-01-30 19:41:54963#define NOTIMPLEMENTED() EAT_STREAM_PARAMETERS
[email protected]0dfc81b2008-08-25 03:44:40964#elif NOTIMPLEMENTED_POLICY == 1
965// TODO, figure out how to generate a warning
avi4ec0dff2015-11-24 14:26:24966#define NOTIMPLEMENTED() static_assert(false, "NOT_IMPLEMENTED")
[email protected]0dfc81b2008-08-25 03:44:40967#elif NOTIMPLEMENTED_POLICY == 2
avi4ec0dff2015-11-24 14:26:24968#define NOTIMPLEMENTED() static_assert(false, "NOT_IMPLEMENTED")
[email protected]0dfc81b2008-08-25 03:44:40969#elif NOTIMPLEMENTED_POLICY == 3
970#define NOTIMPLEMENTED() NOTREACHED()
971#elif NOTIMPLEMENTED_POLICY == 4
[email protected]f6cda752008-10-30 23:54:26972#define NOTIMPLEMENTED() LOG(ERROR) << NOTIMPLEMENTED_MSG
[email protected]0dfc81b2008-08-25 03:44:40973#elif NOTIMPLEMENTED_POLICY == 5
974#define NOTIMPLEMENTED() do {\
[email protected]b70ff012013-02-13 08:32:14975 static bool logged_once = false;\
976 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\
977 logged_once = true;\
978} while(0);\
979EAT_STREAM_PARAMETERS
[email protected]0dfc81b2008-08-25 03:44:40980#endif
981
[email protected]39be4242008-08-07 18:31:40982#endif // BASE_LOGGING_H_