blob: ae67eacc2aaa3ac129caabfa5726eae006aec062 [file] [log] [blame]
[email protected]a502bbe72011-01-07 18:06:451// Copyright (c) 2011 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]c7d5da992010-10-28 00:20:21213inline bool InitLogging(const PathChar* log_file,
[email protected]ff3d0c32010-08-23 19:57:46214 LoggingDestination logging_dest,
215 LogLockingState lock_log,
[email protected]7c10f7552011-01-11 01:03:36216 OldFileDeletionState delete_old,
217 DcheckState dcheck_state) {
218 return BaseInitLoggingImpl(log_file, logging_dest, lock_log,
219 delete_old, dcheck_state);
[email protected]ff3d0c32010-08-23 19:57:46220}
initial.commitd7cae122008-07-26 21:49:38221
222// Sets the log level. Anything at or above this level will be written to the
223// log file/displayed to the user (if applicable). Anything below this level
[email protected]162ac0f2010-11-04 15:50:49224// will be silently ignored. The log level defaults to 0 (everything is logged
225// up to level INFO) if this function is not called.
226// Note that log messages for VLOG(x) are logged at level -x, so setting
227// the min log level to negative values enables verbose logging.
[email protected]0bea7252011-08-05 15:34:00228BASE_EXPORT void SetMinLogLevel(int level);
initial.commitd7cae122008-07-26 21:49:38229
[email protected]8a2986ca2009-04-10 19:13:42230// Gets the current log level.
[email protected]0bea7252011-08-05 15:34:00231BASE_EXPORT int GetMinLogLevel();
initial.commitd7cae122008-07-26 21:49:38232
[email protected]162ac0f2010-11-04 15:50:49233// Gets the VLOG default verbosity level.
[email protected]0bea7252011-08-05 15:34:00234BASE_EXPORT int GetVlogVerbosity();
[email protected]162ac0f2010-11-04 15:50:49235
[email protected]99b7c57f2010-09-29 19:26:36236// Gets the current vlog level for the given file (usually taken from
237// __FILE__).
[email protected]2f4e9a62010-09-29 21:25:14238
239// Note that |N| is the size *with* the null terminator.
[email protected]0bea7252011-08-05 15:34:00240BASE_EXPORT int GetVlogLevelHelper(const char* file_start, size_t N);
[email protected]2f4e9a62010-09-29 21:25:14241
[email protected]99b7c57f2010-09-29 19:26:36242template <size_t N>
243int GetVlogLevel(const char (&file)[N]) {
244 return GetVlogLevelHelper(file, N);
245}
initial.commitd7cae122008-07-26 21:49:38246
247// Sets the common items you want to be prepended to each log message.
248// process and thread IDs default to off, the timestamp defaults to on.
249// If this function is not called, logging defaults to writing the timestamp
250// only.
[email protected]0bea7252011-08-05 15:34:00251BASE_EXPORT void SetLogItems(bool enable_process_id, bool enable_thread_id,
252 bool enable_timestamp, bool enable_tickcount);
initial.commitd7cae122008-07-26 21:49:38253
[email protected]81e0a852010-08-17 00:38:12254// Sets whether or not you'd like to see fatal debug messages popped up in
255// a dialog box or not.
256// Dialogs are not shown by default.
[email protected]0bea7252011-08-05 15:34:00257BASE_EXPORT void SetShowErrorDialogs(bool enable_dialogs);
[email protected]81e0a852010-08-17 00:38:12258
initial.commitd7cae122008-07-26 21:49:38259// Sets the Log Assert Handler that will be used to notify of check failures.
[email protected]fb62a532009-02-12 01:19:05260// The default handler shows a dialog box and then terminate the process,
261// however clients can use this function to override with their own handling
262// (e.g. a silent one for Unit Tests)
initial.commitd7cae122008-07-26 21:49:38263typedef void (*LogAssertHandlerFunction)(const std::string& str);
[email protected]0bea7252011-08-05 15:34:00264BASE_EXPORT void SetLogAssertHandler(LogAssertHandlerFunction handler);
[email protected]64e5cc02010-11-03 19:20:27265
[email protected]fb62a532009-02-12 01:19:05266// Sets the Log Report Handler that will be used to notify of check failures
267// in non-debug mode. The default handler shows a dialog box and continues
268// the execution, however clients can use this function to override with their
269// own handling.
270typedef void (*LogReportHandlerFunction)(const std::string& str);
[email protected]0bea7252011-08-05 15:34:00271BASE_EXPORT void SetLogReportHandler(LogReportHandlerFunction handler);
initial.commitd7cae122008-07-26 21:49:38272
[email protected]2b07b8412009-11-25 15:26:34273// Sets the Log Message Handler that gets passed every log message before
274// it's sent to other log destinations (if any).
275// Returns true to signal that it handled the message and the message
276// should not be sent to other log destinations.
[email protected]162ac0f2010-11-04 15:50:49277typedef bool (*LogMessageHandlerFunction)(int severity,
278 const char* file, int line, size_t message_start, const std::string& str);
[email protected]0bea7252011-08-05 15:34:00279BASE_EXPORT void SetLogMessageHandler(LogMessageHandlerFunction handler);
280BASE_EXPORT LogMessageHandlerFunction GetLogMessageHandler();
[email protected]2b07b8412009-11-25 15:26:34281
initial.commitd7cae122008-07-26 21:49:38282typedef int LogSeverity;
[email protected]162ac0f2010-11-04 15:50:49283const LogSeverity LOG_VERBOSE = -1; // This is level 1 verbosity
284// Note: the log severities are used to index into the array of names,
285// see log_severity_names.
initial.commitd7cae122008-07-26 21:49:38286const LogSeverity LOG_INFO = 0;
287const LogSeverity LOG_WARNING = 1;
288const LogSeverity LOG_ERROR = 2;
[email protected]fb62a532009-02-12 01:19:05289const LogSeverity LOG_ERROR_REPORT = 3;
290const LogSeverity LOG_FATAL = 4;
291const LogSeverity LOG_NUM_SEVERITIES = 5;
initial.commitd7cae122008-07-26 21:49:38292
[email protected]521b0c42010-10-01 23:02:36293// LOG_DFATAL is LOG_FATAL in debug mode, ERROR in normal mode
initial.commitd7cae122008-07-26 21:49:38294#ifdef NDEBUG
[email protected]521b0c42010-10-01 23:02:36295const LogSeverity LOG_DFATAL = LOG_ERROR;
initial.commitd7cae122008-07-26 21:49:38296#else
[email protected]521b0c42010-10-01 23:02:36297const LogSeverity LOG_DFATAL = LOG_FATAL;
initial.commitd7cae122008-07-26 21:49:38298#endif
299
300// A few definitions of macros that don't generate much code. These are used
301// by LOG() and LOG_IF, etc. Since these are used all over our code, it's
302// better to have compact code for these operations.
[email protected]d8617a62009-10-09 23:52:20303#define COMPACT_GOOGLE_LOG_EX_INFO(ClassName, ...) \
304 logging::ClassName(__FILE__, __LINE__, logging::LOG_INFO , ##__VA_ARGS__)
305#define COMPACT_GOOGLE_LOG_EX_WARNING(ClassName, ...) \
306 logging::ClassName(__FILE__, __LINE__, logging::LOG_WARNING , ##__VA_ARGS__)
307#define COMPACT_GOOGLE_LOG_EX_ERROR(ClassName, ...) \
308 logging::ClassName(__FILE__, __LINE__, logging::LOG_ERROR , ##__VA_ARGS__)
309#define COMPACT_GOOGLE_LOG_EX_ERROR_REPORT(ClassName, ...) \
310 logging::ClassName(__FILE__, __LINE__, \
311 logging::LOG_ERROR_REPORT , ##__VA_ARGS__)
312#define COMPACT_GOOGLE_LOG_EX_FATAL(ClassName, ...) \
313 logging::ClassName(__FILE__, __LINE__, logging::LOG_FATAL , ##__VA_ARGS__)
314#define COMPACT_GOOGLE_LOG_EX_DFATAL(ClassName, ...) \
[email protected]521b0c42010-10-01 23:02:36315 logging::ClassName(__FILE__, __LINE__, logging::LOG_DFATAL , ##__VA_ARGS__)
[email protected]d8617a62009-10-09 23:52:20316
initial.commitd7cae122008-07-26 21:49:38317#define COMPACT_GOOGLE_LOG_INFO \
[email protected]d8617a62009-10-09 23:52:20318 COMPACT_GOOGLE_LOG_EX_INFO(LogMessage)
initial.commitd7cae122008-07-26 21:49:38319#define COMPACT_GOOGLE_LOG_WARNING \
[email protected]d8617a62009-10-09 23:52:20320 COMPACT_GOOGLE_LOG_EX_WARNING(LogMessage)
initial.commitd7cae122008-07-26 21:49:38321#define COMPACT_GOOGLE_LOG_ERROR \
[email protected]d8617a62009-10-09 23:52:20322 COMPACT_GOOGLE_LOG_EX_ERROR(LogMessage)
[email protected]fb62a532009-02-12 01:19:05323#define COMPACT_GOOGLE_LOG_ERROR_REPORT \
[email protected]d8617a62009-10-09 23:52:20324 COMPACT_GOOGLE_LOG_EX_ERROR_REPORT(LogMessage)
initial.commitd7cae122008-07-26 21:49:38325#define COMPACT_GOOGLE_LOG_FATAL \
[email protected]d8617a62009-10-09 23:52:20326 COMPACT_GOOGLE_LOG_EX_FATAL(LogMessage)
initial.commitd7cae122008-07-26 21:49:38327#define COMPACT_GOOGLE_LOG_DFATAL \
[email protected]d8617a62009-10-09 23:52:20328 COMPACT_GOOGLE_LOG_EX_DFATAL(LogMessage)
initial.commitd7cae122008-07-26 21:49:38329
330// wingdi.h defines ERROR to be 0. When we call LOG(ERROR), it gets
331// substituted with 0, and it expands to COMPACT_GOOGLE_LOG_0. To allow us
332// to keep using this syntax, we define this macro to do the same thing
333// as COMPACT_GOOGLE_LOG_ERROR, and also define ERROR the same way that
334// the Windows SDK does for consistency.
335#define ERROR 0
[email protected]d8617a62009-10-09 23:52:20336#define COMPACT_GOOGLE_LOG_EX_0(ClassName, ...) \
337 COMPACT_GOOGLE_LOG_EX_ERROR(ClassName , ##__VA_ARGS__)
338#define COMPACT_GOOGLE_LOG_0 COMPACT_GOOGLE_LOG_ERROR
[email protected]521b0c42010-10-01 23:02:36339// Needed for LOG_IS_ON(ERROR).
340const LogSeverity LOG_0 = LOG_ERROR;
341
[email protected]deba0ff2010-11-03 05:30:14342// As special cases, we can assume that LOG_IS_ON(ERROR_REPORT) and
343// LOG_IS_ON(FATAL) always hold. Also, LOG_IS_ON(DFATAL) always holds
344// in debug mode. In particular, CHECK()s will always fire if they
345// fail.
[email protected]521b0c42010-10-01 23:02:36346#define LOG_IS_ON(severity) \
347 ((::logging::LOG_ ## severity) >= ::logging::GetMinLogLevel())
348
349// We can't do any caching tricks with VLOG_IS_ON() like the
350// google-glog version since it requires GCC extensions. This means
351// that using the v-logging functions in conjunction with --vmodule
352// may be slow.
353#define VLOG_IS_ON(verboselevel) \
354 ((verboselevel) <= ::logging::GetVlogLevel(__FILE__))
355
356// Helper macro which avoids evaluating the arguments to a stream if
357// the condition doesn't hold.
358#define LAZY_STREAM(stream, condition) \
359 !(condition) ? (void) 0 : ::logging::LogMessageVoidify() & (stream)
initial.commitd7cae122008-07-26 21:49:38360
361// We use the preprocessor's merging operator, "##", so that, e.g.,
362// LOG(INFO) becomes the token COMPACT_GOOGLE_LOG_INFO. There's some funny
363// subtle difference between ostream member streaming functions (e.g.,
364// ostream::operator<<(int) and ostream non-member streaming functions
365// (e.g., ::operator<<(ostream&, string&): it turns out that it's
366// impossible to stream something like a string directly to an unnamed
367// ostream. We employ a neat hack by calling the stream() member
368// function of LogMessage which seems to avoid the problem.
[email protected]521b0c42010-10-01 23:02:36369#define LOG_STREAM(severity) COMPACT_GOOGLE_LOG_ ## severity.stream()
initial.commitd7cae122008-07-26 21:49:38370
[email protected]521b0c42010-10-01 23:02:36371#define LOG(severity) LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity))
372#define LOG_IF(severity, condition) \
373 LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity) && (condition))
374
initial.commitd7cae122008-07-26 21:49:38375#define SYSLOG(severity) LOG(severity)
[email protected]521b0c42010-10-01 23:02:36376#define SYSLOG_IF(severity, condition) LOG_IF(severity, condition)
377
[email protected]162ac0f2010-11-04 15:50:49378// The VLOG macros log with negative verbosities.
379#define VLOG_STREAM(verbose_level) \
380 logging::LogMessage(__FILE__, __LINE__, -verbose_level).stream()
381
382#define VLOG(verbose_level) \
383 LAZY_STREAM(VLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level))
384
385#define VLOG_IF(verbose_level, condition) \
386 LAZY_STREAM(VLOG_STREAM(verbose_level), \
387 VLOG_IS_ON(verbose_level) && (condition))
[email protected]99b7c57f2010-09-29 19:26:36388
[email protected]fb879b1a2011-03-06 18:16:31389#if defined (OS_WIN)
390#define VPLOG_STREAM(verbose_level) \
391 logging::Win32ErrorLogMessage(__FILE__, __LINE__, -verbose_level, \
392 ::logging::GetLastSystemErrorCode()).stream()
393#elif defined(OS_POSIX)
394#define VPLOG_STREAM(verbose_level) \
395 logging::ErrnoLogMessage(__FILE__, __LINE__, -verbose_level, \
396 ::logging::GetLastSystemErrorCode()).stream()
397#endif
398
399#define VPLOG(verbose_level) \
400 LAZY_STREAM(VPLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level))
401
402#define VPLOG_IF(verbose_level, condition) \
403 LAZY_STREAM(VPLOG_STREAM(verbose_level), \
404 VLOG_IS_ON(verbose_level) && (condition))
405
[email protected]99b7c57f2010-09-29 19:26:36406// TODO(akalin): Add more VLOG variants, e.g. VPLOG.
initial.commitd7cae122008-07-26 21:49:38407
initial.commitd7cae122008-07-26 21:49:38408#define LOG_ASSERT(condition) \
409 LOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". "
410#define SYSLOG_ASSERT(condition) \
411 SYSLOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". "
412
[email protected]d8617a62009-10-09 23:52:20413#if defined(OS_WIN)
[email protected]521b0c42010-10-01 23:02:36414#define LOG_GETLASTERROR_STREAM(severity) \
[email protected]d8617a62009-10-09 23:52:20415 COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \
416 ::logging::GetLastSystemErrorCode()).stream()
[email protected]521b0c42010-10-01 23:02:36417#define LOG_GETLASTERROR(severity) \
418 LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity), LOG_IS_ON(severity))
419#define LOG_GETLASTERROR_MODULE_STREAM(severity, module) \
[email protected]d8617a62009-10-09 23:52:20420 COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \
421 ::logging::GetLastSystemErrorCode(), module).stream()
[email protected]521b0c42010-10-01 23:02:36422#define LOG_GETLASTERROR_MODULE(severity, module) \
423 LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity, module), \
424 LOG_IS_ON(severity))
425// PLOG_STREAM is used by PLOG, which is the usual error logging macro
426// for each platform.
427#define PLOG_STREAM(severity) LOG_GETLASTERROR_STREAM(severity)
[email protected]d8617a62009-10-09 23:52:20428#elif defined(OS_POSIX)
[email protected]521b0c42010-10-01 23:02:36429#define LOG_ERRNO_STREAM(severity) \
[email protected]d8617a62009-10-09 23:52:20430 COMPACT_GOOGLE_LOG_EX_ ## severity(ErrnoLogMessage, \
431 ::logging::GetLastSystemErrorCode()).stream()
[email protected]521b0c42010-10-01 23:02:36432#define LOG_ERRNO(severity) \
433 LAZY_STREAM(LOG_ERRNO_STREAM(severity), LOG_IS_ON(severity))
434// PLOG_STREAM is used by PLOG, which is the usual error logging macro
435// for each platform.
436#define PLOG_STREAM(severity) LOG_ERRNO_STREAM(severity)
[email protected]d8617a62009-10-09 23:52:20437// TODO(tschmelcher): Should we add OSStatus logging for Mac?
438#endif
439
[email protected]521b0c42010-10-01 23:02:36440#define PLOG(severity) \
441 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity))
442
[email protected]d8617a62009-10-09 23:52:20443#define PLOG_IF(severity, condition) \
[email protected]521b0c42010-10-01 23:02:36444 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity) && (condition))
[email protected]d8617a62009-10-09 23:52:20445
[email protected]ddb9b332011-12-02 07:31:09446// http://crbug.com/16512 is open for a real fix for this. For now, Windows
447// uses OFFICIAL_BUILD and other platforms use the branding flag when NDEBUG is
448// defined.
449#if ( defined(OS_WIN) && defined(OFFICIAL_BUILD)) || \
450 (!defined(OS_WIN) && defined(NDEBUG) && defined(GOOGLE_CHROME_BUILD))
451#define LOGGING_IS_OFFICIAL_BUILD 1
452#else
453#define LOGGING_IS_OFFICIAL_BUILD 0
454#endif
455
456// The actual stream used isn't important.
457#define EAT_STREAM_PARAMETERS \
458 true ? (void) 0 : ::logging::LogMessageVoidify() & LOG_STREAM(FATAL)
459
initial.commitd7cae122008-07-26 21:49:38460// CHECK dies with a fatal error if condition is not true. It is *not*
461// controlled by NDEBUG, so the check will be executed regardless of
462// compilation mode.
[email protected]521b0c42010-10-01 23:02:36463//
464// We make sure CHECK et al. always evaluates their arguments, as
465// doing CHECK(FunctionWithSideEffect()) is a common idiom.
[email protected]ddb9b332011-12-02 07:31:09466
467#if LOGGING_IS_OFFICIAL_BUILD
468
469// Make all CHECK functions discard their log strings to reduce code
470// bloat for official builds.
471
472// TODO(akalin): This would be more valuable if there were some way to
473// remove BreakDebugger() from the backtrace, perhaps by turning it
474// into a macro (like __debugbreak() on Windows).
475#define CHECK(condition) \
476 !(condition) ? ::base::debug::BreakDebugger() : EAT_STREAM_PARAMETERS
477
478#define PCHECK(condition) CHECK(condition)
479
480#define CHECK_OP(name, op, val1, val2) CHECK((val1) op (val2))
481
482#else
483
[email protected]521b0c42010-10-01 23:02:36484#define CHECK(condition) \
485 LAZY_STREAM(LOG_STREAM(FATAL), !(condition)) \
486 << "Check failed: " #condition ". "
initial.commitd7cae122008-07-26 21:49:38487
[email protected]d8617a62009-10-09 23:52:20488#define PCHECK(condition) \
[email protected]521b0c42010-10-01 23:02:36489 LAZY_STREAM(PLOG_STREAM(FATAL), !(condition)) \
490 << "Check failed: " #condition ". "
[email protected]d8617a62009-10-09 23:52:20491
[email protected]ddb9b332011-12-02 07:31:09492// Helper macro for binary operators.
493// Don't use this macro directly in your code, use CHECK_EQ et al below.
494//
495// TODO(akalin): Rewrite this so that constructs like if (...)
496// CHECK_EQ(...) else { ... } work properly.
497#define CHECK_OP(name, op, val1, val2) \
498 if (std::string* _result = \
499 logging::Check##name##Impl((val1), (val2), \
500 #val1 " " #op " " #val2)) \
501 logging::LogMessage(__FILE__, __LINE__, _result).stream()
502
503#endif
504
initial.commitd7cae122008-07-26 21:49:38505// Build the error message string. This is separate from the "Impl"
506// function template because it is not performance critical and so can
[email protected]9c7132e2011-02-08 07:39:08507// be out of line, while the "Impl" code should be inline. Caller
508// takes ownership of the returned string.
initial.commitd7cae122008-07-26 21:49:38509template<class t1, class t2>
510std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) {
511 std::ostringstream ss;
512 ss << names << " (" << v1 << " vs. " << v2 << ")";
513 std::string* msg = new std::string(ss.str());
514 return msg;
515}
516
[email protected]6d445d32010-09-30 19:10:03517// MSVC doesn't like complex extern templates and DLLs.
[email protected]dc72da32011-10-24 20:20:30518#if !defined(COMPILER_MSVC)
[email protected]6d445d32010-09-30 19:10:03519// Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated
520// in logging.cc.
[email protected]dc72da32011-10-24 20:20:30521extern template BASE_EXPORT std::string* MakeCheckOpString<int, int>(
[email protected]6d445d32010-09-30 19:10:03522 const int&, const int&, const char* names);
[email protected]dc72da32011-10-24 20:20:30523extern template BASE_EXPORT
524std::string* MakeCheckOpString<unsigned long, unsigned long>(
[email protected]6d445d32010-09-30 19:10:03525 const unsigned long&, const unsigned long&, const char* names);
[email protected]dc72da32011-10-24 20:20:30526extern template BASE_EXPORT
527std::string* MakeCheckOpString<unsigned long, unsigned int>(
[email protected]6d445d32010-09-30 19:10:03528 const unsigned long&, const unsigned int&, const char* names);
[email protected]dc72da32011-10-24 20:20:30529extern template BASE_EXPORT
530std::string* MakeCheckOpString<unsigned int, unsigned long>(
[email protected]6d445d32010-09-30 19:10:03531 const unsigned int&, const unsigned long&, const char* names);
[email protected]dc72da32011-10-24 20:20:30532extern template BASE_EXPORT
533std::string* MakeCheckOpString<std::string, std::string>(
[email protected]6d445d32010-09-30 19:10:03534 const std::string&, const std::string&, const char* name);
535#endif
initial.commitd7cae122008-07-26 21:49:38536
[email protected]71512602010-11-01 22:19:56537// Helper functions for CHECK_OP macro.
538// The (int, int) specialization works around the issue that the compiler
539// will not instantiate the template version of the function on values of
540// unnamed enum type - see comment below.
541#define DEFINE_CHECK_OP_IMPL(name, op) \
542 template <class t1, class t2> \
543 inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \
544 const char* names) { \
545 if (v1 op v2) return NULL; \
546 else return MakeCheckOpString(v1, v2, names); \
547 } \
548 inline std::string* Check##name##Impl(int v1, int v2, const char* names) { \
549 if (v1 op v2) return NULL; \
550 else return MakeCheckOpString(v1, v2, names); \
551 }
552DEFINE_CHECK_OP_IMPL(EQ, ==)
553DEFINE_CHECK_OP_IMPL(NE, !=)
554DEFINE_CHECK_OP_IMPL(LE, <=)
555DEFINE_CHECK_OP_IMPL(LT, < )
556DEFINE_CHECK_OP_IMPL(GE, >=)
557DEFINE_CHECK_OP_IMPL(GT, > )
558#undef DEFINE_CHECK_OP_IMPL
[email protected]e150c0382010-03-02 00:41:12559
560#define CHECK_EQ(val1, val2) CHECK_OP(EQ, ==, val1, val2)
561#define CHECK_NE(val1, val2) CHECK_OP(NE, !=, val1, val2)
562#define CHECK_LE(val1, val2) CHECK_OP(LE, <=, val1, val2)
563#define CHECK_LT(val1, val2) CHECK_OP(LT, < , val1, val2)
564#define CHECK_GE(val1, val2) CHECK_OP(GE, >=, val1, val2)
565#define CHECK_GT(val1, val2) CHECK_OP(GT, > , val1, val2)
566
[email protected]ddb9b332011-12-02 07:31:09567#if LOGGING_IS_OFFICIAL_BUILD
[email protected]e3cca332009-08-20 01:20:29568// In order to have optimized code for official builds, remove DLOGs and
569// DCHECKs.
[email protected]d15e56c2010-09-30 21:12:33570#define ENABLE_DLOG 0
571#define ENABLE_DCHECK 0
572
573#elif defined(NDEBUG)
574// Otherwise, if we're a release build, remove DLOGs but not DCHECKs
575// (since those can still be turned on via a command-line flag).
576#define ENABLE_DLOG 0
577#define ENABLE_DCHECK 1
578
579#else
580// Otherwise, we're a debug build so enable DLOGs and DCHECKs.
581#define ENABLE_DLOG 1
582#define ENABLE_DCHECK 1
[email protected]e3cca332009-08-20 01:20:29583#endif
584
[email protected]d15e56c2010-09-30 21:12:33585// Definitions for DLOG et al.
586
[email protected]d926c202010-10-01 02:58:24587#if ENABLE_DLOG
588
[email protected]5e987802010-11-01 19:49:22589#define DLOG_IS_ON(severity) LOG_IS_ON(severity)
[email protected]d926c202010-10-01 02:58:24590#define DLOG_IF(severity, condition) LOG_IF(severity, condition)
591#define DLOG_ASSERT(condition) LOG_ASSERT(condition)
[email protected]d926c202010-10-01 02:58:24592#define DPLOG_IF(severity, condition) PLOG_IF(severity, condition)
[email protected]521b0c42010-10-01 23:02:36593#define DVLOG_IF(verboselevel, condition) VLOG_IF(verboselevel, condition)
[email protected]fb879b1a2011-03-06 18:16:31594#define DVPLOG_IF(verboselevel, condition) VPLOG_IF(verboselevel, condition)
[email protected]d926c202010-10-01 02:58:24595
596#else // ENABLE_DLOG
597
[email protected]521b0c42010-10-01 23:02:36598// If ENABLE_DLOG is off, we want to avoid emitting any references to
599// |condition| (which may reference a variable defined only if NDEBUG
600// is not defined). Contrast this with DCHECK et al., which has
601// different behavior.
[email protected]d926c202010-10-01 02:58:24602
[email protected]5e987802010-11-01 19:49:22603#define DLOG_IS_ON(severity) false
[email protected]ddb9b332011-12-02 07:31:09604#define DLOG_IF(severity, condition) EAT_STREAM_PARAMETERS
605#define DLOG_ASSERT(condition) EAT_STREAM_PARAMETERS
606#define DPLOG_IF(severity, condition) EAT_STREAM_PARAMETERS
607#define DVLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS
608#define DVPLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS
[email protected]d926c202010-10-01 02:58:24609
610#endif // ENABLE_DLOG
611
[email protected]d15e56c2010-09-30 21:12:33612// DEBUG_MODE is for uses like
613// if (DEBUG_MODE) foo.CheckThatFoo();
614// instead of
615// #ifndef NDEBUG
616// foo.CheckThatFoo();
617// #endif
618//
619// We tie its state to ENABLE_DLOG.
620enum { DEBUG_MODE = ENABLE_DLOG };
621
622#undef ENABLE_DLOG
623
[email protected]521b0c42010-10-01 23:02:36624#define DLOG(severity) \
625 LAZY_STREAM(LOG_STREAM(severity), DLOG_IS_ON(severity))
626
627#if defined(OS_WIN)
628#define DLOG_GETLASTERROR(severity) \
629 LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity), DLOG_IS_ON(severity))
630#define DLOG_GETLASTERROR_MODULE(severity, module) \
631 LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity, module), \
632 DLOG_IS_ON(severity))
633#elif defined(OS_POSIX)
634#define DLOG_ERRNO(severity) \
635 LAZY_STREAM(LOG_ERRNO_STREAM(severity), DLOG_IS_ON(severity))
636#endif
637
638#define DPLOG(severity) \
639 LAZY_STREAM(PLOG_STREAM(severity), DLOG_IS_ON(severity))
640
[email protected]c3ab11c2011-10-25 06:28:45641#define DVLOG(verboselevel) DVLOG_IF(verboselevel, VLOG_IS_ON(verboselevel))
[email protected]521b0c42010-10-01 23:02:36642
[email protected]fb879b1a2011-03-06 18:16:31643#define DVPLOG(verboselevel) DVPLOG_IF(verboselevel, VLOG_IS_ON(verboselevel))
644
[email protected]521b0c42010-10-01 23:02:36645// Definitions for DCHECK et al.
[email protected]d926c202010-10-01 02:58:24646
[email protected]d15e56c2010-09-30 21:12:33647#if ENABLE_DCHECK
[email protected]e3cca332009-08-20 01:20:29648
[email protected]521b0c42010-10-01 23:02:36649#if defined(NDEBUG)
[email protected]d926c202010-10-01 02:58:24650
[email protected]20960e072011-09-20 20:59:01651BASE_EXPORT extern DcheckState g_dcheck_state;
652
653#if defined(DCHECK_ALWAYS_ON)
654
655#define DCHECK_IS_ON() true
656#define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \
657 COMPACT_GOOGLE_LOG_EX_FATAL(ClassName , ##__VA_ARGS__)
658#define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_FATAL
659const LogSeverity LOG_DCHECK = LOG_FATAL;
660
661#else
662
[email protected]deba0ff2010-11-03 05:30:14663#define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \
664 COMPACT_GOOGLE_LOG_EX_ERROR_REPORT(ClassName , ##__VA_ARGS__)
665#define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_ERROR_REPORT
[email protected]521b0c42010-10-01 23:02:36666const LogSeverity LOG_DCHECK = LOG_ERROR_REPORT;
[email protected]7c10f7552011-01-11 01:03:36667#define DCHECK_IS_ON() \
668 ((::logging::g_dcheck_state == \
669 ::logging::ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS) && \
670 LOG_IS_ON(DCHECK))
[email protected]d926c202010-10-01 02:58:24671
[email protected]20960e072011-09-20 20:59:01672#endif // defined(DCHECK_ALWAYS_ON)
673
[email protected]521b0c42010-10-01 23:02:36674#else // defined(NDEBUG)
675
[email protected]5e987802010-11-01 19:49:22676// On a regular debug build, we want to have DCHECKs enabled.
[email protected]deba0ff2010-11-03 05:30:14677#define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \
678 COMPACT_GOOGLE_LOG_EX_FATAL(ClassName , ##__VA_ARGS__)
679#define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_FATAL
[email protected]521b0c42010-10-01 23:02:36680const LogSeverity LOG_DCHECK = LOG_FATAL;
[email protected]deba0ff2010-11-03 05:30:14681#define DCHECK_IS_ON() true
[email protected]521b0c42010-10-01 23:02:36682
683#endif // defined(NDEBUG)
684
685#else // ENABLE_DCHECK
686
[email protected]deba0ff2010-11-03 05:30:14687// These are just dummy values since DCHECK_IS_ON() is always false in
688// this case.
689#define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \
690 COMPACT_GOOGLE_LOG_EX_INFO(ClassName , ##__VA_ARGS__)
691#define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_INFO
692const LogSeverity LOG_DCHECK = LOG_INFO;
[email protected]5e987802010-11-01 19:49:22693#define DCHECK_IS_ON() false
[email protected]521b0c42010-10-01 23:02:36694
695#endif // ENABLE_DCHECK
[email protected]5e987802010-11-01 19:49:22696#undef ENABLE_DCHECK
[email protected]521b0c42010-10-01 23:02:36697
[email protected]deba0ff2010-11-03 05:30:14698// DCHECK et al. make sure to reference |condition| regardless of
[email protected]521b0c42010-10-01 23:02:36699// whether DCHECKs are enabled; this is so that we don't get unused
700// variable warnings if the only use of a variable is in a DCHECK.
701// This behavior is different from DLOG_IF et al.
702
[email protected]deba0ff2010-11-03 05:30:14703#define DCHECK(condition) \
704 LAZY_STREAM(LOG_STREAM(DCHECK), DCHECK_IS_ON() && !(condition)) \
[email protected]521b0c42010-10-01 23:02:36705 << "Check failed: " #condition ". "
706
[email protected]deba0ff2010-11-03 05:30:14707#define DPCHECK(condition) \
708 LAZY_STREAM(PLOG_STREAM(DCHECK), DCHECK_IS_ON() && !(condition)) \
[email protected]521b0c42010-10-01 23:02:36709 << "Check failed: " #condition ". "
[email protected]d926c202010-10-01 02:58:24710
711// Helper macro for binary operators.
712// Don't use this macro directly in your code, use DCHECK_EQ et al below.
[email protected]521b0c42010-10-01 23:02:36713#define DCHECK_OP(name, op, val1, val2) \
[email protected]5e987802010-11-01 19:49:22714 if (DCHECK_IS_ON()) \
[email protected]9c7132e2011-02-08 07:39:08715 if (std::string* _result = \
[email protected]521b0c42010-10-01 23:02:36716 logging::Check##name##Impl((val1), (val2), \
717 #val1 " " #op " " #val2)) \
718 logging::LogMessage( \
719 __FILE__, __LINE__, ::logging::LOG_DCHECK, \
720 _result).stream()
initial.commitd7cae122008-07-26 21:49:38721
[email protected]deba0ff2010-11-03 05:30:14722// Equality/Inequality checks - compare two values, and log a
723// LOG_DCHECK message including the two values when the result is not
724// as expected. The values must have operator<<(ostream, ...)
725// defined.
initial.commitd7cae122008-07-26 21:49:38726//
727// You may append to the error message like so:
728// DCHECK_NE(1, 2) << ": The world must be ending!";
729//
730// We are very careful to ensure that each argument is evaluated exactly
731// once, and that anything which is legal to pass as a function argument is
732// legal here. In particular, the arguments may be temporary expressions
733// which will end up being destroyed at the end of the apparent statement,
734// for example:
735// DCHECK_EQ(string("abc")[1], 'b');
736//
737// WARNING: These may not compile correctly if one of the arguments is a pointer
738// and the other is NULL. To work around this, simply static_cast NULL to the
739// type of the desired pointer.
740
741#define DCHECK_EQ(val1, val2) DCHECK_OP(EQ, ==, val1, val2)
742#define DCHECK_NE(val1, val2) DCHECK_OP(NE, !=, val1, val2)
743#define DCHECK_LE(val1, val2) DCHECK_OP(LE, <=, val1, val2)
744#define DCHECK_LT(val1, val2) DCHECK_OP(LT, < , val1, val2)
745#define DCHECK_GE(val1, val2) DCHECK_OP(GE, >=, val1, val2)
746#define DCHECK_GT(val1, val2) DCHECK_OP(GT, > , val1, val2)
747
initial.commitd7cae122008-07-26 21:49:38748#define NOTREACHED() DCHECK(false)
749
750// Redefine the standard assert to use our nice log files
751#undef assert
752#define assert(x) DLOG_ASSERT(x)
753
754// This class more or less represents a particular log message. You
755// create an instance of LogMessage and then stream stuff to it.
756// When you finish streaming to it, ~LogMessage is called and the
757// full message gets streamed to the appropriate destination.
758//
759// You shouldn't actually use LogMessage's constructor to log things,
760// though. You should use the LOG() macro (and variants thereof)
761// above.
[email protected]0bea7252011-08-05 15:34:00762class BASE_EXPORT LogMessage {
initial.commitd7cae122008-07-26 21:49:38763 public:
764 LogMessage(const char* file, int line, LogSeverity severity, int ctr);
765
766 // Two special constructors that generate reduced amounts of code at
767 // LOG call sites for common cases.
768 //
769 // Used for LOG(INFO): Implied are:
770 // severity = LOG_INFO, ctr = 0
771 //
772 // Using this constructor instead of the more complex constructor above
773 // saves a couple of bytes per call site.
774 LogMessage(const char* file, int line);
775
776 // Used for LOG(severity) where severity != INFO. Implied
777 // are: ctr = 0
778 //
779 // Using this constructor instead of the more complex constructor above
780 // saves a couple of bytes per call site.
781 LogMessage(const char* file, int line, LogSeverity severity);
782
[email protected]9c7132e2011-02-08 07:39:08783 // A special constructor used for check failures. Takes ownership
784 // of the given string.
initial.commitd7cae122008-07-26 21:49:38785 // Implied severity = LOG_FATAL
[email protected]9c7132e2011-02-08 07:39:08786 LogMessage(const char* file, int line, std::string* result);
initial.commitd7cae122008-07-26 21:49:38787
[email protected]fb62a532009-02-12 01:19:05788 // A special constructor used for check failures, with the option to
[email protected]9c7132e2011-02-08 07:39:08789 // specify severity. Takes ownership of the given string.
[email protected]fb62a532009-02-12 01:19:05790 LogMessage(const char* file, int line, LogSeverity severity,
[email protected]9c7132e2011-02-08 07:39:08791 std::string* result);
[email protected]fb62a532009-02-12 01:19:05792
initial.commitd7cae122008-07-26 21:49:38793 ~LogMessage();
794
795 std::ostream& stream() { return stream_; }
796
797 private:
798 void Init(const char* file, int line);
799
800 LogSeverity severity_;
801 std::ostringstream stream_;
[email protected]c88873922008-07-30 13:02:03802 size_t message_start_; // Offset of the start of the message (past prefix
803 // info).
[email protected]162ac0f2010-11-04 15:50:49804 // The file and line information passed in to the constructor.
805 const char* file_;
806 const int line_;
807
[email protected]3f85caa2009-04-14 16:52:11808#if defined(OS_WIN)
809 // Stores the current value of GetLastError in the constructor and restores
810 // it in the destructor by calling SetLastError.
811 // This is useful since the LogMessage class uses a lot of Win32 calls
812 // that will lose the value of GLE and the code that called the log function
813 // will have lost the thread error value when the log call returns.
814 class SaveLastError {
815 public:
816 SaveLastError();
817 ~SaveLastError();
818
819 unsigned long get_error() const { return last_error_; }
820
821 protected:
822 unsigned long last_error_;
823 };
824
825 SaveLastError last_error_;
826#endif
initial.commitd7cae122008-07-26 21:49:38827
[email protected]39be4242008-08-07 18:31:40828 DISALLOW_COPY_AND_ASSIGN(LogMessage);
initial.commitd7cae122008-07-26 21:49:38829};
830
831// A non-macro interface to the log facility; (useful
832// when the logging level is not a compile-time constant).
833inline void LogAtLevel(int const log_level, std::string const &msg) {
834 LogMessage(__FILE__, __LINE__, log_level).stream() << msg;
835}
836
837// This class is used to explicitly ignore values in the conditional
838// logging macros. This avoids compiler warnings like "value computed
839// is not used" and "statement has no effect".
[email protected]23bb71f2011-04-21 22:22:10840class LogMessageVoidify {
initial.commitd7cae122008-07-26 21:49:38841 public:
842 LogMessageVoidify() { }
843 // This has to be an operator with a precedence lower than << but
844 // higher than ?:
845 void operator&(std::ostream&) { }
846};
847
[email protected]d8617a62009-10-09 23:52:20848#if defined(OS_WIN)
849typedef unsigned long SystemErrorCode;
850#elif defined(OS_POSIX)
851typedef int SystemErrorCode;
852#endif
853
854// Alias for ::GetLastError() on Windows and errno on POSIX. Avoids having to
855// pull in windows.h just for GetLastError() and DWORD.
[email protected]0bea7252011-08-05 15:34:00856BASE_EXPORT SystemErrorCode GetLastSystemErrorCode();
[email protected]d8617a62009-10-09 23:52:20857
858#if defined(OS_WIN)
859// Appends a formatted system message of the GetLastError() type.
[email protected]0bea7252011-08-05 15:34:00860class BASE_EXPORT Win32ErrorLogMessage {
[email protected]d8617a62009-10-09 23:52:20861 public:
862 Win32ErrorLogMessage(const char* file,
863 int line,
864 LogSeverity severity,
865 SystemErrorCode err,
866 const char* module);
867
868 Win32ErrorLogMessage(const char* file,
869 int line,
870 LogSeverity severity,
871 SystemErrorCode err);
872
[email protected]d8617a62009-10-09 23:52:20873 // Appends the error message before destructing the encapsulated class.
874 ~Win32ErrorLogMessage();
875
[email protected]a502bbe72011-01-07 18:06:45876 std::ostream& stream() { return log_message_.stream(); }
877
[email protected]d8617a62009-10-09 23:52:20878 private:
879 SystemErrorCode err_;
880 // Optional name of the module defining the error.
881 const char* module_;
882 LogMessage log_message_;
883
884 DISALLOW_COPY_AND_ASSIGN(Win32ErrorLogMessage);
885};
886#elif defined(OS_POSIX)
887// Appends a formatted system message of the errno type
[email protected]0bea7252011-08-05 15:34:00888class BASE_EXPORT ErrnoLogMessage {
[email protected]d8617a62009-10-09 23:52:20889 public:
890 ErrnoLogMessage(const char* file,
891 int line,
892 LogSeverity severity,
893 SystemErrorCode err);
894
[email protected]d8617a62009-10-09 23:52:20895 // Appends the error message before destructing the encapsulated class.
896 ~ErrnoLogMessage();
897
[email protected]a502bbe72011-01-07 18:06:45898 std::ostream& stream() { return log_message_.stream(); }
899
[email protected]d8617a62009-10-09 23:52:20900 private:
901 SystemErrorCode err_;
902 LogMessage log_message_;
903
904 DISALLOW_COPY_AND_ASSIGN(ErrnoLogMessage);
905};
906#endif // OS_WIN
907
initial.commitd7cae122008-07-26 21:49:38908// Closes the log file explicitly if open.
909// NOTE: Since the log file is opened as necessary by the action of logging
910// statements, there's no guarantee that it will stay closed
911// after this call.
[email protected]0bea7252011-08-05 15:34:00912BASE_EXPORT void CloseLogFile();
initial.commitd7cae122008-07-26 21:49:38913
[email protected]e36ddc82009-12-08 04:22:50914// Async signal safe logging mechanism.
[email protected]0bea7252011-08-05 15:34:00915BASE_EXPORT void RawLog(int level, const char* message);
[email protected]e36ddc82009-12-08 04:22:50916
917#define RAW_LOG(level, message) logging::RawLog(logging::LOG_ ## level, message)
918
919#define RAW_CHECK(condition) \
920 do { \
921 if (!(condition)) \
922 logging::RawLog(logging::LOG_FATAL, "Check failed: " #condition "\n"); \
923 } while (0)
924
[email protected]39be4242008-08-07 18:31:40925} // namespace logging
initial.commitd7cae122008-07-26 21:49:38926
[email protected]46ce5b562010-06-16 18:39:53927// These functions are provided as a convenience for logging, which is where we
928// use streams (it is against Google style to use streams in other places). It
929// is designed to allow you to emit non-ASCII Unicode strings to the log file,
930// which is normally ASCII. It is relatively slow, so try not to use it for
931// common cases. Non-ASCII characters will be converted to UTF-8 by these
932// operators.
[email protected]0bea7252011-08-05 15:34:00933BASE_EXPORT std::ostream& operator<<(std::ostream& out, const wchar_t* wstr);
[email protected]46ce5b562010-06-16 18:39:53934inline std::ostream& operator<<(std::ostream& out, const std::wstring& wstr) {
935 return out << wstr.c_str();
936}
937
[email protected]0dfc81b2008-08-25 03:44:40938// The NOTIMPLEMENTED() macro annotates codepaths which have
939// not been implemented yet.
940//
941// The implementation of this macro is controlled by NOTIMPLEMENTED_POLICY:
942// 0 -- Do nothing (stripped by compiler)
943// 1 -- Warn at compile time
944// 2 -- Fail at compile time
945// 3 -- Fail at runtime (DCHECK)
946// 4 -- [default] LOG(ERROR) at runtime
947// 5 -- LOG(ERROR) at runtime, only once per call-site
948
949#ifndef NOTIMPLEMENTED_POLICY
950// Select default policy: LOG(ERROR)
951#define NOTIMPLEMENTED_POLICY 4
952#endif
953
[email protected]f6cda752008-10-30 23:54:26954#if defined(COMPILER_GCC)
955// On Linux, with GCC, we can use __PRETTY_FUNCTION__ to get the demangled name
956// of the current function in the NOTIMPLEMENTED message.
957#define NOTIMPLEMENTED_MSG "Not implemented reached in " << __PRETTY_FUNCTION__
958#else
959#define NOTIMPLEMENTED_MSG "NOT IMPLEMENTED"
960#endif
961
[email protected]0dfc81b2008-08-25 03:44:40962#if NOTIMPLEMENTED_POLICY == 0
963#define NOTIMPLEMENTED() ;
964#elif NOTIMPLEMENTED_POLICY == 1
965// TODO, figure out how to generate a warning
966#define NOTIMPLEMENTED() COMPILE_ASSERT(false, NOT_IMPLEMENTED)
967#elif NOTIMPLEMENTED_POLICY == 2
968#define NOTIMPLEMENTED() COMPILE_ASSERT(false, NOT_IMPLEMENTED)
969#elif NOTIMPLEMENTED_POLICY == 3
970#define NOTIMPLEMENTED() NOTREACHED()
971#elif NOTIMPLEMENTED_POLICY == 4
[email protected]f6cda752008-10-30 23:54:26972#define NOTIMPLEMENTED() LOG(ERROR) << NOTIMPLEMENTED_MSG
[email protected]0dfc81b2008-08-25 03:44:40973#elif NOTIMPLEMENTED_POLICY == 5
974#define NOTIMPLEMENTED() do {\
975 static int count = 0;\
[email protected]f6cda752008-10-30 23:54:26976 LOG_IF(ERROR, 0 == count++) << NOTIMPLEMENTED_MSG;\
[email protected]0dfc81b2008-08-25 03:44:40977} while(0)
978#endif
979
[email protected]39a749c2011-01-28 02:40:46980namespace base {
981
982class StringPiece;
983
[email protected]1f88b5162011-04-01 00:02:29984// Allows StringPiece to be logged.
[email protected]0bea7252011-08-05 15:34:00985BASE_EXPORT std::ostream& operator<<(std::ostream& o, const StringPiece& piece);
[email protected]39a749c2011-01-28 02:40:46986
987} // namespace base
988
[email protected]39be4242008-08-07 18:31:40989#endif // BASE_LOGGING_H_