blob: 6d05ceb6d0758801123ad7f6cf88d7b6585338e1 [file] [log] [blame]
[email protected]a502bbe72011-01-07 18:06:451// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
[email protected]39be4242008-08-07 18:31:405#ifndef BASE_LOGGING_H_
6#define BASE_LOGGING_H_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
initial.commitd7cae122008-07-26 21:49:388
[email protected]e7972d12011-06-18 11:53:149#include <cassert>
initial.commitd7cae122008-07-26 21:49:3810#include <string>
11#include <cstring>
12#include <sstream>
13
[email protected]0bea7252011-08-05 15:34:0014#include "base/base_export.h"
initial.commitd7cae122008-07-26 21:49:3815#include "base/basictypes.h"
[email protected]90509cb2011-03-25 18:46:3816#include "build/build_config.h"
initial.commitd7cae122008-07-26 21:49:3817
18//
19// Optional message capabilities
20// -----------------------------
21// Assertion failed messages and fatal errors are displayed in a dialog box
22// before the application exits. However, running this UI creates a message
23// loop, which causes application messages to be processed and potentially
24// dispatched to existing application windows. Since the application is in a
25// bad state when this assertion dialog is displayed, these messages may not
26// get processed and hang the dialog, or the application might go crazy.
27//
28// Therefore, it can be beneficial to display the error dialog in a separate
29// process from the main application. When the logging system needs to display
30// a fatal error dialog box, it will look for a program called
31// "DebugMessage.exe" in the same directory as the application executable. It
32// will run this application with the message as the command line, and will
33// not include the name of the application as is traditional for easier
34// parsing.
35//
36// The code for DebugMessage.exe is only one line. In WinMain, do:
37// MessageBox(NULL, GetCommandLineW(), L"Fatal Error", 0);
38//
39// If DebugMessage.exe is not found, the logging code will use a normal
40// MessageBox, potentially causing the problems discussed above.
41
42
43// Instructions
44// ------------
45//
46// Make a bunch of macros for logging. The way to log things is to stream
47// things to LOG(<a particular severity level>). E.g.,
48//
49// LOG(INFO) << "Found " << num_cookies << " cookies";
50//
51// You can also do conditional logging:
52//
53// LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
54//
55// The above will cause log messages to be output on the 1st, 11th, 21st, ...
56// times it is executed. Note that the special COUNTER value is used to
57// identify which repetition is happening.
58//
59// The CHECK(condition) macro is active in both debug and release builds and
60// effectively performs a LOG(FATAL) which terminates the process and
61// generates a crashdump unless a debugger is attached.
62//
63// There are also "debug mode" logging macros like the ones above:
64//
65// DLOG(INFO) << "Found cookies";
66//
67// DLOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
68//
69// All "debug mode" logging is compiled away to nothing for non-debug mode
70// compiles. LOG_IF and development flags also work well together
71// because the code can be compiled away sometimes.
72//
73// We also have
74//
75// LOG_ASSERT(assertion);
76// DLOG_ASSERT(assertion);
77//
78// which is syntactic sugar for {,D}LOG_IF(FATAL, assert fails) << assertion;
79//
[email protected]99b7c57f2010-09-29 19:26:3680// There are "verbose level" logging macros. They look like
81//
82// VLOG(1) << "I'm printed when you run the program with --v=1 or more";
83// VLOG(2) << "I'm printed when you run the program with --v=2 or more";
84//
85// These always log at the INFO log level (when they log at all).
86// The verbose logging can also be turned on module-by-module. For instance,
[email protected]b0d38d4c2010-10-29 00:39:4887// --vmodule=profile=2,icon_loader=1,browser_*=3,*/chromeos/*=4 --v=0
[email protected]99b7c57f2010-09-29 19:26:3688// will cause:
89// a. VLOG(2) and lower messages to be printed from profile.{h,cc}
90// b. VLOG(1) and lower messages to be printed from icon_loader.{h,cc}
91// c. VLOG(3) and lower messages to be printed from files prefixed with
92// "browser"
[email protected]e11de722010-11-01 20:50:5593// d. VLOG(4) and lower messages to be printed from files under a
[email protected]b0d38d4c2010-10-29 00:39:4894// "chromeos" directory.
[email protected]e11de722010-11-01 20:50:5595// e. VLOG(0) and lower messages to be printed from elsewhere
[email protected]99b7c57f2010-09-29 19:26:3696//
97// The wildcarding functionality shown by (c) supports both '*' (match
[email protected]b0d38d4c2010-10-29 00:39:4898// 0 or more characters) and '?' (match any single character)
99// wildcards. Any pattern containing a forward or backward slash will
100// be tested against the whole pathname and not just the module.
101// E.g., "*/foo/bar/*=2" would change the logging level for all code
102// in source files under a "foo/bar" directory.
[email protected]99b7c57f2010-09-29 19:26:36103//
104// There's also VLOG_IS_ON(n) "verbose level" condition macro. To be used as
105//
106// if (VLOG_IS_ON(2)) {
107// // do some logging preparation and logging
108// // that can't be accomplished with just VLOG(2) << ...;
109// }
110//
111// There is also a VLOG_IF "verbose level" condition macro for sample
112// cases, when some extra computation and preparation for logs is not
113// needed.
114//
115// VLOG_IF(1, (size > 1024))
116// << "I'm printed when size is more than 1024 and when you run the "
117// "program with --v=1 or more";
118//
initial.commitd7cae122008-07-26 21:49:38119// We also override the standard 'assert' to use 'DLOG_ASSERT'.
120//
[email protected]d8617a62009-10-09 23:52:20121// Lastly, there is:
122//
123// PLOG(ERROR) << "Couldn't do foo";
124// DPLOG(ERROR) << "Couldn't do foo";
125// PLOG_IF(ERROR, cond) << "Couldn't do foo";
126// DPLOG_IF(ERROR, cond) << "Couldn't do foo";
127// PCHECK(condition) << "Couldn't do foo";
128// DPCHECK(condition) << "Couldn't do foo";
129//
130// which append the last system error to the message in string form (taken from
131// GetLastError() on Windows and errno on POSIX).
132//
initial.commitd7cae122008-07-26 21:49:38133// The supported severity levels for macros that allow you to specify one
[email protected]fb62a532009-02-12 01:19:05134// are (in increasing order of severity) INFO, WARNING, ERROR, ERROR_REPORT,
135// and FATAL.
initial.commitd7cae122008-07-26 21:49:38136//
137// Very important: logging a message at the FATAL severity level causes
138// the program to terminate (after the message is logged).
[email protected]fb62a532009-02-12 01:19:05139//
140// Note the special severity of ERROR_REPORT only available/relevant in normal
141// mode, which displays error dialog without terminating the program. There is
142// no error dialog for severity ERROR or below in normal mode.
143//
144// There is also the special severity of DFATAL, which logs FATAL in
[email protected]081bd4c2010-06-24 01:01:04145// debug mode, ERROR in normal mode.
initial.commitd7cae122008-07-26 21:49:38146
147namespace logging {
148
149// Where to record logging output? A flat file and/or system debug log via
[email protected]88aa41e82008-11-18 00:59:04150// OutputDebugString. Defaults on Windows to LOG_ONLY_TO_FILE, and on
151// POSIX to LOG_ONLY_TO_SYSTEM_DEBUG_LOG (aka stderr).
initial.commitd7cae122008-07-26 21:49:38152enum LoggingDestination { LOG_NONE,
153 LOG_ONLY_TO_FILE,
154 LOG_ONLY_TO_SYSTEM_DEBUG_LOG,
155 LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG };
156
157// Indicates that the log file should be locked when being written to.
158// Often, there is no locking, which is fine for a single threaded program.
159// If logging is being done from multiple threads or there can be more than
160// one process doing the logging, the file should be locked during writes to
161// make each log outut atomic. Other writers will block.
162//
163// All processes writing to the log file must have their locking set for it to
164// work properly. Defaults to DONT_LOCK_LOG_FILE.
165enum LogLockingState { LOCK_LOG_FILE, DONT_LOCK_LOG_FILE };
166
167// On startup, should we delete or append to an existing log file (if any)?
168// Defaults to APPEND_TO_OLD_LOG_FILE.
169enum OldFileDeletionState { DELETE_OLD_LOG_FILE, APPEND_TO_OLD_LOG_FILE };
170
[email protected]7c10f7552011-01-11 01:03:36171enum DcheckState {
172 DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS,
173 ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS
174};
175
[email protected]ff3d0c32010-08-23 19:57:46176// TODO(avi): do we want to do a unification of character types here?
177#if defined(OS_WIN)
178typedef wchar_t PathChar;
179#else
180typedef char PathChar;
181#endif
182
183// Define different names for the BaseInitLoggingImpl() function depending on
184// whether NDEBUG is defined or not so that we'll fail to link if someone tries
185// to compile logging.cc with NDEBUG but includes logging.h without defining it,
186// or vice versa.
187#if NDEBUG
188#define BaseInitLoggingImpl BaseInitLoggingImpl_built_with_NDEBUG
189#else
190#define BaseInitLoggingImpl BaseInitLoggingImpl_built_without_NDEBUG
191#endif
192
193// Implementation of the InitLogging() method declared below. We use a
194// more-specific name so we can #define it above without affecting other code
195// that has named stuff "InitLogging".
[email protected]0bea7252011-08-05 15:34:00196BASE_EXPORT bool BaseInitLoggingImpl(const PathChar* log_file,
197 LoggingDestination logging_dest,
198 LogLockingState lock_log,
199 OldFileDeletionState delete_old,
200 DcheckState dcheck_state);
[email protected]ff3d0c32010-08-23 19:57:46201
initial.commitd7cae122008-07-26 21:49:38202// Sets the log file name and other global logging state. Calling this function
203// is recommended, and is normally done at the beginning of application init.
204// If you don't call it, all the flags will be initialized to their default
205// values, and there is a race condition that may leak a critical section
206// object if two threads try to do the first log at the same time.
207// See the definition of the enums above for descriptions and default values.
208//
209// The default log file is initialized to "debug.log" in the application
210// directory. You probably don't want this, especially since the program
211// directory may not be writable on an enduser's system.
[email protected]c7d5da992010-10-28 00:20:21212inline bool InitLogging(const PathChar* log_file,
[email protected]ff3d0c32010-08-23 19:57:46213 LoggingDestination logging_dest,
214 LogLockingState lock_log,
[email protected]7c10f7552011-01-11 01:03:36215 OldFileDeletionState delete_old,
216 DcheckState dcheck_state) {
217 return BaseInitLoggingImpl(log_file, logging_dest, lock_log,
218 delete_old, dcheck_state);
[email protected]ff3d0c32010-08-23 19:57:46219}
initial.commitd7cae122008-07-26 21:49:38220
221// Sets the log level. Anything at or above this level will be written to the
222// log file/displayed to the user (if applicable). Anything below this level
[email protected]162ac0f2010-11-04 15:50:49223// will be silently ignored. The log level defaults to 0 (everything is logged
224// up to level INFO) if this function is not called.
225// Note that log messages for VLOG(x) are logged at level -x, so setting
226// the min log level to negative values enables verbose logging.
[email protected]0bea7252011-08-05 15:34:00227BASE_EXPORT void SetMinLogLevel(int level);
initial.commitd7cae122008-07-26 21:49:38228
[email protected]8a2986ca2009-04-10 19:13:42229// Gets the current log level.
[email protected]0bea7252011-08-05 15:34:00230BASE_EXPORT int GetMinLogLevel();
initial.commitd7cae122008-07-26 21:49:38231
[email protected]162ac0f2010-11-04 15:50:49232// Gets the VLOG default verbosity level.
[email protected]0bea7252011-08-05 15:34:00233BASE_EXPORT int GetVlogVerbosity();
[email protected]162ac0f2010-11-04 15:50:49234
[email protected]99b7c57f2010-09-29 19:26:36235// Gets the current vlog level for the given file (usually taken from
236// __FILE__).
[email protected]2f4e9a62010-09-29 21:25:14237
238// Note that |N| is the size *with* the null terminator.
[email protected]0bea7252011-08-05 15:34:00239BASE_EXPORT int GetVlogLevelHelper(const char* file_start, size_t N);
[email protected]2f4e9a62010-09-29 21:25:14240
[email protected]99b7c57f2010-09-29 19:26:36241template <size_t N>
242int GetVlogLevel(const char (&file)[N]) {
243 return GetVlogLevelHelper(file, N);
244}
initial.commitd7cae122008-07-26 21:49:38245
246// Sets the common items you want to be prepended to each log message.
247// process and thread IDs default to off, the timestamp defaults to on.
248// If this function is not called, logging defaults to writing the timestamp
249// only.
[email protected]0bea7252011-08-05 15:34:00250BASE_EXPORT void SetLogItems(bool enable_process_id, bool enable_thread_id,
251 bool enable_timestamp, bool enable_tickcount);
initial.commitd7cae122008-07-26 21:49:38252
[email protected]81e0a852010-08-17 00:38:12253// Sets whether or not you'd like to see fatal debug messages popped up in
254// a dialog box or not.
255// Dialogs are not shown by default.
[email protected]0bea7252011-08-05 15:34:00256BASE_EXPORT void SetShowErrorDialogs(bool enable_dialogs);
[email protected]81e0a852010-08-17 00:38:12257
initial.commitd7cae122008-07-26 21:49:38258// Sets the Log Assert Handler that will be used to notify of check failures.
[email protected]fb62a532009-02-12 01:19:05259// The default handler shows a dialog box and then terminate the process,
260// however clients can use this function to override with their own handling
261// (e.g. a silent one for Unit Tests)
initial.commitd7cae122008-07-26 21:49:38262typedef void (*LogAssertHandlerFunction)(const std::string& str);
[email protected]0bea7252011-08-05 15:34:00263BASE_EXPORT void SetLogAssertHandler(LogAssertHandlerFunction handler);
[email protected]64e5cc02010-11-03 19:20:27264
[email protected]fb62a532009-02-12 01:19:05265// Sets the Log Report Handler that will be used to notify of check failures
266// in non-debug mode. The default handler shows a dialog box and continues
267// the execution, however clients can use this function to override with their
268// own handling.
269typedef void (*LogReportHandlerFunction)(const std::string& str);
[email protected]0bea7252011-08-05 15:34:00270BASE_EXPORT void SetLogReportHandler(LogReportHandlerFunction handler);
initial.commitd7cae122008-07-26 21:49:38271
[email protected]2b07b8412009-11-25 15:26:34272// Sets the Log Message Handler that gets passed every log message before
273// it's sent to other log destinations (if any).
274// Returns true to signal that it handled the message and the message
275// should not be sent to other log destinations.
[email protected]162ac0f2010-11-04 15:50:49276typedef bool (*LogMessageHandlerFunction)(int severity,
277 const char* file, int line, size_t message_start, const std::string& str);
[email protected]0bea7252011-08-05 15:34:00278BASE_EXPORT void SetLogMessageHandler(LogMessageHandlerFunction handler);
279BASE_EXPORT LogMessageHandlerFunction GetLogMessageHandler();
[email protected]2b07b8412009-11-25 15:26:34280
initial.commitd7cae122008-07-26 21:49:38281typedef int LogSeverity;
[email protected]162ac0f2010-11-04 15:50:49282const LogSeverity LOG_VERBOSE = -1; // This is level 1 verbosity
283// Note: the log severities are used to index into the array of names,
284// see log_severity_names.
initial.commitd7cae122008-07-26 21:49:38285const LogSeverity LOG_INFO = 0;
286const LogSeverity LOG_WARNING = 1;
287const LogSeverity LOG_ERROR = 2;
[email protected]fb62a532009-02-12 01:19:05288const LogSeverity LOG_ERROR_REPORT = 3;
289const LogSeverity LOG_FATAL = 4;
290const LogSeverity LOG_NUM_SEVERITIES = 5;
initial.commitd7cae122008-07-26 21:49:38291
[email protected]521b0c42010-10-01 23:02:36292// LOG_DFATAL is LOG_FATAL in debug mode, ERROR in normal mode
initial.commitd7cae122008-07-26 21:49:38293#ifdef NDEBUG
[email protected]521b0c42010-10-01 23:02:36294const LogSeverity LOG_DFATAL = LOG_ERROR;
initial.commitd7cae122008-07-26 21:49:38295#else
[email protected]521b0c42010-10-01 23:02:36296const LogSeverity LOG_DFATAL = LOG_FATAL;
initial.commitd7cae122008-07-26 21:49:38297#endif
298
299// A few definitions of macros that don't generate much code. These are used
300// by LOG() and LOG_IF, etc. Since these are used all over our code, it's
301// better to have compact code for these operations.
[email protected]d8617a62009-10-09 23:52:20302#define COMPACT_GOOGLE_LOG_EX_INFO(ClassName, ...) \
303 logging::ClassName(__FILE__, __LINE__, logging::LOG_INFO , ##__VA_ARGS__)
304#define COMPACT_GOOGLE_LOG_EX_WARNING(ClassName, ...) \
305 logging::ClassName(__FILE__, __LINE__, logging::LOG_WARNING , ##__VA_ARGS__)
306#define COMPACT_GOOGLE_LOG_EX_ERROR(ClassName, ...) \
307 logging::ClassName(__FILE__, __LINE__, logging::LOG_ERROR , ##__VA_ARGS__)
308#define COMPACT_GOOGLE_LOG_EX_ERROR_REPORT(ClassName, ...) \
309 logging::ClassName(__FILE__, __LINE__, \
310 logging::LOG_ERROR_REPORT , ##__VA_ARGS__)
311#define COMPACT_GOOGLE_LOG_EX_FATAL(ClassName, ...) \
312 logging::ClassName(__FILE__, __LINE__, logging::LOG_FATAL , ##__VA_ARGS__)
313#define COMPACT_GOOGLE_LOG_EX_DFATAL(ClassName, ...) \
[email protected]521b0c42010-10-01 23:02:36314 logging::ClassName(__FILE__, __LINE__, logging::LOG_DFATAL , ##__VA_ARGS__)
[email protected]d8617a62009-10-09 23:52:20315
initial.commitd7cae122008-07-26 21:49:38316#define COMPACT_GOOGLE_LOG_INFO \
[email protected]d8617a62009-10-09 23:52:20317 COMPACT_GOOGLE_LOG_EX_INFO(LogMessage)
initial.commitd7cae122008-07-26 21:49:38318#define COMPACT_GOOGLE_LOG_WARNING \
[email protected]d8617a62009-10-09 23:52:20319 COMPACT_GOOGLE_LOG_EX_WARNING(LogMessage)
initial.commitd7cae122008-07-26 21:49:38320#define COMPACT_GOOGLE_LOG_ERROR \
[email protected]d8617a62009-10-09 23:52:20321 COMPACT_GOOGLE_LOG_EX_ERROR(LogMessage)
[email protected]fb62a532009-02-12 01:19:05322#define COMPACT_GOOGLE_LOG_ERROR_REPORT \
[email protected]d8617a62009-10-09 23:52:20323 COMPACT_GOOGLE_LOG_EX_ERROR_REPORT(LogMessage)
initial.commitd7cae122008-07-26 21:49:38324#define COMPACT_GOOGLE_LOG_FATAL \
[email protected]d8617a62009-10-09 23:52:20325 COMPACT_GOOGLE_LOG_EX_FATAL(LogMessage)
initial.commitd7cae122008-07-26 21:49:38326#define COMPACT_GOOGLE_LOG_DFATAL \
[email protected]d8617a62009-10-09 23:52:20327 COMPACT_GOOGLE_LOG_EX_DFATAL(LogMessage)
initial.commitd7cae122008-07-26 21:49:38328
329// wingdi.h defines ERROR to be 0. When we call LOG(ERROR), it gets
330// substituted with 0, and it expands to COMPACT_GOOGLE_LOG_0. To allow us
331// to keep using this syntax, we define this macro to do the same thing
332// as COMPACT_GOOGLE_LOG_ERROR, and also define ERROR the same way that
333// the Windows SDK does for consistency.
334#define ERROR 0
[email protected]d8617a62009-10-09 23:52:20335#define COMPACT_GOOGLE_LOG_EX_0(ClassName, ...) \
336 COMPACT_GOOGLE_LOG_EX_ERROR(ClassName , ##__VA_ARGS__)
337#define COMPACT_GOOGLE_LOG_0 COMPACT_GOOGLE_LOG_ERROR
[email protected]521b0c42010-10-01 23:02:36338// Needed for LOG_IS_ON(ERROR).
339const LogSeverity LOG_0 = LOG_ERROR;
340
[email protected]deba0ff2010-11-03 05:30:14341// As special cases, we can assume that LOG_IS_ON(ERROR_REPORT) and
342// LOG_IS_ON(FATAL) always hold. Also, LOG_IS_ON(DFATAL) always holds
343// in debug mode. In particular, CHECK()s will always fire if they
344// fail.
[email protected]521b0c42010-10-01 23:02:36345#define LOG_IS_ON(severity) \
346 ((::logging::LOG_ ## severity) >= ::logging::GetMinLogLevel())
347
348// We can't do any caching tricks with VLOG_IS_ON() like the
349// google-glog version since it requires GCC extensions. This means
350// that using the v-logging functions in conjunction with --vmodule
351// may be slow.
352#define VLOG_IS_ON(verboselevel) \
353 ((verboselevel) <= ::logging::GetVlogLevel(__FILE__))
354
355// Helper macro which avoids evaluating the arguments to a stream if
356// the condition doesn't hold.
357#define LAZY_STREAM(stream, condition) \
358 !(condition) ? (void) 0 : ::logging::LogMessageVoidify() & (stream)
initial.commitd7cae122008-07-26 21:49:38359
360// We use the preprocessor's merging operator, "##", so that, e.g.,
361// LOG(INFO) becomes the token COMPACT_GOOGLE_LOG_INFO. There's some funny
362// subtle difference between ostream member streaming functions (e.g.,
363// ostream::operator<<(int) and ostream non-member streaming functions
364// (e.g., ::operator<<(ostream&, string&): it turns out that it's
365// impossible to stream something like a string directly to an unnamed
366// ostream. We employ a neat hack by calling the stream() member
367// function of LogMessage which seems to avoid the problem.
[email protected]521b0c42010-10-01 23:02:36368#define LOG_STREAM(severity) COMPACT_GOOGLE_LOG_ ## severity.stream()
initial.commitd7cae122008-07-26 21:49:38369
[email protected]521b0c42010-10-01 23:02:36370#define LOG(severity) LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity))
371#define LOG_IF(severity, condition) \
372 LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity) && (condition))
373
initial.commitd7cae122008-07-26 21:49:38374#define SYSLOG(severity) LOG(severity)
[email protected]521b0c42010-10-01 23:02:36375#define SYSLOG_IF(severity, condition) LOG_IF(severity, condition)
376
[email protected]162ac0f2010-11-04 15:50:49377// The VLOG macros log with negative verbosities.
378#define VLOG_STREAM(verbose_level) \
379 logging::LogMessage(__FILE__, __LINE__, -verbose_level).stream()
380
381#define VLOG(verbose_level) \
382 LAZY_STREAM(VLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level))
383
384#define VLOG_IF(verbose_level, condition) \
385 LAZY_STREAM(VLOG_STREAM(verbose_level), \
386 VLOG_IS_ON(verbose_level) && (condition))
[email protected]99b7c57f2010-09-29 19:26:36387
[email protected]fb879b1a2011-03-06 18:16:31388#if defined (OS_WIN)
389#define VPLOG_STREAM(verbose_level) \
390 logging::Win32ErrorLogMessage(__FILE__, __LINE__, -verbose_level, \
391 ::logging::GetLastSystemErrorCode()).stream()
392#elif defined(OS_POSIX)
393#define VPLOG_STREAM(verbose_level) \
394 logging::ErrnoLogMessage(__FILE__, __LINE__, -verbose_level, \
395 ::logging::GetLastSystemErrorCode()).stream()
396#endif
397
398#define VPLOG(verbose_level) \
399 LAZY_STREAM(VPLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level))
400
401#define VPLOG_IF(verbose_level, condition) \
402 LAZY_STREAM(VPLOG_STREAM(verbose_level), \
403 VLOG_IS_ON(verbose_level) && (condition))
404
[email protected]99b7c57f2010-09-29 19:26:36405// TODO(akalin): Add more VLOG variants, e.g. VPLOG.
initial.commitd7cae122008-07-26 21:49:38406
initial.commitd7cae122008-07-26 21:49:38407#define LOG_ASSERT(condition) \
408 LOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". "
409#define SYSLOG_ASSERT(condition) \
410 SYSLOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". "
411
[email protected]d8617a62009-10-09 23:52:20412#if defined(OS_WIN)
[email protected]521b0c42010-10-01 23:02:36413#define LOG_GETLASTERROR_STREAM(severity) \
[email protected]d8617a62009-10-09 23:52:20414 COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \
415 ::logging::GetLastSystemErrorCode()).stream()
[email protected]521b0c42010-10-01 23:02:36416#define LOG_GETLASTERROR(severity) \
417 LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity), LOG_IS_ON(severity))
418#define LOG_GETLASTERROR_MODULE_STREAM(severity, module) \
[email protected]d8617a62009-10-09 23:52:20419 COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \
420 ::logging::GetLastSystemErrorCode(), module).stream()
[email protected]521b0c42010-10-01 23:02:36421#define LOG_GETLASTERROR_MODULE(severity, module) \
422 LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity, module), \
423 LOG_IS_ON(severity))
424// PLOG_STREAM is used by PLOG, which is the usual error logging macro
425// for each platform.
426#define PLOG_STREAM(severity) LOG_GETLASTERROR_STREAM(severity)
[email protected]d8617a62009-10-09 23:52:20427#elif defined(OS_POSIX)
[email protected]521b0c42010-10-01 23:02:36428#define LOG_ERRNO_STREAM(severity) \
[email protected]d8617a62009-10-09 23:52:20429 COMPACT_GOOGLE_LOG_EX_ ## severity(ErrnoLogMessage, \
430 ::logging::GetLastSystemErrorCode()).stream()
[email protected]521b0c42010-10-01 23:02:36431#define LOG_ERRNO(severity) \
432 LAZY_STREAM(LOG_ERRNO_STREAM(severity), LOG_IS_ON(severity))
433// PLOG_STREAM is used by PLOG, which is the usual error logging macro
434// for each platform.
435#define PLOG_STREAM(severity) LOG_ERRNO_STREAM(severity)
[email protected]d8617a62009-10-09 23:52:20436// TODO(tschmelcher): Should we add OSStatus logging for Mac?
437#endif
438
[email protected]521b0c42010-10-01 23:02:36439#define PLOG(severity) \
440 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity))
441
[email protected]d8617a62009-10-09 23:52:20442#define PLOG_IF(severity, condition) \
[email protected]521b0c42010-10-01 23:02:36443 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity) && (condition))
[email protected]d8617a62009-10-09 23:52:20444
initial.commitd7cae122008-07-26 21:49:38445// CHECK dies with a fatal error if condition is not true. It is *not*
446// controlled by NDEBUG, so the check will be executed regardless of
447// compilation mode.
[email protected]521b0c42010-10-01 23:02:36448//
449// We make sure CHECK et al. always evaluates their arguments, as
450// doing CHECK(FunctionWithSideEffect()) is a common idiom.
[email protected]521b0c42010-10-01 23:02:36451#define CHECK(condition) \
452 LAZY_STREAM(LOG_STREAM(FATAL), !(condition)) \
453 << "Check failed: " #condition ". "
initial.commitd7cae122008-07-26 21:49:38454
[email protected]d8617a62009-10-09 23:52:20455#define PCHECK(condition) \
[email protected]521b0c42010-10-01 23:02:36456 LAZY_STREAM(PLOG_STREAM(FATAL), !(condition)) \
457 << "Check failed: " #condition ". "
[email protected]d8617a62009-10-09 23:52:20458
initial.commitd7cae122008-07-26 21:49:38459// Build the error message string. This is separate from the "Impl"
460// function template because it is not performance critical and so can
[email protected]9c7132e2011-02-08 07:39:08461// be out of line, while the "Impl" code should be inline. Caller
462// takes ownership of the returned string.
initial.commitd7cae122008-07-26 21:49:38463template<class t1, class t2>
464std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) {
465 std::ostringstream ss;
466 ss << names << " (" << v1 << " vs. " << v2 << ")";
467 std::string* msg = new std::string(ss.str());
468 return msg;
469}
470
[email protected]6d445d32010-09-30 19:10:03471// MSVC doesn't like complex extern templates and DLLs.
[email protected]dc72da32011-10-24 20:20:30472#if !defined(COMPILER_MSVC)
[email protected]6d445d32010-09-30 19:10:03473// Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated
474// in logging.cc.
[email protected]dc72da32011-10-24 20:20:30475extern template BASE_EXPORT std::string* MakeCheckOpString<int, int>(
[email protected]6d445d32010-09-30 19:10:03476 const int&, const int&, const char* names);
[email protected]dc72da32011-10-24 20:20:30477extern template BASE_EXPORT
478std::string* MakeCheckOpString<unsigned long, unsigned long>(
[email protected]6d445d32010-09-30 19:10:03479 const unsigned long&, const unsigned long&, const char* names);
[email protected]dc72da32011-10-24 20:20:30480extern template BASE_EXPORT
481std::string* MakeCheckOpString<unsigned long, unsigned int>(
[email protected]6d445d32010-09-30 19:10:03482 const unsigned long&, const unsigned int&, const char* names);
[email protected]dc72da32011-10-24 20:20:30483extern template BASE_EXPORT
484std::string* MakeCheckOpString<unsigned int, unsigned long>(
[email protected]6d445d32010-09-30 19:10:03485 const unsigned int&, const unsigned long&, const char* names);
[email protected]dc72da32011-10-24 20:20:30486extern template BASE_EXPORT
487std::string* MakeCheckOpString<std::string, std::string>(
[email protected]6d445d32010-09-30 19:10:03488 const std::string&, const std::string&, const char* name);
489#endif
initial.commitd7cae122008-07-26 21:49:38490
[email protected]e150c0382010-03-02 00:41:12491// Helper macro for binary operators.
492// Don't use this macro directly in your code, use CHECK_EQ et al below.
[email protected]521b0c42010-10-01 23:02:36493//
494// TODO(akalin): Rewrite this so that constructs like if (...)
495// CHECK_EQ(...) else { ... } work properly.
496#define CHECK_OP(name, op, val1, val2) \
[email protected]9c7132e2011-02-08 07:39:08497 if (std::string* _result = \
[email protected]521b0c42010-10-01 23:02:36498 logging::Check##name##Impl((val1), (val2), \
499 #val1 " " #op " " #val2)) \
[email protected]8b782102010-09-30 22:38:30500 logging::LogMessage(__FILE__, __LINE__, _result).stream()
[email protected]e150c0382010-03-02 00:41:12501
[email protected]71512602010-11-01 22:19:56502// Helper functions for CHECK_OP macro.
503// The (int, int) specialization works around the issue that the compiler
504// will not instantiate the template version of the function on values of
505// unnamed enum type - see comment below.
506#define DEFINE_CHECK_OP_IMPL(name, op) \
507 template <class t1, class t2> \
508 inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \
509 const char* names) { \
510 if (v1 op v2) return NULL; \
511 else return MakeCheckOpString(v1, v2, names); \
512 } \
513 inline std::string* Check##name##Impl(int v1, int v2, const char* names) { \
514 if (v1 op v2) return NULL; \
515 else return MakeCheckOpString(v1, v2, names); \
516 }
517DEFINE_CHECK_OP_IMPL(EQ, ==)
518DEFINE_CHECK_OP_IMPL(NE, !=)
519DEFINE_CHECK_OP_IMPL(LE, <=)
520DEFINE_CHECK_OP_IMPL(LT, < )
521DEFINE_CHECK_OP_IMPL(GE, >=)
522DEFINE_CHECK_OP_IMPL(GT, > )
523#undef DEFINE_CHECK_OP_IMPL
[email protected]e150c0382010-03-02 00:41:12524
525#define CHECK_EQ(val1, val2) CHECK_OP(EQ, ==, val1, val2)
526#define CHECK_NE(val1, val2) CHECK_OP(NE, !=, val1, val2)
527#define CHECK_LE(val1, val2) CHECK_OP(LE, <=, val1, val2)
528#define CHECK_LT(val1, val2) CHECK_OP(LT, < , val1, val2)
529#define CHECK_GE(val1, val2) CHECK_OP(GE, >=, val1, val2)
530#define CHECK_GT(val1, val2) CHECK_OP(GT, > , val1, val2)
531
[email protected]e3cca332009-08-20 01:20:29532// http://crbug.com/16512 is open for a real fix for this. For now, Windows
533// uses OFFICIAL_BUILD and other platforms use the branding flag when NDEBUG is
534// defined.
535#if ( defined(OS_WIN) && defined(OFFICIAL_BUILD)) || \
536 (!defined(OS_WIN) && defined(NDEBUG) && defined(GOOGLE_CHROME_BUILD))
[email protected]521b0c42010-10-01 23:02:36537// Used by unit tests.
538#define LOGGING_IS_OFFICIAL_BUILD
539
[email protected]e3cca332009-08-20 01:20:29540// In order to have optimized code for official builds, remove DLOGs and
541// DCHECKs.
[email protected]d15e56c2010-09-30 21:12:33542#define ENABLE_DLOG 0
543#define ENABLE_DCHECK 0
544
545#elif defined(NDEBUG)
546// Otherwise, if we're a release build, remove DLOGs but not DCHECKs
547// (since those can still be turned on via a command-line flag).
548#define ENABLE_DLOG 0
549#define ENABLE_DCHECK 1
550
551#else
552// Otherwise, we're a debug build so enable DLOGs and DCHECKs.
553#define ENABLE_DLOG 1
554#define ENABLE_DCHECK 1
[email protected]e3cca332009-08-20 01:20:29555#endif
556
[email protected]d15e56c2010-09-30 21:12:33557// Definitions for DLOG et al.
558
[email protected]d926c202010-10-01 02:58:24559#if ENABLE_DLOG
560
[email protected]5e987802010-11-01 19:49:22561#define DLOG_IS_ON(severity) LOG_IS_ON(severity)
[email protected]d926c202010-10-01 02:58:24562#define DLOG_IF(severity, condition) LOG_IF(severity, condition)
563#define DLOG_ASSERT(condition) LOG_ASSERT(condition)
[email protected]d926c202010-10-01 02:58:24564#define DPLOG_IF(severity, condition) PLOG_IF(severity, condition)
[email protected]521b0c42010-10-01 23:02:36565#define DVLOG_IF(verboselevel, condition) VLOG_IF(verboselevel, condition)
[email protected]fb879b1a2011-03-06 18:16:31566#define DVPLOG_IF(verboselevel, condition) VPLOG_IF(verboselevel, condition)
[email protected]d926c202010-10-01 02:58:24567
568#else // ENABLE_DLOG
569
[email protected]521b0c42010-10-01 23:02:36570// If ENABLE_DLOG is off, we want to avoid emitting any references to
571// |condition| (which may reference a variable defined only if NDEBUG
572// is not defined). Contrast this with DCHECK et al., which has
573// different behavior.
[email protected]d926c202010-10-01 02:58:24574
[email protected]521b0c42010-10-01 23:02:36575#define DLOG_EAT_STREAM_PARAMETERS \
576 true ? (void) 0 : ::logging::LogMessageVoidify() & LOG_STREAM(FATAL)
[email protected]d926c202010-10-01 02:58:24577
[email protected]5e987802010-11-01 19:49:22578#define DLOG_IS_ON(severity) false
[email protected]521b0c42010-10-01 23:02:36579#define DLOG_IF(severity, condition) DLOG_EAT_STREAM_PARAMETERS
580#define DLOG_ASSERT(condition) DLOG_EAT_STREAM_PARAMETERS
581#define DPLOG_IF(severity, condition) DLOG_EAT_STREAM_PARAMETERS
582#define DVLOG_IF(verboselevel, condition) DLOG_EAT_STREAM_PARAMETERS
[email protected]fb879b1a2011-03-06 18:16:31583#define DVPLOG_IF(verboselevel, condition) DLOG_EAT_STREAM_PARAMETERS
[email protected]d926c202010-10-01 02:58:24584
585#endif // ENABLE_DLOG
586
[email protected]d15e56c2010-09-30 21:12:33587// DEBUG_MODE is for uses like
588// if (DEBUG_MODE) foo.CheckThatFoo();
589// instead of
590// #ifndef NDEBUG
591// foo.CheckThatFoo();
592// #endif
593//
594// We tie its state to ENABLE_DLOG.
595enum { DEBUG_MODE = ENABLE_DLOG };
596
597#undef ENABLE_DLOG
598
[email protected]521b0c42010-10-01 23:02:36599#define DLOG(severity) \
600 LAZY_STREAM(LOG_STREAM(severity), DLOG_IS_ON(severity))
601
602#if defined(OS_WIN)
603#define DLOG_GETLASTERROR(severity) \
604 LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity), DLOG_IS_ON(severity))
605#define DLOG_GETLASTERROR_MODULE(severity, module) \
606 LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity, module), \
607 DLOG_IS_ON(severity))
608#elif defined(OS_POSIX)
609#define DLOG_ERRNO(severity) \
610 LAZY_STREAM(LOG_ERRNO_STREAM(severity), DLOG_IS_ON(severity))
611#endif
612
613#define DPLOG(severity) \
614 LAZY_STREAM(PLOG_STREAM(severity), DLOG_IS_ON(severity))
615
616#define DVLOG(verboselevel) DLOG_IF(INFO, VLOG_IS_ON(verboselevel))
617
[email protected]fb879b1a2011-03-06 18:16:31618#define DVPLOG(verboselevel) DVPLOG_IF(verboselevel, VLOG_IS_ON(verboselevel))
619
[email protected]521b0c42010-10-01 23:02:36620// Definitions for DCHECK et al.
[email protected]d926c202010-10-01 02:58:24621
[email protected]d15e56c2010-09-30 21:12:33622#if ENABLE_DCHECK
[email protected]e3cca332009-08-20 01:20:29623
[email protected]521b0c42010-10-01 23:02:36624#if defined(NDEBUG)
[email protected]d926c202010-10-01 02:58:24625
[email protected]20960e072011-09-20 20:59:01626BASE_EXPORT extern DcheckState g_dcheck_state;
627
628#if defined(DCHECK_ALWAYS_ON)
629
630#define DCHECK_IS_ON() true
631#define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \
632 COMPACT_GOOGLE_LOG_EX_FATAL(ClassName , ##__VA_ARGS__)
633#define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_FATAL
634const LogSeverity LOG_DCHECK = LOG_FATAL;
635
636#else
637
[email protected]deba0ff2010-11-03 05:30:14638#define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \
639 COMPACT_GOOGLE_LOG_EX_ERROR_REPORT(ClassName , ##__VA_ARGS__)
640#define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_ERROR_REPORT
[email protected]521b0c42010-10-01 23:02:36641const LogSeverity LOG_DCHECK = LOG_ERROR_REPORT;
[email protected]7c10f7552011-01-11 01:03:36642#define DCHECK_IS_ON() \
643 ((::logging::g_dcheck_state == \
644 ::logging::ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS) && \
645 LOG_IS_ON(DCHECK))
[email protected]d926c202010-10-01 02:58:24646
[email protected]20960e072011-09-20 20:59:01647#endif // defined(DCHECK_ALWAYS_ON)
648
[email protected]521b0c42010-10-01 23:02:36649#else // defined(NDEBUG)
650
[email protected]5e987802010-11-01 19:49:22651// On a regular debug build, we want to have DCHECKs enabled.
[email protected]deba0ff2010-11-03 05:30:14652#define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \
653 COMPACT_GOOGLE_LOG_EX_FATAL(ClassName , ##__VA_ARGS__)
654#define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_FATAL
[email protected]521b0c42010-10-01 23:02:36655const LogSeverity LOG_DCHECK = LOG_FATAL;
[email protected]deba0ff2010-11-03 05:30:14656#define DCHECK_IS_ON() true
[email protected]521b0c42010-10-01 23:02:36657
658#endif // defined(NDEBUG)
659
660#else // ENABLE_DCHECK
661
[email protected]deba0ff2010-11-03 05:30:14662// These are just dummy values since DCHECK_IS_ON() is always false in
663// this case.
664#define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \
665 COMPACT_GOOGLE_LOG_EX_INFO(ClassName , ##__VA_ARGS__)
666#define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_INFO
667const LogSeverity LOG_DCHECK = LOG_INFO;
[email protected]5e987802010-11-01 19:49:22668#define DCHECK_IS_ON() false
[email protected]521b0c42010-10-01 23:02:36669
670#endif // ENABLE_DCHECK
[email protected]5e987802010-11-01 19:49:22671#undef ENABLE_DCHECK
[email protected]521b0c42010-10-01 23:02:36672
[email protected]deba0ff2010-11-03 05:30:14673// DCHECK et al. make sure to reference |condition| regardless of
[email protected]521b0c42010-10-01 23:02:36674// whether DCHECKs are enabled; this is so that we don't get unused
675// variable warnings if the only use of a variable is in a DCHECK.
676// This behavior is different from DLOG_IF et al.
677
[email protected]deba0ff2010-11-03 05:30:14678#define DCHECK(condition) \
679 LAZY_STREAM(LOG_STREAM(DCHECK), DCHECK_IS_ON() && !(condition)) \
[email protected]521b0c42010-10-01 23:02:36680 << "Check failed: " #condition ". "
681
[email protected]deba0ff2010-11-03 05:30:14682#define DPCHECK(condition) \
683 LAZY_STREAM(PLOG_STREAM(DCHECK), DCHECK_IS_ON() && !(condition)) \
[email protected]521b0c42010-10-01 23:02:36684 << "Check failed: " #condition ". "
[email protected]d926c202010-10-01 02:58:24685
686// Helper macro for binary operators.
687// Don't use this macro directly in your code, use DCHECK_EQ et al below.
[email protected]521b0c42010-10-01 23:02:36688#define DCHECK_OP(name, op, val1, val2) \
[email protected]5e987802010-11-01 19:49:22689 if (DCHECK_IS_ON()) \
[email protected]9c7132e2011-02-08 07:39:08690 if (std::string* _result = \
[email protected]521b0c42010-10-01 23:02:36691 logging::Check##name##Impl((val1), (val2), \
692 #val1 " " #op " " #val2)) \
693 logging::LogMessage( \
694 __FILE__, __LINE__, ::logging::LOG_DCHECK, \
695 _result).stream()
initial.commitd7cae122008-07-26 21:49:38696
[email protected]deba0ff2010-11-03 05:30:14697// Equality/Inequality checks - compare two values, and log a
698// LOG_DCHECK message including the two values when the result is not
699// as expected. The values must have operator<<(ostream, ...)
700// defined.
initial.commitd7cae122008-07-26 21:49:38701//
702// You may append to the error message like so:
703// DCHECK_NE(1, 2) << ": The world must be ending!";
704//
705// We are very careful to ensure that each argument is evaluated exactly
706// once, and that anything which is legal to pass as a function argument is
707// legal here. In particular, the arguments may be temporary expressions
708// which will end up being destroyed at the end of the apparent statement,
709// for example:
710// DCHECK_EQ(string("abc")[1], 'b');
711//
712// WARNING: These may not compile correctly if one of the arguments is a pointer
713// and the other is NULL. To work around this, simply static_cast NULL to the
714// type of the desired pointer.
715
716#define DCHECK_EQ(val1, val2) DCHECK_OP(EQ, ==, val1, val2)
717#define DCHECK_NE(val1, val2) DCHECK_OP(NE, !=, val1, val2)
718#define DCHECK_LE(val1, val2) DCHECK_OP(LE, <=, val1, val2)
719#define DCHECK_LT(val1, val2) DCHECK_OP(LT, < , val1, val2)
720#define DCHECK_GE(val1, val2) DCHECK_OP(GE, >=, val1, val2)
721#define DCHECK_GT(val1, val2) DCHECK_OP(GT, > , val1, val2)
722
initial.commitd7cae122008-07-26 21:49:38723#define NOTREACHED() DCHECK(false)
724
725// Redefine the standard assert to use our nice log files
726#undef assert
727#define assert(x) DLOG_ASSERT(x)
728
729// This class more or less represents a particular log message. You
730// create an instance of LogMessage and then stream stuff to it.
731// When you finish streaming to it, ~LogMessage is called and the
732// full message gets streamed to the appropriate destination.
733//
734// You shouldn't actually use LogMessage's constructor to log things,
735// though. You should use the LOG() macro (and variants thereof)
736// above.
[email protected]0bea7252011-08-05 15:34:00737class BASE_EXPORT LogMessage {
initial.commitd7cae122008-07-26 21:49:38738 public:
739 LogMessage(const char* file, int line, LogSeverity severity, int ctr);
740
741 // Two special constructors that generate reduced amounts of code at
742 // LOG call sites for common cases.
743 //
744 // Used for LOG(INFO): Implied are:
745 // severity = LOG_INFO, ctr = 0
746 //
747 // Using this constructor instead of the more complex constructor above
748 // saves a couple of bytes per call site.
749 LogMessage(const char* file, int line);
750
751 // Used for LOG(severity) where severity != INFO. Implied
752 // are: ctr = 0
753 //
754 // Using this constructor instead of the more complex constructor above
755 // saves a couple of bytes per call site.
756 LogMessage(const char* file, int line, LogSeverity severity);
757
[email protected]9c7132e2011-02-08 07:39:08758 // A special constructor used for check failures. Takes ownership
759 // of the given string.
initial.commitd7cae122008-07-26 21:49:38760 // Implied severity = LOG_FATAL
[email protected]9c7132e2011-02-08 07:39:08761 LogMessage(const char* file, int line, std::string* result);
initial.commitd7cae122008-07-26 21:49:38762
[email protected]fb62a532009-02-12 01:19:05763 // A special constructor used for check failures, with the option to
[email protected]9c7132e2011-02-08 07:39:08764 // specify severity. Takes ownership of the given string.
[email protected]fb62a532009-02-12 01:19:05765 LogMessage(const char* file, int line, LogSeverity severity,
[email protected]9c7132e2011-02-08 07:39:08766 std::string* result);
[email protected]fb62a532009-02-12 01:19:05767
initial.commitd7cae122008-07-26 21:49:38768 ~LogMessage();
769
770 std::ostream& stream() { return stream_; }
771
772 private:
773 void Init(const char* file, int line);
774
775 LogSeverity severity_;
776 std::ostringstream stream_;
[email protected]c88873922008-07-30 13:02:03777 size_t message_start_; // Offset of the start of the message (past prefix
778 // info).
[email protected]162ac0f2010-11-04 15:50:49779 // The file and line information passed in to the constructor.
780 const char* file_;
781 const int line_;
782
[email protected]3f85caa2009-04-14 16:52:11783#if defined(OS_WIN)
784 // Stores the current value of GetLastError in the constructor and restores
785 // it in the destructor by calling SetLastError.
786 // This is useful since the LogMessage class uses a lot of Win32 calls
787 // that will lose the value of GLE and the code that called the log function
788 // will have lost the thread error value when the log call returns.
789 class SaveLastError {
790 public:
791 SaveLastError();
792 ~SaveLastError();
793
794 unsigned long get_error() const { return last_error_; }
795
796 protected:
797 unsigned long last_error_;
798 };
799
800 SaveLastError last_error_;
801#endif
initial.commitd7cae122008-07-26 21:49:38802
[email protected]39be4242008-08-07 18:31:40803 DISALLOW_COPY_AND_ASSIGN(LogMessage);
initial.commitd7cae122008-07-26 21:49:38804};
805
806// A non-macro interface to the log facility; (useful
807// when the logging level is not a compile-time constant).
808inline void LogAtLevel(int const log_level, std::string const &msg) {
809 LogMessage(__FILE__, __LINE__, log_level).stream() << msg;
810}
811
812// This class is used to explicitly ignore values in the conditional
813// logging macros. This avoids compiler warnings like "value computed
814// is not used" and "statement has no effect".
[email protected]23bb71f2011-04-21 22:22:10815class LogMessageVoidify {
initial.commitd7cae122008-07-26 21:49:38816 public:
817 LogMessageVoidify() { }
818 // This has to be an operator with a precedence lower than << but
819 // higher than ?:
820 void operator&(std::ostream&) { }
821};
822
[email protected]d8617a62009-10-09 23:52:20823#if defined(OS_WIN)
824typedef unsigned long SystemErrorCode;
825#elif defined(OS_POSIX)
826typedef int SystemErrorCode;
827#endif
828
829// Alias for ::GetLastError() on Windows and errno on POSIX. Avoids having to
830// pull in windows.h just for GetLastError() and DWORD.
[email protected]0bea7252011-08-05 15:34:00831BASE_EXPORT SystemErrorCode GetLastSystemErrorCode();
[email protected]d8617a62009-10-09 23:52:20832
833#if defined(OS_WIN)
834// Appends a formatted system message of the GetLastError() type.
[email protected]0bea7252011-08-05 15:34:00835class BASE_EXPORT Win32ErrorLogMessage {
[email protected]d8617a62009-10-09 23:52:20836 public:
837 Win32ErrorLogMessage(const char* file,
838 int line,
839 LogSeverity severity,
840 SystemErrorCode err,
841 const char* module);
842
843 Win32ErrorLogMessage(const char* file,
844 int line,
845 LogSeverity severity,
846 SystemErrorCode err);
847
[email protected]d8617a62009-10-09 23:52:20848 // Appends the error message before destructing the encapsulated class.
849 ~Win32ErrorLogMessage();
850
[email protected]a502bbe72011-01-07 18:06:45851 std::ostream& stream() { return log_message_.stream(); }
852
[email protected]d8617a62009-10-09 23:52:20853 private:
854 SystemErrorCode err_;
855 // Optional name of the module defining the error.
856 const char* module_;
857 LogMessage log_message_;
858
859 DISALLOW_COPY_AND_ASSIGN(Win32ErrorLogMessage);
860};
861#elif defined(OS_POSIX)
862// Appends a formatted system message of the errno type
[email protected]0bea7252011-08-05 15:34:00863class BASE_EXPORT ErrnoLogMessage {
[email protected]d8617a62009-10-09 23:52:20864 public:
865 ErrnoLogMessage(const char* file,
866 int line,
867 LogSeverity severity,
868 SystemErrorCode err);
869
[email protected]d8617a62009-10-09 23:52:20870 // Appends the error message before destructing the encapsulated class.
871 ~ErrnoLogMessage();
872
[email protected]a502bbe72011-01-07 18:06:45873 std::ostream& stream() { return log_message_.stream(); }
874
[email protected]d8617a62009-10-09 23:52:20875 private:
876 SystemErrorCode err_;
877 LogMessage log_message_;
878
879 DISALLOW_COPY_AND_ASSIGN(ErrnoLogMessage);
880};
881#endif // OS_WIN
882
initial.commitd7cae122008-07-26 21:49:38883// Closes the log file explicitly if open.
884// NOTE: Since the log file is opened as necessary by the action of logging
885// statements, there's no guarantee that it will stay closed
886// after this call.
[email protected]0bea7252011-08-05 15:34:00887BASE_EXPORT void CloseLogFile();
initial.commitd7cae122008-07-26 21:49:38888
[email protected]e36ddc82009-12-08 04:22:50889// Async signal safe logging mechanism.
[email protected]0bea7252011-08-05 15:34:00890BASE_EXPORT void RawLog(int level, const char* message);
[email protected]e36ddc82009-12-08 04:22:50891
892#define RAW_LOG(level, message) logging::RawLog(logging::LOG_ ## level, message)
893
894#define RAW_CHECK(condition) \
895 do { \
896 if (!(condition)) \
897 logging::RawLog(logging::LOG_FATAL, "Check failed: " #condition "\n"); \
898 } while (0)
899
[email protected]39be4242008-08-07 18:31:40900} // namespace logging
initial.commitd7cae122008-07-26 21:49:38901
[email protected]46ce5b562010-06-16 18:39:53902// These functions are provided as a convenience for logging, which is where we
903// use streams (it is against Google style to use streams in other places). It
904// is designed to allow you to emit non-ASCII Unicode strings to the log file,
905// which is normally ASCII. It is relatively slow, so try not to use it for
906// common cases. Non-ASCII characters will be converted to UTF-8 by these
907// operators.
[email protected]0bea7252011-08-05 15:34:00908BASE_EXPORT std::ostream& operator<<(std::ostream& out, const wchar_t* wstr);
[email protected]46ce5b562010-06-16 18:39:53909inline std::ostream& operator<<(std::ostream& out, const std::wstring& wstr) {
910 return out << wstr.c_str();
911}
912
[email protected]0dfc81b2008-08-25 03:44:40913// The NOTIMPLEMENTED() macro annotates codepaths which have
914// not been implemented yet.
915//
916// The implementation of this macro is controlled by NOTIMPLEMENTED_POLICY:
917// 0 -- Do nothing (stripped by compiler)
918// 1 -- Warn at compile time
919// 2 -- Fail at compile time
920// 3 -- Fail at runtime (DCHECK)
921// 4 -- [default] LOG(ERROR) at runtime
922// 5 -- LOG(ERROR) at runtime, only once per call-site
923
924#ifndef NOTIMPLEMENTED_POLICY
925// Select default policy: LOG(ERROR)
926#define NOTIMPLEMENTED_POLICY 4
927#endif
928
[email protected]f6cda752008-10-30 23:54:26929#if defined(COMPILER_GCC)
930// On Linux, with GCC, we can use __PRETTY_FUNCTION__ to get the demangled name
931// of the current function in the NOTIMPLEMENTED message.
932#define NOTIMPLEMENTED_MSG "Not implemented reached in " << __PRETTY_FUNCTION__
933#else
934#define NOTIMPLEMENTED_MSG "NOT IMPLEMENTED"
935#endif
936
[email protected]0dfc81b2008-08-25 03:44:40937#if NOTIMPLEMENTED_POLICY == 0
938#define NOTIMPLEMENTED() ;
939#elif NOTIMPLEMENTED_POLICY == 1
940// TODO, figure out how to generate a warning
941#define NOTIMPLEMENTED() COMPILE_ASSERT(false, NOT_IMPLEMENTED)
942#elif NOTIMPLEMENTED_POLICY == 2
943#define NOTIMPLEMENTED() COMPILE_ASSERT(false, NOT_IMPLEMENTED)
944#elif NOTIMPLEMENTED_POLICY == 3
945#define NOTIMPLEMENTED() NOTREACHED()
946#elif NOTIMPLEMENTED_POLICY == 4
[email protected]f6cda752008-10-30 23:54:26947#define NOTIMPLEMENTED() LOG(ERROR) << NOTIMPLEMENTED_MSG
[email protected]0dfc81b2008-08-25 03:44:40948#elif NOTIMPLEMENTED_POLICY == 5
949#define NOTIMPLEMENTED() do {\
950 static int count = 0;\
[email protected]f6cda752008-10-30 23:54:26951 LOG_IF(ERROR, 0 == count++) << NOTIMPLEMENTED_MSG;\
[email protected]0dfc81b2008-08-25 03:44:40952} while(0)
953#endif
954
[email protected]39a749c2011-01-28 02:40:46955namespace base {
956
957class StringPiece;
958
[email protected]1f88b5162011-04-01 00:02:29959// Allows StringPiece to be logged.
[email protected]0bea7252011-08-05 15:34:00960BASE_EXPORT std::ostream& operator<<(std::ostream& o, const StringPiece& piece);
[email protected]39a749c2011-01-28 02:40:46961
962} // namespace base
963
[email protected]39be4242008-08-07 18:31:40964#endif // BASE_LOGGING_H_