blob: 6ea430dad963e18259985c3c958e1303bb832ec9 [file] [log] [blame]
akalin@chromium.org227369d2012-01-20 15:33:27 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botf003cfe2008-08-24 09:55:55 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit3f4a7322008-07-27 06:49:38 +09004
brettw@google.come3c034a2008-08-08 03:31:40 +09005#ifndef BASE_LOGGING_H_
6#define BASE_LOGGING_H_
initial.commit3f4a7322008-07-27 06:49:38 +09007
tzik@chromium.org0a1ccf42011-06-18 20:53:14 +09008#include <cassert>
initial.commit3f4a7322008-07-27 06:49:38 +09009#include <string>
10#include <cstring>
11#include <sstream>
12
darin@chromium.orge585bed2011-08-06 00:34:00 +090013#include "base/base_export.h"
initial.commit3f4a7322008-07-27 06:49:38 +090014#include "base/basictypes.h"
akalin@chromium.org836c6e62011-12-02 16:31:09 +090015#include "base/debug/debugger.h"
rvargas@google.com4891e7d2011-03-26 03:46:38 +090016#include "build/build_config.h"
initial.commit3f4a7322008-07-27 06:49:38 +090017
18//
19// Optional message capabilities
20// -----------------------------
21// Assertion failed messages and fatal errors are displayed in a dialog box
22// before the application exits. However, running this UI creates a message
23// loop, which causes application messages to be processed and potentially
24// dispatched to existing application windows. Since the application is in a
25// bad state when this assertion dialog is displayed, these messages may not
26// get processed and hang the dialog, or the application might go crazy.
27//
28// Therefore, it can be beneficial to display the error dialog in a separate
29// process from the main application. When the logging system needs to display
30// a fatal error dialog box, it will look for a program called
31// "DebugMessage.exe" in the same directory as the application executable. It
32// will run this application with the message as the command line, and will
33// not include the name of the application as is traditional for easier
34// parsing.
35//
36// The code for DebugMessage.exe is only one line. In WinMain, do:
37// MessageBox(NULL, GetCommandLineW(), L"Fatal Error", 0);
38//
39// If DebugMessage.exe is not found, the logging code will use a normal
40// MessageBox, potentially causing the problems discussed above.
41
42
43// Instructions
44// ------------
45//
46// Make a bunch of macros for logging. The way to log things is to stream
47// things to LOG(<a particular severity level>). E.g.,
48//
49// LOG(INFO) << "Found " << num_cookies << " cookies";
50//
51// You can also do conditional logging:
52//
53// LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
54//
55// The above will cause log messages to be output on the 1st, 11th, 21st, ...
56// times it is executed. Note that the special COUNTER value is used to
57// identify which repetition is happening.
58//
59// The CHECK(condition) macro is active in both debug and release builds and
60// effectively performs a LOG(FATAL) which terminates the process and
61// generates a crashdump unless a debugger is attached.
62//
63// There are also "debug mode" logging macros like the ones above:
64//
65// DLOG(INFO) << "Found cookies";
66//
67// DLOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
68//
69// All "debug mode" logging is compiled away to nothing for non-debug mode
70// compiles. LOG_IF and development flags also work well together
71// because the code can be compiled away sometimes.
72//
73// We also have
74//
75// LOG_ASSERT(assertion);
76// DLOG_ASSERT(assertion);
77//
78// which is syntactic sugar for {,D}LOG_IF(FATAL, assert fails) << assertion;
79//
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +090080// There are "verbose level" logging macros. They look like
81//
82// VLOG(1) << "I'm printed when you run the program with --v=1 or more";
83// VLOG(2) << "I'm printed when you run the program with --v=2 or more";
84//
85// These always log at the INFO log level (when they log at all).
86// The verbose logging can also be turned on module-by-module. For instance,
akalin@chromium.org859d7d42010-10-29 09:39:48 +090087// --vmodule=profile=2,icon_loader=1,browser_*=3,*/chromeos/*=4 --v=0
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +090088// will cause:
89// a. VLOG(2) and lower messages to be printed from profile.{h,cc}
90// b. VLOG(1) and lower messages to be printed from icon_loader.{h,cc}
91// c. VLOG(3) and lower messages to be printed from files prefixed with
92// "browser"
akalin@chromium.org55a8a812010-11-02 05:50:55 +090093// d. VLOG(4) and lower messages to be printed from files under a
akalin@chromium.org859d7d42010-10-29 09:39:48 +090094// "chromeos" directory.
akalin@chromium.org55a8a812010-11-02 05:50:55 +090095// e. VLOG(0) and lower messages to be printed from elsewhere
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +090096//
97// The wildcarding functionality shown by (c) supports both '*' (match
akalin@chromium.org859d7d42010-10-29 09:39:48 +090098// 0 or more characters) and '?' (match any single character)
99// wildcards. Any pattern containing a forward or backward slash will
100// be tested against the whole pathname and not just the module.
101// E.g., "*/foo/bar/*=2" would change the logging level for all code
102// in source files under a "foo/bar" directory.
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +0900103//
104// There's also VLOG_IS_ON(n) "verbose level" condition macro. To be used as
105//
106// if (VLOG_IS_ON(2)) {
107// // do some logging preparation and logging
108// // that can't be accomplished with just VLOG(2) << ...;
109// }
110//
111// There is also a VLOG_IF "verbose level" condition macro for sample
112// cases, when some extra computation and preparation for logs is not
113// needed.
114//
115// VLOG_IF(1, (size > 1024))
116// << "I'm printed when size is more than 1024 and when you run the "
117// "program with --v=1 or more";
118//
initial.commit3f4a7322008-07-27 06:49:38 +0900119// We also override the standard 'assert' to use 'DLOG_ASSERT'.
120//
tschmelcher@chromium.orgf29a4fc2009-10-10 08:52:20 +0900121// Lastly, there is:
122//
123// PLOG(ERROR) << "Couldn't do foo";
124// DPLOG(ERROR) << "Couldn't do foo";
125// PLOG_IF(ERROR, cond) << "Couldn't do foo";
126// DPLOG_IF(ERROR, cond) << "Couldn't do foo";
127// PCHECK(condition) << "Couldn't do foo";
128// DPCHECK(condition) << "Couldn't do foo";
129//
130// which append the last system error to the message in string form (taken from
131// GetLastError() on Windows and errno on POSIX).
132//
initial.commit3f4a7322008-07-27 06:49:38 +0900133// The supported severity levels for macros that allow you to specify one
huanr@chromium.org656253e2009-02-12 10:19:05 +0900134// are (in increasing order of severity) INFO, WARNING, ERROR, ERROR_REPORT,
135// and FATAL.
initial.commit3f4a7322008-07-27 06:49:38 +0900136//
137// Very important: logging a message at the FATAL severity level causes
138// the program to terminate (after the message is logged).
huanr@chromium.org656253e2009-02-12 10:19:05 +0900139//
140// Note the special severity of ERROR_REPORT only available/relevant in normal
141// mode, which displays error dialog without terminating the program. There is
142// no error dialog for severity ERROR or below in normal mode.
143//
144// There is also the special severity of DFATAL, which logs FATAL in
jar@chromium.orgac61a722010-06-24 10:01:04 +0900145// debug mode, ERROR in normal mode.
initial.commit3f4a7322008-07-27 06:49:38 +0900146
147namespace logging {
148
149// Where to record logging output? A flat file and/or system debug log via
evanm@google.com4eb4f372008-11-18 09:59:04 +0900150// OutputDebugString. Defaults on Windows to LOG_ONLY_TO_FILE, and on
151// POSIX to LOG_ONLY_TO_SYSTEM_DEBUG_LOG (aka stderr).
initial.commit3f4a7322008-07-27 06:49:38 +0900152enum LoggingDestination { LOG_NONE,
153 LOG_ONLY_TO_FILE,
154 LOG_ONLY_TO_SYSTEM_DEBUG_LOG,
155 LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG };
156
157// Indicates that the log file should be locked when being written to.
158// Often, there is no locking, which is fine for a single threaded program.
159// If logging is being done from multiple threads or there can be more than
160// one process doing the logging, the file should be locked during writes to
161// make each log outut atomic. Other writers will block.
162//
163// All processes writing to the log file must have their locking set for it to
164// work properly. Defaults to DONT_LOCK_LOG_FILE.
165enum LogLockingState { LOCK_LOG_FILE, DONT_LOCK_LOG_FILE };
166
167// On startup, should we delete or append to an existing log file (if any)?
168// Defaults to APPEND_TO_OLD_LOG_FILE.
169enum OldFileDeletionState { DELETE_OLD_LOG_FILE, APPEND_TO_OLD_LOG_FILE };
170
akalin@chromium.orgfaed5f82011-01-11 10:03:36 +0900171enum DcheckState {
172 DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS,
173 ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS
174};
175
derat@chromium.orgb2aa7c42010-08-24 04:57:46 +0900176// TODO(avi): do we want to do a unification of character types here?
177#if defined(OS_WIN)
178typedef wchar_t PathChar;
179#else
180typedef char PathChar;
181#endif
182
183// Define different names for the BaseInitLoggingImpl() function depending on
184// whether NDEBUG is defined or not so that we'll fail to link if someone tries
185// to compile logging.cc with NDEBUG but includes logging.h without defining it,
186// or vice versa.
187#if NDEBUG
188#define BaseInitLoggingImpl BaseInitLoggingImpl_built_with_NDEBUG
189#else
190#define BaseInitLoggingImpl BaseInitLoggingImpl_built_without_NDEBUG
191#endif
192
193// Implementation of the InitLogging() method declared below. We use a
194// more-specific name so we can #define it above without affecting other code
195// that has named stuff "InitLogging".
darin@chromium.orge585bed2011-08-06 00:34:00 +0900196BASE_EXPORT bool BaseInitLoggingImpl(const PathChar* log_file,
197 LoggingDestination logging_dest,
198 LogLockingState lock_log,
199 OldFileDeletionState delete_old,
200 DcheckState dcheck_state);
derat@chromium.orgb2aa7c42010-08-24 04:57:46 +0900201
initial.commit3f4a7322008-07-27 06:49:38 +0900202// Sets the log file name and other global logging state. Calling this function
203// is recommended, and is normally done at the beginning of application init.
204// If you don't call it, all the flags will be initialized to their default
205// values, and there is a race condition that may leak a critical section
206// object if two threads try to do the first log at the same time.
207// See the definition of the enums above for descriptions and default values.
208//
209// The default log file is initialized to "debug.log" in the application
210// directory. You probably don't want this, especially since the program
211// directory may not be writable on an enduser's system.
stevenjb@chromium.org6c56fa42011-12-03 09:30:08 +0900212//
213// This function may be called a second time to re-direct logging (e.g after
214// loging in to a user partition), however it should never be called more than
215// twice.
gspencer@chromium.org49c808a2010-10-28 09:20:21 +0900216inline bool InitLogging(const PathChar* log_file,
derat@chromium.orgb2aa7c42010-08-24 04:57:46 +0900217 LoggingDestination logging_dest,
218 LogLockingState lock_log,
akalin@chromium.orgfaed5f82011-01-11 10:03:36 +0900219 OldFileDeletionState delete_old,
220 DcheckState dcheck_state) {
221 return BaseInitLoggingImpl(log_file, logging_dest, lock_log,
222 delete_old, dcheck_state);
derat@chromium.orgb2aa7c42010-08-24 04:57:46 +0900223}
initial.commit3f4a7322008-07-27 06:49:38 +0900224
225// Sets the log level. Anything at or above this level will be written to the
226// log file/displayed to the user (if applicable). Anything below this level
siggi@chromium.org25396e12010-11-05 00:50:49 +0900227// will be silently ignored. The log level defaults to 0 (everything is logged
228// up to level INFO) if this function is not called.
229// Note that log messages for VLOG(x) are logged at level -x, so setting
230// the min log level to negative values enables verbose logging.
darin@chromium.orge585bed2011-08-06 00:34:00 +0900231BASE_EXPORT void SetMinLogLevel(int level);
initial.commit3f4a7322008-07-27 06:49:38 +0900232
ericroman@google.com38450852009-04-11 04:13:42 +0900233// Gets the current log level.
darin@chromium.orge585bed2011-08-06 00:34:00 +0900234BASE_EXPORT int GetMinLogLevel();
initial.commit3f4a7322008-07-27 06:49:38 +0900235
siggi@chromium.org25396e12010-11-05 00:50:49 +0900236// Gets the VLOG default verbosity level.
darin@chromium.orge585bed2011-08-06 00:34:00 +0900237BASE_EXPORT int GetVlogVerbosity();
siggi@chromium.org25396e12010-11-05 00:50:49 +0900238
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +0900239// Gets the current vlog level for the given file (usually taken from
240// __FILE__).
akalin@chromium.org59c9f212010-09-30 06:25:14 +0900241
242// Note that |N| is the size *with* the null terminator.
darin@chromium.orge585bed2011-08-06 00:34:00 +0900243BASE_EXPORT int GetVlogLevelHelper(const char* file_start, size_t N);
akalin@chromium.org59c9f212010-09-30 06:25:14 +0900244
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +0900245template <size_t N>
246int GetVlogLevel(const char (&file)[N]) {
247 return GetVlogLevelHelper(file, N);
248}
initial.commit3f4a7322008-07-27 06:49:38 +0900249
250// Sets the common items you want to be prepended to each log message.
251// process and thread IDs default to off, the timestamp defaults to on.
252// If this function is not called, logging defaults to writing the timestamp
253// only.
darin@chromium.orge585bed2011-08-06 00:34:00 +0900254BASE_EXPORT void SetLogItems(bool enable_process_id, bool enable_thread_id,
255 bool enable_timestamp, bool enable_tickcount);
initial.commit3f4a7322008-07-27 06:49:38 +0900256
cmasone@google.comf7802482010-08-17 09:38:12 +0900257// Sets whether or not you'd like to see fatal debug messages popped up in
258// a dialog box or not.
259// Dialogs are not shown by default.
darin@chromium.orge585bed2011-08-06 00:34:00 +0900260BASE_EXPORT void SetShowErrorDialogs(bool enable_dialogs);
cmasone@google.comf7802482010-08-17 09:38:12 +0900261
initial.commit3f4a7322008-07-27 06:49:38 +0900262// Sets the Log Assert Handler that will be used to notify of check failures.
huanr@chromium.org656253e2009-02-12 10:19:05 +0900263// The default handler shows a dialog box and then terminate the process,
264// however clients can use this function to override with their own handling
265// (e.g. a silent one for Unit Tests)
initial.commit3f4a7322008-07-27 06:49:38 +0900266typedef void (*LogAssertHandlerFunction)(const std::string& str);
darin@chromium.orge585bed2011-08-06 00:34:00 +0900267BASE_EXPORT void SetLogAssertHandler(LogAssertHandlerFunction handler);
hansl@google.com519197c2010-11-04 04:20:27 +0900268
huanr@chromium.org656253e2009-02-12 10:19:05 +0900269// Sets the Log Report Handler that will be used to notify of check failures
270// in non-debug mode. The default handler shows a dialog box and continues
271// the execution, however clients can use this function to override with their
272// own handling.
273typedef void (*LogReportHandlerFunction)(const std::string& str);
darin@chromium.orge585bed2011-08-06 00:34:00 +0900274BASE_EXPORT void SetLogReportHandler(LogReportHandlerFunction handler);
initial.commit3f4a7322008-07-27 06:49:38 +0900275
siggi@chromium.org4db65792009-11-26 00:26:34 +0900276// Sets the Log Message Handler that gets passed every log message before
277// it's sent to other log destinations (if any).
278// Returns true to signal that it handled the message and the message
279// should not be sent to other log destinations.
siggi@chromium.org25396e12010-11-05 00:50:49 +0900280typedef bool (*LogMessageHandlerFunction)(int severity,
281 const char* file, int line, size_t message_start, const std::string& str);
darin@chromium.orge585bed2011-08-06 00:34:00 +0900282BASE_EXPORT void SetLogMessageHandler(LogMessageHandlerFunction handler);
283BASE_EXPORT LogMessageHandlerFunction GetLogMessageHandler();
siggi@chromium.org4db65792009-11-26 00:26:34 +0900284
initial.commit3f4a7322008-07-27 06:49:38 +0900285typedef int LogSeverity;
siggi@chromium.org25396e12010-11-05 00:50:49 +0900286const LogSeverity LOG_VERBOSE = -1; // This is level 1 verbosity
287// Note: the log severities are used to index into the array of names,
288// see log_severity_names.
initial.commit3f4a7322008-07-27 06:49:38 +0900289const LogSeverity LOG_INFO = 0;
290const LogSeverity LOG_WARNING = 1;
291const LogSeverity LOG_ERROR = 2;
huanr@chromium.org656253e2009-02-12 10:19:05 +0900292const LogSeverity LOG_ERROR_REPORT = 3;
293const LogSeverity LOG_FATAL = 4;
294const LogSeverity LOG_NUM_SEVERITIES = 5;
initial.commit3f4a7322008-07-27 06:49:38 +0900295
akalin@chromium.orga88579b2010-10-02 08:02:36 +0900296// LOG_DFATAL is LOG_FATAL in debug mode, ERROR in normal mode
initial.commit3f4a7322008-07-27 06:49:38 +0900297#ifdef NDEBUG
akalin@chromium.orga88579b2010-10-02 08:02:36 +0900298const LogSeverity LOG_DFATAL = LOG_ERROR;
initial.commit3f4a7322008-07-27 06:49:38 +0900299#else
akalin@chromium.orga88579b2010-10-02 08:02:36 +0900300const LogSeverity LOG_DFATAL = LOG_FATAL;
initial.commit3f4a7322008-07-27 06:49:38 +0900301#endif
302
303// A few definitions of macros that don't generate much code. These are used
304// by LOG() and LOG_IF, etc. Since these are used all over our code, it's
305// better to have compact code for these operations.
tschmelcher@chromium.orgf29a4fc2009-10-10 08:52:20 +0900306#define COMPACT_GOOGLE_LOG_EX_INFO(ClassName, ...) \
307 logging::ClassName(__FILE__, __LINE__, logging::LOG_INFO , ##__VA_ARGS__)
308#define COMPACT_GOOGLE_LOG_EX_WARNING(ClassName, ...) \
309 logging::ClassName(__FILE__, __LINE__, logging::LOG_WARNING , ##__VA_ARGS__)
310#define COMPACT_GOOGLE_LOG_EX_ERROR(ClassName, ...) \
311 logging::ClassName(__FILE__, __LINE__, logging::LOG_ERROR , ##__VA_ARGS__)
312#define COMPACT_GOOGLE_LOG_EX_ERROR_REPORT(ClassName, ...) \
313 logging::ClassName(__FILE__, __LINE__, \
314 logging::LOG_ERROR_REPORT , ##__VA_ARGS__)
315#define COMPACT_GOOGLE_LOG_EX_FATAL(ClassName, ...) \
316 logging::ClassName(__FILE__, __LINE__, logging::LOG_FATAL , ##__VA_ARGS__)
317#define COMPACT_GOOGLE_LOG_EX_DFATAL(ClassName, ...) \
akalin@chromium.orga88579b2010-10-02 08:02:36 +0900318 logging::ClassName(__FILE__, __LINE__, logging::LOG_DFATAL , ##__VA_ARGS__)
tschmelcher@chromium.orgf29a4fc2009-10-10 08:52:20 +0900319
initial.commit3f4a7322008-07-27 06:49:38 +0900320#define COMPACT_GOOGLE_LOG_INFO \
tschmelcher@chromium.orgf29a4fc2009-10-10 08:52:20 +0900321 COMPACT_GOOGLE_LOG_EX_INFO(LogMessage)
initial.commit3f4a7322008-07-27 06:49:38 +0900322#define COMPACT_GOOGLE_LOG_WARNING \
tschmelcher@chromium.orgf29a4fc2009-10-10 08:52:20 +0900323 COMPACT_GOOGLE_LOG_EX_WARNING(LogMessage)
initial.commit3f4a7322008-07-27 06:49:38 +0900324#define COMPACT_GOOGLE_LOG_ERROR \
tschmelcher@chromium.orgf29a4fc2009-10-10 08:52:20 +0900325 COMPACT_GOOGLE_LOG_EX_ERROR(LogMessage)
huanr@chromium.org656253e2009-02-12 10:19:05 +0900326#define COMPACT_GOOGLE_LOG_ERROR_REPORT \
tschmelcher@chromium.orgf29a4fc2009-10-10 08:52:20 +0900327 COMPACT_GOOGLE_LOG_EX_ERROR_REPORT(LogMessage)
initial.commit3f4a7322008-07-27 06:49:38 +0900328#define COMPACT_GOOGLE_LOG_FATAL \
tschmelcher@chromium.orgf29a4fc2009-10-10 08:52:20 +0900329 COMPACT_GOOGLE_LOG_EX_FATAL(LogMessage)
initial.commit3f4a7322008-07-27 06:49:38 +0900330#define COMPACT_GOOGLE_LOG_DFATAL \
tschmelcher@chromium.orgf29a4fc2009-10-10 08:52:20 +0900331 COMPACT_GOOGLE_LOG_EX_DFATAL(LogMessage)
initial.commit3f4a7322008-07-27 06:49:38 +0900332
333// wingdi.h defines ERROR to be 0. When we call LOG(ERROR), it gets
334// substituted with 0, and it expands to COMPACT_GOOGLE_LOG_0. To allow us
335// to keep using this syntax, we define this macro to do the same thing
336// as COMPACT_GOOGLE_LOG_ERROR, and also define ERROR the same way that
337// the Windows SDK does for consistency.
338#define ERROR 0
tschmelcher@chromium.orgf29a4fc2009-10-10 08:52:20 +0900339#define COMPACT_GOOGLE_LOG_EX_0(ClassName, ...) \
340 COMPACT_GOOGLE_LOG_EX_ERROR(ClassName , ##__VA_ARGS__)
341#define COMPACT_GOOGLE_LOG_0 COMPACT_GOOGLE_LOG_ERROR
akalin@chromium.orga88579b2010-10-02 08:02:36 +0900342// Needed for LOG_IS_ON(ERROR).
343const LogSeverity LOG_0 = LOG_ERROR;
344
akalin@chromium.org434c5b62010-11-03 14:30:14 +0900345// As special cases, we can assume that LOG_IS_ON(ERROR_REPORT) and
346// LOG_IS_ON(FATAL) always hold. Also, LOG_IS_ON(DFATAL) always holds
347// in debug mode. In particular, CHECK()s will always fire if they
348// fail.
akalin@chromium.orga88579b2010-10-02 08:02:36 +0900349#define LOG_IS_ON(severity) \
350 ((::logging::LOG_ ## severity) >= ::logging::GetMinLogLevel())
351
352// We can't do any caching tricks with VLOG_IS_ON() like the
353// google-glog version since it requires GCC extensions. This means
354// that using the v-logging functions in conjunction with --vmodule
355// may be slow.
356#define VLOG_IS_ON(verboselevel) \
357 ((verboselevel) <= ::logging::GetVlogLevel(__FILE__))
358
359// Helper macro which avoids evaluating the arguments to a stream if
360// the condition doesn't hold.
361#define LAZY_STREAM(stream, condition) \
362 !(condition) ? (void) 0 : ::logging::LogMessageVoidify() & (stream)
initial.commit3f4a7322008-07-27 06:49:38 +0900363
364// We use the preprocessor's merging operator, "##", so that, e.g.,
365// LOG(INFO) becomes the token COMPACT_GOOGLE_LOG_INFO. There's some funny
366// subtle difference between ostream member streaming functions (e.g.,
367// ostream::operator<<(int) and ostream non-member streaming functions
368// (e.g., ::operator<<(ostream&, string&): it turns out that it's
369// impossible to stream something like a string directly to an unnamed
370// ostream. We employ a neat hack by calling the stream() member
371// function of LogMessage which seems to avoid the problem.
akalin@chromium.orga88579b2010-10-02 08:02:36 +0900372#define LOG_STREAM(severity) COMPACT_GOOGLE_LOG_ ## severity.stream()
initial.commit3f4a7322008-07-27 06:49:38 +0900373
akalin@chromium.orga88579b2010-10-02 08:02:36 +0900374#define LOG(severity) LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity))
375#define LOG_IF(severity, condition) \
376 LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity) && (condition))
377
initial.commit3f4a7322008-07-27 06:49:38 +0900378#define SYSLOG(severity) LOG(severity)
akalin@chromium.orga88579b2010-10-02 08:02:36 +0900379#define SYSLOG_IF(severity, condition) LOG_IF(severity, condition)
380
siggi@chromium.org25396e12010-11-05 00:50:49 +0900381// The VLOG macros log with negative verbosities.
382#define VLOG_STREAM(verbose_level) \
383 logging::LogMessage(__FILE__, __LINE__, -verbose_level).stream()
384
385#define VLOG(verbose_level) \
386 LAZY_STREAM(VLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level))
387
388#define VLOG_IF(verbose_level, condition) \
389 LAZY_STREAM(VLOG_STREAM(verbose_level), \
390 VLOG_IS_ON(verbose_level) && (condition))
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +0900391
sail@chromium.org4015fc02011-03-07 03:16:31 +0900392#if defined (OS_WIN)
393#define VPLOG_STREAM(verbose_level) \
394 logging::Win32ErrorLogMessage(__FILE__, __LINE__, -verbose_level, \
395 ::logging::GetLastSystemErrorCode()).stream()
396#elif defined(OS_POSIX)
397#define VPLOG_STREAM(verbose_level) \
398 logging::ErrnoLogMessage(__FILE__, __LINE__, -verbose_level, \
399 ::logging::GetLastSystemErrorCode()).stream()
400#endif
401
402#define VPLOG(verbose_level) \
403 LAZY_STREAM(VPLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level))
404
405#define VPLOG_IF(verbose_level, condition) \
406 LAZY_STREAM(VPLOG_STREAM(verbose_level), \
407 VLOG_IS_ON(verbose_level) && (condition))
408
akalin@chromium.orgf0ee79c2010-09-30 04:26:36 +0900409// TODO(akalin): Add more VLOG variants, e.g. VPLOG.
initial.commit3f4a7322008-07-27 06:49:38 +0900410
initial.commit3f4a7322008-07-27 06:49:38 +0900411#define LOG_ASSERT(condition) \
412 LOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". "
413#define SYSLOG_ASSERT(condition) \
414 SYSLOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". "
415
tschmelcher@chromium.orgf29a4fc2009-10-10 08:52:20 +0900416#if defined(OS_WIN)
akalin@chromium.orga88579b2010-10-02 08:02:36 +0900417#define LOG_GETLASTERROR_STREAM(severity) \
tschmelcher@chromium.orgf29a4fc2009-10-10 08:52:20 +0900418 COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \
419 ::logging::GetLastSystemErrorCode()).stream()
akalin@chromium.orga88579b2010-10-02 08:02:36 +0900420#define LOG_GETLASTERROR(severity) \
421 LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity), LOG_IS_ON(severity))
422#define LOG_GETLASTERROR_MODULE_STREAM(severity, module) \
tschmelcher@chromium.orgf29a4fc2009-10-10 08:52:20 +0900423 COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \
424 ::logging::GetLastSystemErrorCode(), module).stream()
akalin@chromium.orga88579b2010-10-02 08:02:36 +0900425#define LOG_GETLASTERROR_MODULE(severity, module) \
426 LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity, module), \
427 LOG_IS_ON(severity))
428// PLOG_STREAM is used by PLOG, which is the usual error logging macro
429// for each platform.
430#define PLOG_STREAM(severity) LOG_GETLASTERROR_STREAM(severity)
tschmelcher@chromium.orgf29a4fc2009-10-10 08:52:20 +0900431#elif defined(OS_POSIX)
akalin@chromium.orga88579b2010-10-02 08:02:36 +0900432#define LOG_ERRNO_STREAM(severity) \
tschmelcher@chromium.orgf29a4fc2009-10-10 08:52:20 +0900433 COMPACT_GOOGLE_LOG_EX_ ## severity(ErrnoLogMessage, \
434 ::logging::GetLastSystemErrorCode()).stream()
akalin@chromium.orga88579b2010-10-02 08:02:36 +0900435#define LOG_ERRNO(severity) \
436 LAZY_STREAM(LOG_ERRNO_STREAM(severity), LOG_IS_ON(severity))
437// PLOG_STREAM is used by PLOG, which is the usual error logging macro
438// for each platform.
439#define PLOG_STREAM(severity) LOG_ERRNO_STREAM(severity)
tschmelcher@chromium.orgf29a4fc2009-10-10 08:52:20 +0900440#endif
441
akalin@chromium.orga88579b2010-10-02 08:02:36 +0900442#define PLOG(severity) \
443 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity))
444
tschmelcher@chromium.orgf29a4fc2009-10-10 08:52:20 +0900445#define PLOG_IF(severity, condition) \
akalin@chromium.orga88579b2010-10-02 08:02:36 +0900446 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity) && (condition))
tschmelcher@chromium.orgf29a4fc2009-10-10 08:52:20 +0900447
akalin@chromium.org836c6e62011-12-02 16:31:09 +0900448// http://crbug.com/16512 is open for a real fix for this. For now, Windows
449// uses OFFICIAL_BUILD and other platforms use the branding flag when NDEBUG is
450// defined.
451#if ( defined(OS_WIN) && defined(OFFICIAL_BUILD)) || \
452 (!defined(OS_WIN) && defined(NDEBUG) && defined(GOOGLE_CHROME_BUILD))
453#define LOGGING_IS_OFFICIAL_BUILD 1
454#else
455#define LOGGING_IS_OFFICIAL_BUILD 0
456#endif
457
458// The actual stream used isn't important.
459#define EAT_STREAM_PARAMETERS \
460 true ? (void) 0 : ::logging::LogMessageVoidify() & LOG_STREAM(FATAL)
461
initial.commit3f4a7322008-07-27 06:49:38 +0900462// CHECK dies with a fatal error if condition is not true. It is *not*
463// controlled by NDEBUG, so the check will be executed regardless of
464// compilation mode.
akalin@chromium.orga88579b2010-10-02 08:02:36 +0900465//
466// We make sure CHECK et al. always evaluates their arguments, as
467// doing CHECK(FunctionWithSideEffect()) is a common idiom.
akalin@chromium.org836c6e62011-12-02 16:31:09 +0900468
469#if LOGGING_IS_OFFICIAL_BUILD
470
471// Make all CHECK functions discard their log strings to reduce code
472// bloat for official builds.
473
474// TODO(akalin): This would be more valuable if there were some way to
475// remove BreakDebugger() from the backtrace, perhaps by turning it
476// into a macro (like __debugbreak() on Windows).
477#define CHECK(condition) \
478 !(condition) ? ::base::debug::BreakDebugger() : EAT_STREAM_PARAMETERS
479
480#define PCHECK(condition) CHECK(condition)
481
482#define CHECK_OP(name, op, val1, val2) CHECK((val1) op (val2))
483
484#else
485
akalin@chromium.orga88579b2010-10-02 08:02:36 +0900486#define CHECK(condition) \
487 LAZY_STREAM(LOG_STREAM(FATAL), !(condition)) \
488 << "Check failed: " #condition ". "
initial.commit3f4a7322008-07-27 06:49:38 +0900489
tschmelcher@chromium.orgf29a4fc2009-10-10 08:52:20 +0900490#define PCHECK(condition) \
akalin@chromium.orga88579b2010-10-02 08:02:36 +0900491 LAZY_STREAM(PLOG_STREAM(FATAL), !(condition)) \
492 << "Check failed: " #condition ". "
tschmelcher@chromium.orgf29a4fc2009-10-10 08:52:20 +0900493
akalin@chromium.org836c6e62011-12-02 16:31:09 +0900494// Helper macro for binary operators.
495// Don't use this macro directly in your code, use CHECK_EQ et al below.
496//
497// TODO(akalin): Rewrite this so that constructs like if (...)
498// CHECK_EQ(...) else { ... } work properly.
499#define CHECK_OP(name, op, val1, val2) \
500 if (std::string* _result = \
501 logging::Check##name##Impl((val1), (val2), \
502 #val1 " " #op " " #val2)) \
503 logging::LogMessage(__FILE__, __LINE__, _result).stream()
504
505#endif
506
initial.commit3f4a7322008-07-27 06:49:38 +0900507// Build the error message string. This is separate from the "Impl"
508// function template because it is not performance critical and so can
akalin@chromium.org63dab762011-02-08 16:39:08 +0900509// be out of line, while the "Impl" code should be inline. Caller
510// takes ownership of the returned string.
initial.commit3f4a7322008-07-27 06:49:38 +0900511template<class t1, class t2>
512std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) {
513 std::ostringstream ss;
514 ss << names << " (" << v1 << " vs. " << v2 << ")";
515 std::string* msg = new std::string(ss.str());
516 return msg;
517}
518
erg@google.com6575f362010-10-01 04:10:03 +0900519// MSVC doesn't like complex extern templates and DLLs.
erg@chromium.org37128d42011-10-25 05:20:30 +0900520#if !defined(COMPILER_MSVC)
erg@google.com6575f362010-10-01 04:10:03 +0900521// Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated
522// in logging.cc.
erg@chromium.org37128d42011-10-25 05:20:30 +0900523extern template BASE_EXPORT std::string* MakeCheckOpString<int, int>(
erg@google.com6575f362010-10-01 04:10:03 +0900524 const int&, const int&, const char* names);
erg@chromium.org37128d42011-10-25 05:20:30 +0900525extern template BASE_EXPORT
526std::string* MakeCheckOpString<unsigned long, unsigned long>(
erg@google.com6575f362010-10-01 04:10:03 +0900527 const unsigned long&, const unsigned long&, const char* names);
erg@chromium.org37128d42011-10-25 05:20:30 +0900528extern template BASE_EXPORT
529std::string* MakeCheckOpString<unsigned long, unsigned int>(
erg@google.com6575f362010-10-01 04:10:03 +0900530 const unsigned long&, const unsigned int&, const char* names);
erg@chromium.org37128d42011-10-25 05:20:30 +0900531extern template BASE_EXPORT
532std::string* MakeCheckOpString<unsigned int, unsigned long>(
erg@google.com6575f362010-10-01 04:10:03 +0900533 const unsigned int&, const unsigned long&, const char* names);
erg@chromium.org37128d42011-10-25 05:20:30 +0900534extern template BASE_EXPORT
535std::string* MakeCheckOpString<std::string, std::string>(
erg@google.com6575f362010-10-01 04:10:03 +0900536 const std::string&, const std::string&, const char* name);
537#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900538
akalin@chromium.orgad6c1722010-11-02 07:19:56 +0900539// Helper functions for CHECK_OP macro.
540// The (int, int) specialization works around the issue that the compiler
541// will not instantiate the template version of the function on values of
542// unnamed enum type - see comment below.
543#define DEFINE_CHECK_OP_IMPL(name, op) \
544 template <class t1, class t2> \
545 inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \
546 const char* names) { \
547 if (v1 op v2) return NULL; \
548 else return MakeCheckOpString(v1, v2, names); \
549 } \
550 inline std::string* Check##name##Impl(int v1, int v2, const char* names) { \
551 if (v1 op v2) return NULL; \
552 else return MakeCheckOpString(v1, v2, names); \
553 }
554DEFINE_CHECK_OP_IMPL(EQ, ==)
555DEFINE_CHECK_OP_IMPL(NE, !=)
556DEFINE_CHECK_OP_IMPL(LE, <=)
557DEFINE_CHECK_OP_IMPL(LT, < )
558DEFINE_CHECK_OP_IMPL(GE, >=)
559DEFINE_CHECK_OP_IMPL(GT, > )
560#undef DEFINE_CHECK_OP_IMPL
willchan@chromium.orge5ec8202010-03-02 09:41:12 +0900561
562#define CHECK_EQ(val1, val2) CHECK_OP(EQ, ==, val1, val2)
563#define CHECK_NE(val1, val2) CHECK_OP(NE, !=, val1, val2)
564#define CHECK_LE(val1, val2) CHECK_OP(LE, <=, val1, val2)
565#define CHECK_LT(val1, val2) CHECK_OP(LT, < , val1, val2)
566#define CHECK_GE(val1, val2) CHECK_OP(GE, >=, val1, val2)
567#define CHECK_GT(val1, val2) CHECK_OP(GT, > , val1, val2)
568
akalin@chromium.org836c6e62011-12-02 16:31:09 +0900569#if LOGGING_IS_OFFICIAL_BUILD
mark@chromium.org667f6212009-08-20 10:20:29 +0900570// In order to have optimized code for official builds, remove DLOGs and
571// DCHECKs.
akalin@chromium.org85510482010-10-01 06:12:33 +0900572#define ENABLE_DLOG 0
573#define ENABLE_DCHECK 0
574
575#elif defined(NDEBUG)
576// Otherwise, if we're a release build, remove DLOGs but not DCHECKs
577// (since those can still be turned on via a command-line flag).
578#define ENABLE_DLOG 0
579#define ENABLE_DCHECK 1
580
581#else
582// Otherwise, we're a debug build so enable DLOGs and DCHECKs.
583#define ENABLE_DLOG 1
584#define ENABLE_DCHECK 1
mark@chromium.org667f6212009-08-20 10:20:29 +0900585#endif
586
akalin@chromium.org85510482010-10-01 06:12:33 +0900587// Definitions for DLOG et al.
588
akalin@chromium.orgf6b59fa2010-10-01 11:58:24 +0900589#if ENABLE_DLOG
590
akalin@chromium.org25cef532010-11-02 04:49:22 +0900591#define DLOG_IS_ON(severity) LOG_IS_ON(severity)
akalin@chromium.orgf6b59fa2010-10-01 11:58:24 +0900592#define DLOG_IF(severity, condition) LOG_IF(severity, condition)
593#define DLOG_ASSERT(condition) LOG_ASSERT(condition)
akalin@chromium.orgf6b59fa2010-10-01 11:58:24 +0900594#define DPLOG_IF(severity, condition) PLOG_IF(severity, condition)
akalin@chromium.orga88579b2010-10-02 08:02:36 +0900595#define DVLOG_IF(verboselevel, condition) VLOG_IF(verboselevel, condition)
sail@chromium.org4015fc02011-03-07 03:16:31 +0900596#define DVPLOG_IF(verboselevel, condition) VPLOG_IF(verboselevel, condition)
akalin@chromium.orgf6b59fa2010-10-01 11:58:24 +0900597
598#else // ENABLE_DLOG
599
akalin@chromium.orga88579b2010-10-02 08:02:36 +0900600// If ENABLE_DLOG is off, we want to avoid emitting any references to
601// |condition| (which may reference a variable defined only if NDEBUG
602// is not defined). Contrast this with DCHECK et al., which has
603// different behavior.
akalin@chromium.orgf6b59fa2010-10-01 11:58:24 +0900604
akalin@chromium.org25cef532010-11-02 04:49:22 +0900605#define DLOG_IS_ON(severity) false
akalin@chromium.org836c6e62011-12-02 16:31:09 +0900606#define DLOG_IF(severity, condition) EAT_STREAM_PARAMETERS
607#define DLOG_ASSERT(condition) EAT_STREAM_PARAMETERS
608#define DPLOG_IF(severity, condition) EAT_STREAM_PARAMETERS
609#define DVLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS
610#define DVPLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS
akalin@chromium.orgf6b59fa2010-10-01 11:58:24 +0900611
612#endif // ENABLE_DLOG
613
akalin@chromium.org85510482010-10-01 06:12:33 +0900614// DEBUG_MODE is for uses like
615// if (DEBUG_MODE) foo.CheckThatFoo();
616// instead of
617// #ifndef NDEBUG
618// foo.CheckThatFoo();
619// #endif
620//
621// We tie its state to ENABLE_DLOG.
622enum { DEBUG_MODE = ENABLE_DLOG };
623
624#undef ENABLE_DLOG
625
akalin@chromium.orga88579b2010-10-02 08:02:36 +0900626#define DLOG(severity) \
627 LAZY_STREAM(LOG_STREAM(severity), DLOG_IS_ON(severity))
628
629#if defined(OS_WIN)
630#define DLOG_GETLASTERROR(severity) \
631 LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity), DLOG_IS_ON(severity))
632#define DLOG_GETLASTERROR_MODULE(severity, module) \
633 LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity, module), \
634 DLOG_IS_ON(severity))
635#elif defined(OS_POSIX)
636#define DLOG_ERRNO(severity) \
637 LAZY_STREAM(LOG_ERRNO_STREAM(severity), DLOG_IS_ON(severity))
638#endif
639
640#define DPLOG(severity) \
641 LAZY_STREAM(PLOG_STREAM(severity), DLOG_IS_ON(severity))
642
akalin@chromium.orga3c1d482011-10-25 15:28:45 +0900643#define DVLOG(verboselevel) DVLOG_IF(verboselevel, VLOG_IS_ON(verboselevel))
akalin@chromium.orga88579b2010-10-02 08:02:36 +0900644
sail@chromium.org4015fc02011-03-07 03:16:31 +0900645#define DVPLOG(verboselevel) DVPLOG_IF(verboselevel, VLOG_IS_ON(verboselevel))
646
akalin@chromium.orga88579b2010-10-02 08:02:36 +0900647// Definitions for DCHECK et al.
akalin@chromium.orgf6b59fa2010-10-01 11:58:24 +0900648
akalin@chromium.org85510482010-10-01 06:12:33 +0900649#if ENABLE_DCHECK
mark@chromium.org667f6212009-08-20 10:20:29 +0900650
akalin@chromium.orga88579b2010-10-02 08:02:36 +0900651#if defined(NDEBUG)
akalin@chromium.orgf6b59fa2010-10-01 11:58:24 +0900652
nsylvain@chromium.org675aad32011-09-21 05:59:01 +0900653BASE_EXPORT extern DcheckState g_dcheck_state;
654
655#if defined(DCHECK_ALWAYS_ON)
656
657#define DCHECK_IS_ON() true
658#define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \
659 COMPACT_GOOGLE_LOG_EX_FATAL(ClassName , ##__VA_ARGS__)
660#define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_FATAL
661const LogSeverity LOG_DCHECK = LOG_FATAL;
662
663#else
664
akalin@chromium.org434c5b62010-11-03 14:30:14 +0900665#define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \
666 COMPACT_GOOGLE_LOG_EX_ERROR_REPORT(ClassName , ##__VA_ARGS__)
667#define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_ERROR_REPORT
akalin@chromium.orga88579b2010-10-02 08:02:36 +0900668const LogSeverity LOG_DCHECK = LOG_ERROR_REPORT;
akalin@chromium.orgfaed5f82011-01-11 10:03:36 +0900669#define DCHECK_IS_ON() \
670 ((::logging::g_dcheck_state == \
671 ::logging::ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS) && \
672 LOG_IS_ON(DCHECK))
akalin@chromium.orgf6b59fa2010-10-01 11:58:24 +0900673
nsylvain@chromium.org675aad32011-09-21 05:59:01 +0900674#endif // defined(DCHECK_ALWAYS_ON)
675
akalin@chromium.orga88579b2010-10-02 08:02:36 +0900676#else // defined(NDEBUG)
677
akalin@chromium.org25cef532010-11-02 04:49:22 +0900678// On a regular debug build, we want to have DCHECKs enabled.
akalin@chromium.org434c5b62010-11-03 14:30:14 +0900679#define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \
680 COMPACT_GOOGLE_LOG_EX_FATAL(ClassName , ##__VA_ARGS__)
681#define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_FATAL
akalin@chromium.orga88579b2010-10-02 08:02:36 +0900682const LogSeverity LOG_DCHECK = LOG_FATAL;
akalin@chromium.org434c5b62010-11-03 14:30:14 +0900683#define DCHECK_IS_ON() true
akalin@chromium.orga88579b2010-10-02 08:02:36 +0900684
685#endif // defined(NDEBUG)
686
687#else // ENABLE_DCHECK
688
akalin@chromium.org434c5b62010-11-03 14:30:14 +0900689// These are just dummy values since DCHECK_IS_ON() is always false in
690// this case.
691#define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \
692 COMPACT_GOOGLE_LOG_EX_INFO(ClassName , ##__VA_ARGS__)
693#define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_INFO
694const LogSeverity LOG_DCHECK = LOG_INFO;
akalin@chromium.org25cef532010-11-02 04:49:22 +0900695#define DCHECK_IS_ON() false
akalin@chromium.orga88579b2010-10-02 08:02:36 +0900696
697#endif // ENABLE_DCHECK
akalin@chromium.org25cef532010-11-02 04:49:22 +0900698#undef ENABLE_DCHECK
akalin@chromium.orga88579b2010-10-02 08:02:36 +0900699
akalin@chromium.org434c5b62010-11-03 14:30:14 +0900700// DCHECK et al. make sure to reference |condition| regardless of
akalin@chromium.orga88579b2010-10-02 08:02:36 +0900701// whether DCHECKs are enabled; this is so that we don't get unused
702// variable warnings if the only use of a variable is in a DCHECK.
703// This behavior is different from DLOG_IF et al.
704
akalin@chromium.org434c5b62010-11-03 14:30:14 +0900705#define DCHECK(condition) \
706 LAZY_STREAM(LOG_STREAM(DCHECK), DCHECK_IS_ON() && !(condition)) \
akalin@chromium.orga88579b2010-10-02 08:02:36 +0900707 << "Check failed: " #condition ". "
708
akalin@chromium.org434c5b62010-11-03 14:30:14 +0900709#define DPCHECK(condition) \
710 LAZY_STREAM(PLOG_STREAM(DCHECK), DCHECK_IS_ON() && !(condition)) \
akalin@chromium.orga88579b2010-10-02 08:02:36 +0900711 << "Check failed: " #condition ". "
akalin@chromium.orgf6b59fa2010-10-01 11:58:24 +0900712
713// Helper macro for binary operators.
714// Don't use this macro directly in your code, use DCHECK_EQ et al below.
akalin@chromium.orga88579b2010-10-02 08:02:36 +0900715#define DCHECK_OP(name, op, val1, val2) \
akalin@chromium.org25cef532010-11-02 04:49:22 +0900716 if (DCHECK_IS_ON()) \
akalin@chromium.org63dab762011-02-08 16:39:08 +0900717 if (std::string* _result = \
akalin@chromium.orga88579b2010-10-02 08:02:36 +0900718 logging::Check##name##Impl((val1), (val2), \
719 #val1 " " #op " " #val2)) \
720 logging::LogMessage( \
721 __FILE__, __LINE__, ::logging::LOG_DCHECK, \
722 _result).stream()
initial.commit3f4a7322008-07-27 06:49:38 +0900723
akalin@chromium.org434c5b62010-11-03 14:30:14 +0900724// Equality/Inequality checks - compare two values, and log a
725// LOG_DCHECK message including the two values when the result is not
726// as expected. The values must have operator<<(ostream, ...)
727// defined.
initial.commit3f4a7322008-07-27 06:49:38 +0900728//
729// You may append to the error message like so:
730// DCHECK_NE(1, 2) << ": The world must be ending!";
731//
732// We are very careful to ensure that each argument is evaluated exactly
733// once, and that anything which is legal to pass as a function argument is
734// legal here. In particular, the arguments may be temporary expressions
735// which will end up being destroyed at the end of the apparent statement,
736// for example:
737// DCHECK_EQ(string("abc")[1], 'b');
738//
739// WARNING: These may not compile correctly if one of the arguments is a pointer
740// and the other is NULL. To work around this, simply static_cast NULL to the
741// type of the desired pointer.
742
743#define DCHECK_EQ(val1, val2) DCHECK_OP(EQ, ==, val1, val2)
744#define DCHECK_NE(val1, val2) DCHECK_OP(NE, !=, val1, val2)
745#define DCHECK_LE(val1, val2) DCHECK_OP(LE, <=, val1, val2)
746#define DCHECK_LT(val1, val2) DCHECK_OP(LT, < , val1, val2)
747#define DCHECK_GE(val1, val2) DCHECK_OP(GE, >=, val1, val2)
748#define DCHECK_GT(val1, val2) DCHECK_OP(GT, > , val1, val2)
749
initial.commit3f4a7322008-07-27 06:49:38 +0900750#define NOTREACHED() DCHECK(false)
751
752// Redefine the standard assert to use our nice log files
753#undef assert
754#define assert(x) DLOG_ASSERT(x)
755
756// This class more or less represents a particular log message. You
757// create an instance of LogMessage and then stream stuff to it.
758// When you finish streaming to it, ~LogMessage is called and the
759// full message gets streamed to the appropriate destination.
760//
761// You shouldn't actually use LogMessage's constructor to log things,
762// though. You should use the LOG() macro (and variants thereof)
763// above.
darin@chromium.orge585bed2011-08-06 00:34:00 +0900764class BASE_EXPORT LogMessage {
initial.commit3f4a7322008-07-27 06:49:38 +0900765 public:
766 LogMessage(const char* file, int line, LogSeverity severity, int ctr);
767
768 // Two special constructors that generate reduced amounts of code at
769 // LOG call sites for common cases.
770 //
771 // Used for LOG(INFO): Implied are:
772 // severity = LOG_INFO, ctr = 0
773 //
774 // Using this constructor instead of the more complex constructor above
775 // saves a couple of bytes per call site.
776 LogMessage(const char* file, int line);
777
778 // Used for LOG(severity) where severity != INFO. Implied
779 // are: ctr = 0
780 //
781 // Using this constructor instead of the more complex constructor above
782 // saves a couple of bytes per call site.
783 LogMessage(const char* file, int line, LogSeverity severity);
784
akalin@chromium.org63dab762011-02-08 16:39:08 +0900785 // A special constructor used for check failures. Takes ownership
786 // of the given string.
initial.commit3f4a7322008-07-27 06:49:38 +0900787 // Implied severity = LOG_FATAL
akalin@chromium.org63dab762011-02-08 16:39:08 +0900788 LogMessage(const char* file, int line, std::string* result);
initial.commit3f4a7322008-07-27 06:49:38 +0900789
huanr@chromium.org656253e2009-02-12 10:19:05 +0900790 // A special constructor used for check failures, with the option to
akalin@chromium.org63dab762011-02-08 16:39:08 +0900791 // specify severity. Takes ownership of the given string.
huanr@chromium.org656253e2009-02-12 10:19:05 +0900792 LogMessage(const char* file, int line, LogSeverity severity,
akalin@chromium.org63dab762011-02-08 16:39:08 +0900793 std::string* result);
huanr@chromium.org656253e2009-02-12 10:19:05 +0900794
initial.commit3f4a7322008-07-27 06:49:38 +0900795 ~LogMessage();
796
797 std::ostream& stream() { return stream_; }
798
799 private:
800 void Init(const char* file, int line);
801
802 LogSeverity severity_;
803 std::ostringstream stream_;
maruel@google.coma855b0f2008-07-30 22:02:03 +0900804 size_t message_start_; // Offset of the start of the message (past prefix
805 // info).
siggi@chromium.org25396e12010-11-05 00:50:49 +0900806 // The file and line information passed in to the constructor.
807 const char* file_;
808 const int line_;
809
tommi@google.com82788e12009-04-15 01:52:11 +0900810#if defined(OS_WIN)
811 // Stores the current value of GetLastError in the constructor and restores
812 // it in the destructor by calling SetLastError.
813 // This is useful since the LogMessage class uses a lot of Win32 calls
814 // that will lose the value of GLE and the code that called the log function
815 // will have lost the thread error value when the log call returns.
816 class SaveLastError {
817 public:
818 SaveLastError();
819 ~SaveLastError();
820
821 unsigned long get_error() const { return last_error_; }
822
823 protected:
824 unsigned long last_error_;
825 };
826
827 SaveLastError last_error_;
828#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900829
brettw@google.come3c034a2008-08-08 03:31:40 +0900830 DISALLOW_COPY_AND_ASSIGN(LogMessage);
initial.commit3f4a7322008-07-27 06:49:38 +0900831};
832
833// A non-macro interface to the log facility; (useful
834// when the logging level is not a compile-time constant).
835inline void LogAtLevel(int const log_level, std::string const &msg) {
836 LogMessage(__FILE__, __LINE__, log_level).stream() << msg;
837}
838
839// This class is used to explicitly ignore values in the conditional
840// logging macros. This avoids compiler warnings like "value computed
841// is not used" and "statement has no effect".
rvargas@google.com97a20ec2011-04-22 07:22:10 +0900842class LogMessageVoidify {
initial.commit3f4a7322008-07-27 06:49:38 +0900843 public:
844 LogMessageVoidify() { }
845 // This has to be an operator with a precedence lower than << but
846 // higher than ?:
847 void operator&(std::ostream&) { }
848};
849
tschmelcher@chromium.orgf29a4fc2009-10-10 08:52:20 +0900850#if defined(OS_WIN)
851typedef unsigned long SystemErrorCode;
852#elif defined(OS_POSIX)
853typedef int SystemErrorCode;
854#endif
855
856// Alias for ::GetLastError() on Windows and errno on POSIX. Avoids having to
857// pull in windows.h just for GetLastError() and DWORD.
darin@chromium.orge585bed2011-08-06 00:34:00 +0900858BASE_EXPORT SystemErrorCode GetLastSystemErrorCode();
tschmelcher@chromium.orgf29a4fc2009-10-10 08:52:20 +0900859
860#if defined(OS_WIN)
861// Appends a formatted system message of the GetLastError() type.
darin@chromium.orge585bed2011-08-06 00:34:00 +0900862class BASE_EXPORT Win32ErrorLogMessage {
tschmelcher@chromium.orgf29a4fc2009-10-10 08:52:20 +0900863 public:
864 Win32ErrorLogMessage(const char* file,
865 int line,
866 LogSeverity severity,
867 SystemErrorCode err,
868 const char* module);
869
870 Win32ErrorLogMessage(const char* file,
871 int line,
872 LogSeverity severity,
873 SystemErrorCode err);
874
tschmelcher@chromium.orgf29a4fc2009-10-10 08:52:20 +0900875 // Appends the error message before destructing the encapsulated class.
876 ~Win32ErrorLogMessage();
877
erg@google.comd5fffd42011-01-08 03:06:45 +0900878 std::ostream& stream() { return log_message_.stream(); }
879
tschmelcher@chromium.orgf29a4fc2009-10-10 08:52:20 +0900880 private:
881 SystemErrorCode err_;
882 // Optional name of the module defining the error.
883 const char* module_;
884 LogMessage log_message_;
885
886 DISALLOW_COPY_AND_ASSIGN(Win32ErrorLogMessage);
887};
888#elif defined(OS_POSIX)
889// Appends a formatted system message of the errno type
darin@chromium.orge585bed2011-08-06 00:34:00 +0900890class BASE_EXPORT ErrnoLogMessage {
tschmelcher@chromium.orgf29a4fc2009-10-10 08:52:20 +0900891 public:
892 ErrnoLogMessage(const char* file,
893 int line,
894 LogSeverity severity,
895 SystemErrorCode err);
896
tschmelcher@chromium.orgf29a4fc2009-10-10 08:52:20 +0900897 // Appends the error message before destructing the encapsulated class.
898 ~ErrnoLogMessage();
899
erg@google.comd5fffd42011-01-08 03:06:45 +0900900 std::ostream& stream() { return log_message_.stream(); }
901
tschmelcher@chromium.orgf29a4fc2009-10-10 08:52:20 +0900902 private:
903 SystemErrorCode err_;
904 LogMessage log_message_;
905
906 DISALLOW_COPY_AND_ASSIGN(ErrnoLogMessage);
907};
908#endif // OS_WIN
909
initial.commit3f4a7322008-07-27 06:49:38 +0900910// Closes the log file explicitly if open.
911// NOTE: Since the log file is opened as necessary by the action of logging
912// statements, there's no guarantee that it will stay closed
913// after this call.
darin@chromium.orge585bed2011-08-06 00:34:00 +0900914BASE_EXPORT void CloseLogFile();
initial.commit3f4a7322008-07-27 06:49:38 +0900915
willchan@chromium.org85113a12009-12-08 13:22:50 +0900916// Async signal safe logging mechanism.
darin@chromium.orge585bed2011-08-06 00:34:00 +0900917BASE_EXPORT void RawLog(int level, const char* message);
willchan@chromium.org85113a12009-12-08 13:22:50 +0900918
919#define RAW_LOG(level, message) logging::RawLog(logging::LOG_ ## level, message)
920
921#define RAW_CHECK(condition) \
922 do { \
923 if (!(condition)) \
924 logging::RawLog(logging::LOG_FATAL, "Check failed: " #condition "\n"); \
925 } while (0)
926
brettw@google.come3c034a2008-08-08 03:31:40 +0900927} // namespace logging
initial.commit3f4a7322008-07-27 06:49:38 +0900928
thakis@chromium.orgb13bd1d2010-06-17 03:39:53 +0900929// These functions are provided as a convenience for logging, which is where we
930// use streams (it is against Google style to use streams in other places). It
931// is designed to allow you to emit non-ASCII Unicode strings to the log file,
932// which is normally ASCII. It is relatively slow, so try not to use it for
933// common cases. Non-ASCII characters will be converted to UTF-8 by these
934// operators.
darin@chromium.orge585bed2011-08-06 00:34:00 +0900935BASE_EXPORT std::ostream& operator<<(std::ostream& out, const wchar_t* wstr);
thakis@chromium.orgb13bd1d2010-06-17 03:39:53 +0900936inline std::ostream& operator<<(std::ostream& out, const std::wstring& wstr) {
937 return out << wstr.c_str();
938}
939
ericroman@google.comfa95b462008-08-25 12:44:40 +0900940// The NOTIMPLEMENTED() macro annotates codepaths which have
941// not been implemented yet.
942//
943// The implementation of this macro is controlled by NOTIMPLEMENTED_POLICY:
944// 0 -- Do nothing (stripped by compiler)
945// 1 -- Warn at compile time
946// 2 -- Fail at compile time
947// 3 -- Fail at runtime (DCHECK)
948// 4 -- [default] LOG(ERROR) at runtime
949// 5 -- LOG(ERROR) at runtime, only once per call-site
950
951#ifndef NOTIMPLEMENTED_POLICY
yfriedman@chromium.org6ecced42012-07-26 01:17:57 +0900952#if defined(OS_ANDROID) && defined(OFFICIAL_BUILD)
953#define NOTIMPLEMENTED_POLICY 0
954#else
ericroman@google.comfa95b462008-08-25 12:44:40 +0900955// Select default policy: LOG(ERROR)
956#define NOTIMPLEMENTED_POLICY 4
957#endif
yfriedman@chromium.org6ecced42012-07-26 01:17:57 +0900958#endif
ericroman@google.comfa95b462008-08-25 12:44:40 +0900959
agl@chromium.orgfc910612008-10-31 08:54:26 +0900960#if defined(COMPILER_GCC)
961// On Linux, with GCC, we can use __PRETTY_FUNCTION__ to get the demangled name
962// of the current function in the NOTIMPLEMENTED message.
963#define NOTIMPLEMENTED_MSG "Not implemented reached in " << __PRETTY_FUNCTION__
964#else
965#define NOTIMPLEMENTED_MSG "NOT IMPLEMENTED"
966#endif
967
ericroman@google.comfa95b462008-08-25 12:44:40 +0900968#if NOTIMPLEMENTED_POLICY == 0
torne@chromium.org186eab22012-01-31 04:41:54 +0900969#define NOTIMPLEMENTED() EAT_STREAM_PARAMETERS
ericroman@google.comfa95b462008-08-25 12:44:40 +0900970#elif NOTIMPLEMENTED_POLICY == 1
971// TODO, figure out how to generate a warning
972#define NOTIMPLEMENTED() COMPILE_ASSERT(false, NOT_IMPLEMENTED)
973#elif NOTIMPLEMENTED_POLICY == 2
974#define NOTIMPLEMENTED() COMPILE_ASSERT(false, NOT_IMPLEMENTED)
975#elif NOTIMPLEMENTED_POLICY == 3
976#define NOTIMPLEMENTED() NOTREACHED()
977#elif NOTIMPLEMENTED_POLICY == 4
agl@chromium.orgfc910612008-10-31 08:54:26 +0900978#define NOTIMPLEMENTED() LOG(ERROR) << NOTIMPLEMENTED_MSG
ericroman@google.comfa95b462008-08-25 12:44:40 +0900979#elif NOTIMPLEMENTED_POLICY == 5
980#define NOTIMPLEMENTED() do {\
981 static int count = 0;\
agl@chromium.orgfc910612008-10-31 08:54:26 +0900982 LOG_IF(ERROR, 0 == count++) << NOTIMPLEMENTED_MSG;\
ericroman@google.comfa95b462008-08-25 12:44:40 +0900983} while(0)
984#endif
985
brettw@google.come3c034a2008-08-08 03:31:40 +0900986#endif // BASE_LOGGING_H_