blob: b1bc0b0535bf2a654d2b0a6c430bf960588c9073 [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
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]7c10f7552011-01-11 01:03:36168enum DcheckState {
169 DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS,
170 ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS
171};
172
[email protected]ff3d0c32010-08-23 19:57:46173// TODO(avi): do we want to do a unification of character types here?
174#if defined(OS_WIN)
175typedef wchar_t PathChar;
176#else
177typedef char PathChar;
178#endif
179
180// Define different names for the BaseInitLoggingImpl() function depending on
181// whether NDEBUG is defined or not so that we'll fail to link if someone tries
182// to compile logging.cc with NDEBUG but includes logging.h without defining it,
183// or vice versa.
184#if NDEBUG
185#define BaseInitLoggingImpl BaseInitLoggingImpl_built_with_NDEBUG
186#else
187#define BaseInitLoggingImpl BaseInitLoggingImpl_built_without_NDEBUG
188#endif
189
190// Implementation of the InitLogging() method declared below. We use a
191// more-specific name so we can #define it above without affecting other code
192// that has named stuff "InitLogging".
[email protected]c7d5da992010-10-28 00:20:21193bool BaseInitLoggingImpl(const PathChar* log_file,
[email protected]ff3d0c32010-08-23 19:57:46194 LoggingDestination logging_dest,
195 LogLockingState lock_log,
[email protected]7c10f7552011-01-11 01:03:36196 OldFileDeletionState delete_old,
197 DcheckState dcheck_state);
[email protected]ff3d0c32010-08-23 19:57:46198
initial.commitd7cae122008-07-26 21:49:38199// Sets the log file name and other global logging state. Calling this function
200// is recommended, and is normally done at the beginning of application init.
201// If you don't call it, all the flags will be initialized to their default
202// values, and there is a race condition that may leak a critical section
203// object if two threads try to do the first log at the same time.
204// See the definition of the enums above for descriptions and default values.
205//
206// The default log file is initialized to "debug.log" in the application
207// directory. You probably don't want this, especially since the program
208// directory may not be writable on an enduser's system.
[email protected]c7d5da992010-10-28 00:20:21209inline bool InitLogging(const PathChar* log_file,
[email protected]ff3d0c32010-08-23 19:57:46210 LoggingDestination logging_dest,
211 LogLockingState lock_log,
[email protected]7c10f7552011-01-11 01:03:36212 OldFileDeletionState delete_old,
213 DcheckState dcheck_state) {
214 return BaseInitLoggingImpl(log_file, logging_dest, lock_log,
215 delete_old, dcheck_state);
[email protected]ff3d0c32010-08-23 19:57:46216}
initial.commitd7cae122008-07-26 21:49:38217
218// Sets the log level. Anything at or above this level will be written to the
219// log file/displayed to the user (if applicable). Anything below this level
[email protected]162ac0f2010-11-04 15:50:49220// will be silently ignored. The log level defaults to 0 (everything is logged
221// up to level INFO) if this function is not called.
222// Note that log messages for VLOG(x) are logged at level -x, so setting
223// the min log level to negative values enables verbose logging.
initial.commitd7cae122008-07-26 21:49:38224void SetMinLogLevel(int level);
225
[email protected]8a2986ca2009-04-10 19:13:42226// Gets the current log level.
initial.commitd7cae122008-07-26 21:49:38227int GetMinLogLevel();
228
[email protected]162ac0f2010-11-04 15:50:49229// Gets the VLOG default verbosity level.
230int GetVlogVerbosity();
231
[email protected]99b7c57f2010-09-29 19:26:36232// Gets the current vlog level for the given file (usually taken from
233// __FILE__).
[email protected]2f4e9a62010-09-29 21:25:14234
235// Note that |N| is the size *with* the null terminator.
236int GetVlogLevelHelper(const char* file_start, size_t N);
237
[email protected]99b7c57f2010-09-29 19:26:36238template <size_t N>
239int GetVlogLevel(const char (&file)[N]) {
240 return GetVlogLevelHelper(file, N);
241}
initial.commitd7cae122008-07-26 21:49:38242
243// Sets the common items you want to be prepended to each log message.
244// process and thread IDs default to off, the timestamp defaults to on.
245// If this function is not called, logging defaults to writing the timestamp
246// only.
247void SetLogItems(bool enable_process_id, bool enable_thread_id,
248 bool enable_timestamp, bool enable_tickcount);
249
[email protected]81e0a852010-08-17 00:38:12250// Sets whether or not you'd like to see fatal debug messages popped up in
251// a dialog box or not.
252// Dialogs are not shown by default.
253void SetShowErrorDialogs(bool enable_dialogs);
254
initial.commitd7cae122008-07-26 21:49:38255// Sets the Log Assert Handler that will be used to notify of check failures.
[email protected]fb62a532009-02-12 01:19:05256// The default handler shows a dialog box and then terminate the process,
257// however clients can use this function to override with their own handling
258// (e.g. a silent one for Unit Tests)
initial.commitd7cae122008-07-26 21:49:38259typedef void (*LogAssertHandlerFunction)(const std::string& str);
260void SetLogAssertHandler(LogAssertHandlerFunction handler);
[email protected]64e5cc02010-11-03 19:20:27261
[email protected]fb62a532009-02-12 01:19:05262// Sets the Log Report Handler that will be used to notify of check failures
263// in non-debug mode. The default handler shows a dialog box and continues
264// the execution, however clients can use this function to override with their
265// own handling.
266typedef void (*LogReportHandlerFunction)(const std::string& str);
267void SetLogReportHandler(LogReportHandlerFunction handler);
initial.commitd7cae122008-07-26 21:49:38268
[email protected]2b07b8412009-11-25 15:26:34269// Sets the Log Message Handler that gets passed every log message before
270// it's sent to other log destinations (if any).
271// Returns true to signal that it handled the message and the message
272// should not be sent to other log destinations.
[email protected]162ac0f2010-11-04 15:50:49273typedef bool (*LogMessageHandlerFunction)(int severity,
274 const char* file, int line, size_t message_start, const std::string& str);
[email protected]2b07b8412009-11-25 15:26:34275void SetLogMessageHandler(LogMessageHandlerFunction handler);
[email protected]64e5cc02010-11-03 19:20:27276LogMessageHandlerFunction GetLogMessageHandler();
[email protected]2b07b8412009-11-25 15:26:34277
initial.commitd7cae122008-07-26 21:49:38278typedef int LogSeverity;
[email protected]162ac0f2010-11-04 15:50:49279const LogSeverity LOG_VERBOSE = -1; // This is level 1 verbosity
280// Note: the log severities are used to index into the array of names,
281// see log_severity_names.
initial.commitd7cae122008-07-26 21:49:38282const LogSeverity LOG_INFO = 0;
283const LogSeverity LOG_WARNING = 1;
284const LogSeverity LOG_ERROR = 2;
[email protected]fb62a532009-02-12 01:19:05285const LogSeverity LOG_ERROR_REPORT = 3;
286const LogSeverity LOG_FATAL = 4;
287const LogSeverity LOG_NUM_SEVERITIES = 5;
initial.commitd7cae122008-07-26 21:49:38288
[email protected]521b0c42010-10-01 23:02:36289// LOG_DFATAL is LOG_FATAL in debug mode, ERROR in normal mode
initial.commitd7cae122008-07-26 21:49:38290#ifdef NDEBUG
[email protected]521b0c42010-10-01 23:02:36291const LogSeverity LOG_DFATAL = LOG_ERROR;
initial.commitd7cae122008-07-26 21:49:38292#else
[email protected]521b0c42010-10-01 23:02:36293const LogSeverity LOG_DFATAL = LOG_FATAL;
initial.commitd7cae122008-07-26 21:49:38294#endif
295
296// A few definitions of macros that don't generate much code. These are used
297// by LOG() and LOG_IF, etc. Since these are used all over our code, it's
298// better to have compact code for these operations.
[email protected]d8617a62009-10-09 23:52:20299#define COMPACT_GOOGLE_LOG_EX_INFO(ClassName, ...) \
300 logging::ClassName(__FILE__, __LINE__, logging::LOG_INFO , ##__VA_ARGS__)
301#define COMPACT_GOOGLE_LOG_EX_WARNING(ClassName, ...) \
302 logging::ClassName(__FILE__, __LINE__, logging::LOG_WARNING , ##__VA_ARGS__)
303#define COMPACT_GOOGLE_LOG_EX_ERROR(ClassName, ...) \
304 logging::ClassName(__FILE__, __LINE__, logging::LOG_ERROR , ##__VA_ARGS__)
305#define COMPACT_GOOGLE_LOG_EX_ERROR_REPORT(ClassName, ...) \
306 logging::ClassName(__FILE__, __LINE__, \
307 logging::LOG_ERROR_REPORT , ##__VA_ARGS__)
308#define COMPACT_GOOGLE_LOG_EX_FATAL(ClassName, ...) \
309 logging::ClassName(__FILE__, __LINE__, logging::LOG_FATAL , ##__VA_ARGS__)
310#define COMPACT_GOOGLE_LOG_EX_DFATAL(ClassName, ...) \
[email protected]521b0c42010-10-01 23:02:36311 logging::ClassName(__FILE__, __LINE__, logging::LOG_DFATAL , ##__VA_ARGS__)
[email protected]d8617a62009-10-09 23:52:20312
initial.commitd7cae122008-07-26 21:49:38313#define COMPACT_GOOGLE_LOG_INFO \
[email protected]d8617a62009-10-09 23:52:20314 COMPACT_GOOGLE_LOG_EX_INFO(LogMessage)
initial.commitd7cae122008-07-26 21:49:38315#define COMPACT_GOOGLE_LOG_WARNING \
[email protected]d8617a62009-10-09 23:52:20316 COMPACT_GOOGLE_LOG_EX_WARNING(LogMessage)
initial.commitd7cae122008-07-26 21:49:38317#define COMPACT_GOOGLE_LOG_ERROR \
[email protected]d8617a62009-10-09 23:52:20318 COMPACT_GOOGLE_LOG_EX_ERROR(LogMessage)
[email protected]fb62a532009-02-12 01:19:05319#define COMPACT_GOOGLE_LOG_ERROR_REPORT \
[email protected]d8617a62009-10-09 23:52:20320 COMPACT_GOOGLE_LOG_EX_ERROR_REPORT(LogMessage)
initial.commitd7cae122008-07-26 21:49:38321#define COMPACT_GOOGLE_LOG_FATAL \
[email protected]d8617a62009-10-09 23:52:20322 COMPACT_GOOGLE_LOG_EX_FATAL(LogMessage)
initial.commitd7cae122008-07-26 21:49:38323#define COMPACT_GOOGLE_LOG_DFATAL \
[email protected]d8617a62009-10-09 23:52:20324 COMPACT_GOOGLE_LOG_EX_DFATAL(LogMessage)
initial.commitd7cae122008-07-26 21:49:38325
326// wingdi.h defines ERROR to be 0. When we call LOG(ERROR), it gets
327// substituted with 0, and it expands to COMPACT_GOOGLE_LOG_0. To allow us
328// to keep using this syntax, we define this macro to do the same thing
329// as COMPACT_GOOGLE_LOG_ERROR, and also define ERROR the same way that
330// the Windows SDK does for consistency.
331#define ERROR 0
[email protected]d8617a62009-10-09 23:52:20332#define COMPACT_GOOGLE_LOG_EX_0(ClassName, ...) \
333 COMPACT_GOOGLE_LOG_EX_ERROR(ClassName , ##__VA_ARGS__)
334#define COMPACT_GOOGLE_LOG_0 COMPACT_GOOGLE_LOG_ERROR
[email protected]521b0c42010-10-01 23:02:36335// Needed for LOG_IS_ON(ERROR).
336const LogSeverity LOG_0 = LOG_ERROR;
337
[email protected]deba0ff2010-11-03 05:30:14338// As special cases, we can assume that LOG_IS_ON(ERROR_REPORT) and
339// LOG_IS_ON(FATAL) always hold. Also, LOG_IS_ON(DFATAL) always holds
340// in debug mode. In particular, CHECK()s will always fire if they
341// fail.
[email protected]521b0c42010-10-01 23:02:36342#define LOG_IS_ON(severity) \
343 ((::logging::LOG_ ## severity) >= ::logging::GetMinLogLevel())
344
345// We can't do any caching tricks with VLOG_IS_ON() like the
346// google-glog version since it requires GCC extensions. This means
347// that using the v-logging functions in conjunction with --vmodule
348// may be slow.
349#define VLOG_IS_ON(verboselevel) \
350 ((verboselevel) <= ::logging::GetVlogLevel(__FILE__))
351
352// Helper macro which avoids evaluating the arguments to a stream if
353// the condition doesn't hold.
354#define LAZY_STREAM(stream, condition) \
355 !(condition) ? (void) 0 : ::logging::LogMessageVoidify() & (stream)
initial.commitd7cae122008-07-26 21:49:38356
357// We use the preprocessor's merging operator, "##", so that, e.g.,
358// LOG(INFO) becomes the token COMPACT_GOOGLE_LOG_INFO. There's some funny
359// subtle difference between ostream member streaming functions (e.g.,
360// ostream::operator<<(int) and ostream non-member streaming functions
361// (e.g., ::operator<<(ostream&, string&): it turns out that it's
362// impossible to stream something like a string directly to an unnamed
363// ostream. We employ a neat hack by calling the stream() member
364// function of LogMessage which seems to avoid the problem.
[email protected]521b0c42010-10-01 23:02:36365#define LOG_STREAM(severity) COMPACT_GOOGLE_LOG_ ## severity.stream()
initial.commitd7cae122008-07-26 21:49:38366
[email protected]521b0c42010-10-01 23:02:36367#define LOG(severity) LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity))
368#define LOG_IF(severity, condition) \
369 LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity) && (condition))
370
initial.commitd7cae122008-07-26 21:49:38371#define SYSLOG(severity) LOG(severity)
[email protected]521b0c42010-10-01 23:02:36372#define SYSLOG_IF(severity, condition) LOG_IF(severity, condition)
373
[email protected]162ac0f2010-11-04 15:50:49374// The VLOG macros log with negative verbosities.
375#define VLOG_STREAM(verbose_level) \
376 logging::LogMessage(__FILE__, __LINE__, -verbose_level).stream()
377
378#define VLOG(verbose_level) \
379 LAZY_STREAM(VLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level))
380
381#define VLOG_IF(verbose_level, condition) \
382 LAZY_STREAM(VLOG_STREAM(verbose_level), \
383 VLOG_IS_ON(verbose_level) && (condition))
[email protected]99b7c57f2010-09-29 19:26:36384
385// TODO(akalin): Add more VLOG variants, e.g. VPLOG.
initial.commitd7cae122008-07-26 21:49:38386
initial.commitd7cae122008-07-26 21:49:38387#define LOG_ASSERT(condition) \
388 LOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". "
389#define SYSLOG_ASSERT(condition) \
390 SYSLOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". "
391
[email protected]d8617a62009-10-09 23:52:20392#if defined(OS_WIN)
[email protected]521b0c42010-10-01 23:02:36393#define LOG_GETLASTERROR_STREAM(severity) \
[email protected]d8617a62009-10-09 23:52:20394 COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \
395 ::logging::GetLastSystemErrorCode()).stream()
[email protected]521b0c42010-10-01 23:02:36396#define LOG_GETLASTERROR(severity) \
397 LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity), LOG_IS_ON(severity))
398#define LOG_GETLASTERROR_MODULE_STREAM(severity, module) \
[email protected]d8617a62009-10-09 23:52:20399 COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \
400 ::logging::GetLastSystemErrorCode(), module).stream()
[email protected]521b0c42010-10-01 23:02:36401#define LOG_GETLASTERROR_MODULE(severity, module) \
402 LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity, module), \
403 LOG_IS_ON(severity))
404// PLOG_STREAM is used by PLOG, which is the usual error logging macro
405// for each platform.
406#define PLOG_STREAM(severity) LOG_GETLASTERROR_STREAM(severity)
[email protected]d8617a62009-10-09 23:52:20407#elif defined(OS_POSIX)
[email protected]521b0c42010-10-01 23:02:36408#define LOG_ERRNO_STREAM(severity) \
[email protected]d8617a62009-10-09 23:52:20409 COMPACT_GOOGLE_LOG_EX_ ## severity(ErrnoLogMessage, \
410 ::logging::GetLastSystemErrorCode()).stream()
[email protected]521b0c42010-10-01 23:02:36411#define LOG_ERRNO(severity) \
412 LAZY_STREAM(LOG_ERRNO_STREAM(severity), LOG_IS_ON(severity))
413// PLOG_STREAM is used by PLOG, which is the usual error logging macro
414// for each platform.
415#define PLOG_STREAM(severity) LOG_ERRNO_STREAM(severity)
[email protected]d8617a62009-10-09 23:52:20416// TODO(tschmelcher): Should we add OSStatus logging for Mac?
417#endif
418
[email protected]521b0c42010-10-01 23:02:36419#define PLOG(severity) \
420 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity))
421
[email protected]d8617a62009-10-09 23:52:20422#define PLOG_IF(severity, condition) \
[email protected]521b0c42010-10-01 23:02:36423 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity) && (condition))
[email protected]d8617a62009-10-09 23:52:20424
initial.commitd7cae122008-07-26 21:49:38425// CHECK dies with a fatal error if condition is not true. It is *not*
426// controlled by NDEBUG, so the check will be executed regardless of
427// compilation mode.
[email protected]521b0c42010-10-01 23:02:36428//
429// We make sure CHECK et al. always evaluates their arguments, as
430// doing CHECK(FunctionWithSideEffect()) is a common idiom.
[email protected]521b0c42010-10-01 23:02:36431#define CHECK(condition) \
432 LAZY_STREAM(LOG_STREAM(FATAL), !(condition)) \
433 << "Check failed: " #condition ". "
initial.commitd7cae122008-07-26 21:49:38434
[email protected]d8617a62009-10-09 23:52:20435#define PCHECK(condition) \
[email protected]521b0c42010-10-01 23:02:36436 LAZY_STREAM(PLOG_STREAM(FATAL), !(condition)) \
437 << "Check failed: " #condition ". "
[email protected]d8617a62009-10-09 23:52:20438
initial.commitd7cae122008-07-26 21:49:38439// Build the error message string. This is separate from the "Impl"
440// function template because it is not performance critical and so can
[email protected]9c7132e2011-02-08 07:39:08441// be out of line, while the "Impl" code should be inline. Caller
442// takes ownership of the returned string.
initial.commitd7cae122008-07-26 21:49:38443template<class t1, class t2>
444std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) {
445 std::ostringstream ss;
446 ss << names << " (" << v1 << " vs. " << v2 << ")";
447 std::string* msg = new std::string(ss.str());
448 return msg;
449}
450
[email protected]6d445d32010-09-30 19:10:03451// MSVC doesn't like complex extern templates and DLLs.
452#if !defined(COMPILER_MSVC)
453// Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated
454// in logging.cc.
455extern template std::string* MakeCheckOpString<int, int>(
456 const int&, const int&, const char* names);
457extern template std::string* MakeCheckOpString<unsigned long, unsigned long>(
458 const unsigned long&, const unsigned long&, const char* names);
459extern template std::string* MakeCheckOpString<unsigned long, unsigned int>(
460 const unsigned long&, const unsigned int&, const char* names);
461extern template std::string* MakeCheckOpString<unsigned int, unsigned long>(
462 const unsigned int&, const unsigned long&, const char* names);
463extern template std::string* MakeCheckOpString<std::string, std::string>(
464 const std::string&, const std::string&, const char* name);
465#endif
initial.commitd7cae122008-07-26 21:49:38466
[email protected]e150c0382010-03-02 00:41:12467// Helper macro for binary operators.
468// Don't use this macro directly in your code, use CHECK_EQ et al below.
[email protected]521b0c42010-10-01 23:02:36469//
470// TODO(akalin): Rewrite this so that constructs like if (...)
471// CHECK_EQ(...) else { ... } work properly.
472#define CHECK_OP(name, op, val1, val2) \
[email protected]9c7132e2011-02-08 07:39:08473 if (std::string* _result = \
[email protected]521b0c42010-10-01 23:02:36474 logging::Check##name##Impl((val1), (val2), \
475 #val1 " " #op " " #val2)) \
[email protected]8b782102010-09-30 22:38:30476 logging::LogMessage(__FILE__, __LINE__, _result).stream()
[email protected]e150c0382010-03-02 00:41:12477
[email protected]71512602010-11-01 22:19:56478// Helper functions for CHECK_OP macro.
479// The (int, int) specialization works around the issue that the compiler
480// will not instantiate the template version of the function on values of
481// unnamed enum type - see comment below.
482#define DEFINE_CHECK_OP_IMPL(name, op) \
483 template <class t1, class t2> \
484 inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \
485 const char* names) { \
486 if (v1 op v2) return NULL; \
487 else return MakeCheckOpString(v1, v2, names); \
488 } \
489 inline std::string* Check##name##Impl(int v1, int v2, const char* names) { \
490 if (v1 op v2) return NULL; \
491 else return MakeCheckOpString(v1, v2, names); \
492 }
493DEFINE_CHECK_OP_IMPL(EQ, ==)
494DEFINE_CHECK_OP_IMPL(NE, !=)
495DEFINE_CHECK_OP_IMPL(LE, <=)
496DEFINE_CHECK_OP_IMPL(LT, < )
497DEFINE_CHECK_OP_IMPL(GE, >=)
498DEFINE_CHECK_OP_IMPL(GT, > )
499#undef DEFINE_CHECK_OP_IMPL
[email protected]e150c0382010-03-02 00:41:12500
501#define CHECK_EQ(val1, val2) CHECK_OP(EQ, ==, val1, val2)
502#define CHECK_NE(val1, val2) CHECK_OP(NE, !=, val1, val2)
503#define CHECK_LE(val1, val2) CHECK_OP(LE, <=, val1, val2)
504#define CHECK_LT(val1, val2) CHECK_OP(LT, < , val1, val2)
505#define CHECK_GE(val1, val2) CHECK_OP(GE, >=, val1, val2)
506#define CHECK_GT(val1, val2) CHECK_OP(GT, > , val1, val2)
507
[email protected]e3cca332009-08-20 01:20:29508// http://crbug.com/16512 is open for a real fix for this. For now, Windows
509// uses OFFICIAL_BUILD and other platforms use the branding flag when NDEBUG is
510// defined.
511#if ( defined(OS_WIN) && defined(OFFICIAL_BUILD)) || \
512 (!defined(OS_WIN) && defined(NDEBUG) && defined(GOOGLE_CHROME_BUILD))
[email protected]521b0c42010-10-01 23:02:36513// Used by unit tests.
514#define LOGGING_IS_OFFICIAL_BUILD
515
[email protected]e3cca332009-08-20 01:20:29516// In order to have optimized code for official builds, remove DLOGs and
517// DCHECKs.
[email protected]d15e56c2010-09-30 21:12:33518#define ENABLE_DLOG 0
519#define ENABLE_DCHECK 0
520
521#elif defined(NDEBUG)
522// Otherwise, if we're a release build, remove DLOGs but not DCHECKs
523// (since those can still be turned on via a command-line flag).
524#define ENABLE_DLOG 0
525#define ENABLE_DCHECK 1
526
527#else
528// Otherwise, we're a debug build so enable DLOGs and DCHECKs.
529#define ENABLE_DLOG 1
530#define ENABLE_DCHECK 1
[email protected]e3cca332009-08-20 01:20:29531#endif
532
[email protected]d15e56c2010-09-30 21:12:33533// Definitions for DLOG et al.
534
[email protected]d926c202010-10-01 02:58:24535#if ENABLE_DLOG
536
[email protected]5e987802010-11-01 19:49:22537#define DLOG_IS_ON(severity) LOG_IS_ON(severity)
[email protected]d926c202010-10-01 02:58:24538#define DLOG_IF(severity, condition) LOG_IF(severity, condition)
539#define DLOG_ASSERT(condition) LOG_ASSERT(condition)
[email protected]d926c202010-10-01 02:58:24540#define DPLOG_IF(severity, condition) PLOG_IF(severity, condition)
[email protected]521b0c42010-10-01 23:02:36541#define DVLOG_IF(verboselevel, condition) VLOG_IF(verboselevel, condition)
[email protected]d926c202010-10-01 02:58:24542
543#else // ENABLE_DLOG
544
[email protected]521b0c42010-10-01 23:02:36545// If ENABLE_DLOG is off, we want to avoid emitting any references to
546// |condition| (which may reference a variable defined only if NDEBUG
547// is not defined). Contrast this with DCHECK et al., which has
548// different behavior.
[email protected]d926c202010-10-01 02:58:24549
[email protected]521b0c42010-10-01 23:02:36550#define DLOG_EAT_STREAM_PARAMETERS \
551 true ? (void) 0 : ::logging::LogMessageVoidify() & LOG_STREAM(FATAL)
[email protected]d926c202010-10-01 02:58:24552
[email protected]5e987802010-11-01 19:49:22553#define DLOG_IS_ON(severity) false
[email protected]521b0c42010-10-01 23:02:36554#define DLOG_IF(severity, condition) DLOG_EAT_STREAM_PARAMETERS
555#define DLOG_ASSERT(condition) DLOG_EAT_STREAM_PARAMETERS
556#define DPLOG_IF(severity, condition) DLOG_EAT_STREAM_PARAMETERS
557#define DVLOG_IF(verboselevel, condition) DLOG_EAT_STREAM_PARAMETERS
[email protected]d926c202010-10-01 02:58:24558
559#endif // ENABLE_DLOG
560
[email protected]d15e56c2010-09-30 21:12:33561// DEBUG_MODE is for uses like
562// if (DEBUG_MODE) foo.CheckThatFoo();
563// instead of
564// #ifndef NDEBUG
565// foo.CheckThatFoo();
566// #endif
567//
568// We tie its state to ENABLE_DLOG.
569enum { DEBUG_MODE = ENABLE_DLOG };
570
571#undef ENABLE_DLOG
572
[email protected]521b0c42010-10-01 23:02:36573#define DLOG(severity) \
574 LAZY_STREAM(LOG_STREAM(severity), DLOG_IS_ON(severity))
575
576#if defined(OS_WIN)
577#define DLOG_GETLASTERROR(severity) \
578 LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity), DLOG_IS_ON(severity))
579#define DLOG_GETLASTERROR_MODULE(severity, module) \
580 LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity, module), \
581 DLOG_IS_ON(severity))
582#elif defined(OS_POSIX)
583#define DLOG_ERRNO(severity) \
584 LAZY_STREAM(LOG_ERRNO_STREAM(severity), DLOG_IS_ON(severity))
585#endif
586
587#define DPLOG(severity) \
588 LAZY_STREAM(PLOG_STREAM(severity), DLOG_IS_ON(severity))
589
590#define DVLOG(verboselevel) DLOG_IF(INFO, VLOG_IS_ON(verboselevel))
591
592// Definitions for DCHECK et al.
[email protected]d926c202010-10-01 02:58:24593
[email protected]d15e56c2010-09-30 21:12:33594#if ENABLE_DCHECK
[email protected]e3cca332009-08-20 01:20:29595
[email protected]521b0c42010-10-01 23:02:36596#if defined(NDEBUG)
[email protected]d926c202010-10-01 02:58:24597
[email protected]deba0ff2010-11-03 05:30:14598#define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \
599 COMPACT_GOOGLE_LOG_EX_ERROR_REPORT(ClassName , ##__VA_ARGS__)
600#define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_ERROR_REPORT
[email protected]521b0c42010-10-01 23:02:36601const LogSeverity LOG_DCHECK = LOG_ERROR_REPORT;
[email protected]7c10f7552011-01-11 01:03:36602extern DcheckState g_dcheck_state;
603#define DCHECK_IS_ON() \
604 ((::logging::g_dcheck_state == \
605 ::logging::ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS) && \
606 LOG_IS_ON(DCHECK))
[email protected]d926c202010-10-01 02:58:24607
[email protected]521b0c42010-10-01 23:02:36608#else // defined(NDEBUG)
609
[email protected]5e987802010-11-01 19:49:22610// On a regular debug build, we want to have DCHECKs enabled.
[email protected]deba0ff2010-11-03 05:30:14611#define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \
612 COMPACT_GOOGLE_LOG_EX_FATAL(ClassName , ##__VA_ARGS__)
613#define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_FATAL
[email protected]521b0c42010-10-01 23:02:36614const LogSeverity LOG_DCHECK = LOG_FATAL;
[email protected]deba0ff2010-11-03 05:30:14615#define DCHECK_IS_ON() true
[email protected]521b0c42010-10-01 23:02:36616
617#endif // defined(NDEBUG)
618
619#else // ENABLE_DCHECK
620
[email protected]deba0ff2010-11-03 05:30:14621// These are just dummy values since DCHECK_IS_ON() is always false in
622// this case.
623#define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \
624 COMPACT_GOOGLE_LOG_EX_INFO(ClassName , ##__VA_ARGS__)
625#define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_INFO
626const LogSeverity LOG_DCHECK = LOG_INFO;
[email protected]5e987802010-11-01 19:49:22627#define DCHECK_IS_ON() false
[email protected]521b0c42010-10-01 23:02:36628
629#endif // ENABLE_DCHECK
[email protected]5e987802010-11-01 19:49:22630#undef ENABLE_DCHECK
[email protected]521b0c42010-10-01 23:02:36631
[email protected]deba0ff2010-11-03 05:30:14632// DCHECK et al. make sure to reference |condition| regardless of
[email protected]521b0c42010-10-01 23:02:36633// whether DCHECKs are enabled; this is so that we don't get unused
634// variable warnings if the only use of a variable is in a DCHECK.
635// This behavior is different from DLOG_IF et al.
636
[email protected]deba0ff2010-11-03 05:30:14637#define DCHECK(condition) \
638 LAZY_STREAM(LOG_STREAM(DCHECK), DCHECK_IS_ON() && !(condition)) \
[email protected]521b0c42010-10-01 23:02:36639 << "Check failed: " #condition ". "
640
[email protected]deba0ff2010-11-03 05:30:14641#define DPCHECK(condition) \
642 LAZY_STREAM(PLOG_STREAM(DCHECK), DCHECK_IS_ON() && !(condition)) \
[email protected]521b0c42010-10-01 23:02:36643 << "Check failed: " #condition ". "
[email protected]d926c202010-10-01 02:58:24644
645// Helper macro for binary operators.
646// Don't use this macro directly in your code, use DCHECK_EQ et al below.
[email protected]521b0c42010-10-01 23:02:36647#define DCHECK_OP(name, op, val1, val2) \
[email protected]5e987802010-11-01 19:49:22648 if (DCHECK_IS_ON()) \
[email protected]9c7132e2011-02-08 07:39:08649 if (std::string* _result = \
[email protected]521b0c42010-10-01 23:02:36650 logging::Check##name##Impl((val1), (val2), \
651 #val1 " " #op " " #val2)) \
652 logging::LogMessage( \
653 __FILE__, __LINE__, ::logging::LOG_DCHECK, \
654 _result).stream()
initial.commitd7cae122008-07-26 21:49:38655
[email protected]deba0ff2010-11-03 05:30:14656// Equality/Inequality checks - compare two values, and log a
657// LOG_DCHECK message including the two values when the result is not
658// as expected. The values must have operator<<(ostream, ...)
659// defined.
initial.commitd7cae122008-07-26 21:49:38660//
661// You may append to the error message like so:
662// DCHECK_NE(1, 2) << ": The world must be ending!";
663//
664// We are very careful to ensure that each argument is evaluated exactly
665// once, and that anything which is legal to pass as a function argument is
666// legal here. In particular, the arguments may be temporary expressions
667// which will end up being destroyed at the end of the apparent statement,
668// for example:
669// DCHECK_EQ(string("abc")[1], 'b');
670//
671// WARNING: These may not compile correctly if one of the arguments is a pointer
672// and the other is NULL. To work around this, simply static_cast NULL to the
673// type of the desired pointer.
674
675#define DCHECK_EQ(val1, val2) DCHECK_OP(EQ, ==, val1, val2)
676#define DCHECK_NE(val1, val2) DCHECK_OP(NE, !=, val1, val2)
677#define DCHECK_LE(val1, val2) DCHECK_OP(LE, <=, val1, val2)
678#define DCHECK_LT(val1, val2) DCHECK_OP(LT, < , val1, val2)
679#define DCHECK_GE(val1, val2) DCHECK_OP(GE, >=, val1, val2)
680#define DCHECK_GT(val1, val2) DCHECK_OP(GT, > , val1, val2)
681
initial.commitd7cae122008-07-26 21:49:38682#define NOTREACHED() DCHECK(false)
683
684// Redefine the standard assert to use our nice log files
685#undef assert
686#define assert(x) DLOG_ASSERT(x)
687
688// This class more or less represents a particular log message. You
689// create an instance of LogMessage and then stream stuff to it.
690// When you finish streaming to it, ~LogMessage is called and the
691// full message gets streamed to the appropriate destination.
692//
693// You shouldn't actually use LogMessage's constructor to log things,
694// though. You should use the LOG() macro (and variants thereof)
695// above.
696class LogMessage {
697 public:
698 LogMessage(const char* file, int line, LogSeverity severity, int ctr);
699
700 // Two special constructors that generate reduced amounts of code at
701 // LOG call sites for common cases.
702 //
703 // Used for LOG(INFO): Implied are:
704 // severity = LOG_INFO, ctr = 0
705 //
706 // Using this constructor instead of the more complex constructor above
707 // saves a couple of bytes per call site.
708 LogMessage(const char* file, int line);
709
710 // Used for LOG(severity) where severity != INFO. Implied
711 // are: ctr = 0
712 //
713 // Using this constructor instead of the more complex constructor above
714 // saves a couple of bytes per call site.
715 LogMessage(const char* file, int line, LogSeverity severity);
716
[email protected]9c7132e2011-02-08 07:39:08717 // A special constructor used for check failures. Takes ownership
718 // of the given string.
initial.commitd7cae122008-07-26 21:49:38719 // Implied severity = LOG_FATAL
[email protected]9c7132e2011-02-08 07:39:08720 LogMessage(const char* file, int line, std::string* result);
initial.commitd7cae122008-07-26 21:49:38721
[email protected]fb62a532009-02-12 01:19:05722 // A special constructor used for check failures, with the option to
[email protected]9c7132e2011-02-08 07:39:08723 // specify severity. Takes ownership of the given string.
[email protected]fb62a532009-02-12 01:19:05724 LogMessage(const char* file, int line, LogSeverity severity,
[email protected]9c7132e2011-02-08 07:39:08725 std::string* result);
[email protected]fb62a532009-02-12 01:19:05726
initial.commitd7cae122008-07-26 21:49:38727 ~LogMessage();
728
729 std::ostream& stream() { return stream_; }
730
731 private:
732 void Init(const char* file, int line);
733
734 LogSeverity severity_;
735 std::ostringstream stream_;
[email protected]c88873922008-07-30 13:02:03736 size_t message_start_; // Offset of the start of the message (past prefix
737 // info).
[email protected]162ac0f2010-11-04 15:50:49738 // The file and line information passed in to the constructor.
739 const char* file_;
740 const int line_;
741
[email protected]3f85caa2009-04-14 16:52:11742#if defined(OS_WIN)
743 // Stores the current value of GetLastError in the constructor and restores
744 // it in the destructor by calling SetLastError.
745 // This is useful since the LogMessage class uses a lot of Win32 calls
746 // that will lose the value of GLE and the code that called the log function
747 // will have lost the thread error value when the log call returns.
748 class SaveLastError {
749 public:
750 SaveLastError();
751 ~SaveLastError();
752
753 unsigned long get_error() const { return last_error_; }
754
755 protected:
756 unsigned long last_error_;
757 };
758
759 SaveLastError last_error_;
760#endif
initial.commitd7cae122008-07-26 21:49:38761
[email protected]39be4242008-08-07 18:31:40762 DISALLOW_COPY_AND_ASSIGN(LogMessage);
initial.commitd7cae122008-07-26 21:49:38763};
764
765// A non-macro interface to the log facility; (useful
766// when the logging level is not a compile-time constant).
767inline void LogAtLevel(int const log_level, std::string const &msg) {
768 LogMessage(__FILE__, __LINE__, log_level).stream() << msg;
769}
770
771// This class is used to explicitly ignore values in the conditional
772// logging macros. This avoids compiler warnings like "value computed
773// is not used" and "statement has no effect".
774class LogMessageVoidify {
775 public:
776 LogMessageVoidify() { }
777 // This has to be an operator with a precedence lower than << but
778 // higher than ?:
779 void operator&(std::ostream&) { }
780};
781
[email protected]d8617a62009-10-09 23:52:20782#if defined(OS_WIN)
783typedef unsigned long SystemErrorCode;
784#elif defined(OS_POSIX)
785typedef int SystemErrorCode;
786#endif
787
788// Alias for ::GetLastError() on Windows and errno on POSIX. Avoids having to
789// pull in windows.h just for GetLastError() and DWORD.
790SystemErrorCode GetLastSystemErrorCode();
791
792#if defined(OS_WIN)
793// Appends a formatted system message of the GetLastError() type.
794class Win32ErrorLogMessage {
795 public:
796 Win32ErrorLogMessage(const char* file,
797 int line,
798 LogSeverity severity,
799 SystemErrorCode err,
800 const char* module);
801
802 Win32ErrorLogMessage(const char* file,
803 int line,
804 LogSeverity severity,
805 SystemErrorCode err);
806
[email protected]d8617a62009-10-09 23:52:20807 // Appends the error message before destructing the encapsulated class.
808 ~Win32ErrorLogMessage();
809
[email protected]a502bbe72011-01-07 18:06:45810 std::ostream& stream() { return log_message_.stream(); }
811
[email protected]d8617a62009-10-09 23:52:20812 private:
813 SystemErrorCode err_;
814 // Optional name of the module defining the error.
815 const char* module_;
816 LogMessage log_message_;
817
818 DISALLOW_COPY_AND_ASSIGN(Win32ErrorLogMessage);
819};
820#elif defined(OS_POSIX)
821// Appends a formatted system message of the errno type
822class ErrnoLogMessage {
823 public:
824 ErrnoLogMessage(const char* file,
825 int line,
826 LogSeverity severity,
827 SystemErrorCode err);
828
[email protected]d8617a62009-10-09 23:52:20829 // Appends the error message before destructing the encapsulated class.
830 ~ErrnoLogMessage();
831
[email protected]a502bbe72011-01-07 18:06:45832 std::ostream& stream() { return log_message_.stream(); }
833
[email protected]d8617a62009-10-09 23:52:20834 private:
835 SystemErrorCode err_;
836 LogMessage log_message_;
837
838 DISALLOW_COPY_AND_ASSIGN(ErrnoLogMessage);
839};
840#endif // OS_WIN
841
initial.commitd7cae122008-07-26 21:49:38842// Closes the log file explicitly if open.
843// NOTE: Since the log file is opened as necessary by the action of logging
844// statements, there's no guarantee that it will stay closed
845// after this call.
846void CloseLogFile();
847
[email protected]e36ddc82009-12-08 04:22:50848// Async signal safe logging mechanism.
849void RawLog(int level, const char* message);
850
851#define RAW_LOG(level, message) logging::RawLog(logging::LOG_ ## level, message)
852
853#define RAW_CHECK(condition) \
854 do { \
855 if (!(condition)) \
856 logging::RawLog(logging::LOG_FATAL, "Check failed: " #condition "\n"); \
857 } while (0)
858
[email protected]39be4242008-08-07 18:31:40859} // namespace logging
initial.commitd7cae122008-07-26 21:49:38860
[email protected]46ce5b562010-06-16 18:39:53861// These functions are provided as a convenience for logging, which is where we
862// use streams (it is against Google style to use streams in other places). It
863// is designed to allow you to emit non-ASCII Unicode strings to the log file,
864// which is normally ASCII. It is relatively slow, so try not to use it for
865// common cases. Non-ASCII characters will be converted to UTF-8 by these
866// operators.
867std::ostream& operator<<(std::ostream& out, const wchar_t* wstr);
868inline std::ostream& operator<<(std::ostream& out, const std::wstring& wstr) {
869 return out << wstr.c_str();
870}
871
[email protected]0dfc81b2008-08-25 03:44:40872// The NOTIMPLEMENTED() macro annotates codepaths which have
873// not been implemented yet.
874//
875// The implementation of this macro is controlled by NOTIMPLEMENTED_POLICY:
876// 0 -- Do nothing (stripped by compiler)
877// 1 -- Warn at compile time
878// 2 -- Fail at compile time
879// 3 -- Fail at runtime (DCHECK)
880// 4 -- [default] LOG(ERROR) at runtime
881// 5 -- LOG(ERROR) at runtime, only once per call-site
882
883#ifndef NOTIMPLEMENTED_POLICY
884// Select default policy: LOG(ERROR)
885#define NOTIMPLEMENTED_POLICY 4
886#endif
887
[email protected]f6cda752008-10-30 23:54:26888#if defined(COMPILER_GCC)
889// On Linux, with GCC, we can use __PRETTY_FUNCTION__ to get the demangled name
890// of the current function in the NOTIMPLEMENTED message.
891#define NOTIMPLEMENTED_MSG "Not implemented reached in " << __PRETTY_FUNCTION__
892#else
893#define NOTIMPLEMENTED_MSG "NOT IMPLEMENTED"
894#endif
895
[email protected]0dfc81b2008-08-25 03:44:40896#if NOTIMPLEMENTED_POLICY == 0
897#define NOTIMPLEMENTED() ;
898#elif NOTIMPLEMENTED_POLICY == 1
899// TODO, figure out how to generate a warning
900#define NOTIMPLEMENTED() COMPILE_ASSERT(false, NOT_IMPLEMENTED)
901#elif NOTIMPLEMENTED_POLICY == 2
902#define NOTIMPLEMENTED() COMPILE_ASSERT(false, NOT_IMPLEMENTED)
903#elif NOTIMPLEMENTED_POLICY == 3
904#define NOTIMPLEMENTED() NOTREACHED()
905#elif NOTIMPLEMENTED_POLICY == 4
[email protected]f6cda752008-10-30 23:54:26906#define NOTIMPLEMENTED() LOG(ERROR) << NOTIMPLEMENTED_MSG
[email protected]0dfc81b2008-08-25 03:44:40907#elif NOTIMPLEMENTED_POLICY == 5
908#define NOTIMPLEMENTED() do {\
909 static int count = 0;\
[email protected]f6cda752008-10-30 23:54:26910 LOG_IF(ERROR, 0 == count++) << NOTIMPLEMENTED_MSG;\
[email protected]0dfc81b2008-08-25 03:44:40911} while(0)
912#endif
913
[email protected]39a749c2011-01-28 02:40:46914namespace base {
915
916class StringPiece;
917
918// allow StringPiece to be logged (needed for unit testing).
919extern std::ostream& operator<<(std::ostream& o, const StringPiece& piece);
920
921} // namespace base
922
[email protected]39be4242008-08-07 18:31:40923#endif // BASE_LOGGING_H_