michael@0: // Copyright (c) 2012 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #ifndef BASE_LOGGING_H_ michael@0: #define BASE_LOGGING_H_ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "base/base_export.h" michael@0: #include "base/basictypes.h" michael@0: #include "base/debug/debugger.h" michael@0: #include "build/build_config.h" michael@0: michael@0: // michael@0: // Optional message capabilities michael@0: // ----------------------------- michael@0: // Assertion failed messages and fatal errors are displayed in a dialog box michael@0: // before the application exits. However, running this UI creates a message michael@0: // loop, which causes application messages to be processed and potentially michael@0: // dispatched to existing application windows. Since the application is in a michael@0: // bad state when this assertion dialog is displayed, these messages may not michael@0: // get processed and hang the dialog, or the application might go crazy. michael@0: // michael@0: // Therefore, it can be beneficial to display the error dialog in a separate michael@0: // process from the main application. When the logging system needs to display michael@0: // a fatal error dialog box, it will look for a program called michael@0: // "DebugMessage.exe" in the same directory as the application executable. It michael@0: // will run this application with the message as the command line, and will michael@0: // not include the name of the application as is traditional for easier michael@0: // parsing. michael@0: // michael@0: // The code for DebugMessage.exe is only one line. In WinMain, do: michael@0: // MessageBox(NULL, GetCommandLineW(), L"Fatal Error", 0); michael@0: // michael@0: // If DebugMessage.exe is not found, the logging code will use a normal michael@0: // MessageBox, potentially causing the problems discussed above. michael@0: michael@0: michael@0: // Instructions michael@0: // ------------ michael@0: // michael@0: // Make a bunch of macros for logging. The way to log things is to stream michael@0: // things to LOG(). E.g., michael@0: // michael@0: // LOG(INFO) << "Found " << num_cookies << " cookies"; michael@0: // michael@0: // You can also do conditional logging: michael@0: // michael@0: // LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies"; michael@0: // michael@0: // The above will cause log messages to be output on the 1st, 11th, 21st, ... michael@0: // times it is executed. Note that the special COUNTER value is used to michael@0: // identify which repetition is happening. michael@0: // michael@0: // The CHECK(condition) macro is active in both debug and release builds and michael@0: // effectively performs a LOG(FATAL) which terminates the process and michael@0: // generates a crashdump unless a debugger is attached. michael@0: // michael@0: // There are also "debug mode" logging macros like the ones above: michael@0: // michael@0: // DLOG(INFO) << "Found cookies"; michael@0: // michael@0: // DLOG_IF(INFO, num_cookies > 10) << "Got lots of cookies"; michael@0: // michael@0: // All "debug mode" logging is compiled away to nothing for non-debug mode michael@0: // compiles. LOG_IF and development flags also work well together michael@0: // because the code can be compiled away sometimes. michael@0: // michael@0: // We also have michael@0: // michael@0: // LOG_ASSERT(assertion); michael@0: // DLOG_ASSERT(assertion); michael@0: // michael@0: // which is syntactic sugar for {,D}LOG_IF(FATAL, assert fails) << assertion; michael@0: // michael@0: // There are "verbose level" logging macros. They look like michael@0: // michael@0: // VLOG(1) << "I'm printed when you run the program with --v=1 or more"; michael@0: // VLOG(2) << "I'm printed when you run the program with --v=2 or more"; michael@0: // michael@0: // These always log at the INFO log level (when they log at all). michael@0: // The verbose logging can also be turned on module-by-module. For instance, michael@0: // --vmodule=profile=2,icon_loader=1,browser_*=3,*/chromeos/*=4 --v=0 michael@0: // will cause: michael@0: // a. VLOG(2) and lower messages to be printed from profile.{h,cc} michael@0: // b. VLOG(1) and lower messages to be printed from icon_loader.{h,cc} michael@0: // c. VLOG(3) and lower messages to be printed from files prefixed with michael@0: // "browser" michael@0: // d. VLOG(4) and lower messages to be printed from files under a michael@0: // "chromeos" directory. michael@0: // e. VLOG(0) and lower messages to be printed from elsewhere michael@0: // michael@0: // The wildcarding functionality shown by (c) supports both '*' (match michael@0: // 0 or more characters) and '?' (match any single character) michael@0: // wildcards. Any pattern containing a forward or backward slash will michael@0: // be tested against the whole pathname and not just the module. michael@0: // E.g., "*/foo/bar/*=2" would change the logging level for all code michael@0: // in source files under a "foo/bar" directory. michael@0: // michael@0: // There's also VLOG_IS_ON(n) "verbose level" condition macro. To be used as michael@0: // michael@0: // if (VLOG_IS_ON(2)) { michael@0: // // do some logging preparation and logging michael@0: // // that can't be accomplished with just VLOG(2) << ...; michael@0: // } michael@0: // michael@0: // There is also a VLOG_IF "verbose level" condition macro for sample michael@0: // cases, when some extra computation and preparation for logs is not michael@0: // needed. michael@0: // michael@0: // VLOG_IF(1, (size > 1024)) michael@0: // << "I'm printed when size is more than 1024 and when you run the " michael@0: // "program with --v=1 or more"; michael@0: // michael@0: // We also override the standard 'assert' to use 'DLOG_ASSERT'. michael@0: // michael@0: // Lastly, there is: michael@0: // michael@0: // PLOG(ERROR) << "Couldn't do foo"; michael@0: // DPLOG(ERROR) << "Couldn't do foo"; michael@0: // PLOG_IF(ERROR, cond) << "Couldn't do foo"; michael@0: // DPLOG_IF(ERROR, cond) << "Couldn't do foo"; michael@0: // PCHECK(condition) << "Couldn't do foo"; michael@0: // DPCHECK(condition) << "Couldn't do foo"; michael@0: // michael@0: // which append the last system error to the message in string form (taken from michael@0: // GetLastError() on Windows and errno on POSIX). michael@0: // michael@0: // The supported severity levels for macros that allow you to specify one michael@0: // are (in increasing order of severity) INFO, WARNING, ERROR, ERROR_REPORT, michael@0: // and FATAL. michael@0: // michael@0: // Very important: logging a message at the FATAL severity level causes michael@0: // the program to terminate (after the message is logged). michael@0: // michael@0: // Note the special severity of ERROR_REPORT only available/relevant in normal michael@0: // mode, which displays error dialog without terminating the program. There is michael@0: // no error dialog for severity ERROR or below in normal mode. michael@0: // michael@0: // There is also the special severity of DFATAL, which logs FATAL in michael@0: // debug mode, ERROR in normal mode. michael@0: michael@0: namespace logging { michael@0: michael@0: // TODO(avi): do we want to do a unification of character types here? michael@0: #if defined(OS_WIN) michael@0: typedef wchar_t PathChar; michael@0: #else michael@0: typedef char PathChar; michael@0: #endif michael@0: michael@0: // Where to record logging output? A flat file and/or system debug log michael@0: // via OutputDebugString. michael@0: enum LoggingDestination { michael@0: LOG_NONE = 0, michael@0: LOG_TO_FILE = 1 << 0, michael@0: LOG_TO_SYSTEM_DEBUG_LOG = 1 << 1, michael@0: michael@0: LOG_TO_ALL = LOG_TO_FILE | LOG_TO_SYSTEM_DEBUG_LOG, michael@0: michael@0: // On Windows, use a file next to the exe; on POSIX platforms, where michael@0: // it may not even be possible to locate the executable on disk, use michael@0: // stderr. michael@0: #if defined(OS_WIN) michael@0: LOG_DEFAULT = LOG_TO_FILE, michael@0: #elif defined(OS_POSIX) michael@0: LOG_DEFAULT = LOG_TO_SYSTEM_DEBUG_LOG, michael@0: #endif michael@0: }; michael@0: michael@0: // Indicates that the log file should be locked when being written to. michael@0: // Unless there is only one single-threaded process that is logging to michael@0: // the log file, the file should be locked during writes to make each michael@0: // log outut atomic. Other writers will block. michael@0: // michael@0: // All processes writing to the log file must have their locking set for it to michael@0: // work properly. Defaults to LOCK_LOG_FILE. michael@0: enum LogLockingState { LOCK_LOG_FILE, DONT_LOCK_LOG_FILE }; michael@0: michael@0: // On startup, should we delete or append to an existing log file (if any)? michael@0: // Defaults to APPEND_TO_OLD_LOG_FILE. michael@0: enum OldFileDeletionState { DELETE_OLD_LOG_FILE, APPEND_TO_OLD_LOG_FILE }; michael@0: michael@0: enum DcheckState { michael@0: DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS, michael@0: ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS michael@0: }; michael@0: michael@0: struct BASE_EXPORT LoggingSettings { michael@0: // The defaults values are: michael@0: // michael@0: // logging_dest: LOG_DEFAULT michael@0: // log_file: NULL michael@0: // lock_log: LOCK_LOG_FILE michael@0: // delete_old: APPEND_TO_OLD_LOG_FILE michael@0: // dcheck_state: DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS michael@0: LoggingSettings(); michael@0: michael@0: LoggingDestination logging_dest; michael@0: michael@0: // The three settings below have an effect only when LOG_TO_FILE is michael@0: // set in |logging_dest|. michael@0: const PathChar* log_file; michael@0: LogLockingState lock_log; michael@0: OldFileDeletionState delete_old; michael@0: michael@0: DcheckState dcheck_state; michael@0: }; michael@0: michael@0: // Define different names for the BaseInitLoggingImpl() function depending on michael@0: // whether NDEBUG is defined or not so that we'll fail to link if someone tries michael@0: // to compile logging.cc with NDEBUG but includes logging.h without defining it, michael@0: // or vice versa. michael@0: #if NDEBUG michael@0: #define BaseInitLoggingImpl BaseInitLoggingImpl_built_with_NDEBUG michael@0: #else michael@0: #define BaseInitLoggingImpl BaseInitLoggingImpl_built_without_NDEBUG michael@0: #endif michael@0: michael@0: // Implementation of the InitLogging() method declared below. We use a michael@0: // more-specific name so we can #define it above without affecting other code michael@0: // that has named stuff "InitLogging". michael@0: BASE_EXPORT bool BaseInitLoggingImpl(const LoggingSettings& settings); michael@0: michael@0: // Sets the log file name and other global logging state. Calling this function michael@0: // is recommended, and is normally done at the beginning of application init. michael@0: // If you don't call it, all the flags will be initialized to their default michael@0: // values, and there is a race condition that may leak a critical section michael@0: // object if two threads try to do the first log at the same time. michael@0: // See the definition of the enums above for descriptions and default values. michael@0: // michael@0: // The default log file is initialized to "debug.log" in the application michael@0: // directory. You probably don't want this, especially since the program michael@0: // directory may not be writable on an enduser's system. michael@0: // michael@0: // This function may be called a second time to re-direct logging (e.g after michael@0: // loging in to a user partition), however it should never be called more than michael@0: // twice. michael@0: inline bool InitLogging(const LoggingSettings& settings) { michael@0: return BaseInitLoggingImpl(settings); michael@0: } michael@0: michael@0: // Sets the log level. Anything at or above this level will be written to the michael@0: // log file/displayed to the user (if applicable). Anything below this level michael@0: // will be silently ignored. The log level defaults to 0 (everything is logged michael@0: // up to level INFO) if this function is not called. michael@0: // Note that log messages for VLOG(x) are logged at level -x, so setting michael@0: // the min log level to negative values enables verbose logging. michael@0: BASE_EXPORT void SetMinLogLevel(int level); michael@0: michael@0: // Gets the current log level. michael@0: BASE_EXPORT int GetMinLogLevel(); michael@0: michael@0: // Gets the VLOG default verbosity level. michael@0: BASE_EXPORT int GetVlogVerbosity(); michael@0: michael@0: // Gets the current vlog level for the given file (usually taken from michael@0: // __FILE__). michael@0: michael@0: // Note that |N| is the size *with* the null terminator. michael@0: BASE_EXPORT int GetVlogLevelHelper(const char* file_start, size_t N); michael@0: michael@0: template michael@0: int GetVlogLevel(const char (&file)[N]) { michael@0: return GetVlogLevelHelper(file, N); michael@0: } michael@0: michael@0: // Sets the common items you want to be prepended to each log message. michael@0: // process and thread IDs default to off, the timestamp defaults to on. michael@0: // If this function is not called, logging defaults to writing the timestamp michael@0: // only. michael@0: BASE_EXPORT void SetLogItems(bool enable_process_id, bool enable_thread_id, michael@0: bool enable_timestamp, bool enable_tickcount); michael@0: michael@0: // Sets whether or not you'd like to see fatal debug messages popped up in michael@0: // a dialog box or not. michael@0: // Dialogs are not shown by default. michael@0: BASE_EXPORT void SetShowErrorDialogs(bool enable_dialogs); michael@0: michael@0: // Sets the Log Assert Handler that will be used to notify of check failures. michael@0: // The default handler shows a dialog box and then terminate the process, michael@0: // however clients can use this function to override with their own handling michael@0: // (e.g. a silent one for Unit Tests) michael@0: typedef void (*LogAssertHandlerFunction)(const std::string& str); michael@0: BASE_EXPORT void SetLogAssertHandler(LogAssertHandlerFunction handler); michael@0: michael@0: // Sets the Log Report Handler that will be used to notify of check failures michael@0: // in non-debug mode. The default handler shows a dialog box and continues michael@0: // the execution, however clients can use this function to override with their michael@0: // own handling. michael@0: typedef void (*LogReportHandlerFunction)(const std::string& str); michael@0: BASE_EXPORT void SetLogReportHandler(LogReportHandlerFunction handler); michael@0: michael@0: // Sets the Log Message Handler that gets passed every log message before michael@0: // it's sent to other log destinations (if any). michael@0: // Returns true to signal that it handled the message and the message michael@0: // should not be sent to other log destinations. michael@0: typedef bool (*LogMessageHandlerFunction)(int severity, michael@0: const char* file, int line, size_t message_start, const std::string& str); michael@0: BASE_EXPORT void SetLogMessageHandler(LogMessageHandlerFunction handler); michael@0: BASE_EXPORT LogMessageHandlerFunction GetLogMessageHandler(); michael@0: michael@0: typedef int LogSeverity; michael@0: const LogSeverity LOG_VERBOSE = -1; // This is level 1 verbosity michael@0: // Note: the log severities are used to index into the array of names, michael@0: // see log_severity_names. michael@0: const LogSeverity LOG_INFO = 0; michael@0: const LogSeverity LOG_WARNING = 1; michael@0: const LogSeverity LOG_ERROR = 2; michael@0: const LogSeverity LOG_ERROR_REPORT = 3; michael@0: const LogSeverity LOG_FATAL = 4; michael@0: const LogSeverity LOG_NUM_SEVERITIES = 5; michael@0: michael@0: // LOG_DFATAL is LOG_FATAL in debug mode, ERROR in normal mode michael@0: #ifdef NDEBUG michael@0: const LogSeverity LOG_DFATAL = LOG_ERROR; michael@0: #else michael@0: const LogSeverity LOG_DFATAL = LOG_FATAL; michael@0: #endif michael@0: michael@0: // A few definitions of macros that don't generate much code. These are used michael@0: // by LOG() and LOG_IF, etc. Since these are used all over our code, it's michael@0: // better to have compact code for these operations. michael@0: #define COMPACT_GOOGLE_LOG_EX_INFO(ClassName, ...) \ michael@0: logging::ClassName(__FILE__, __LINE__, logging::LOG_INFO , ##__VA_ARGS__) michael@0: #define COMPACT_GOOGLE_LOG_EX_WARNING(ClassName, ...) \ michael@0: logging::ClassName(__FILE__, __LINE__, logging::LOG_WARNING , ##__VA_ARGS__) michael@0: #define COMPACT_GOOGLE_LOG_EX_ERROR(ClassName, ...) \ michael@0: logging::ClassName(__FILE__, __LINE__, logging::LOG_ERROR , ##__VA_ARGS__) michael@0: #define COMPACT_GOOGLE_LOG_EX_ERROR_REPORT(ClassName, ...) \ michael@0: logging::ClassName(__FILE__, __LINE__, \ michael@0: logging::LOG_ERROR_REPORT , ##__VA_ARGS__) michael@0: #define COMPACT_GOOGLE_LOG_EX_FATAL(ClassName, ...) \ michael@0: logging::ClassName(__FILE__, __LINE__, logging::LOG_FATAL , ##__VA_ARGS__) michael@0: #define COMPACT_GOOGLE_LOG_EX_DFATAL(ClassName, ...) \ michael@0: logging::ClassName(__FILE__, __LINE__, logging::LOG_DFATAL , ##__VA_ARGS__) michael@0: michael@0: #define COMPACT_GOOGLE_LOG_INFO \ michael@0: COMPACT_GOOGLE_LOG_EX_INFO(LogMessage) michael@0: #define COMPACT_GOOGLE_LOG_WARNING \ michael@0: COMPACT_GOOGLE_LOG_EX_WARNING(LogMessage) michael@0: #define COMPACT_GOOGLE_LOG_ERROR \ michael@0: COMPACT_GOOGLE_LOG_EX_ERROR(LogMessage) michael@0: #define COMPACT_GOOGLE_LOG_ERROR_REPORT \ michael@0: COMPACT_GOOGLE_LOG_EX_ERROR_REPORT(LogMessage) michael@0: #define COMPACT_GOOGLE_LOG_FATAL \ michael@0: COMPACT_GOOGLE_LOG_EX_FATAL(LogMessage) michael@0: #define COMPACT_GOOGLE_LOG_DFATAL \ michael@0: COMPACT_GOOGLE_LOG_EX_DFATAL(LogMessage) michael@0: michael@0: #if defined(OS_WIN) michael@0: // wingdi.h defines ERROR to be 0. When we call LOG(ERROR), it gets michael@0: // substituted with 0, and it expands to COMPACT_GOOGLE_LOG_0. To allow us michael@0: // to keep using this syntax, we define this macro to do the same thing michael@0: // as COMPACT_GOOGLE_LOG_ERROR, and also define ERROR the same way that michael@0: // the Windows SDK does for consistency. michael@0: #define ERROR 0 michael@0: #define COMPACT_GOOGLE_LOG_EX_0(ClassName, ...) \ michael@0: COMPACT_GOOGLE_LOG_EX_ERROR(ClassName , ##__VA_ARGS__) michael@0: #define COMPACT_GOOGLE_LOG_0 COMPACT_GOOGLE_LOG_ERROR michael@0: // Needed for LOG_IS_ON(ERROR). michael@0: const LogSeverity LOG_0 = LOG_ERROR; michael@0: #endif michael@0: michael@0: // As special cases, we can assume that LOG_IS_ON(ERROR_REPORT) and michael@0: // LOG_IS_ON(FATAL) always hold. Also, LOG_IS_ON(DFATAL) always holds michael@0: // in debug mode. In particular, CHECK()s will always fire if they michael@0: // fail. michael@0: #define LOG_IS_ON(severity) \ michael@0: ((::logging::LOG_ ## severity) >= ::logging::GetMinLogLevel()) michael@0: michael@0: // We can't do any caching tricks with VLOG_IS_ON() like the michael@0: // google-glog version since it requires GCC extensions. This means michael@0: // that using the v-logging functions in conjunction with --vmodule michael@0: // may be slow. michael@0: #define VLOG_IS_ON(verboselevel) \ michael@0: ((verboselevel) <= ::logging::GetVlogLevel(__FILE__)) michael@0: michael@0: // Helper macro which avoids evaluating the arguments to a stream if michael@0: // the condition doesn't hold. michael@0: #define LAZY_STREAM(stream, condition) \ michael@0: !(condition) ? (void) 0 : ::logging::LogMessageVoidify() & (stream) michael@0: michael@0: // We use the preprocessor's merging operator, "##", so that, e.g., michael@0: // LOG(INFO) becomes the token COMPACT_GOOGLE_LOG_INFO. There's some funny michael@0: // subtle difference between ostream member streaming functions (e.g., michael@0: // ostream::operator<<(int) and ostream non-member streaming functions michael@0: // (e.g., ::operator<<(ostream&, string&): it turns out that it's michael@0: // impossible to stream something like a string directly to an unnamed michael@0: // ostream. We employ a neat hack by calling the stream() member michael@0: // function of LogMessage which seems to avoid the problem. michael@0: #define LOG_STREAM(severity) COMPACT_GOOGLE_LOG_ ## severity.stream() michael@0: michael@0: #define LOG(severity) LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity)) michael@0: #define LOG_IF(severity, condition) \ michael@0: LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity) && (condition)) michael@0: michael@0: #define SYSLOG(severity) LOG(severity) michael@0: #define SYSLOG_IF(severity, condition) LOG_IF(severity, condition) michael@0: michael@0: // The VLOG macros log with negative verbosities. michael@0: #define VLOG_STREAM(verbose_level) \ michael@0: logging::LogMessage(__FILE__, __LINE__, -verbose_level).stream() michael@0: michael@0: #define VLOG(verbose_level) \ michael@0: LAZY_STREAM(VLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level)) michael@0: michael@0: #define VLOG_IF(verbose_level, condition) \ michael@0: LAZY_STREAM(VLOG_STREAM(verbose_level), \ michael@0: VLOG_IS_ON(verbose_level) && (condition)) michael@0: michael@0: #if defined (OS_WIN) michael@0: #define VPLOG_STREAM(verbose_level) \ michael@0: logging::Win32ErrorLogMessage(__FILE__, __LINE__, -verbose_level, \ michael@0: ::logging::GetLastSystemErrorCode()).stream() michael@0: #elif defined(OS_POSIX) michael@0: #define VPLOG_STREAM(verbose_level) \ michael@0: logging::ErrnoLogMessage(__FILE__, __LINE__, -verbose_level, \ michael@0: ::logging::GetLastSystemErrorCode()).stream() michael@0: #endif michael@0: michael@0: #define VPLOG(verbose_level) \ michael@0: LAZY_STREAM(VPLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level)) michael@0: michael@0: #define VPLOG_IF(verbose_level, condition) \ michael@0: LAZY_STREAM(VPLOG_STREAM(verbose_level), \ michael@0: VLOG_IS_ON(verbose_level) && (condition)) michael@0: michael@0: // TODO(akalin): Add more VLOG variants, e.g. VPLOG. michael@0: michael@0: #define LOG_ASSERT(condition) \ michael@0: LOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". " michael@0: #define SYSLOG_ASSERT(condition) \ michael@0: SYSLOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". " michael@0: michael@0: #if defined(OS_WIN) michael@0: #define LOG_GETLASTERROR_STREAM(severity) \ michael@0: COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \ michael@0: ::logging::GetLastSystemErrorCode()).stream() michael@0: #define LOG_GETLASTERROR(severity) \ michael@0: LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity), LOG_IS_ON(severity)) michael@0: #define LOG_GETLASTERROR_MODULE_STREAM(severity, module) \ michael@0: COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \ michael@0: ::logging::GetLastSystemErrorCode(), module).stream() michael@0: #define LOG_GETLASTERROR_MODULE(severity, module) \ michael@0: LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity, module), \ michael@0: LOG_IS_ON(severity)) michael@0: // PLOG_STREAM is used by PLOG, which is the usual error logging macro michael@0: // for each platform. michael@0: #define PLOG_STREAM(severity) LOG_GETLASTERROR_STREAM(severity) michael@0: #elif defined(OS_POSIX) michael@0: #define LOG_ERRNO_STREAM(severity) \ michael@0: COMPACT_GOOGLE_LOG_EX_ ## severity(ErrnoLogMessage, \ michael@0: ::logging::GetLastSystemErrorCode()).stream() michael@0: #define LOG_ERRNO(severity) \ michael@0: LAZY_STREAM(LOG_ERRNO_STREAM(severity), LOG_IS_ON(severity)) michael@0: // PLOG_STREAM is used by PLOG, which is the usual error logging macro michael@0: // for each platform. michael@0: #define PLOG_STREAM(severity) LOG_ERRNO_STREAM(severity) michael@0: #endif michael@0: michael@0: #define PLOG(severity) \ michael@0: LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity)) michael@0: michael@0: #define PLOG_IF(severity, condition) \ michael@0: LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity) && (condition)) michael@0: michael@0: #if !defined(NDEBUG) michael@0: // Debug builds always include DCHECK and DLOG. michael@0: #undef LOGGING_IS_OFFICIAL_BUILD michael@0: #define LOGGING_IS_OFFICIAL_BUILD 0 michael@0: #elif defined(OFFICIAL_BUILD) michael@0: // Official release builds always disable and remove DCHECK and DLOG. michael@0: #undef LOGGING_IS_OFFICIAL_BUILD michael@0: #define LOGGING_IS_OFFICIAL_BUILD 1 michael@0: #elif !defined(LOGGING_IS_OFFICIAL_BUILD) michael@0: // Unless otherwise specified, unofficial release builds include michael@0: // DCHECK and DLOG. michael@0: #define LOGGING_IS_OFFICIAL_BUILD 0 michael@0: #endif michael@0: michael@0: // The actual stream used isn't important. michael@0: #define EAT_STREAM_PARAMETERS \ michael@0: true ? (void) 0 : ::logging::LogMessageVoidify() & LOG_STREAM(FATAL) michael@0: michael@0: // CHECK dies with a fatal error if condition is not true. It is *not* michael@0: // controlled by NDEBUG, so the check will be executed regardless of michael@0: // compilation mode. michael@0: // michael@0: // We make sure CHECK et al. always evaluates their arguments, as michael@0: // doing CHECK(FunctionWithSideEffect()) is a common idiom. michael@0: michael@0: #if LOGGING_IS_OFFICIAL_BUILD michael@0: michael@0: // Make all CHECK functions discard their log strings to reduce code michael@0: // bloat for official builds. michael@0: michael@0: // TODO(akalin): This would be more valuable if there were some way to michael@0: // remove BreakDebugger() from the backtrace, perhaps by turning it michael@0: // into a macro (like __debugbreak() on Windows). michael@0: #define CHECK(condition) \ michael@0: !(condition) ? ::base::debug::BreakDebugger() : EAT_STREAM_PARAMETERS michael@0: michael@0: #define PCHECK(condition) CHECK(condition) michael@0: michael@0: #define CHECK_OP(name, op, val1, val2) CHECK((val1) op (val2)) michael@0: michael@0: #else michael@0: michael@0: #define CHECK(condition) \ michael@0: LAZY_STREAM(LOG_STREAM(FATAL), !(condition)) \ michael@0: << "Check failed: " #condition ". " michael@0: michael@0: #define PCHECK(condition) \ michael@0: LAZY_STREAM(PLOG_STREAM(FATAL), !(condition)) \ michael@0: << "Check failed: " #condition ". " michael@0: michael@0: // Helper macro for binary operators. michael@0: // Don't use this macro directly in your code, use CHECK_EQ et al below. michael@0: // michael@0: // TODO(akalin): Rewrite this so that constructs like if (...) michael@0: // CHECK_EQ(...) else { ... } work properly. michael@0: #define CHECK_OP(name, op, val1, val2) \ michael@0: if (std::string* _result = \ michael@0: logging::Check##name##Impl((val1), (val2), \ michael@0: #val1 " " #op " " #val2)) \ michael@0: logging::LogMessage(__FILE__, __LINE__, _result).stream() michael@0: michael@0: #endif michael@0: michael@0: // Build the error message string. This is separate from the "Impl" michael@0: // function template because it is not performance critical and so can michael@0: // be out of line, while the "Impl" code should be inline. Caller michael@0: // takes ownership of the returned string. michael@0: template michael@0: std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) { michael@0: std::ostringstream ss; michael@0: ss << names << " (" << v1 << " vs. " << v2 << ")"; michael@0: std::string* msg = new std::string(ss.str()); michael@0: return msg; michael@0: } michael@0: michael@0: // MSVC doesn't like complex extern templates and DLLs. michael@0: #if !defined(COMPILER_MSVC) michael@0: // Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated michael@0: // in logging.cc. michael@0: extern template BASE_EXPORT std::string* MakeCheckOpString( michael@0: const int&, const int&, const char* names); michael@0: extern template BASE_EXPORT michael@0: std::string* MakeCheckOpString( michael@0: const unsigned long&, const unsigned long&, const char* names); michael@0: extern template BASE_EXPORT michael@0: std::string* MakeCheckOpString( michael@0: const unsigned long&, const unsigned int&, const char* names); michael@0: extern template BASE_EXPORT michael@0: std::string* MakeCheckOpString( michael@0: const unsigned int&, const unsigned long&, const char* names); michael@0: extern template BASE_EXPORT michael@0: std::string* MakeCheckOpString( michael@0: const std::string&, const std::string&, const char* name); michael@0: #endif michael@0: michael@0: // Helper functions for CHECK_OP macro. michael@0: // The (int, int) specialization works around the issue that the compiler michael@0: // will not instantiate the template version of the function on values of michael@0: // unnamed enum type - see comment below. michael@0: #define DEFINE_CHECK_OP_IMPL(name, op) \ michael@0: template \ michael@0: inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \ michael@0: const char* names) { \ michael@0: if (v1 op v2) return NULL; \ michael@0: else return MakeCheckOpString(v1, v2, names); \ michael@0: } \ michael@0: inline std::string* Check##name##Impl(int v1, int v2, const char* names) { \ michael@0: if (v1 op v2) return NULL; \ michael@0: else return MakeCheckOpString(v1, v2, names); \ michael@0: } michael@0: DEFINE_CHECK_OP_IMPL(EQ, ==) michael@0: DEFINE_CHECK_OP_IMPL(NE, !=) michael@0: DEFINE_CHECK_OP_IMPL(LE, <=) michael@0: DEFINE_CHECK_OP_IMPL(LT, < ) michael@0: DEFINE_CHECK_OP_IMPL(GE, >=) michael@0: DEFINE_CHECK_OP_IMPL(GT, > ) michael@0: #undef DEFINE_CHECK_OP_IMPL michael@0: michael@0: #define CHECK_EQ(val1, val2) CHECK_OP(EQ, ==, val1, val2) michael@0: #define CHECK_NE(val1, val2) CHECK_OP(NE, !=, val1, val2) michael@0: #define CHECK_LE(val1, val2) CHECK_OP(LE, <=, val1, val2) michael@0: #define CHECK_LT(val1, val2) CHECK_OP(LT, < , val1, val2) michael@0: #define CHECK_GE(val1, val2) CHECK_OP(GE, >=, val1, val2) michael@0: #define CHECK_GT(val1, val2) CHECK_OP(GT, > , val1, val2) michael@0: michael@0: #if LOGGING_IS_OFFICIAL_BUILD michael@0: // In order to have optimized code for official builds, remove DLOGs and michael@0: // DCHECKs. michael@0: #define ENABLE_DLOG 0 michael@0: #define ENABLE_DCHECK 0 michael@0: michael@0: #elif defined(NDEBUG) michael@0: // Otherwise, if we're a release build, remove DLOGs but not DCHECKs michael@0: // (since those can still be turned on via a command-line flag). michael@0: #define ENABLE_DLOG 0 michael@0: #define ENABLE_DCHECK 1 michael@0: michael@0: #else michael@0: // Otherwise, we're a debug build so enable DLOGs and DCHECKs. michael@0: #define ENABLE_DLOG 1 michael@0: #define ENABLE_DCHECK 1 michael@0: #endif michael@0: michael@0: // Definitions for DLOG et al. michael@0: michael@0: #if ENABLE_DLOG michael@0: michael@0: #define DLOG_IS_ON(severity) LOG_IS_ON(severity) michael@0: #define DLOG_IF(severity, condition) LOG_IF(severity, condition) michael@0: #define DLOG_ASSERT(condition) LOG_ASSERT(condition) michael@0: #define DPLOG_IF(severity, condition) PLOG_IF(severity, condition) michael@0: #define DVLOG_IF(verboselevel, condition) VLOG_IF(verboselevel, condition) michael@0: #define DVPLOG_IF(verboselevel, condition) VPLOG_IF(verboselevel, condition) michael@0: michael@0: #else // ENABLE_DLOG michael@0: michael@0: // If ENABLE_DLOG is off, we want to avoid emitting any references to michael@0: // |condition| (which may reference a variable defined only if NDEBUG michael@0: // is not defined). Contrast this with DCHECK et al., which has michael@0: // different behavior. michael@0: michael@0: #define DLOG_IS_ON(severity) false michael@0: #define DLOG_IF(severity, condition) EAT_STREAM_PARAMETERS michael@0: #define DLOG_ASSERT(condition) EAT_STREAM_PARAMETERS michael@0: #define DPLOG_IF(severity, condition) EAT_STREAM_PARAMETERS michael@0: #define DVLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS michael@0: #define DVPLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS michael@0: michael@0: #endif // ENABLE_DLOG michael@0: michael@0: // DEBUG_MODE is for uses like michael@0: // if (DEBUG_MODE) foo.CheckThatFoo(); michael@0: // instead of michael@0: // #ifndef NDEBUG michael@0: // foo.CheckThatFoo(); michael@0: // #endif michael@0: // michael@0: // We tie its state to ENABLE_DLOG. michael@0: enum { DEBUG_MODE = ENABLE_DLOG }; michael@0: michael@0: #undef ENABLE_DLOG michael@0: michael@0: #define DLOG(severity) \ michael@0: LAZY_STREAM(LOG_STREAM(severity), DLOG_IS_ON(severity)) michael@0: michael@0: #if defined(OS_WIN) michael@0: #define DLOG_GETLASTERROR(severity) \ michael@0: LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity), DLOG_IS_ON(severity)) michael@0: #define DLOG_GETLASTERROR_MODULE(severity, module) \ michael@0: LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity, module), \ michael@0: DLOG_IS_ON(severity)) michael@0: #elif defined(OS_POSIX) michael@0: #define DLOG_ERRNO(severity) \ michael@0: LAZY_STREAM(LOG_ERRNO_STREAM(severity), DLOG_IS_ON(severity)) michael@0: #endif michael@0: michael@0: #define DPLOG(severity) \ michael@0: LAZY_STREAM(PLOG_STREAM(severity), DLOG_IS_ON(severity)) michael@0: michael@0: #define DVLOG(verboselevel) DVLOG_IF(verboselevel, VLOG_IS_ON(verboselevel)) michael@0: michael@0: #define DVPLOG(verboselevel) DVPLOG_IF(verboselevel, VLOG_IS_ON(verboselevel)) michael@0: michael@0: // Definitions for DCHECK et al. michael@0: michael@0: #if ENABLE_DCHECK michael@0: michael@0: #if defined(NDEBUG) michael@0: michael@0: BASE_EXPORT DcheckState get_dcheck_state(); michael@0: BASE_EXPORT void set_dcheck_state(DcheckState state); michael@0: michael@0: #if defined(DCHECK_ALWAYS_ON) michael@0: michael@0: #define DCHECK_IS_ON() true michael@0: #define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \ michael@0: COMPACT_GOOGLE_LOG_EX_FATAL(ClassName , ##__VA_ARGS__) michael@0: #define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_FATAL michael@0: const LogSeverity LOG_DCHECK = LOG_FATAL; michael@0: michael@0: #else michael@0: michael@0: #define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \ michael@0: COMPACT_GOOGLE_LOG_EX_ERROR_REPORT(ClassName , ##__VA_ARGS__) michael@0: #define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_ERROR_REPORT michael@0: const LogSeverity LOG_DCHECK = LOG_ERROR_REPORT; michael@0: #define DCHECK_IS_ON() \ michael@0: ((::logging::get_dcheck_state() == \ michael@0: ::logging::ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS) && \ michael@0: LOG_IS_ON(DCHECK)) michael@0: michael@0: #endif // defined(DCHECK_ALWAYS_ON) michael@0: michael@0: #else // defined(NDEBUG) michael@0: michael@0: // On a regular debug build, we want to have DCHECKs enabled. michael@0: #define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \ michael@0: COMPACT_GOOGLE_LOG_EX_FATAL(ClassName , ##__VA_ARGS__) michael@0: #define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_FATAL michael@0: const LogSeverity LOG_DCHECK = LOG_FATAL; michael@0: #define DCHECK_IS_ON() true michael@0: michael@0: #endif // defined(NDEBUG) michael@0: michael@0: #else // ENABLE_DCHECK michael@0: michael@0: // These are just dummy values since DCHECK_IS_ON() is always false in michael@0: // this case. michael@0: #define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \ michael@0: COMPACT_GOOGLE_LOG_EX_INFO(ClassName , ##__VA_ARGS__) michael@0: #define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_INFO michael@0: const LogSeverity LOG_DCHECK = LOG_INFO; michael@0: #define DCHECK_IS_ON() false michael@0: michael@0: #endif // ENABLE_DCHECK michael@0: #undef ENABLE_DCHECK michael@0: michael@0: // DCHECK et al. make sure to reference |condition| regardless of michael@0: // whether DCHECKs are enabled; this is so that we don't get unused michael@0: // variable warnings if the only use of a variable is in a DCHECK. michael@0: // This behavior is different from DLOG_IF et al. michael@0: michael@0: #define DCHECK(condition) \ michael@0: LAZY_STREAM(LOG_STREAM(DCHECK), DCHECK_IS_ON() && !(condition)) \ michael@0: << "Check failed: " #condition ". " michael@0: michael@0: #define DPCHECK(condition) \ michael@0: LAZY_STREAM(PLOG_STREAM(DCHECK), DCHECK_IS_ON() && !(condition)) \ michael@0: << "Check failed: " #condition ". " michael@0: michael@0: // Helper macro for binary operators. michael@0: // Don't use this macro directly in your code, use DCHECK_EQ et al below. michael@0: #define DCHECK_OP(name, op, val1, val2) \ michael@0: if (DCHECK_IS_ON()) \ michael@0: if (std::string* _result = \ michael@0: logging::Check##name##Impl((val1), (val2), \ michael@0: #val1 " " #op " " #val2)) \ michael@0: logging::LogMessage( \ michael@0: __FILE__, __LINE__, ::logging::LOG_DCHECK, \ michael@0: _result).stream() michael@0: michael@0: // Equality/Inequality checks - compare two values, and log a michael@0: // LOG_DCHECK message including the two values when the result is not michael@0: // as expected. The values must have operator<<(ostream, ...) michael@0: // defined. michael@0: // michael@0: // You may append to the error message like so: michael@0: // DCHECK_NE(1, 2) << ": The world must be ending!"; michael@0: // michael@0: // We are very careful to ensure that each argument is evaluated exactly michael@0: // once, and that anything which is legal to pass as a function argument is michael@0: // legal here. In particular, the arguments may be temporary expressions michael@0: // which will end up being destroyed at the end of the apparent statement, michael@0: // for example: michael@0: // DCHECK_EQ(string("abc")[1], 'b'); michael@0: // michael@0: // WARNING: These may not compile correctly if one of the arguments is a pointer michael@0: // and the other is NULL. To work around this, simply static_cast NULL to the michael@0: // type of the desired pointer. michael@0: michael@0: #define DCHECK_EQ(val1, val2) DCHECK_OP(EQ, ==, val1, val2) michael@0: #define DCHECK_NE(val1, val2) DCHECK_OP(NE, !=, val1, val2) michael@0: #define DCHECK_LE(val1, val2) DCHECK_OP(LE, <=, val1, val2) michael@0: #define DCHECK_LT(val1, val2) DCHECK_OP(LT, < , val1, val2) michael@0: #define DCHECK_GE(val1, val2) DCHECK_OP(GE, >=, val1, val2) michael@0: #define DCHECK_GT(val1, val2) DCHECK_OP(GT, > , val1, val2) michael@0: michael@0: #define NOTREACHED() DCHECK(false) michael@0: michael@0: // Redefine the standard assert to use our nice log files michael@0: #undef assert michael@0: #define assert(x) DLOG_ASSERT(x) michael@0: michael@0: // This class more or less represents a particular log message. You michael@0: // create an instance of LogMessage and then stream stuff to it. michael@0: // When you finish streaming to it, ~LogMessage is called and the michael@0: // full message gets streamed to the appropriate destination. michael@0: // michael@0: // You shouldn't actually use LogMessage's constructor to log things, michael@0: // though. You should use the LOG() macro (and variants thereof) michael@0: // above. michael@0: class BASE_EXPORT LogMessage { michael@0: public: michael@0: LogMessage(const char* file, int line, LogSeverity severity, int ctr); michael@0: michael@0: // Two special constructors that generate reduced amounts of code at michael@0: // LOG call sites for common cases. michael@0: // michael@0: // Used for LOG(INFO): Implied are: michael@0: // severity = LOG_INFO, ctr = 0 michael@0: // michael@0: // Using this constructor instead of the more complex constructor above michael@0: // saves a couple of bytes per call site. michael@0: LogMessage(const char* file, int line); michael@0: michael@0: // Used for LOG(severity) where severity != INFO. Implied michael@0: // are: ctr = 0 michael@0: // michael@0: // Using this constructor instead of the more complex constructor above michael@0: // saves a couple of bytes per call site. michael@0: LogMessage(const char* file, int line, LogSeverity severity); michael@0: michael@0: // A special constructor used for check failures. Takes ownership michael@0: // of the given string. michael@0: // Implied severity = LOG_FATAL michael@0: LogMessage(const char* file, int line, std::string* result); michael@0: michael@0: // A special constructor used for check failures, with the option to michael@0: // specify severity. Takes ownership of the given string. michael@0: LogMessage(const char* file, int line, LogSeverity severity, michael@0: std::string* result); michael@0: michael@0: ~LogMessage(); michael@0: michael@0: std::ostream& stream() { return stream_; } michael@0: michael@0: private: michael@0: void Init(const char* file, int line); michael@0: michael@0: LogSeverity severity_; michael@0: std::ostringstream stream_; michael@0: size_t message_start_; // Offset of the start of the message (past prefix michael@0: // info). michael@0: // The file and line information passed in to the constructor. michael@0: const char* file_; michael@0: const int line_; michael@0: michael@0: #if defined(OS_WIN) michael@0: // Stores the current value of GetLastError in the constructor and restores michael@0: // it in the destructor by calling SetLastError. michael@0: // This is useful since the LogMessage class uses a lot of Win32 calls michael@0: // that will lose the value of GLE and the code that called the log function michael@0: // will have lost the thread error value when the log call returns. michael@0: class SaveLastError { michael@0: public: michael@0: SaveLastError(); michael@0: ~SaveLastError(); michael@0: michael@0: unsigned long get_error() const { return last_error_; } michael@0: michael@0: protected: michael@0: unsigned long last_error_; michael@0: }; michael@0: michael@0: SaveLastError last_error_; michael@0: #endif michael@0: michael@0: DISALLOW_COPY_AND_ASSIGN(LogMessage); michael@0: }; michael@0: michael@0: // A non-macro interface to the log facility; (useful michael@0: // when the logging level is not a compile-time constant). michael@0: inline void LogAtLevel(int const log_level, std::string const &msg) { michael@0: LogMessage(__FILE__, __LINE__, log_level).stream() << msg; michael@0: } michael@0: michael@0: // This class is used to explicitly ignore values in the conditional michael@0: // logging macros. This avoids compiler warnings like "value computed michael@0: // is not used" and "statement has no effect". michael@0: class LogMessageVoidify { michael@0: public: michael@0: LogMessageVoidify() { } michael@0: // This has to be an operator with a precedence lower than << but michael@0: // higher than ?: michael@0: void operator&(std::ostream&) { } michael@0: }; michael@0: michael@0: #if defined(OS_WIN) michael@0: typedef unsigned long SystemErrorCode; michael@0: #elif defined(OS_POSIX) michael@0: typedef int SystemErrorCode; michael@0: #endif michael@0: michael@0: // Alias for ::GetLastError() on Windows and errno on POSIX. Avoids having to michael@0: // pull in windows.h just for GetLastError() and DWORD. michael@0: BASE_EXPORT SystemErrorCode GetLastSystemErrorCode(); michael@0: michael@0: #if defined(OS_WIN) michael@0: // Appends a formatted system message of the GetLastError() type. michael@0: class BASE_EXPORT Win32ErrorLogMessage { michael@0: public: michael@0: Win32ErrorLogMessage(const char* file, michael@0: int line, michael@0: LogSeverity severity, michael@0: SystemErrorCode err, michael@0: const char* module); michael@0: michael@0: Win32ErrorLogMessage(const char* file, michael@0: int line, michael@0: LogSeverity severity, michael@0: SystemErrorCode err); michael@0: michael@0: // Appends the error message before destructing the encapsulated class. michael@0: ~Win32ErrorLogMessage(); michael@0: michael@0: std::ostream& stream() { return log_message_.stream(); } michael@0: michael@0: private: michael@0: SystemErrorCode err_; michael@0: // Optional name of the module defining the error. michael@0: const char* module_; michael@0: LogMessage log_message_; michael@0: michael@0: DISALLOW_COPY_AND_ASSIGN(Win32ErrorLogMessage); michael@0: }; michael@0: #elif defined(OS_POSIX) michael@0: // Appends a formatted system message of the errno type michael@0: class BASE_EXPORT ErrnoLogMessage { michael@0: public: michael@0: ErrnoLogMessage(const char* file, michael@0: int line, michael@0: LogSeverity severity, michael@0: SystemErrorCode err); michael@0: michael@0: // Appends the error message before destructing the encapsulated class. michael@0: ~ErrnoLogMessage(); michael@0: michael@0: std::ostream& stream() { return log_message_.stream(); } michael@0: michael@0: private: michael@0: SystemErrorCode err_; michael@0: LogMessage log_message_; michael@0: michael@0: DISALLOW_COPY_AND_ASSIGN(ErrnoLogMessage); michael@0: }; michael@0: #endif // OS_WIN michael@0: michael@0: // Closes the log file explicitly if open. michael@0: // NOTE: Since the log file is opened as necessary by the action of logging michael@0: // statements, there's no guarantee that it will stay closed michael@0: // after this call. michael@0: BASE_EXPORT void CloseLogFile(); michael@0: michael@0: // Async signal safe logging mechanism. michael@0: BASE_EXPORT void RawLog(int level, const char* message); michael@0: michael@0: #define RAW_LOG(level, message) logging::RawLog(logging::LOG_ ## level, message) michael@0: michael@0: #define RAW_CHECK(condition) \ michael@0: do { \ michael@0: if (!(condition)) \ michael@0: logging::RawLog(logging::LOG_FATAL, "Check failed: " #condition "\n"); \ michael@0: } while (0) michael@0: michael@0: #if defined(OS_WIN) michael@0: // Returns the default log file path. michael@0: BASE_EXPORT std::wstring GetLogFileFullPath(); michael@0: #endif michael@0: michael@0: } // namespace logging michael@0: michael@0: // These functions are provided as a convenience for logging, which is where we michael@0: // use streams (it is against Google style to use streams in other places). It michael@0: // is designed to allow you to emit non-ASCII Unicode strings to the log file, michael@0: // which is normally ASCII. It is relatively slow, so try not to use it for michael@0: // common cases. Non-ASCII characters will be converted to UTF-8 by these michael@0: // operators. michael@0: BASE_EXPORT std::ostream& operator<<(std::ostream& out, const wchar_t* wstr); michael@0: inline std::ostream& operator<<(std::ostream& out, const std::wstring& wstr) { michael@0: return out << wstr.c_str(); michael@0: } michael@0: michael@0: // The NOTIMPLEMENTED() macro annotates codepaths which have michael@0: // not been implemented yet. michael@0: // michael@0: // The implementation of this macro is controlled by NOTIMPLEMENTED_POLICY: michael@0: // 0 -- Do nothing (stripped by compiler) michael@0: // 1 -- Warn at compile time michael@0: // 2 -- Fail at compile time michael@0: // 3 -- Fail at runtime (DCHECK) michael@0: // 4 -- [default] LOG(ERROR) at runtime michael@0: // 5 -- LOG(ERROR) at runtime, only once per call-site michael@0: michael@0: #ifndef NOTIMPLEMENTED_POLICY michael@0: #if defined(OS_ANDROID) && defined(OFFICIAL_BUILD) michael@0: #define NOTIMPLEMENTED_POLICY 0 michael@0: #else michael@0: // Select default policy: LOG(ERROR) michael@0: #define NOTIMPLEMENTED_POLICY 4 michael@0: #endif michael@0: #endif michael@0: michael@0: #if defined(COMPILER_GCC) michael@0: // On Linux, with GCC, we can use __PRETTY_FUNCTION__ to get the demangled name michael@0: // of the current function in the NOTIMPLEMENTED message. michael@0: #define NOTIMPLEMENTED_MSG "Not implemented reached in " << __PRETTY_FUNCTION__ michael@0: #else michael@0: #define NOTIMPLEMENTED_MSG "NOT IMPLEMENTED" michael@0: #endif michael@0: michael@0: #if NOTIMPLEMENTED_POLICY == 0 michael@0: #define NOTIMPLEMENTED() EAT_STREAM_PARAMETERS michael@0: #elif NOTIMPLEMENTED_POLICY == 1 michael@0: // TODO, figure out how to generate a warning michael@0: #define NOTIMPLEMENTED() COMPILE_ASSERT(false, NOT_IMPLEMENTED) michael@0: #elif NOTIMPLEMENTED_POLICY == 2 michael@0: #define NOTIMPLEMENTED() COMPILE_ASSERT(false, NOT_IMPLEMENTED) michael@0: #elif NOTIMPLEMENTED_POLICY == 3 michael@0: #define NOTIMPLEMENTED() NOTREACHED() michael@0: #elif NOTIMPLEMENTED_POLICY == 4 michael@0: #define NOTIMPLEMENTED() LOG(ERROR) << NOTIMPLEMENTED_MSG michael@0: #elif NOTIMPLEMENTED_POLICY == 5 michael@0: #define NOTIMPLEMENTED() do {\ michael@0: static bool logged_once = false;\ michael@0: LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ michael@0: logged_once = true;\ michael@0: } while(0);\ michael@0: EAT_STREAM_PARAMETERS michael@0: #endif michael@0: michael@0: #endif // BASE_LOGGING_H_