blob: cac520a172cf36631e31b4f582f2b8115ba5dadd [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_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
initial.commitd7cae122008-07-26 21:49:388
[email protected]e7972d12011-06-18 11:53:149#include <cassert>
initial.commitd7cae122008-07-26 21:49:3810#include <string>
11#include <cstring>
12#include <sstream>
13
[email protected]0bea7252011-08-05 15:34:0014#include "base/base_export.h"
initial.commitd7cae122008-07-26 21:49:3815#include "base/basictypes.h"
[email protected]ddb9b332011-12-02 07:31:0916#include "base/debug/debugger.h"
[email protected]90509cb2011-03-25 18:46:3817#include "build/build_config.h"
initial.commitd7cae122008-07-26 21:49:3818
19//
20// Optional message capabilities
21// -----------------------------
22// Assertion failed messages and fatal errors are displayed in a dialog box
23// before the application exits. However, running this UI creates a message
24// loop, which causes application messages to be processed and potentially
25// dispatched to existing application windows. Since the application is in a
26// bad state when this assertion dialog is displayed, these messages may not
27// get processed and hang the dialog, or the application might go crazy.
28//
29// Therefore, it can be beneficial to display the error dialog in a separate
30// process from the main application. When the logging system needs to display
31// a fatal error dialog box, it will look for a program called
32// "DebugMessage.exe" in the same directory as the application executable. It
33// will run this application with the message as the command line, and will
34// not include the name of the application as is traditional for easier
35// parsing.
36//
37// The code for DebugMessage.exe is only one line. In WinMain, do:
38// MessageBox(NULL, GetCommandLineW(), L"Fatal Error", 0);
39//
40// If DebugMessage.exe is not found, the logging code will use a normal
41// MessageBox, potentially causing the problems discussed above.
42
43
44// Instructions
45// ------------
46//
47// Make a bunch of macros for logging. The way to log things is to stream
48// things to LOG(<a particular severity level>). E.g.,
49//
50// LOG(INFO) << "Found " << num_cookies << " cookies";
51//
52// You can also do conditional logging:
53//
54// LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
55//
56// The above will cause log messages to be output on the 1st, 11th, 21st, ...
57// times it is executed. Note that the special COUNTER value is used to
58// identify which repetition is happening.
59//
60// The CHECK(condition) macro is active in both debug and release builds and
61// effectively performs a LOG(FATAL) which terminates the process and
62// generates a crashdump unless a debugger is attached.
63//
64// There are also "debug mode" logging macros like the ones above:
65//
66// DLOG(INFO) << "Found cookies";
67//
68// DLOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
69//
70// All "debug mode" logging is compiled away to nothing for non-debug mode
71// compiles. LOG_IF and development flags also work well together
72// because the code can be compiled away sometimes.
73//
74// We also have
75//
76// LOG_ASSERT(assertion);
77// DLOG_ASSERT(assertion);
78//
79// which is syntactic sugar for {,D}LOG_IF(FATAL, assert fails) << assertion;
80//
[email protected]99b7c57f2010-09-29 19:26:3681// There are "verbose level" logging macros. They look like
82//
83// VLOG(1) << "I'm printed when you run the program with --v=1 or more";
84// VLOG(2) << "I'm printed when you run the program with --v=2 or more";
85//
86// These always log at the INFO log level (when they log at all).
87// The verbose logging can also be turned on module-by-module. For instance,
[email protected]b0d38d4c2010-10-29 00:39:4888// --vmodule=profile=2,icon_loader=1,browser_*=3,*/chromeos/*=4 --v=0
[email protected]99b7c57f2010-09-29 19:26:3689// will cause:
90// a. VLOG(2) and lower messages to be printed from profile.{h,cc}
91// b. VLOG(1) and lower messages to be printed from icon_loader.{h,cc}
92// c. VLOG(3) and lower messages to be printed from files prefixed with
93// "browser"
[email protected]e11de722010-11-01 20:50:5594// d. VLOG(4) and lower messages to be printed from files under a
[email protected]b0d38d4c2010-10-29 00:39:4895// "chromeos" directory.
[email protected]e11de722010-11-01 20:50:5596// e. VLOG(0) and lower messages to be printed from elsewhere
[email protected]99b7c57f2010-09-29 19:26:3697//
98// The wildcarding functionality shown by (c) supports both '*' (match
[email protected]b0d38d4c2010-10-29 00:39:4899// 0 or more characters) and '?' (match any single character)
100// wildcards. Any pattern containing a forward or backward slash will
101// be tested against the whole pathname and not just the module.
102// E.g., "*/foo/bar/*=2" would change the logging level for all code
103// in source files under a "foo/bar" directory.
[email protected]99b7c57f2010-09-29 19:26:36104//
105// There's also VLOG_IS_ON(n) "verbose level" condition macro. To be used as
106//
107// if (VLOG_IS_ON(2)) {
108// // do some logging preparation and logging
109// // that can't be accomplished with just VLOG(2) << ...;
110// }
111//
112// There is also a VLOG_IF "verbose level" condition macro for sample
113// cases, when some extra computation and preparation for logs is not
114// needed.
115//
116// VLOG_IF(1, (size > 1024))
117// << "I'm printed when size is more than 1024 and when you run the "
118// "program with --v=1 or more";
119//
initial.commitd7cae122008-07-26 21:49:38120// We also override the standard 'assert' to use 'DLOG_ASSERT'.
121//
[email protected]d8617a62009-10-09 23:52:20122// Lastly, there is:
123//
124// PLOG(ERROR) << "Couldn't do foo";
125// DPLOG(ERROR) << "Couldn't do foo";
126// PLOG_IF(ERROR, cond) << "Couldn't do foo";
127// DPLOG_IF(ERROR, cond) << "Couldn't do foo";
128// PCHECK(condition) << "Couldn't do foo";
129// DPCHECK(condition) << "Couldn't do foo";
130//
131// which append the last system error to the message in string form (taken from
132// GetLastError() on Windows and errno on POSIX).
133//
initial.commitd7cae122008-07-26 21:49:38134// The supported severity levels for macros that allow you to specify one
[email protected]fb62a532009-02-12 01:19:05135// are (in increasing order of severity) INFO, WARNING, ERROR, ERROR_REPORT,
136// and FATAL.
initial.commitd7cae122008-07-26 21:49:38137//
138// Very important: logging a message at the FATAL severity level causes
139// the program to terminate (after the message is logged).
[email protected]fb62a532009-02-12 01:19:05140//
141// Note the special severity of ERROR_REPORT only available/relevant in normal
142// mode, which displays error dialog without terminating the program. There is
143// no error dialog for severity ERROR or below in normal mode.
144//
145// There is also the special severity of DFATAL, which logs FATAL in
[email protected]081bd4c2010-06-24 01:01:04146// debug mode, ERROR in normal mode.
initial.commitd7cae122008-07-26 21:49:38147
148namespace logging {
149
150// Where to record logging output? A flat file and/or system debug log via
[email protected]88aa41e82008-11-18 00:59:04151// OutputDebugString. Defaults on Windows to LOG_ONLY_TO_FILE, and on
152// POSIX to LOG_ONLY_TO_SYSTEM_DEBUG_LOG (aka stderr).
initial.commitd7cae122008-07-26 21:49:38153enum LoggingDestination { LOG_NONE,
154 LOG_ONLY_TO_FILE,
155 LOG_ONLY_TO_SYSTEM_DEBUG_LOG,
156 LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG };
157
158// Indicates that the log file should be locked when being written to.
159// Often, there is no locking, which is fine for a single threaded program.
160// If logging is being done from multiple threads or there can be more than
161// one process doing the logging, the file should be locked during writes to
162// make each log outut atomic. Other writers will block.
163//
164// All processes writing to the log file must have their locking set for it to
165// work properly. Defaults to DONT_LOCK_LOG_FILE.
166enum LogLockingState { LOCK_LOG_FILE, DONT_LOCK_LOG_FILE };
167
168// On startup, should we delete or append to an existing log file (if any)?
169// Defaults to APPEND_TO_OLD_LOG_FILE.
170enum OldFileDeletionState { DELETE_OLD_LOG_FILE, APPEND_TO_OLD_LOG_FILE };
171
[email protected]7c10f7552011-01-11 01:03:36172enum DcheckState {
173 DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS,
174 ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS
175};
176
[email protected]ff3d0c32010-08-23 19:57:46177// TODO(avi): do we want to do a unification of character types here?
178#if defined(OS_WIN)
179typedef wchar_t PathChar;
180#else
181typedef char PathChar;
182#endif
183
184// Define different names for the BaseInitLoggingImpl() function depending on
185// whether NDEBUG is defined or not so that we'll fail to link if someone tries
186// to compile logging.cc with NDEBUG but includes logging.h without defining it,
187// or vice versa.
188#if NDEBUG
189#define BaseInitLoggingImpl BaseInitLoggingImpl_built_with_NDEBUG
190#else
191#define BaseInitLoggingImpl BaseInitLoggingImpl_built_without_NDEBUG
192#endif
193
194// Implementation of the InitLogging() method declared below. We use a
195// more-specific name so we can #define it above without affecting other code
196// that has named stuff "InitLogging".
[email protected]0bea7252011-08-05 15:34:00197BASE_EXPORT bool BaseInitLoggingImpl(const PathChar* log_file,
198 LoggingDestination logging_dest,
199 LogLockingState lock_log,
200 OldFileDeletionState delete_old,
201 DcheckState dcheck_state);
[email protected]ff3d0c32010-08-23 19:57:46202
initial.commitd7cae122008-07-26 21:49:38203// Sets the log file name and other global logging state. Calling this function
204// is recommended, and is normally done at the beginning of application init.
205// If you don't call it, all the flags will be initialized to their default
206// values, and there is a race condition that may leak a critical section
207// object if two threads try to do the first log at the same time.
208// See the definition of the enums above for descriptions and default values.
209//
210// The default log file is initialized to "debug.log" in the application
211// directory. You probably don't want this, especially since the program
212// directory may not be writable on an enduser's system.
[email protected]064aa162011-12-03 00:30:08213//
214// This function may be called a second time to re-direct logging (e.g after
215// loging in to a user partition), however it should never be called more than
216// twice.
[email protected]c7d5da992010-10-28 00:20:21217inline bool InitLogging(const PathChar* log_file,
[email protected]ff3d0c32010-08-23 19:57:46218 LoggingDestination logging_dest,
219 LogLockingState lock_log,
[email protected]7c10f7552011-01-11 01:03:36220 OldFileDeletionState delete_old,
221 DcheckState dcheck_state) {
222 return BaseInitLoggingImpl(log_file, logging_dest, lock_log,
223 delete_old, dcheck_state);
[email protected]ff3d0c32010-08-23 19:57:46224}
initial.commitd7cae122008-07-26 21:49:38225
226// Sets the log level. Anything at or above this level will be written to the
227// log file/displayed to the user (if applicable). Anything below this level
[email protected]162ac0f2010-11-04 15:50:49228// will be silently ignored. The log level defaults to 0 (everything is logged
229// up to level INFO) if this function is not called.
230// Note that log messages for VLOG(x) are logged at level -x, so setting
231// the min log level to negative values enables verbose logging.
[email protected]0bea7252011-08-05 15:34:00232BASE_EXPORT void SetMinLogLevel(int level);
initial.commitd7cae122008-07-26 21:49:38233
[email protected]8a2986ca2009-04-10 19:13:42234// Gets the current log level.
[email protected]0bea7252011-08-05 15:34:00235BASE_EXPORT int GetMinLogLevel();
initial.commitd7cae122008-07-26 21:49:38236
[email protected]162ac0f2010-11-04 15:50:49237// Gets the VLOG default verbosity level.
[email protected]0bea7252011-08-05 15:34:00238BASE_EXPORT int GetVlogVerbosity();
[email protected]162ac0f2010-11-04 15:50:49239
[email protected]99b7c57f2010-09-29 19:26:36240// Gets the current vlog level for the given file (usually taken from
241// __FILE__).
[email protected]2f4e9a62010-09-29 21:25:14242
243// Note that |N| is the size *with* the null terminator.
[email protected]0bea7252011-08-05 15:34:00244BASE_EXPORT int GetVlogLevelHelper(const char* file_start, size_t N);
[email protected]2f4e9a62010-09-29 21:25:14245
[email protected]99b7c57f2010-09-29 19:26:36246template <size_t N>
247int GetVlogLevel(const char (&file)[N]) {
248 return GetVlogLevelHelper(file, N);
249}
initial.commitd7cae122008-07-26 21:49:38250
251// Sets the common items you want to be prepended to each log message.
252// process and thread IDs default to off, the timestamp defaults to on.
253// If this function is not called, logging defaults to writing the timestamp
254// only.
[email protected]0bea7252011-08-05 15:34:00255BASE_EXPORT void SetLogItems(bool enable_process_id, bool enable_thread_id,
256 bool enable_timestamp, bool enable_tickcount);
initial.commitd7cae122008-07-26 21:49:38257
[email protected]81e0a852010-08-17 00:38:12258// Sets whether or not you'd like to see fatal debug messages popped up in
259// a dialog box or not.
260// Dialogs are not shown by default.
[email protected]0bea7252011-08-05 15:34:00261BASE_EXPORT void SetShowErrorDialogs(bool enable_dialogs);
[email protected]81e0a852010-08-17 00:38:12262
initial.commitd7cae122008-07-26 21:49:38263// Sets the Log Assert Handler that will be used to notify of check failures.
[email protected]fb62a532009-02-12 01:19:05264// The default handler shows a dialog box and then terminate the process,
265// however clients can use this function to override with their own handling
266// (e.g. a silent one for Unit Tests)
initial.commitd7cae122008-07-26 21:49:38267typedef void (*LogAssertHandlerFunction)(const std::string& str);
[email protected]0bea7252011-08-05 15:34:00268BASE_EXPORT void SetLogAssertHandler(LogAssertHandlerFunction handler);
[email protected]64e5cc02010-11-03 19:20:27269
[email protected]fb62a532009-02-12 01:19:05270// Sets the Log Report Handler that will be used to notify of check failures
271// in non-debug mode. The default handler shows a dialog box and continues
272// the execution, however clients can use this function to override with their
273// own handling.
274typedef void (*LogReportHandlerFunction)(const std::string& str);
[email protected]0bea7252011-08-05 15:34:00275BASE_EXPORT void SetLogReportHandler(LogReportHandlerFunction handler);
initial.commitd7cae122008-07-26 21:49:38276
[email protected]2b07b8412009-11-25 15:26:34277// Sets the Log Message Handler that gets passed every log message before
278// it's sent to other log destinations (if any).
279// Returns true to signal that it handled the message and the message
280// should not be sent to other log destinations.
[email protected]162ac0f2010-11-04 15:50:49281typedef bool (*LogMessageHandlerFunction)(int severity,
282 const char* file, int line, size_t message_start, const std::string& str);
[email protected]0bea7252011-08-05 15:34:00283BASE_EXPORT void SetLogMessageHandler(LogMessageHandlerFunction handler);
284BASE_EXPORT LogMessageHandlerFunction GetLogMessageHandler();
[email protected]2b07b8412009-11-25 15:26:34285
initial.commitd7cae122008-07-26 21:49:38286typedef int LogSeverity;
[email protected]162ac0f2010-11-04 15:50:49287const LogSeverity LOG_VERBOSE = -1; // This is level 1 verbosity
288// Note: the log severities are used to index into the array of names,
289// see log_severity_names.
initial.commitd7cae122008-07-26 21:49:38290const LogSeverity LOG_INFO = 0;
291const LogSeverity LOG_WARNING = 1;
292const LogSeverity LOG_ERROR = 2;
[email protected]fb62a532009-02-12 01:19:05293const LogSeverity LOG_ERROR_REPORT = 3;
294const LogSeverity LOG_FATAL = 4;
295const LogSeverity LOG_NUM_SEVERITIES = 5;
initial.commitd7cae122008-07-26 21:49:38296
[email protected]521b0c42010-10-01 23:02:36297// LOG_DFATAL is LOG_FATAL in debug mode, ERROR in normal mode
initial.commitd7cae122008-07-26 21:49:38298#ifdef NDEBUG
[email protected]521b0c42010-10-01 23:02:36299const LogSeverity LOG_DFATAL = LOG_ERROR;
initial.commitd7cae122008-07-26 21:49:38300#else
[email protected]521b0c42010-10-01 23:02:36301const LogSeverity LOG_DFATAL = LOG_FATAL;
initial.commitd7cae122008-07-26 21:49:38302#endif
303
304// A few definitions of macros that don't generate much code. These are used
305// by LOG() and LOG_IF, etc. Since these are used all over our code, it's
306// better to have compact code for these operations.
[email protected]d8617a62009-10-09 23:52:20307#define COMPACT_GOOGLE_LOG_EX_INFO(ClassName, ...) \
308 logging::ClassName(__FILE__, __LINE__, logging::LOG_INFO , ##__VA_ARGS__)
309#define COMPACT_GOOGLE_LOG_EX_WARNING(ClassName, ...) \
310 logging::ClassName(__FILE__, __LINE__, logging::LOG_WARNING , ##__VA_ARGS__)
311#define COMPACT_GOOGLE_LOG_EX_ERROR(ClassName, ...) \
312 logging::ClassName(__FILE__, __LINE__, logging::LOG_ERROR , ##__VA_ARGS__)
313#define COMPACT_GOOGLE_LOG_EX_ERROR_REPORT(ClassName, ...) \
314 logging::ClassName(__FILE__, __LINE__, \
315 logging::LOG_ERROR_REPORT , ##__VA_ARGS__)
316#define COMPACT_GOOGLE_LOG_EX_FATAL(ClassName, ...) \
317 logging::ClassName(__FILE__, __LINE__, logging::LOG_FATAL , ##__VA_ARGS__)
318#define COMPACT_GOOGLE_LOG_EX_DFATAL(ClassName, ...) \
[email protected]521b0c42010-10-01 23:02:36319 logging::ClassName(__FILE__, __LINE__, logging::LOG_DFATAL , ##__VA_ARGS__)
[email protected]d8617a62009-10-09 23:52:20320
initial.commitd7cae122008-07-26 21:49:38321#define COMPACT_GOOGLE_LOG_INFO \
[email protected]d8617a62009-10-09 23:52:20322 COMPACT_GOOGLE_LOG_EX_INFO(LogMessage)
initial.commitd7cae122008-07-26 21:49:38323#define COMPACT_GOOGLE_LOG_WARNING \
[email protected]d8617a62009-10-09 23:52:20324 COMPACT_GOOGLE_LOG_EX_WARNING(LogMessage)
initial.commitd7cae122008-07-26 21:49:38325#define COMPACT_GOOGLE_LOG_ERROR \
[email protected]d8617a62009-10-09 23:52:20326 COMPACT_GOOGLE_LOG_EX_ERROR(LogMessage)
[email protected]fb62a532009-02-12 01:19:05327#define COMPACT_GOOGLE_LOG_ERROR_REPORT \
[email protected]d8617a62009-10-09 23:52:20328 COMPACT_GOOGLE_LOG_EX_ERROR_REPORT(LogMessage)
initial.commitd7cae122008-07-26 21:49:38329#define COMPACT_GOOGLE_LOG_FATAL \
[email protected]d8617a62009-10-09 23:52:20330 COMPACT_GOOGLE_LOG_EX_FATAL(LogMessage)
initial.commitd7cae122008-07-26 21:49:38331#define COMPACT_GOOGLE_LOG_DFATAL \
[email protected]d8617a62009-10-09 23:52:20332 COMPACT_GOOGLE_LOG_EX_DFATAL(LogMessage)
initial.commitd7cae122008-07-26 21:49:38333
334// wingdi.h defines ERROR to be 0. When we call LOG(ERROR), it gets
335// substituted with 0, and it expands to COMPACT_GOOGLE_LOG_0. To allow us
336// to keep using this syntax, we define this macro to do the same thing
337// as COMPACT_GOOGLE_LOG_ERROR, and also define ERROR the same way that
338// the Windows SDK does for consistency.
339#define ERROR 0
[email protected]d8617a62009-10-09 23:52:20340#define COMPACT_GOOGLE_LOG_EX_0(ClassName, ...) \
341 COMPACT_GOOGLE_LOG_EX_ERROR(ClassName , ##__VA_ARGS__)
342#define COMPACT_GOOGLE_LOG_0 COMPACT_GOOGLE_LOG_ERROR
[email protected]521b0c42010-10-01 23:02:36343// Needed for LOG_IS_ON(ERROR).
344const LogSeverity LOG_0 = LOG_ERROR;
345
[email protected]deba0ff2010-11-03 05:30:14346// As special cases, we can assume that LOG_IS_ON(ERROR_REPORT) and
347// LOG_IS_ON(FATAL) always hold. Also, LOG_IS_ON(DFATAL) always holds
348// in debug mode. In particular, CHECK()s will always fire if they
349// fail.
[email protected]521b0c42010-10-01 23:02:36350#define LOG_IS_ON(severity) \
351 ((::logging::LOG_ ## severity) >= ::logging::GetMinLogLevel())
352
353// We can't do any caching tricks with VLOG_IS_ON() like the
354// google-glog version since it requires GCC extensions. This means
355// that using the v-logging functions in conjunction with --vmodule
356// may be slow.
357#define VLOG_IS_ON(verboselevel) \
358 ((verboselevel) <= ::logging::GetVlogLevel(__FILE__))
359
360// Helper macro which avoids evaluating the arguments to a stream if
361// the condition doesn't hold.
362#define LAZY_STREAM(stream, condition) \
363 !(condition) ? (void) 0 : ::logging::LogMessageVoidify() & (stream)
initial.commitd7cae122008-07-26 21:49:38364
365// We use the preprocessor's merging operator, "##", so that, e.g.,
366// LOG(INFO) becomes the token COMPACT_GOOGLE_LOG_INFO. There's some funny
367// subtle difference between ostream member streaming functions (e.g.,
368// ostream::operator<<(int) and ostream non-member streaming functions
369// (e.g., ::operator<<(ostream&, string&): it turns out that it's
370// impossible to stream something like a string directly to an unnamed
371// ostream. We employ a neat hack by calling the stream() member
372// function of LogMessage which seems to avoid the problem.
[email protected]521b0c42010-10-01 23:02:36373#define LOG_STREAM(severity) COMPACT_GOOGLE_LOG_ ## severity.stream()
initial.commitd7cae122008-07-26 21:49:38374
[email protected]521b0c42010-10-01 23:02:36375#define LOG(severity) LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity))
376#define LOG_IF(severity, condition) \
377 LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity) && (condition))
378
initial.commitd7cae122008-07-26 21:49:38379#define SYSLOG(severity) LOG(severity)
[email protected]521b0c42010-10-01 23:02:36380#define SYSLOG_IF(severity, condition) LOG_IF(severity, condition)
381
[email protected]162ac0f2010-11-04 15:50:49382// The VLOG macros log with negative verbosities.
383#define VLOG_STREAM(verbose_level) \
384 logging::LogMessage(__FILE__, __LINE__, -verbose_level).stream()
385
386#define VLOG(verbose_level) \
387 LAZY_STREAM(VLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level))
388
389#define VLOG_IF(verbose_level, condition) \
390 LAZY_STREAM(VLOG_STREAM(verbose_level), \
391 VLOG_IS_ON(verbose_level) && (condition))
[email protected]99b7c57f2010-09-29 19:26:36392
[email protected]fb879b1a2011-03-06 18:16:31393#if defined (OS_WIN)
394#define VPLOG_STREAM(verbose_level) \
395 logging::Win32ErrorLogMessage(__FILE__, __LINE__, -verbose_level, \
396 ::logging::GetLastSystemErrorCode()).stream()
397#elif defined(OS_POSIX)
398#define VPLOG_STREAM(verbose_level) \
399 logging::ErrnoLogMessage(__FILE__, __LINE__, -verbose_level, \
400 ::logging::GetLastSystemErrorCode()).stream()
401#endif
402
403#define VPLOG(verbose_level) \
404 LAZY_STREAM(VPLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level))
405
406#define VPLOG_IF(verbose_level, condition) \
407 LAZY_STREAM(VPLOG_STREAM(verbose_level), \
408 VLOG_IS_ON(verbose_level) && (condition))
409
[email protected]99b7c57f2010-09-29 19:26:36410// TODO(akalin): Add more VLOG variants, e.g. VPLOG.
initial.commitd7cae122008-07-26 21:49:38411
initial.commitd7cae122008-07-26 21:49:38412#define LOG_ASSERT(condition) \
413 LOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". "
414#define SYSLOG_ASSERT(condition) \
415 SYSLOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". "
416
[email protected]d8617a62009-10-09 23:52:20417#if defined(OS_WIN)
[email protected]521b0c42010-10-01 23:02:36418#define LOG_GETLASTERROR_STREAM(severity) \
[email protected]d8617a62009-10-09 23:52:20419 COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \
420 ::logging::GetLastSystemErrorCode()).stream()
[email protected]521b0c42010-10-01 23:02:36421#define LOG_GETLASTERROR(severity) \
422 LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity), LOG_IS_ON(severity))
423#define LOG_GETLASTERROR_MODULE_STREAM(severity, module) \
[email protected]d8617a62009-10-09 23:52:20424 COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \
425 ::logging::GetLastSystemErrorCode(), module).stream()
[email protected]521b0c42010-10-01 23:02:36426#define LOG_GETLASTERROR_MODULE(severity, module) \
427 LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity, module), \
428 LOG_IS_ON(severity))
429// PLOG_STREAM is used by PLOG, which is the usual error logging macro
430// for each platform.
431#define PLOG_STREAM(severity) LOG_GETLASTERROR_STREAM(severity)
[email protected]d8617a62009-10-09 23:52:20432#elif defined(OS_POSIX)
[email protected]521b0c42010-10-01 23:02:36433#define LOG_ERRNO_STREAM(severity) \
[email protected]d8617a62009-10-09 23:52:20434 COMPACT_GOOGLE_LOG_EX_ ## severity(ErrnoLogMessage, \
435 ::logging::GetLastSystemErrorCode()).stream()
[email protected]521b0c42010-10-01 23:02:36436#define LOG_ERRNO(severity) \
437 LAZY_STREAM(LOG_ERRNO_STREAM(severity), LOG_IS_ON(severity))
438// PLOG_STREAM is used by PLOG, which is the usual error logging macro
439// for each platform.
440#define PLOG_STREAM(severity) LOG_ERRNO_STREAM(severity)
[email protected]d8617a62009-10-09 23:52:20441#endif
442
[email protected]521b0c42010-10-01 23:02:36443#define PLOG(severity) \
444 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity))
445
[email protected]d8617a62009-10-09 23:52:20446#define PLOG_IF(severity, condition) \
[email protected]521b0c42010-10-01 23:02:36447 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity) && (condition))
[email protected]d8617a62009-10-09 23:52:20448
[email protected]ddb9b332011-12-02 07:31:09449// http://crbug.com/16512 is open for a real fix for this. For now, Windows
450// uses OFFICIAL_BUILD and other platforms use the branding flag when NDEBUG is
451// defined.
452#if ( defined(OS_WIN) && defined(OFFICIAL_BUILD)) || \
453 (!defined(OS_WIN) && defined(NDEBUG) && defined(GOOGLE_CHROME_BUILD))
454#define LOGGING_IS_OFFICIAL_BUILD 1
455#else
456#define LOGGING_IS_OFFICIAL_BUILD 0
457#endif
458
459// The actual stream used isn't important.
460#define EAT_STREAM_PARAMETERS \
461 true ? (void) 0 : ::logging::LogMessageVoidify() & LOG_STREAM(FATAL)
462
initial.commitd7cae122008-07-26 21:49:38463// CHECK dies with a fatal error if condition is not true. It is *not*
464// controlled by NDEBUG, so the check will be executed regardless of
465// compilation mode.
[email protected]521b0c42010-10-01 23:02:36466//
467// We make sure CHECK et al. always evaluates their arguments, as
468// doing CHECK(FunctionWithSideEffect()) is a common idiom.
[email protected]ddb9b332011-12-02 07:31:09469
470#if LOGGING_IS_OFFICIAL_BUILD
471
472// Make all CHECK functions discard their log strings to reduce code
473// bloat for official builds.
474
475// TODO(akalin): This would be more valuable if there were some way to
476// remove BreakDebugger() from the backtrace, perhaps by turning it
477// into a macro (like __debugbreak() on Windows).
478#define CHECK(condition) \
479 !(condition) ? ::base::debug::BreakDebugger() : EAT_STREAM_PARAMETERS
480
481#define PCHECK(condition) CHECK(condition)
482
483#define CHECK_OP(name, op, val1, val2) CHECK((val1) op (val2))
484
485#else
486
[email protected]521b0c42010-10-01 23:02:36487#define CHECK(condition) \
488 LAZY_STREAM(LOG_STREAM(FATAL), !(condition)) \
489 << "Check failed: " #condition ". "
initial.commitd7cae122008-07-26 21:49:38490
[email protected]d8617a62009-10-09 23:52:20491#define PCHECK(condition) \
[email protected]521b0c42010-10-01 23:02:36492 LAZY_STREAM(PLOG_STREAM(FATAL), !(condition)) \
493 << "Check failed: " #condition ". "
[email protected]d8617a62009-10-09 23:52:20494
[email protected]ddb9b332011-12-02 07:31:09495// Helper macro for binary operators.
496// Don't use this macro directly in your code, use CHECK_EQ et al below.
497//
498// TODO(akalin): Rewrite this so that constructs like if (...)
499// CHECK_EQ(...) else { ... } work properly.
500#define CHECK_OP(name, op, val1, val2) \
501 if (std::string* _result = \
502 logging::Check##name##Impl((val1), (val2), \
503 #val1 " " #op " " #val2)) \
504 logging::LogMessage(__FILE__, __LINE__, _result).stream()
505
506#endif
507
initial.commitd7cae122008-07-26 21:49:38508// Build the error message string. This is separate from the "Impl"
509// function template because it is not performance critical and so can
[email protected]9c7132e2011-02-08 07:39:08510// be out of line, while the "Impl" code should be inline. Caller
511// takes ownership of the returned string.
initial.commitd7cae122008-07-26 21:49:38512template<class t1, class t2>
513std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) {
514 std::ostringstream ss;
515 ss << names << " (" << v1 << " vs. " << v2 << ")";
516 std::string* msg = new std::string(ss.str());
517 return msg;
518}
519
[email protected]6d445d32010-09-30 19:10:03520// MSVC doesn't like complex extern templates and DLLs.
[email protected]dc72da32011-10-24 20:20:30521#if !defined(COMPILER_MSVC)
[email protected]6d445d32010-09-30 19:10:03522// Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated
523// in logging.cc.
[email protected]dc72da32011-10-24 20:20:30524extern template BASE_EXPORT std::string* MakeCheckOpString<int, int>(
[email protected]6d445d32010-09-30 19:10:03525 const int&, const int&, const char* names);
[email protected]dc72da32011-10-24 20:20:30526extern template BASE_EXPORT
527std::string* MakeCheckOpString<unsigned long, unsigned long>(
[email protected]6d445d32010-09-30 19:10:03528 const unsigned long&, const unsigned long&, const char* names);
[email protected]dc72da32011-10-24 20:20:30529extern template BASE_EXPORT
530std::string* MakeCheckOpString<unsigned long, unsigned int>(
[email protected]6d445d32010-09-30 19:10:03531 const unsigned long&, const unsigned int&, const char* names);
[email protected]dc72da32011-10-24 20:20:30532extern template BASE_EXPORT
533std::string* MakeCheckOpString<unsigned int, unsigned long>(
[email protected]6d445d32010-09-30 19:10:03534 const unsigned int&, const unsigned long&, const char* names);
[email protected]dc72da32011-10-24 20:20:30535extern template BASE_EXPORT
536std::string* MakeCheckOpString<std::string, std::string>(
[email protected]6d445d32010-09-30 19:10:03537 const std::string&, const std::string&, const char* name);
538#endif
initial.commitd7cae122008-07-26 21:49:38539
[email protected]71512602010-11-01 22:19:56540// Helper functions for CHECK_OP macro.
541// The (int, int) specialization works around the issue that the compiler
542// will not instantiate the template version of the function on values of
543// unnamed enum type - see comment below.
544#define DEFINE_CHECK_OP_IMPL(name, op) \
545 template <class t1, class t2> \
546 inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \
547 const char* names) { \
548 if (v1 op v2) return NULL; \
549 else return MakeCheckOpString(v1, v2, names); \
550 } \
551 inline std::string* Check##name##Impl(int v1, int v2, const char* names) { \
552 if (v1 op v2) return NULL; \
553 else return MakeCheckOpString(v1, v2, names); \
554 }
555DEFINE_CHECK_OP_IMPL(EQ, ==)
556DEFINE_CHECK_OP_IMPL(NE, !=)
557DEFINE_CHECK_OP_IMPL(LE, <=)
558DEFINE_CHECK_OP_IMPL(LT, < )
559DEFINE_CHECK_OP_IMPL(GE, >=)
560DEFINE_CHECK_OP_IMPL(GT, > )
561#undef DEFINE_CHECK_OP_IMPL
[email protected]e150c0382010-03-02 00:41:12562
563#define CHECK_EQ(val1, val2) CHECK_OP(EQ, ==, val1, val2)
564#define CHECK_NE(val1, val2) CHECK_OP(NE, !=, val1, val2)
565#define CHECK_LE(val1, val2) CHECK_OP(LE, <=, val1, val2)
566#define CHECK_LT(val1, val2) CHECK_OP(LT, < , val1, val2)
567#define CHECK_GE(val1, val2) CHECK_OP(GE, >=, val1, val2)
568#define CHECK_GT(val1, val2) CHECK_OP(GT, > , val1, val2)
569
[email protected]ddb9b332011-12-02 07:31:09570#if LOGGING_IS_OFFICIAL_BUILD
[email protected]e3cca332009-08-20 01:20:29571// In order to have optimized code for official builds, remove DLOGs and
572// DCHECKs.
[email protected]d15e56c2010-09-30 21:12:33573#define ENABLE_DLOG 0
574#define ENABLE_DCHECK 0
575
576#elif defined(NDEBUG)
577// Otherwise, if we're a release build, remove DLOGs but not DCHECKs
578// (since those can still be turned on via a command-line flag).
579#define ENABLE_DLOG 0
580#define ENABLE_DCHECK 1
581
582#else
583// Otherwise, we're a debug build so enable DLOGs and DCHECKs.
584#define ENABLE_DLOG 1
585#define ENABLE_DCHECK 1
[email protected]e3cca332009-08-20 01:20:29586#endif
587
[email protected]d15e56c2010-09-30 21:12:33588// Definitions for DLOG et al.
589
[email protected]d926c202010-10-01 02:58:24590#if ENABLE_DLOG
591
[email protected]5e987802010-11-01 19:49:22592#define DLOG_IS_ON(severity) LOG_IS_ON(severity)
[email protected]d926c202010-10-01 02:58:24593#define DLOG_IF(severity, condition) LOG_IF(severity, condition)
594#define DLOG_ASSERT(condition) LOG_ASSERT(condition)
[email protected]d926c202010-10-01 02:58:24595#define DPLOG_IF(severity, condition) PLOG_IF(severity, condition)
[email protected]521b0c42010-10-01 23:02:36596#define DVLOG_IF(verboselevel, condition) VLOG_IF(verboselevel, condition)
[email protected]fb879b1a2011-03-06 18:16:31597#define DVPLOG_IF(verboselevel, condition) VPLOG_IF(verboselevel, condition)
[email protected]d926c202010-10-01 02:58:24598
599#else // ENABLE_DLOG
600
[email protected]521b0c42010-10-01 23:02:36601// If ENABLE_DLOG is off, we want to avoid emitting any references to
602// |condition| (which may reference a variable defined only if NDEBUG
603// is not defined). Contrast this with DCHECK et al., which has
604// different behavior.
[email protected]d926c202010-10-01 02:58:24605
[email protected]5e987802010-11-01 19:49:22606#define DLOG_IS_ON(severity) false
[email protected]ddb9b332011-12-02 07:31:09607#define DLOG_IF(severity, condition) EAT_STREAM_PARAMETERS
608#define DLOG_ASSERT(condition) EAT_STREAM_PARAMETERS
609#define DPLOG_IF(severity, condition) EAT_STREAM_PARAMETERS
610#define DVLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS
611#define DVPLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS
[email protected]d926c202010-10-01 02:58:24612
613#endif // ENABLE_DLOG
614
[email protected]d15e56c2010-09-30 21:12:33615// DEBUG_MODE is for uses like
616// if (DEBUG_MODE) foo.CheckThatFoo();
617// instead of
618// #ifndef NDEBUG
619// foo.CheckThatFoo();
620// #endif
621//
622// We tie its state to ENABLE_DLOG.
623enum { DEBUG_MODE = ENABLE_DLOG };
624
625#undef ENABLE_DLOG
626
[email protected]521b0c42010-10-01 23:02:36627#define DLOG(severity) \
628 LAZY_STREAM(LOG_STREAM(severity), DLOG_IS_ON(severity))
629
630#if defined(OS_WIN)
631#define DLOG_GETLASTERROR(severity) \
632 LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity), DLOG_IS_ON(severity))
633#define DLOG_GETLASTERROR_MODULE(severity, module) \
634 LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity, module), \
635 DLOG_IS_ON(severity))
636#elif defined(OS_POSIX)
637#define DLOG_ERRNO(severity) \
638 LAZY_STREAM(LOG_ERRNO_STREAM(severity), DLOG_IS_ON(severity))
639#endif
640
641#define DPLOG(severity) \
642 LAZY_STREAM(PLOG_STREAM(severity), DLOG_IS_ON(severity))
643
[email protected]c3ab11c2011-10-25 06:28:45644#define DVLOG(verboselevel) DVLOG_IF(verboselevel, VLOG_IS_ON(verboselevel))
[email protected]521b0c42010-10-01 23:02:36645
[email protected]fb879b1a2011-03-06 18:16:31646#define DVPLOG(verboselevel) DVPLOG_IF(verboselevel, VLOG_IS_ON(verboselevel))
647
[email protected]521b0c42010-10-01 23:02:36648// Definitions for DCHECK et al.
[email protected]d926c202010-10-01 02:58:24649
[email protected]d15e56c2010-09-30 21:12:33650#if ENABLE_DCHECK
[email protected]e3cca332009-08-20 01:20:29651
[email protected]521b0c42010-10-01 23:02:36652#if defined(NDEBUG)
[email protected]d926c202010-10-01 02:58:24653
[email protected]20960e072011-09-20 20:59:01654BASE_EXPORT extern DcheckState g_dcheck_state;
655
656#if defined(DCHECK_ALWAYS_ON)
657
658#define DCHECK_IS_ON() true
659#define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \
660 COMPACT_GOOGLE_LOG_EX_FATAL(ClassName , ##__VA_ARGS__)
661#define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_FATAL
662const LogSeverity LOG_DCHECK = LOG_FATAL;
663
664#else
665
[email protected]deba0ff2010-11-03 05:30:14666#define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \
667 COMPACT_GOOGLE_LOG_EX_ERROR_REPORT(ClassName , ##__VA_ARGS__)
668#define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_ERROR_REPORT
[email protected]521b0c42010-10-01 23:02:36669const LogSeverity LOG_DCHECK = LOG_ERROR_REPORT;
[email protected]7c10f7552011-01-11 01:03:36670#define DCHECK_IS_ON() \
671 ((::logging::g_dcheck_state == \
672 ::logging::ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS) && \
673 LOG_IS_ON(DCHECK))
[email protected]d926c202010-10-01 02:58:24674
[email protected]20960e072011-09-20 20:59:01675#endif // defined(DCHECK_ALWAYS_ON)
676
[email protected]521b0c42010-10-01 23:02:36677#else // defined(NDEBUG)
678
[email protected]5e987802010-11-01 19:49:22679// On a regular debug build, we want to have DCHECKs enabled.
[email protected]deba0ff2010-11-03 05:30:14680#define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \
681 COMPACT_GOOGLE_LOG_EX_FATAL(ClassName , ##__VA_ARGS__)
682#define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_FATAL
[email protected]521b0c42010-10-01 23:02:36683const LogSeverity LOG_DCHECK = LOG_FATAL;
[email protected]deba0ff2010-11-03 05:30:14684#define DCHECK_IS_ON() true
[email protected]521b0c42010-10-01 23:02:36685
686#endif // defined(NDEBUG)
687
688#else // ENABLE_DCHECK
689
[email protected]deba0ff2010-11-03 05:30:14690// These are just dummy values since DCHECK_IS_ON() is always false in
691// this case.
692#define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \
693 COMPACT_GOOGLE_LOG_EX_INFO(ClassName , ##__VA_ARGS__)
694#define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_INFO
695const LogSeverity LOG_DCHECK = LOG_INFO;
[email protected]5e987802010-11-01 19:49:22696#define DCHECK_IS_ON() false
[email protected]521b0c42010-10-01 23:02:36697
698#endif // ENABLE_DCHECK
[email protected]5e987802010-11-01 19:49:22699#undef ENABLE_DCHECK
[email protected]521b0c42010-10-01 23:02:36700
[email protected]deba0ff2010-11-03 05:30:14701// DCHECK et al. make sure to reference |condition| regardless of
[email protected]521b0c42010-10-01 23:02:36702// whether DCHECKs are enabled; this is so that we don't get unused
703// variable warnings if the only use of a variable is in a DCHECK.
704// This behavior is different from DLOG_IF et al.
705
[email protected]deba0ff2010-11-03 05:30:14706#define DCHECK(condition) \
707 LAZY_STREAM(LOG_STREAM(DCHECK), DCHECK_IS_ON() && !(condition)) \
[email protected]521b0c42010-10-01 23:02:36708 << "Check failed: " #condition ". "
709
[email protected]deba0ff2010-11-03 05:30:14710#define DPCHECK(condition) \
711 LAZY_STREAM(PLOG_STREAM(DCHECK), DCHECK_IS_ON() && !(condition)) \
[email protected]521b0c42010-10-01 23:02:36712 << "Check failed: " #condition ". "
[email protected]d926c202010-10-01 02:58:24713
714// Helper macro for binary operators.
715// Don't use this macro directly in your code, use DCHECK_EQ et al below.
[email protected]521b0c42010-10-01 23:02:36716#define DCHECK_OP(name, op, val1, val2) \
[email protected]5e987802010-11-01 19:49:22717 if (DCHECK_IS_ON()) \
[email protected]9c7132e2011-02-08 07:39:08718 if (std::string* _result = \
[email protected]521b0c42010-10-01 23:02:36719 logging::Check##name##Impl((val1), (val2), \
720 #val1 " " #op " " #val2)) \
721 logging::LogMessage( \
722 __FILE__, __LINE__, ::logging::LOG_DCHECK, \
723 _result).stream()
initial.commitd7cae122008-07-26 21:49:38724
[email protected]deba0ff2010-11-03 05:30:14725// Equality/Inequality checks - compare two values, and log a
726// LOG_DCHECK message including the two values when the result is not
727// as expected. The values must have operator<<(ostream, ...)
728// defined.
initial.commitd7cae122008-07-26 21:49:38729//
730// You may append to the error message like so:
731// DCHECK_NE(1, 2) << ": The world must be ending!";
732//
733// We are very careful to ensure that each argument is evaluated exactly
734// once, and that anything which is legal to pass as a function argument is
735// legal here. In particular, the arguments may be temporary expressions
736// which will end up being destroyed at the end of the apparent statement,
737// for example:
738// DCHECK_EQ(string("abc")[1], 'b');
739//
740// WARNING: These may not compile correctly if one of the arguments is a pointer
741// and the other is NULL. To work around this, simply static_cast NULL to the
742// type of the desired pointer.
743
744#define DCHECK_EQ(val1, val2) DCHECK_OP(EQ, ==, val1, val2)
745#define DCHECK_NE(val1, val2) DCHECK_OP(NE, !=, val1, val2)
746#define DCHECK_LE(val1, val2) DCHECK_OP(LE, <=, val1, val2)
747#define DCHECK_LT(val1, val2) DCHECK_OP(LT, < , val1, val2)
748#define DCHECK_GE(val1, val2) DCHECK_OP(GE, >=, val1, val2)
749#define DCHECK_GT(val1, val2) DCHECK_OP(GT, > , val1, val2)
750
initial.commitd7cae122008-07-26 21:49:38751#define NOTREACHED() DCHECK(false)
752
753// Redefine the standard assert to use our nice log files
754#undef assert
755#define assert(x) DLOG_ASSERT(x)
756
757// This class more or less represents a particular log message. You
758// create an instance of LogMessage and then stream stuff to it.
759// When you finish streaming to it, ~LogMessage is called and the
760// full message gets streamed to the appropriate destination.
761//
762// You shouldn't actually use LogMessage's constructor to log things,
763// though. You should use the LOG() macro (and variants thereof)
764// above.
[email protected]0bea7252011-08-05 15:34:00765class BASE_EXPORT LogMessage {
initial.commitd7cae122008-07-26 21:49:38766 public:
767 LogMessage(const char* file, int line, LogSeverity severity, int ctr);
768
769 // Two special constructors that generate reduced amounts of code at
770 // LOG call sites for common cases.
771 //
772 // Used for LOG(INFO): Implied are:
773 // severity = LOG_INFO, ctr = 0
774 //
775 // Using this constructor instead of the more complex constructor above
776 // saves a couple of bytes per call site.
777 LogMessage(const char* file, int line);
778
779 // Used for LOG(severity) where severity != INFO. Implied
780 // are: ctr = 0
781 //
782 // Using this constructor instead of the more complex constructor above
783 // saves a couple of bytes per call site.
784 LogMessage(const char* file, int line, LogSeverity severity);
785
[email protected]9c7132e2011-02-08 07:39:08786 // A special constructor used for check failures. Takes ownership
787 // of the given string.
initial.commitd7cae122008-07-26 21:49:38788 // Implied severity = LOG_FATAL
[email protected]9c7132e2011-02-08 07:39:08789 LogMessage(const char* file, int line, std::string* result);
initial.commitd7cae122008-07-26 21:49:38790
[email protected]fb62a532009-02-12 01:19:05791 // A special constructor used for check failures, with the option to
[email protected]9c7132e2011-02-08 07:39:08792 // specify severity. Takes ownership of the given string.
[email protected]fb62a532009-02-12 01:19:05793 LogMessage(const char* file, int line, LogSeverity severity,
[email protected]9c7132e2011-02-08 07:39:08794 std::string* result);
[email protected]fb62a532009-02-12 01:19:05795
initial.commitd7cae122008-07-26 21:49:38796 ~LogMessage();
797
798 std::ostream& stream() { return stream_; }
799
800 private:
801 void Init(const char* file, int line);
802
803 LogSeverity severity_;
804 std::ostringstream stream_;
[email protected]c88873922008-07-30 13:02:03805 size_t message_start_; // Offset of the start of the message (past prefix
806 // info).
[email protected]162ac0f2010-11-04 15:50:49807 // The file and line information passed in to the constructor.
808 const char* file_;
809 const int line_;
810
[email protected]3f85caa2009-04-14 16:52:11811#if defined(OS_WIN)
812 // Stores the current value of GetLastError in the constructor and restores
813 // it in the destructor by calling SetLastError.
814 // This is useful since the LogMessage class uses a lot of Win32 calls
815 // that will lose the value of GLE and the code that called the log function
816 // will have lost the thread error value when the log call returns.
817 class SaveLastError {
818 public:
819 SaveLastError();
820 ~SaveLastError();
821
822 unsigned long get_error() const { return last_error_; }
823
824 protected:
825 unsigned long last_error_;
826 };
827
828 SaveLastError last_error_;
829#endif
initial.commitd7cae122008-07-26 21:49:38830
[email protected]39be4242008-08-07 18:31:40831 DISALLOW_COPY_AND_ASSIGN(LogMessage);
initial.commitd7cae122008-07-26 21:49:38832};
833
834// A non-macro interface to the log facility; (useful
835// when the logging level is not a compile-time constant).
836inline void LogAtLevel(int const log_level, std::string const &msg) {
837 LogMessage(__FILE__, __LINE__, log_level).stream() << msg;
838}
839
840// This class is used to explicitly ignore values in the conditional
841// logging macros. This avoids compiler warnings like "value computed
842// is not used" and "statement has no effect".
[email protected]23bb71f2011-04-21 22:22:10843class LogMessageVoidify {
initial.commitd7cae122008-07-26 21:49:38844 public:
845 LogMessageVoidify() { }
846 // This has to be an operator with a precedence lower than << but
847 // higher than ?:
848 void operator&(std::ostream&) { }
849};
850
[email protected]d8617a62009-10-09 23:52:20851#if defined(OS_WIN)
852typedef unsigned long SystemErrorCode;
853#elif defined(OS_POSIX)
854typedef int SystemErrorCode;
855#endif
856
857// Alias for ::GetLastError() on Windows and errno on POSIX. Avoids having to
858// pull in windows.h just for GetLastError() and DWORD.
[email protected]0bea7252011-08-05 15:34:00859BASE_EXPORT SystemErrorCode GetLastSystemErrorCode();
[email protected]d8617a62009-10-09 23:52:20860
861#if defined(OS_WIN)
862// Appends a formatted system message of the GetLastError() type.
[email protected]0bea7252011-08-05 15:34:00863class BASE_EXPORT Win32ErrorLogMessage {
[email protected]d8617a62009-10-09 23:52:20864 public:
865 Win32ErrorLogMessage(const char* file,
866 int line,
867 LogSeverity severity,
868 SystemErrorCode err,
869 const char* module);
870
871 Win32ErrorLogMessage(const char* file,
872 int line,
873 LogSeverity severity,
874 SystemErrorCode err);
875
[email protected]d8617a62009-10-09 23:52:20876 // Appends the error message before destructing the encapsulated class.
877 ~Win32ErrorLogMessage();
878
[email protected]a502bbe72011-01-07 18:06:45879 std::ostream& stream() { return log_message_.stream(); }
880
[email protected]d8617a62009-10-09 23:52:20881 private:
882 SystemErrorCode err_;
883 // Optional name of the module defining the error.
884 const char* module_;
885 LogMessage log_message_;
886
887 DISALLOW_COPY_AND_ASSIGN(Win32ErrorLogMessage);
888};
889#elif defined(OS_POSIX)
890// Appends a formatted system message of the errno type
[email protected]0bea7252011-08-05 15:34:00891class BASE_EXPORT ErrnoLogMessage {
[email protected]d8617a62009-10-09 23:52:20892 public:
893 ErrnoLogMessage(const char* file,
894 int line,
895 LogSeverity severity,
896 SystemErrorCode err);
897
[email protected]d8617a62009-10-09 23:52:20898 // Appends the error message before destructing the encapsulated class.
899 ~ErrnoLogMessage();
900
[email protected]a502bbe72011-01-07 18:06:45901 std::ostream& stream() { return log_message_.stream(); }
902
[email protected]d8617a62009-10-09 23:52:20903 private:
904 SystemErrorCode err_;
905 LogMessage log_message_;
906
907 DISALLOW_COPY_AND_ASSIGN(ErrnoLogMessage);
908};
909#endif // OS_WIN
910
initial.commitd7cae122008-07-26 21:49:38911// Closes the log file explicitly if open.
912// NOTE: Since the log file is opened as necessary by the action of logging
913// statements, there's no guarantee that it will stay closed
914// after this call.
[email protected]0bea7252011-08-05 15:34:00915BASE_EXPORT void CloseLogFile();
initial.commitd7cae122008-07-26 21:49:38916
[email protected]e36ddc82009-12-08 04:22:50917// Async signal safe logging mechanism.
[email protected]0bea7252011-08-05 15:34:00918BASE_EXPORT void RawLog(int level, const char* message);
[email protected]e36ddc82009-12-08 04:22:50919
920#define RAW_LOG(level, message) logging::RawLog(logging::LOG_ ## level, message)
921
922#define RAW_CHECK(condition) \
923 do { \
924 if (!(condition)) \
925 logging::RawLog(logging::LOG_FATAL, "Check failed: " #condition "\n"); \
926 } while (0)
927
[email protected]39be4242008-08-07 18:31:40928} // namespace logging
initial.commitd7cae122008-07-26 21:49:38929
[email protected]46ce5b562010-06-16 18:39:53930// These functions are provided as a convenience for logging, which is where we
931// use streams (it is against Google style to use streams in other places). It
932// is designed to allow you to emit non-ASCII Unicode strings to the log file,
933// which is normally ASCII. It is relatively slow, so try not to use it for
934// common cases. Non-ASCII characters will be converted to UTF-8 by these
935// operators.
[email protected]0bea7252011-08-05 15:34:00936BASE_EXPORT std::ostream& operator<<(std::ostream& out, const wchar_t* wstr);
[email protected]46ce5b562010-06-16 18:39:53937inline std::ostream& operator<<(std::ostream& out, const std::wstring& wstr) {
938 return out << wstr.c_str();
939}
940
[email protected]0dfc81b2008-08-25 03:44:40941// The NOTIMPLEMENTED() macro annotates codepaths which have
942// not been implemented yet.
943//
944// The implementation of this macro is controlled by NOTIMPLEMENTED_POLICY:
945// 0 -- Do nothing (stripped by compiler)
946// 1 -- Warn at compile time
947// 2 -- Fail at compile time
948// 3 -- Fail at runtime (DCHECK)
949// 4 -- [default] LOG(ERROR) at runtime
950// 5 -- LOG(ERROR) at runtime, only once per call-site
951
952#ifndef NOTIMPLEMENTED_POLICY
953// Select default policy: LOG(ERROR)
954#define NOTIMPLEMENTED_POLICY 4
955#endif
956
[email protected]f6cda752008-10-30 23:54:26957#if defined(COMPILER_GCC)
958// On Linux, with GCC, we can use __PRETTY_FUNCTION__ to get the demangled name
959// of the current function in the NOTIMPLEMENTED message.
960#define NOTIMPLEMENTED_MSG "Not implemented reached in " << __PRETTY_FUNCTION__
961#else
962#define NOTIMPLEMENTED_MSG "NOT IMPLEMENTED"
963#endif
964
[email protected]0dfc81b2008-08-25 03:44:40965#if NOTIMPLEMENTED_POLICY == 0
[email protected]38227292012-01-30 19:41:54966#define NOTIMPLEMENTED() EAT_STREAM_PARAMETERS
[email protected]0dfc81b2008-08-25 03:44:40967#elif NOTIMPLEMENTED_POLICY == 1
968// TODO, figure out how to generate a warning
969#define NOTIMPLEMENTED() COMPILE_ASSERT(false, NOT_IMPLEMENTED)
970#elif NOTIMPLEMENTED_POLICY == 2
971#define NOTIMPLEMENTED() COMPILE_ASSERT(false, NOT_IMPLEMENTED)
972#elif NOTIMPLEMENTED_POLICY == 3
973#define NOTIMPLEMENTED() NOTREACHED()
974#elif NOTIMPLEMENTED_POLICY == 4
[email protected]f6cda752008-10-30 23:54:26975#define NOTIMPLEMENTED() LOG(ERROR) << NOTIMPLEMENTED_MSG
[email protected]0dfc81b2008-08-25 03:44:40976#elif NOTIMPLEMENTED_POLICY == 5
977#define NOTIMPLEMENTED() do {\
978 static int count = 0;\
[email protected]f6cda752008-10-30 23:54:26979 LOG_IF(ERROR, 0 == count++) << NOTIMPLEMENTED_MSG;\
[email protected]0dfc81b2008-08-25 03:44:40980} while(0)
981#endif
982
[email protected]39be4242008-08-07 18:31:40983#endif // BASE_LOGGING_H_