blob: 68a1c741d399c3d8d995d862feaae93323b0a068 [file] [log] [blame]
[email protected]b0d38d4c2010-10-29 00:39:481// Copyright (c) 2010 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
9#include <string>
10#include <cstring>
11#include <sstream>
12
13#include "base/basictypes.h"
initial.commitd7cae122008-07-26 21:49:3814
15//
16// Optional message capabilities
17// -----------------------------
18// Assertion failed messages and fatal errors are displayed in a dialog box
19// before the application exits. However, running this UI creates a message
20// loop, which causes application messages to be processed and potentially
21// dispatched to existing application windows. Since the application is in a
22// bad state when this assertion dialog is displayed, these messages may not
23// get processed and hang the dialog, or the application might go crazy.
24//
25// Therefore, it can be beneficial to display the error dialog in a separate
26// process from the main application. When the logging system needs to display
27// a fatal error dialog box, it will look for a program called
28// "DebugMessage.exe" in the same directory as the application executable. It
29// will run this application with the message as the command line, and will
30// not include the name of the application as is traditional for easier
31// parsing.
32//
33// The code for DebugMessage.exe is only one line. In WinMain, do:
34// MessageBox(NULL, GetCommandLineW(), L"Fatal Error", 0);
35//
36// If DebugMessage.exe is not found, the logging code will use a normal
37// MessageBox, potentially causing the problems discussed above.
38
39
40// Instructions
41// ------------
42//
43// Make a bunch of macros for logging. The way to log things is to stream
44// things to LOG(<a particular severity level>). E.g.,
45//
46// LOG(INFO) << "Found " << num_cookies << " cookies";
47//
48// You can also do conditional logging:
49//
50// LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
51//
52// The above will cause log messages to be output on the 1st, 11th, 21st, ...
53// times it is executed. Note that the special COUNTER value is used to
54// identify which repetition is happening.
55//
56// The CHECK(condition) macro is active in both debug and release builds and
57// effectively performs a LOG(FATAL) which terminates the process and
58// generates a crashdump unless a debugger is attached.
59//
60// There are also "debug mode" logging macros like the ones above:
61//
62// DLOG(INFO) << "Found cookies";
63//
64// DLOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
65//
66// All "debug mode" logging is compiled away to nothing for non-debug mode
67// compiles. LOG_IF and development flags also work well together
68// because the code can be compiled away sometimes.
69//
70// We also have
71//
72// LOG_ASSERT(assertion);
73// DLOG_ASSERT(assertion);
74//
75// which is syntactic sugar for {,D}LOG_IF(FATAL, assert fails) << assertion;
76//
[email protected]99b7c57f2010-09-29 19:26:3677// There are "verbose level" logging macros. They look like
78//
79// VLOG(1) << "I'm printed when you run the program with --v=1 or more";
80// VLOG(2) << "I'm printed when you run the program with --v=2 or more";
81//
82// These always log at the INFO log level (when they log at all).
83// The verbose logging can also be turned on module-by-module. For instance,
[email protected]b0d38d4c2010-10-29 00:39:4884// --vmodule=profile=2,icon_loader=1,browser_*=3,*/chromeos/*=4 --v=0
[email protected]99b7c57f2010-09-29 19:26:3685// will cause:
86// a. VLOG(2) and lower messages to be printed from profile.{h,cc}
87// b. VLOG(1) and lower messages to be printed from icon_loader.{h,cc}
88// c. VLOG(3) and lower messages to be printed from files prefixed with
89// "browser"
[email protected]e11de722010-11-01 20:50:5590// d. VLOG(4) and lower messages to be printed from files under a
[email protected]b0d38d4c2010-10-29 00:39:4891// "chromeos" directory.
[email protected]e11de722010-11-01 20:50:5592// e. VLOG(0) and lower messages to be printed from elsewhere
[email protected]99b7c57f2010-09-29 19:26:3693//
94// The wildcarding functionality shown by (c) supports both '*' (match
[email protected]b0d38d4c2010-10-29 00:39:4895// 0 or more characters) and '?' (match any single character)
96// wildcards. Any pattern containing a forward or backward slash will
97// be tested against the whole pathname and not just the module.
98// E.g., "*/foo/bar/*=2" would change the logging level for all code
99// in source files under a "foo/bar" directory.
[email protected]99b7c57f2010-09-29 19:26:36100//
101// There's also VLOG_IS_ON(n) "verbose level" condition macro. To be used as
102//
103// if (VLOG_IS_ON(2)) {
104// // do some logging preparation and logging
105// // that can't be accomplished with just VLOG(2) << ...;
106// }
107//
108// There is also a VLOG_IF "verbose level" condition macro for sample
109// cases, when some extra computation and preparation for logs is not
110// needed.
111//
112// VLOG_IF(1, (size > 1024))
113// << "I'm printed when size is more than 1024 and when you run the "
114// "program with --v=1 or more";
115//
initial.commitd7cae122008-07-26 21:49:38116// We also override the standard 'assert' to use 'DLOG_ASSERT'.
117//
[email protected]d8617a62009-10-09 23:52:20118// Lastly, there is:
119//
120// PLOG(ERROR) << "Couldn't do foo";
121// DPLOG(ERROR) << "Couldn't do foo";
122// PLOG_IF(ERROR, cond) << "Couldn't do foo";
123// DPLOG_IF(ERROR, cond) << "Couldn't do foo";
124// PCHECK(condition) << "Couldn't do foo";
125// DPCHECK(condition) << "Couldn't do foo";
126//
127// which append the last system error to the message in string form (taken from
128// GetLastError() on Windows and errno on POSIX).
129//
initial.commitd7cae122008-07-26 21:49:38130// The supported severity levels for macros that allow you to specify one
[email protected]fb62a532009-02-12 01:19:05131// are (in increasing order of severity) INFO, WARNING, ERROR, ERROR_REPORT,
132// and FATAL.
initial.commitd7cae122008-07-26 21:49:38133//
134// Very important: logging a message at the FATAL severity level causes
135// the program to terminate (after the message is logged).
[email protected]fb62a532009-02-12 01:19:05136//
137// Note the special severity of ERROR_REPORT only available/relevant in normal
138// mode, which displays error dialog without terminating the program. There is
139// no error dialog for severity ERROR or below in normal mode.
140//
141// There is also the special severity of DFATAL, which logs FATAL in
[email protected]081bd4c2010-06-24 01:01:04142// debug mode, ERROR in normal mode.
initial.commitd7cae122008-07-26 21:49:38143
144namespace logging {
145
146// Where to record logging output? A flat file and/or system debug log via
[email protected]88aa41e82008-11-18 00:59:04147// OutputDebugString. Defaults on Windows to LOG_ONLY_TO_FILE, and on
148// POSIX to LOG_ONLY_TO_SYSTEM_DEBUG_LOG (aka stderr).
initial.commitd7cae122008-07-26 21:49:38149enum LoggingDestination { LOG_NONE,
150 LOG_ONLY_TO_FILE,
151 LOG_ONLY_TO_SYSTEM_DEBUG_LOG,
152 LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG };
153
154// Indicates that the log file should be locked when being written to.
155// Often, there is no locking, which is fine for a single threaded program.
156// If logging is being done from multiple threads or there can be more than
157// one process doing the logging, the file should be locked during writes to
158// make each log outut atomic. Other writers will block.
159//
160// All processes writing to the log file must have their locking set for it to
161// work properly. Defaults to DONT_LOCK_LOG_FILE.
162enum LogLockingState { LOCK_LOG_FILE, DONT_LOCK_LOG_FILE };
163
164// On startup, should we delete or append to an existing log file (if any)?
165// Defaults to APPEND_TO_OLD_LOG_FILE.
166enum OldFileDeletionState { DELETE_OLD_LOG_FILE, APPEND_TO_OLD_LOG_FILE };
167
[email protected]ff3d0c32010-08-23 19:57:46168// TODO(avi): do we want to do a unification of character types here?
169#if defined(OS_WIN)
170typedef wchar_t PathChar;
171#else
172typedef char PathChar;
173#endif
174
175// Define different names for the BaseInitLoggingImpl() function depending on
176// whether NDEBUG is defined or not so that we'll fail to link if someone tries
177// to compile logging.cc with NDEBUG but includes logging.h without defining it,
178// or vice versa.
179#if NDEBUG
180#define BaseInitLoggingImpl BaseInitLoggingImpl_built_with_NDEBUG
181#else
182#define BaseInitLoggingImpl BaseInitLoggingImpl_built_without_NDEBUG
183#endif
184
185// Implementation of the InitLogging() method declared below. We use a
186// more-specific name so we can #define it above without affecting other code
187// that has named stuff "InitLogging".
[email protected]c7d5da992010-10-28 00:20:21188bool BaseInitLoggingImpl(const PathChar* log_file,
[email protected]ff3d0c32010-08-23 19:57:46189 LoggingDestination logging_dest,
190 LogLockingState lock_log,
191 OldFileDeletionState delete_old);
192
initial.commitd7cae122008-07-26 21:49:38193// Sets the log file name and other global logging state. Calling this function
194// is recommended, and is normally done at the beginning of application init.
195// If you don't call it, all the flags will be initialized to their default
196// values, and there is a race condition that may leak a critical section
197// object if two threads try to do the first log at the same time.
198// See the definition of the enums above for descriptions and default values.
199//
200// The default log file is initialized to "debug.log" in the application
201// directory. You probably don't want this, especially since the program
202// directory may not be writable on an enduser's system.
[email protected]c7d5da992010-10-28 00:20:21203inline bool InitLogging(const PathChar* log_file,
[email protected]ff3d0c32010-08-23 19:57:46204 LoggingDestination logging_dest,
205 LogLockingState lock_log,
206 OldFileDeletionState delete_old) {
[email protected]c7d5da992010-10-28 00:20:21207 return BaseInitLoggingImpl(log_file, logging_dest, lock_log, delete_old);
[email protected]ff3d0c32010-08-23 19:57:46208}
initial.commitd7cae122008-07-26 21:49:38209
210// Sets the log level. Anything at or above this level will be written to the
211// log file/displayed to the user (if applicable). Anything below this level
212// will be silently ignored. The log level defaults to 0 (everything is logged)
213// if this function is not called.
214void SetMinLogLevel(int level);
215
[email protected]8a2986ca2009-04-10 19:13:42216// Gets the current log level.
initial.commitd7cae122008-07-26 21:49:38217int GetMinLogLevel();
218
[email protected]99b7c57f2010-09-29 19:26:36219// Gets the current vlog level for the given file (usually taken from
220// __FILE__).
[email protected]2f4e9a62010-09-29 21:25:14221
222// Note that |N| is the size *with* the null terminator.
223int GetVlogLevelHelper(const char* file_start, size_t N);
224
[email protected]99b7c57f2010-09-29 19:26:36225template <size_t N>
226int GetVlogLevel(const char (&file)[N]) {
227 return GetVlogLevelHelper(file, N);
228}
initial.commitd7cae122008-07-26 21:49:38229
230// Sets the common items you want to be prepended to each log message.
231// process and thread IDs default to off, the timestamp defaults to on.
232// If this function is not called, logging defaults to writing the timestamp
233// only.
234void SetLogItems(bool enable_process_id, bool enable_thread_id,
235 bool enable_timestamp, bool enable_tickcount);
236
[email protected]81e0a852010-08-17 00:38:12237// Sets whether or not you'd like to see fatal debug messages popped up in
238// a dialog box or not.
239// Dialogs are not shown by default.
240void SetShowErrorDialogs(bool enable_dialogs);
241
initial.commitd7cae122008-07-26 21:49:38242// Sets the Log Assert Handler that will be used to notify of check failures.
[email protected]fb62a532009-02-12 01:19:05243// The default handler shows a dialog box and then terminate the process,
244// however clients can use this function to override with their own handling
245// (e.g. a silent one for Unit Tests)
initial.commitd7cae122008-07-26 21:49:38246typedef void (*LogAssertHandlerFunction)(const std::string& str);
247void SetLogAssertHandler(LogAssertHandlerFunction handler);
[email protected]fb62a532009-02-12 01:19:05248// Sets the Log Report Handler that will be used to notify of check failures
249// in non-debug mode. The default handler shows a dialog box and continues
250// the execution, however clients can use this function to override with their
251// own handling.
252typedef void (*LogReportHandlerFunction)(const std::string& str);
253void SetLogReportHandler(LogReportHandlerFunction handler);
initial.commitd7cae122008-07-26 21:49:38254
[email protected]2b07b8412009-11-25 15:26:34255// Sets the Log Message Handler that gets passed every log message before
256// it's sent to other log destinations (if any).
257// Returns true to signal that it handled the message and the message
258// should not be sent to other log destinations.
259typedef bool (*LogMessageHandlerFunction)(int severity, const std::string& str);
260void SetLogMessageHandler(LogMessageHandlerFunction handler);
261
initial.commitd7cae122008-07-26 21:49:38262typedef int LogSeverity;
263const LogSeverity LOG_INFO = 0;
264const LogSeverity LOG_WARNING = 1;
265const LogSeverity LOG_ERROR = 2;
[email protected]fb62a532009-02-12 01:19:05266const LogSeverity LOG_ERROR_REPORT = 3;
267const LogSeverity LOG_FATAL = 4;
268const LogSeverity LOG_NUM_SEVERITIES = 5;
initial.commitd7cae122008-07-26 21:49:38269
[email protected]521b0c42010-10-01 23:02:36270// LOG_DFATAL is LOG_FATAL in debug mode, ERROR in normal mode
initial.commitd7cae122008-07-26 21:49:38271#ifdef NDEBUG
[email protected]521b0c42010-10-01 23:02:36272const LogSeverity LOG_DFATAL = LOG_ERROR;
initial.commitd7cae122008-07-26 21:49:38273#else
[email protected]521b0c42010-10-01 23:02:36274const LogSeverity LOG_DFATAL = LOG_FATAL;
initial.commitd7cae122008-07-26 21:49:38275#endif
276
277// A few definitions of macros that don't generate much code. These are used
278// by LOG() and LOG_IF, etc. Since these are used all over our code, it's
279// better to have compact code for these operations.
[email protected]d8617a62009-10-09 23:52:20280#define COMPACT_GOOGLE_LOG_EX_INFO(ClassName, ...) \
281 logging::ClassName(__FILE__, __LINE__, logging::LOG_INFO , ##__VA_ARGS__)
282#define COMPACT_GOOGLE_LOG_EX_WARNING(ClassName, ...) \
283 logging::ClassName(__FILE__, __LINE__, logging::LOG_WARNING , ##__VA_ARGS__)
284#define COMPACT_GOOGLE_LOG_EX_ERROR(ClassName, ...) \
285 logging::ClassName(__FILE__, __LINE__, logging::LOG_ERROR , ##__VA_ARGS__)
286#define COMPACT_GOOGLE_LOG_EX_ERROR_REPORT(ClassName, ...) \
287 logging::ClassName(__FILE__, __LINE__, \
288 logging::LOG_ERROR_REPORT , ##__VA_ARGS__)
289#define COMPACT_GOOGLE_LOG_EX_FATAL(ClassName, ...) \
290 logging::ClassName(__FILE__, __LINE__, logging::LOG_FATAL , ##__VA_ARGS__)
291#define COMPACT_GOOGLE_LOG_EX_DFATAL(ClassName, ...) \
[email protected]521b0c42010-10-01 23:02:36292 logging::ClassName(__FILE__, __LINE__, logging::LOG_DFATAL , ##__VA_ARGS__)
[email protected]d8617a62009-10-09 23:52:20293
initial.commitd7cae122008-07-26 21:49:38294#define COMPACT_GOOGLE_LOG_INFO \
[email protected]d8617a62009-10-09 23:52:20295 COMPACT_GOOGLE_LOG_EX_INFO(LogMessage)
initial.commitd7cae122008-07-26 21:49:38296#define COMPACT_GOOGLE_LOG_WARNING \
[email protected]d8617a62009-10-09 23:52:20297 COMPACT_GOOGLE_LOG_EX_WARNING(LogMessage)
initial.commitd7cae122008-07-26 21:49:38298#define COMPACT_GOOGLE_LOG_ERROR \
[email protected]d8617a62009-10-09 23:52:20299 COMPACT_GOOGLE_LOG_EX_ERROR(LogMessage)
[email protected]fb62a532009-02-12 01:19:05300#define COMPACT_GOOGLE_LOG_ERROR_REPORT \
[email protected]d8617a62009-10-09 23:52:20301 COMPACT_GOOGLE_LOG_EX_ERROR_REPORT(LogMessage)
initial.commitd7cae122008-07-26 21:49:38302#define COMPACT_GOOGLE_LOG_FATAL \
[email protected]d8617a62009-10-09 23:52:20303 COMPACT_GOOGLE_LOG_EX_FATAL(LogMessage)
initial.commitd7cae122008-07-26 21:49:38304#define COMPACT_GOOGLE_LOG_DFATAL \
[email protected]d8617a62009-10-09 23:52:20305 COMPACT_GOOGLE_LOG_EX_DFATAL(LogMessage)
initial.commitd7cae122008-07-26 21:49:38306
307// wingdi.h defines ERROR to be 0. When we call LOG(ERROR), it gets
308// substituted with 0, and it expands to COMPACT_GOOGLE_LOG_0. To allow us
309// to keep using this syntax, we define this macro to do the same thing
310// as COMPACT_GOOGLE_LOG_ERROR, and also define ERROR the same way that
311// the Windows SDK does for consistency.
312#define ERROR 0
[email protected]d8617a62009-10-09 23:52:20313#define COMPACT_GOOGLE_LOG_EX_0(ClassName, ...) \
314 COMPACT_GOOGLE_LOG_EX_ERROR(ClassName , ##__VA_ARGS__)
315#define COMPACT_GOOGLE_LOG_0 COMPACT_GOOGLE_LOG_ERROR
[email protected]521b0c42010-10-01 23:02:36316// Needed for LOG_IS_ON(ERROR).
317const LogSeverity LOG_0 = LOG_ERROR;
318
319#define LOG_IS_ON(severity) \
320 ((::logging::LOG_ ## severity) >= ::logging::GetMinLogLevel())
321
322// We can't do any caching tricks with VLOG_IS_ON() like the
323// google-glog version since it requires GCC extensions. This means
324// that using the v-logging functions in conjunction with --vmodule
325// may be slow.
326#define VLOG_IS_ON(verboselevel) \
327 ((verboselevel) <= ::logging::GetVlogLevel(__FILE__))
328
329// Helper macro which avoids evaluating the arguments to a stream if
330// the condition doesn't hold.
331#define LAZY_STREAM(stream, condition) \
332 !(condition) ? (void) 0 : ::logging::LogMessageVoidify() & (stream)
initial.commitd7cae122008-07-26 21:49:38333
334// We use the preprocessor's merging operator, "##", so that, e.g.,
335// LOG(INFO) becomes the token COMPACT_GOOGLE_LOG_INFO. There's some funny
336// subtle difference between ostream member streaming functions (e.g.,
337// ostream::operator<<(int) and ostream non-member streaming functions
338// (e.g., ::operator<<(ostream&, string&): it turns out that it's
339// impossible to stream something like a string directly to an unnamed
340// ostream. We employ a neat hack by calling the stream() member
341// function of LogMessage which seems to avoid the problem.
[email protected]521b0c42010-10-01 23:02:36342#define LOG_STREAM(severity) COMPACT_GOOGLE_LOG_ ## severity.stream()
initial.commitd7cae122008-07-26 21:49:38343
[email protected]521b0c42010-10-01 23:02:36344#define LOG(severity) LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity))
345#define LOG_IF(severity, condition) \
346 LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity) && (condition))
347
initial.commitd7cae122008-07-26 21:49:38348#define SYSLOG(severity) LOG(severity)
[email protected]521b0c42010-10-01 23:02:36349#define SYSLOG_IF(severity, condition) LOG_IF(severity, condition)
350
[email protected]99b7c57f2010-09-29 19:26:36351#define VLOG(verboselevel) LOG_IF(INFO, VLOG_IS_ON(verboselevel))
[email protected]521b0c42010-10-01 23:02:36352#define VLOG_IF(verboselevel, condition) \
353 LOG_IF(INFO, VLOG_IS_ON(verboselevel) && (condition))
[email protected]99b7c57f2010-09-29 19:26:36354
355// TODO(akalin): Add more VLOG variants, e.g. VPLOG.
initial.commitd7cae122008-07-26 21:49:38356
initial.commitd7cae122008-07-26 21:49:38357#define LOG_ASSERT(condition) \
358 LOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". "
359#define SYSLOG_ASSERT(condition) \
360 SYSLOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". "
361
[email protected]d8617a62009-10-09 23:52:20362#if defined(OS_WIN)
[email protected]521b0c42010-10-01 23:02:36363#define LOG_GETLASTERROR_STREAM(severity) \
[email protected]d8617a62009-10-09 23:52:20364 COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \
365 ::logging::GetLastSystemErrorCode()).stream()
[email protected]521b0c42010-10-01 23:02:36366#define LOG_GETLASTERROR(severity) \
367 LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity), LOG_IS_ON(severity))
368#define LOG_GETLASTERROR_MODULE_STREAM(severity, module) \
[email protected]d8617a62009-10-09 23:52:20369 COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \
370 ::logging::GetLastSystemErrorCode(), module).stream()
[email protected]521b0c42010-10-01 23:02:36371#define LOG_GETLASTERROR_MODULE(severity, module) \
372 LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity, module), \
373 LOG_IS_ON(severity))
374// PLOG_STREAM is used by PLOG, which is the usual error logging macro
375// for each platform.
376#define PLOG_STREAM(severity) LOG_GETLASTERROR_STREAM(severity)
[email protected]d8617a62009-10-09 23:52:20377#elif defined(OS_POSIX)
[email protected]521b0c42010-10-01 23:02:36378#define LOG_ERRNO_STREAM(severity) \
[email protected]d8617a62009-10-09 23:52:20379 COMPACT_GOOGLE_LOG_EX_ ## severity(ErrnoLogMessage, \
380 ::logging::GetLastSystemErrorCode()).stream()
[email protected]521b0c42010-10-01 23:02:36381#define LOG_ERRNO(severity) \
382 LAZY_STREAM(LOG_ERRNO_STREAM(severity), LOG_IS_ON(severity))
383// PLOG_STREAM is used by PLOG, which is the usual error logging macro
384// for each platform.
385#define PLOG_STREAM(severity) LOG_ERRNO_STREAM(severity)
[email protected]d8617a62009-10-09 23:52:20386// TODO(tschmelcher): Should we add OSStatus logging for Mac?
387#endif
388
[email protected]521b0c42010-10-01 23:02:36389#define PLOG(severity) \
390 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity))
391
[email protected]d8617a62009-10-09 23:52:20392#define PLOG_IF(severity, condition) \
[email protected]521b0c42010-10-01 23:02:36393 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity) && (condition))
[email protected]d8617a62009-10-09 23:52:20394
initial.commitd7cae122008-07-26 21:49:38395// CHECK dies with a fatal error if condition is not true. It is *not*
396// controlled by NDEBUG, so the check will be executed regardless of
397// compilation mode.
[email protected]521b0c42010-10-01 23:02:36398//
399// We make sure CHECK et al. always evaluates their arguments, as
400// doing CHECK(FunctionWithSideEffect()) is a common idiom.
401//
402// TODO(akalin): Fix the problem where if the min log level is >
403// FATAL, CHECK() et al. won't terminate the program.
404#define CHECK(condition) \
405 LAZY_STREAM(LOG_STREAM(FATAL), !(condition)) \
406 << "Check failed: " #condition ". "
initial.commitd7cae122008-07-26 21:49:38407
[email protected]d8617a62009-10-09 23:52:20408#define PCHECK(condition) \
[email protected]521b0c42010-10-01 23:02:36409 LAZY_STREAM(PLOG_STREAM(FATAL), !(condition)) \
410 << "Check failed: " #condition ". "
[email protected]d8617a62009-10-09 23:52:20411
initial.commitd7cae122008-07-26 21:49:38412// A container for a string pointer which can be evaluated to a bool -
413// true iff the pointer is NULL.
414struct CheckOpString {
415 CheckOpString(std::string* str) : str_(str) { }
416 // No destructor: if str_ is non-NULL, we're about to LOG(FATAL),
417 // so there's no point in cleaning up str_.
418 operator bool() const { return str_ != NULL; }
419 std::string* str_;
420};
421
422// Build the error message string. This is separate from the "Impl"
423// function template because it is not performance critical and so can
424// be out of line, while the "Impl" code should be inline.
425template<class t1, class t2>
426std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) {
427 std::ostringstream ss;
428 ss << names << " (" << v1 << " vs. " << v2 << ")";
429 std::string* msg = new std::string(ss.str());
430 return msg;
431}
432
[email protected]6d445d32010-09-30 19:10:03433// MSVC doesn't like complex extern templates and DLLs.
434#if !defined(COMPILER_MSVC)
435// Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated
436// in logging.cc.
437extern template std::string* MakeCheckOpString<int, int>(
438 const int&, const int&, const char* names);
439extern template std::string* MakeCheckOpString<unsigned long, unsigned long>(
440 const unsigned long&, const unsigned long&, const char* names);
441extern template std::string* MakeCheckOpString<unsigned long, unsigned int>(
442 const unsigned long&, const unsigned int&, const char* names);
443extern template std::string* MakeCheckOpString<unsigned int, unsigned long>(
444 const unsigned int&, const unsigned long&, const char* names);
445extern template std::string* MakeCheckOpString<std::string, std::string>(
446 const std::string&, const std::string&, const char* name);
447#endif
initial.commitd7cae122008-07-26 21:49:38448
[email protected]e150c0382010-03-02 00:41:12449// Helper macro for binary operators.
450// Don't use this macro directly in your code, use CHECK_EQ et al below.
[email protected]521b0c42010-10-01 23:02:36451//
452// TODO(akalin): Rewrite this so that constructs like if (...)
453// CHECK_EQ(...) else { ... } work properly.
454#define CHECK_OP(name, op, val1, val2) \
455 if (logging::CheckOpString _result = \
456 logging::Check##name##Impl((val1), (val2), \
457 #val1 " " #op " " #val2)) \
[email protected]8b782102010-09-30 22:38:30458 logging::LogMessage(__FILE__, __LINE__, _result).stream()
[email protected]e150c0382010-03-02 00:41:12459
460// Helper functions for string comparisons.
461// To avoid bloat, the definitions are in logging.cc.
[email protected]521b0c42010-10-01 23:02:36462//
463// TODO(akalin): Actually have the implementations in logging.cc, or
464// remove these.
[email protected]e150c0382010-03-02 00:41:12465#define DECLARE_CHECK_STROP_IMPL(func, expected) \
466 std::string* Check##func##expected##Impl(const char* s1, \
467 const char* s2, \
468 const char* names);
469DECLARE_CHECK_STROP_IMPL(strcmp, true)
470DECLARE_CHECK_STROP_IMPL(strcmp, false)
471DECLARE_CHECK_STROP_IMPL(_stricmp, true)
472DECLARE_CHECK_STROP_IMPL(_stricmp, false)
473#undef DECLARE_CHECK_STROP_IMPL
474
475// Helper macro for string comparisons.
476// Don't use this macro directly in your code, use CHECK_STREQ et al below.
477#define CHECK_STROP(func, op, expected, s1, s2) \
478 while (CheckOpString _result = \
479 logging::Check##func##expected##Impl((s1), (s2), \
480 #s1 " " #op " " #s2)) \
481 LOG(FATAL) << *_result.str_
482
483// String (char*) equality/inequality checks.
484// CASE versions are case-insensitive.
485//
486// Note that "s1" and "s2" may be temporary strings which are destroyed
487// by the compiler at the end of the current "full expression"
488// (e.g. CHECK_STREQ(Foo().c_str(), Bar().c_str())).
489
490#define CHECK_STREQ(s1, s2) CHECK_STROP(strcmp, ==, true, s1, s2)
491#define CHECK_STRNE(s1, s2) CHECK_STROP(strcmp, !=, false, s1, s2)
492#define CHECK_STRCASEEQ(s1, s2) CHECK_STROP(_stricmp, ==, true, s1, s2)
493#define CHECK_STRCASENE(s1, s2) CHECK_STROP(_stricmp, !=, false, s1, s2)
494
495#define CHECK_INDEX(I,A) CHECK(I < (sizeof(A)/sizeof(A[0])))
496#define CHECK_BOUND(B,A) CHECK(B <= (sizeof(A)/sizeof(A[0])))
497
498#define CHECK_EQ(val1, val2) CHECK_OP(EQ, ==, val1, val2)
499#define CHECK_NE(val1, val2) CHECK_OP(NE, !=, val1, val2)
500#define CHECK_LE(val1, val2) CHECK_OP(LE, <=, val1, val2)
501#define CHECK_LT(val1, val2) CHECK_OP(LT, < , val1, val2)
502#define CHECK_GE(val1, val2) CHECK_OP(GE, >=, val1, val2)
503#define CHECK_GT(val1, val2) CHECK_OP(GT, > , val1, val2)
504
[email protected]e3cca332009-08-20 01:20:29505// http://crbug.com/16512 is open for a real fix for this. For now, Windows
506// uses OFFICIAL_BUILD and other platforms use the branding flag when NDEBUG is
507// defined.
508#if ( defined(OS_WIN) && defined(OFFICIAL_BUILD)) || \
509 (!defined(OS_WIN) && defined(NDEBUG) && defined(GOOGLE_CHROME_BUILD))
[email protected]521b0c42010-10-01 23:02:36510// Used by unit tests.
511#define LOGGING_IS_OFFICIAL_BUILD
512
[email protected]e3cca332009-08-20 01:20:29513// In order to have optimized code for official builds, remove DLOGs and
514// DCHECKs.
[email protected]d15e56c2010-09-30 21:12:33515#define ENABLE_DLOG 0
516#define ENABLE_DCHECK 0
517
518#elif defined(NDEBUG)
519// Otherwise, if we're a release build, remove DLOGs but not DCHECKs
520// (since those can still be turned on via a command-line flag).
521#define ENABLE_DLOG 0
522#define ENABLE_DCHECK 1
523
524#else
525// Otherwise, we're a debug build so enable DLOGs and DCHECKs.
526#define ENABLE_DLOG 1
527#define ENABLE_DCHECK 1
[email protected]e3cca332009-08-20 01:20:29528#endif
529
[email protected]d15e56c2010-09-30 21:12:33530// Definitions for DLOG et al.
531
[email protected]d926c202010-10-01 02:58:24532#if ENABLE_DLOG
533
[email protected]5e987802010-11-01 19:49:22534#define DLOG_IS_ON(severity) LOG_IS_ON(severity)
[email protected]d926c202010-10-01 02:58:24535#define DLOG_IF(severity, condition) LOG_IF(severity, condition)
536#define DLOG_ASSERT(condition) LOG_ASSERT(condition)
[email protected]d926c202010-10-01 02:58:24537#define DPLOG_IF(severity, condition) PLOG_IF(severity, condition)
[email protected]521b0c42010-10-01 23:02:36538#define DVLOG_IF(verboselevel, condition) VLOG_IF(verboselevel, condition)
[email protected]d926c202010-10-01 02:58:24539
540#else // ENABLE_DLOG
541
[email protected]521b0c42010-10-01 23:02:36542// If ENABLE_DLOG is off, we want to avoid emitting any references to
543// |condition| (which may reference a variable defined only if NDEBUG
544// is not defined). Contrast this with DCHECK et al., which has
545// different behavior.
[email protected]d926c202010-10-01 02:58:24546
[email protected]521b0c42010-10-01 23:02:36547#define DLOG_EAT_STREAM_PARAMETERS \
548 true ? (void) 0 : ::logging::LogMessageVoidify() & LOG_STREAM(FATAL)
[email protected]d926c202010-10-01 02:58:24549
[email protected]5e987802010-11-01 19:49:22550#define DLOG_IS_ON(severity) false
[email protected]521b0c42010-10-01 23:02:36551#define DLOG_IF(severity, condition) DLOG_EAT_STREAM_PARAMETERS
552#define DLOG_ASSERT(condition) DLOG_EAT_STREAM_PARAMETERS
553#define DPLOG_IF(severity, condition) DLOG_EAT_STREAM_PARAMETERS
554#define DVLOG_IF(verboselevel, condition) DLOG_EAT_STREAM_PARAMETERS
[email protected]d926c202010-10-01 02:58:24555
556#endif // ENABLE_DLOG
557
[email protected]d15e56c2010-09-30 21:12:33558// DEBUG_MODE is for uses like
559// if (DEBUG_MODE) foo.CheckThatFoo();
560// instead of
561// #ifndef NDEBUG
562// foo.CheckThatFoo();
563// #endif
564//
565// We tie its state to ENABLE_DLOG.
566enum { DEBUG_MODE = ENABLE_DLOG };
567
568#undef ENABLE_DLOG
569
[email protected]521b0c42010-10-01 23:02:36570#define DLOG(severity) \
571 LAZY_STREAM(LOG_STREAM(severity), DLOG_IS_ON(severity))
572
573#if defined(OS_WIN)
574#define DLOG_GETLASTERROR(severity) \
575 LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity), DLOG_IS_ON(severity))
576#define DLOG_GETLASTERROR_MODULE(severity, module) \
577 LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity, module), \
578 DLOG_IS_ON(severity))
579#elif defined(OS_POSIX)
580#define DLOG_ERRNO(severity) \
581 LAZY_STREAM(LOG_ERRNO_STREAM(severity), DLOG_IS_ON(severity))
582#endif
583
584#define DPLOG(severity) \
585 LAZY_STREAM(PLOG_STREAM(severity), DLOG_IS_ON(severity))
586
587#define DVLOG(verboselevel) DLOG_IF(INFO, VLOG_IS_ON(verboselevel))
588
589// Definitions for DCHECK et al.
[email protected]d926c202010-10-01 02:58:24590
[email protected]d15e56c2010-09-30 21:12:33591#if ENABLE_DCHECK
[email protected]e3cca332009-08-20 01:20:29592
[email protected]521b0c42010-10-01 23:02:36593#if defined(NDEBUG)
[email protected]d926c202010-10-01 02:58:24594
[email protected]521b0c42010-10-01 23:02:36595#define DCHECK_SEVERITY ERROR_REPORT
596const LogSeverity LOG_DCHECK = LOG_ERROR_REPORT;
[email protected]5e987802010-11-01 19:49:22597// This is set to true in InitLogging when we want to enable the
598// DCHECKs in release.
599extern bool g_enable_dcheck;
600#define DCHECK_IS_ON() (::logging::g_enable_dcheck && LOG_IS_ON(DCHECK))
[email protected]d926c202010-10-01 02:58:24601
[email protected]521b0c42010-10-01 23:02:36602#else // defined(NDEBUG)
603
[email protected]5e987802010-11-01 19:49:22604// On a regular debug build, we want to have DCHECKs enabled.
[email protected]521b0c42010-10-01 23:02:36605#define DCHECK_SEVERITY FATAL
606const LogSeverity LOG_DCHECK = LOG_FATAL;
[email protected]5e987802010-11-01 19:49:22607// TODO(akalin): We don't define this as 'true' since if the log level
608// is above FATAL, the DCHECK won't go through anyway. Make it so
609// that DCHECKs work regardless of the logging level, then set this to
610// 'true'.
611#define DCHECK_IS_ON() LOG_IS_ON(DCHECK)
[email protected]521b0c42010-10-01 23:02:36612
613#endif // defined(NDEBUG)
614
615#else // ENABLE_DCHECK
616
[email protected]521b0c42010-10-01 23:02:36617#define DCHECK_SEVERITY FATAL
618const LogSeverity LOG_DCHECK = LOG_FATAL;
[email protected]5e987802010-11-01 19:49:22619#define DCHECK_IS_ON() false
[email protected]521b0c42010-10-01 23:02:36620
621#endif // ENABLE_DCHECK
[email protected]5e987802010-11-01 19:49:22622#undef ENABLE_DCHECK
[email protected]521b0c42010-10-01 23:02:36623
624// Unlike CHECK et al., DCHECK et al. *does* evaluate their arguments
625// lazily.
626
627// DCHECK et al. also make sure to reference |condition| regardless of
628// whether DCHECKs are enabled; this is so that we don't get unused
629// variable warnings if the only use of a variable is in a DCHECK.
630// This behavior is different from DLOG_IF et al.
631
632#define DCHECK(condition) \
633 !DCHECK_IS_ON() ? (void) 0 : \
634 LOG_IF(DCHECK_SEVERITY, !(condition)) \
635 << "Check failed: " #condition ". "
636
637#define DPCHECK(condition) \
638 !DCHECK_IS_ON() ? (void) 0 : \
639 PLOG_IF(DCHECK_SEVERITY, !(condition)) \
640 << "Check failed: " #condition ". "
[email protected]d926c202010-10-01 02:58:24641
642// Helper macro for binary operators.
643// Don't use this macro directly in your code, use DCHECK_EQ et al below.
[email protected]521b0c42010-10-01 23:02:36644#define DCHECK_OP(name, op, val1, val2) \
[email protected]5e987802010-11-01 19:49:22645 if (DCHECK_IS_ON()) \
[email protected]521b0c42010-10-01 23:02:36646 if (logging::CheckOpString _result = \
647 logging::Check##name##Impl((val1), (val2), \
648 #val1 " " #op " " #val2)) \
649 logging::LogMessage( \
650 __FILE__, __LINE__, ::logging::LOG_DCHECK, \
651 _result).stream()
initial.commitd7cae122008-07-26 21:49:38652
initial.commitd7cae122008-07-26 21:49:38653// Equality/Inequality checks - compare two values, and log a LOG_FATAL message
654// including the two values when the result is not as expected. The values
655// must have operator<<(ostream, ...) defined.
656//
657// You may append to the error message like so:
658// DCHECK_NE(1, 2) << ": The world must be ending!";
659//
660// We are very careful to ensure that each argument is evaluated exactly
661// once, and that anything which is legal to pass as a function argument is
662// legal here. In particular, the arguments may be temporary expressions
663// which will end up being destroyed at the end of the apparent statement,
664// for example:
665// DCHECK_EQ(string("abc")[1], 'b');
666//
667// WARNING: These may not compile correctly if one of the arguments is a pointer
668// and the other is NULL. To work around this, simply static_cast NULL to the
669// type of the desired pointer.
670
671#define DCHECK_EQ(val1, val2) DCHECK_OP(EQ, ==, val1, val2)
672#define DCHECK_NE(val1, val2) DCHECK_OP(NE, !=, val1, val2)
673#define DCHECK_LE(val1, val2) DCHECK_OP(LE, <=, val1, val2)
674#define DCHECK_LT(val1, val2) DCHECK_OP(LT, < , val1, val2)
675#define DCHECK_GE(val1, val2) DCHECK_OP(GE, >=, val1, val2)
676#define DCHECK_GT(val1, val2) DCHECK_OP(GT, > , val1, val2)
677
[email protected]521b0c42010-10-01 23:02:36678// Helper macro for string comparisons.
679// Don't use this macro directly in your code, use DCHECK_STREQ et al below.
680#define DCHECK_STROP(func, op, expected, s1, s2) \
681 if (DCHECK_IS_ON()) CHECK_STROP(func, op, expected, s1, s2)
initial.commitd7cae122008-07-26 21:49:38682
[email protected]521b0c42010-10-01 23:02:36683// String (char*) equality/inequality checks.
684// CASE versions are case-insensitive.
685//
686// Note that "s1" and "s2" may be temporary strings which are destroyed
687// by the compiler at the end of the current "full expression"
688// (e.g. DCHECK_STREQ(Foo().c_str(), Bar().c_str())).
[email protected]d15e56c2010-09-30 21:12:33689
[email protected]521b0c42010-10-01 23:02:36690#define DCHECK_STREQ(s1, s2) DCHECK_STROP(strcmp, ==, true, s1, s2)
691#define DCHECK_STRNE(s1, s2) DCHECK_STROP(strcmp, !=, false, s1, s2)
692#define DCHECK_STRCASEEQ(s1, s2) DCHECK_STROP(_stricmp, ==, true, s1, s2)
693#define DCHECK_STRCASENE(s1, s2) DCHECK_STROP(_stricmp, !=, false, s1, s2)
[email protected]d15e56c2010-09-30 21:12:33694
[email protected]521b0c42010-10-01 23:02:36695#define DCHECK_INDEX(I,A) DCHECK(I < (sizeof(A)/sizeof(A[0])))
696#define DCHECK_BOUND(B,A) DCHECK(B <= (sizeof(A)/sizeof(A[0])))
[email protected]d926c202010-10-01 02:58:24697
[email protected]e150c0382010-03-02 00:41:12698// Helper functions for CHECK_OP macro.
699// The (int, int) specialization works around the issue that the compiler
700// will not instantiate the template version of the function on values of
701// unnamed enum type - see comment below.
702#define DEFINE_CHECK_OP_IMPL(name, op) \
703 template <class t1, class t2> \
704 inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \
705 const char* names) { \
706 if (v1 op v2) return NULL; \
707 else return MakeCheckOpString(v1, v2, names); \
708 } \
709 inline std::string* Check##name##Impl(int v1, int v2, const char* names) { \
710 if (v1 op v2) return NULL; \
711 else return MakeCheckOpString(v1, v2, names); \
712 }
713DEFINE_CHECK_OP_IMPL(EQ, ==)
714DEFINE_CHECK_OP_IMPL(NE, !=)
715DEFINE_CHECK_OP_IMPL(LE, <=)
716DEFINE_CHECK_OP_IMPL(LT, < )
717DEFINE_CHECK_OP_IMPL(GE, >=)
718DEFINE_CHECK_OP_IMPL(GT, > )
719#undef DEFINE_CHECK_OP_IMPL
720
initial.commitd7cae122008-07-26 21:49:38721#define NOTREACHED() DCHECK(false)
722
723// Redefine the standard assert to use our nice log files
724#undef assert
725#define assert(x) DLOG_ASSERT(x)
726
727// This class more or less represents a particular log message. You
728// create an instance of LogMessage and then stream stuff to it.
729// When you finish streaming to it, ~LogMessage is called and the
730// full message gets streamed to the appropriate destination.
731//
732// You shouldn't actually use LogMessage's constructor to log things,
733// though. You should use the LOG() macro (and variants thereof)
734// above.
735class LogMessage {
736 public:
737 LogMessage(const char* file, int line, LogSeverity severity, int ctr);
738
739 // Two special constructors that generate reduced amounts of code at
740 // LOG call sites for common cases.
741 //
742 // Used for LOG(INFO): Implied are:
743 // severity = LOG_INFO, ctr = 0
744 //
745 // Using this constructor instead of the more complex constructor above
746 // saves a couple of bytes per call site.
747 LogMessage(const char* file, int line);
748
749 // Used for LOG(severity) where severity != INFO. Implied
750 // are: ctr = 0
751 //
752 // Using this constructor instead of the more complex constructor above
753 // saves a couple of bytes per call site.
754 LogMessage(const char* file, int line, LogSeverity severity);
755
756 // A special constructor used for check failures.
757 // Implied severity = LOG_FATAL
758 LogMessage(const char* file, int line, const CheckOpString& result);
759
[email protected]fb62a532009-02-12 01:19:05760 // A special constructor used for check failures, with the option to
761 // specify severity.
762 LogMessage(const char* file, int line, LogSeverity severity,
763 const CheckOpString& result);
764
initial.commitd7cae122008-07-26 21:49:38765 ~LogMessage();
766
767 std::ostream& stream() { return stream_; }
768
769 private:
770 void Init(const char* file, int line);
771
772 LogSeverity severity_;
773 std::ostringstream stream_;
[email protected]c88873922008-07-30 13:02:03774 size_t message_start_; // Offset of the start of the message (past prefix
775 // info).
[email protected]3f85caa2009-04-14 16:52:11776#if defined(OS_WIN)
777 // Stores the current value of GetLastError in the constructor and restores
778 // it in the destructor by calling SetLastError.
779 // This is useful since the LogMessage class uses a lot of Win32 calls
780 // that will lose the value of GLE and the code that called the log function
781 // will have lost the thread error value when the log call returns.
782 class SaveLastError {
783 public:
784 SaveLastError();
785 ~SaveLastError();
786
787 unsigned long get_error() const { return last_error_; }
788
789 protected:
790 unsigned long last_error_;
791 };
792
793 SaveLastError last_error_;
794#endif
initial.commitd7cae122008-07-26 21:49:38795
[email protected]39be4242008-08-07 18:31:40796 DISALLOW_COPY_AND_ASSIGN(LogMessage);
initial.commitd7cae122008-07-26 21:49:38797};
798
799// A non-macro interface to the log facility; (useful
800// when the logging level is not a compile-time constant).
801inline void LogAtLevel(int const log_level, std::string const &msg) {
802 LogMessage(__FILE__, __LINE__, log_level).stream() << msg;
803}
804
805// This class is used to explicitly ignore values in the conditional
806// logging macros. This avoids compiler warnings like "value computed
807// is not used" and "statement has no effect".
808class LogMessageVoidify {
809 public:
810 LogMessageVoidify() { }
811 // This has to be an operator with a precedence lower than << but
812 // higher than ?:
813 void operator&(std::ostream&) { }
814};
815
[email protected]d8617a62009-10-09 23:52:20816#if defined(OS_WIN)
817typedef unsigned long SystemErrorCode;
818#elif defined(OS_POSIX)
819typedef int SystemErrorCode;
820#endif
821
822// Alias for ::GetLastError() on Windows and errno on POSIX. Avoids having to
823// pull in windows.h just for GetLastError() and DWORD.
824SystemErrorCode GetLastSystemErrorCode();
825
826#if defined(OS_WIN)
827// Appends a formatted system message of the GetLastError() type.
828class Win32ErrorLogMessage {
829 public:
830 Win32ErrorLogMessage(const char* file,
831 int line,
832 LogSeverity severity,
833 SystemErrorCode err,
834 const char* module);
835
836 Win32ErrorLogMessage(const char* file,
837 int line,
838 LogSeverity severity,
839 SystemErrorCode err);
840
841 std::ostream& stream() { return log_message_.stream(); }
842
843 // Appends the error message before destructing the encapsulated class.
844 ~Win32ErrorLogMessage();
845
846 private:
847 SystemErrorCode err_;
848 // Optional name of the module defining the error.
849 const char* module_;
850 LogMessage log_message_;
851
852 DISALLOW_COPY_AND_ASSIGN(Win32ErrorLogMessage);
853};
854#elif defined(OS_POSIX)
855// Appends a formatted system message of the errno type
856class ErrnoLogMessage {
857 public:
858 ErrnoLogMessage(const char* file,
859 int line,
860 LogSeverity severity,
861 SystemErrorCode err);
862
863 std::ostream& stream() { return log_message_.stream(); }
864
865 // Appends the error message before destructing the encapsulated class.
866 ~ErrnoLogMessage();
867
868 private:
869 SystemErrorCode err_;
870 LogMessage log_message_;
871
872 DISALLOW_COPY_AND_ASSIGN(ErrnoLogMessage);
873};
874#endif // OS_WIN
875
initial.commitd7cae122008-07-26 21:49:38876// Closes the log file explicitly if open.
877// NOTE: Since the log file is opened as necessary by the action of logging
878// statements, there's no guarantee that it will stay closed
879// after this call.
880void CloseLogFile();
881
[email protected]e36ddc82009-12-08 04:22:50882// Async signal safe logging mechanism.
883void RawLog(int level, const char* message);
884
885#define RAW_LOG(level, message) logging::RawLog(logging::LOG_ ## level, message)
886
887#define RAW_CHECK(condition) \
888 do { \
889 if (!(condition)) \
890 logging::RawLog(logging::LOG_FATAL, "Check failed: " #condition "\n"); \
891 } while (0)
892
[email protected]39be4242008-08-07 18:31:40893} // namespace logging
initial.commitd7cae122008-07-26 21:49:38894
[email protected]46ce5b562010-06-16 18:39:53895// These functions are provided as a convenience for logging, which is where we
896// use streams (it is against Google style to use streams in other places). It
897// is designed to allow you to emit non-ASCII Unicode strings to the log file,
898// which is normally ASCII. It is relatively slow, so try not to use it for
899// common cases. Non-ASCII characters will be converted to UTF-8 by these
900// operators.
901std::ostream& operator<<(std::ostream& out, const wchar_t* wstr);
902inline std::ostream& operator<<(std::ostream& out, const std::wstring& wstr) {
903 return out << wstr.c_str();
904}
905
[email protected]0dfc81b2008-08-25 03:44:40906// The NOTIMPLEMENTED() macro annotates codepaths which have
907// not been implemented yet.
908//
909// The implementation of this macro is controlled by NOTIMPLEMENTED_POLICY:
910// 0 -- Do nothing (stripped by compiler)
911// 1 -- Warn at compile time
912// 2 -- Fail at compile time
913// 3 -- Fail at runtime (DCHECK)
914// 4 -- [default] LOG(ERROR) at runtime
915// 5 -- LOG(ERROR) at runtime, only once per call-site
916
917#ifndef NOTIMPLEMENTED_POLICY
918// Select default policy: LOG(ERROR)
919#define NOTIMPLEMENTED_POLICY 4
920#endif
921
[email protected]f6cda752008-10-30 23:54:26922#if defined(COMPILER_GCC)
923// On Linux, with GCC, we can use __PRETTY_FUNCTION__ to get the demangled name
924// of the current function in the NOTIMPLEMENTED message.
925#define NOTIMPLEMENTED_MSG "Not implemented reached in " << __PRETTY_FUNCTION__
926#else
927#define NOTIMPLEMENTED_MSG "NOT IMPLEMENTED"
928#endif
929
[email protected]0dfc81b2008-08-25 03:44:40930#if NOTIMPLEMENTED_POLICY == 0
931#define NOTIMPLEMENTED() ;
932#elif NOTIMPLEMENTED_POLICY == 1
933// TODO, figure out how to generate a warning
934#define NOTIMPLEMENTED() COMPILE_ASSERT(false, NOT_IMPLEMENTED)
935#elif NOTIMPLEMENTED_POLICY == 2
936#define NOTIMPLEMENTED() COMPILE_ASSERT(false, NOT_IMPLEMENTED)
937#elif NOTIMPLEMENTED_POLICY == 3
938#define NOTIMPLEMENTED() NOTREACHED()
939#elif NOTIMPLEMENTED_POLICY == 4
[email protected]f6cda752008-10-30 23:54:26940#define NOTIMPLEMENTED() LOG(ERROR) << NOTIMPLEMENTED_MSG
[email protected]0dfc81b2008-08-25 03:44:40941#elif NOTIMPLEMENTED_POLICY == 5
942#define NOTIMPLEMENTED() do {\
943 static int count = 0;\
[email protected]f6cda752008-10-30 23:54:26944 LOG_IF(ERROR, 0 == count++) << NOTIMPLEMENTED_MSG;\
[email protected]0dfc81b2008-08-25 03:44:40945} while(0)
946#endif
947
[email protected]39be4242008-08-07 18:31:40948#endif // BASE_LOGGING_H_