security/sandbox/chromium/base/logging.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/sandbox/chromium/base/logging.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,1022 @@
     1.4 +// Copyright (c) 2012 The Chromium Authors. All rights reserved.
     1.5 +// Use of this source code is governed by a BSD-style license that can be
     1.6 +// found in the LICENSE file.
     1.7 +
     1.8 +#ifndef BASE_LOGGING_H_
     1.9 +#define BASE_LOGGING_H_
    1.10 +
    1.11 +#include <cassert>
    1.12 +#include <string>
    1.13 +#include <cstring>
    1.14 +#include <sstream>
    1.15 +
    1.16 +#include "base/base_export.h"
    1.17 +#include "base/basictypes.h"
    1.18 +#include "base/debug/debugger.h"
    1.19 +#include "build/build_config.h"
    1.20 +
    1.21 +//
    1.22 +// Optional message capabilities
    1.23 +// -----------------------------
    1.24 +// Assertion failed messages and fatal errors are displayed in a dialog box
    1.25 +// before the application exits. However, running this UI creates a message
    1.26 +// loop, which causes application messages to be processed and potentially
    1.27 +// dispatched to existing application windows. Since the application is in a
    1.28 +// bad state when this assertion dialog is displayed, these messages may not
    1.29 +// get processed and hang the dialog, or the application might go crazy.
    1.30 +//
    1.31 +// Therefore, it can be beneficial to display the error dialog in a separate
    1.32 +// process from the main application. When the logging system needs to display
    1.33 +// a fatal error dialog box, it will look for a program called
    1.34 +// "DebugMessage.exe" in the same directory as the application executable. It
    1.35 +// will run this application with the message as the command line, and will
    1.36 +// not include the name of the application as is traditional for easier
    1.37 +// parsing.
    1.38 +//
    1.39 +// The code for DebugMessage.exe is only one line. In WinMain, do:
    1.40 +//   MessageBox(NULL, GetCommandLineW(), L"Fatal Error", 0);
    1.41 +//
    1.42 +// If DebugMessage.exe is not found, the logging code will use a normal
    1.43 +// MessageBox, potentially causing the problems discussed above.
    1.44 +
    1.45 +
    1.46 +// Instructions
    1.47 +// ------------
    1.48 +//
    1.49 +// Make a bunch of macros for logging.  The way to log things is to stream
    1.50 +// things to LOG(<a particular severity level>).  E.g.,
    1.51 +//
    1.52 +//   LOG(INFO) << "Found " << num_cookies << " cookies";
    1.53 +//
    1.54 +// You can also do conditional logging:
    1.55 +//
    1.56 +//   LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
    1.57 +//
    1.58 +// The above will cause log messages to be output on the 1st, 11th, 21st, ...
    1.59 +// times it is executed.  Note that the special COUNTER value is used to
    1.60 +// identify which repetition is happening.
    1.61 +//
    1.62 +// The CHECK(condition) macro is active in both debug and release builds and
    1.63 +// effectively performs a LOG(FATAL) which terminates the process and
    1.64 +// generates a crashdump unless a debugger is attached.
    1.65 +//
    1.66 +// There are also "debug mode" logging macros like the ones above:
    1.67 +//
    1.68 +//   DLOG(INFO) << "Found cookies";
    1.69 +//
    1.70 +//   DLOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
    1.71 +//
    1.72 +// All "debug mode" logging is compiled away to nothing for non-debug mode
    1.73 +// compiles.  LOG_IF and development flags also work well together
    1.74 +// because the code can be compiled away sometimes.
    1.75 +//
    1.76 +// We also have
    1.77 +//
    1.78 +//   LOG_ASSERT(assertion);
    1.79 +//   DLOG_ASSERT(assertion);
    1.80 +//
    1.81 +// which is syntactic sugar for {,D}LOG_IF(FATAL, assert fails) << assertion;
    1.82 +//
    1.83 +// There are "verbose level" logging macros.  They look like
    1.84 +//
    1.85 +//   VLOG(1) << "I'm printed when you run the program with --v=1 or more";
    1.86 +//   VLOG(2) << "I'm printed when you run the program with --v=2 or more";
    1.87 +//
    1.88 +// These always log at the INFO log level (when they log at all).
    1.89 +// The verbose logging can also be turned on module-by-module.  For instance,
    1.90 +//    --vmodule=profile=2,icon_loader=1,browser_*=3,*/chromeos/*=4 --v=0
    1.91 +// will cause:
    1.92 +//   a. VLOG(2) and lower messages to be printed from profile.{h,cc}
    1.93 +//   b. VLOG(1) and lower messages to be printed from icon_loader.{h,cc}
    1.94 +//   c. VLOG(3) and lower messages to be printed from files prefixed with
    1.95 +//      "browser"
    1.96 +//   d. VLOG(4) and lower messages to be printed from files under a
    1.97 +//     "chromeos" directory.
    1.98 +//   e. VLOG(0) and lower messages to be printed from elsewhere
    1.99 +//
   1.100 +// The wildcarding functionality shown by (c) supports both '*' (match
   1.101 +// 0 or more characters) and '?' (match any single character)
   1.102 +// wildcards.  Any pattern containing a forward or backward slash will
   1.103 +// be tested against the whole pathname and not just the module.
   1.104 +// E.g., "*/foo/bar/*=2" would change the logging level for all code
   1.105 +// in source files under a "foo/bar" directory.
   1.106 +//
   1.107 +// There's also VLOG_IS_ON(n) "verbose level" condition macro. To be used as
   1.108 +//
   1.109 +//   if (VLOG_IS_ON(2)) {
   1.110 +//     // do some logging preparation and logging
   1.111 +//     // that can't be accomplished with just VLOG(2) << ...;
   1.112 +//   }
   1.113 +//
   1.114 +// There is also a VLOG_IF "verbose level" condition macro for sample
   1.115 +// cases, when some extra computation and preparation for logs is not
   1.116 +// needed.
   1.117 +//
   1.118 +//   VLOG_IF(1, (size > 1024))
   1.119 +//      << "I'm printed when size is more than 1024 and when you run the "
   1.120 +//         "program with --v=1 or more";
   1.121 +//
   1.122 +// We also override the standard 'assert' to use 'DLOG_ASSERT'.
   1.123 +//
   1.124 +// Lastly, there is:
   1.125 +//
   1.126 +//   PLOG(ERROR) << "Couldn't do foo";
   1.127 +//   DPLOG(ERROR) << "Couldn't do foo";
   1.128 +//   PLOG_IF(ERROR, cond) << "Couldn't do foo";
   1.129 +//   DPLOG_IF(ERROR, cond) << "Couldn't do foo";
   1.130 +//   PCHECK(condition) << "Couldn't do foo";
   1.131 +//   DPCHECK(condition) << "Couldn't do foo";
   1.132 +//
   1.133 +// which append the last system error to the message in string form (taken from
   1.134 +// GetLastError() on Windows and errno on POSIX).
   1.135 +//
   1.136 +// The supported severity levels for macros that allow you to specify one
   1.137 +// are (in increasing order of severity) INFO, WARNING, ERROR, ERROR_REPORT,
   1.138 +// and FATAL.
   1.139 +//
   1.140 +// Very important: logging a message at the FATAL severity level causes
   1.141 +// the program to terminate (after the message is logged).
   1.142 +//
   1.143 +// Note the special severity of ERROR_REPORT only available/relevant in normal
   1.144 +// mode, which displays error dialog without terminating the program. There is
   1.145 +// no error dialog for severity ERROR or below in normal mode.
   1.146 +//
   1.147 +// There is also the special severity of DFATAL, which logs FATAL in
   1.148 +// debug mode, ERROR in normal mode.
   1.149 +
   1.150 +namespace logging {
   1.151 +
   1.152 +// TODO(avi): do we want to do a unification of character types here?
   1.153 +#if defined(OS_WIN)
   1.154 +typedef wchar_t PathChar;
   1.155 +#else
   1.156 +typedef char PathChar;
   1.157 +#endif
   1.158 +
   1.159 +// Where to record logging output? A flat file and/or system debug log
   1.160 +// via OutputDebugString.
   1.161 +enum LoggingDestination {
   1.162 +  LOG_NONE                = 0,
   1.163 +  LOG_TO_FILE             = 1 << 0,
   1.164 +  LOG_TO_SYSTEM_DEBUG_LOG = 1 << 1,
   1.165 +
   1.166 +  LOG_TO_ALL = LOG_TO_FILE | LOG_TO_SYSTEM_DEBUG_LOG,
   1.167 +
   1.168 +  // On Windows, use a file next to the exe; on POSIX platforms, where
   1.169 +  // it may not even be possible to locate the executable on disk, use
   1.170 +  // stderr.
   1.171 +#if defined(OS_WIN)
   1.172 +  LOG_DEFAULT = LOG_TO_FILE,
   1.173 +#elif defined(OS_POSIX)
   1.174 +  LOG_DEFAULT = LOG_TO_SYSTEM_DEBUG_LOG,
   1.175 +#endif
   1.176 +};
   1.177 +
   1.178 +// Indicates that the log file should be locked when being written to.
   1.179 +// Unless there is only one single-threaded process that is logging to
   1.180 +// the log file, the file should be locked during writes to make each
   1.181 +// log outut atomic. Other writers will block.
   1.182 +//
   1.183 +// All processes writing to the log file must have their locking set for it to
   1.184 +// work properly. Defaults to LOCK_LOG_FILE.
   1.185 +enum LogLockingState { LOCK_LOG_FILE, DONT_LOCK_LOG_FILE };
   1.186 +
   1.187 +// On startup, should we delete or append to an existing log file (if any)?
   1.188 +// Defaults to APPEND_TO_OLD_LOG_FILE.
   1.189 +enum OldFileDeletionState { DELETE_OLD_LOG_FILE, APPEND_TO_OLD_LOG_FILE };
   1.190 +
   1.191 +enum DcheckState {
   1.192 +  DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS,
   1.193 +  ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS
   1.194 +};
   1.195 +
   1.196 +struct BASE_EXPORT LoggingSettings {
   1.197 +  // The defaults values are:
   1.198 +  //
   1.199 +  //  logging_dest: LOG_DEFAULT
   1.200 +  //  log_file:     NULL
   1.201 +  //  lock_log:     LOCK_LOG_FILE
   1.202 +  //  delete_old:   APPEND_TO_OLD_LOG_FILE
   1.203 +  //  dcheck_state: DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS
   1.204 +  LoggingSettings();
   1.205 +
   1.206 +  LoggingDestination logging_dest;
   1.207 +
   1.208 +  // The three settings below have an effect only when LOG_TO_FILE is
   1.209 +  // set in |logging_dest|.
   1.210 +  const PathChar* log_file;
   1.211 +  LogLockingState lock_log;
   1.212 +  OldFileDeletionState delete_old;
   1.213 +
   1.214 +  DcheckState dcheck_state;
   1.215 +};
   1.216 +
   1.217 +// Define different names for the BaseInitLoggingImpl() function depending on
   1.218 +// whether NDEBUG is defined or not so that we'll fail to link if someone tries
   1.219 +// to compile logging.cc with NDEBUG but includes logging.h without defining it,
   1.220 +// or vice versa.
   1.221 +#if NDEBUG
   1.222 +#define BaseInitLoggingImpl BaseInitLoggingImpl_built_with_NDEBUG
   1.223 +#else
   1.224 +#define BaseInitLoggingImpl BaseInitLoggingImpl_built_without_NDEBUG
   1.225 +#endif
   1.226 +
   1.227 +// Implementation of the InitLogging() method declared below.  We use a
   1.228 +// more-specific name so we can #define it above without affecting other code
   1.229 +// that has named stuff "InitLogging".
   1.230 +BASE_EXPORT bool BaseInitLoggingImpl(const LoggingSettings& settings);
   1.231 +
   1.232 +// Sets the log file name and other global logging state. Calling this function
   1.233 +// is recommended, and is normally done at the beginning of application init.
   1.234 +// If you don't call it, all the flags will be initialized to their default
   1.235 +// values, and there is a race condition that may leak a critical section
   1.236 +// object if two threads try to do the first log at the same time.
   1.237 +// See the definition of the enums above for descriptions and default values.
   1.238 +//
   1.239 +// The default log file is initialized to "debug.log" in the application
   1.240 +// directory. You probably don't want this, especially since the program
   1.241 +// directory may not be writable on an enduser's system.
   1.242 +//
   1.243 +// This function may be called a second time to re-direct logging (e.g after
   1.244 +// loging in to a user partition), however it should never be called more than
   1.245 +// twice.
   1.246 +inline bool InitLogging(const LoggingSettings& settings) {
   1.247 +  return BaseInitLoggingImpl(settings);
   1.248 +}
   1.249 +
   1.250 +// Sets the log level. Anything at or above this level will be written to the
   1.251 +// log file/displayed to the user (if applicable). Anything below this level
   1.252 +// will be silently ignored. The log level defaults to 0 (everything is logged
   1.253 +// up to level INFO) if this function is not called.
   1.254 +// Note that log messages for VLOG(x) are logged at level -x, so setting
   1.255 +// the min log level to negative values enables verbose logging.
   1.256 +BASE_EXPORT void SetMinLogLevel(int level);
   1.257 +
   1.258 +// Gets the current log level.
   1.259 +BASE_EXPORT int GetMinLogLevel();
   1.260 +
   1.261 +// Gets the VLOG default verbosity level.
   1.262 +BASE_EXPORT int GetVlogVerbosity();
   1.263 +
   1.264 +// Gets the current vlog level for the given file (usually taken from
   1.265 +// __FILE__).
   1.266 +
   1.267 +// Note that |N| is the size *with* the null terminator.
   1.268 +BASE_EXPORT int GetVlogLevelHelper(const char* file_start, size_t N);
   1.269 +
   1.270 +template <size_t N>
   1.271 +int GetVlogLevel(const char (&file)[N]) {
   1.272 +  return GetVlogLevelHelper(file, N);
   1.273 +}
   1.274 +
   1.275 +// Sets the common items you want to be prepended to each log message.
   1.276 +// process and thread IDs default to off, the timestamp defaults to on.
   1.277 +// If this function is not called, logging defaults to writing the timestamp
   1.278 +// only.
   1.279 +BASE_EXPORT void SetLogItems(bool enable_process_id, bool enable_thread_id,
   1.280 +                             bool enable_timestamp, bool enable_tickcount);
   1.281 +
   1.282 +// Sets whether or not you'd like to see fatal debug messages popped up in
   1.283 +// a dialog box or not.
   1.284 +// Dialogs are not shown by default.
   1.285 +BASE_EXPORT void SetShowErrorDialogs(bool enable_dialogs);
   1.286 +
   1.287 +// Sets the Log Assert Handler that will be used to notify of check failures.
   1.288 +// The default handler shows a dialog box and then terminate the process,
   1.289 +// however clients can use this function to override with their own handling
   1.290 +// (e.g. a silent one for Unit Tests)
   1.291 +typedef void (*LogAssertHandlerFunction)(const std::string& str);
   1.292 +BASE_EXPORT void SetLogAssertHandler(LogAssertHandlerFunction handler);
   1.293 +
   1.294 +// Sets the Log Report Handler that will be used to notify of check failures
   1.295 +// in non-debug mode. The default handler shows a dialog box and continues
   1.296 +// the execution, however clients can use this function to override with their
   1.297 +// own handling.
   1.298 +typedef void (*LogReportHandlerFunction)(const std::string& str);
   1.299 +BASE_EXPORT void SetLogReportHandler(LogReportHandlerFunction handler);
   1.300 +
   1.301 +// Sets the Log Message Handler that gets passed every log message before
   1.302 +// it's sent to other log destinations (if any).
   1.303 +// Returns true to signal that it handled the message and the message
   1.304 +// should not be sent to other log destinations.
   1.305 +typedef bool (*LogMessageHandlerFunction)(int severity,
   1.306 +    const char* file, int line, size_t message_start, const std::string& str);
   1.307 +BASE_EXPORT void SetLogMessageHandler(LogMessageHandlerFunction handler);
   1.308 +BASE_EXPORT LogMessageHandlerFunction GetLogMessageHandler();
   1.309 +
   1.310 +typedef int LogSeverity;
   1.311 +const LogSeverity LOG_VERBOSE = -1;  // This is level 1 verbosity
   1.312 +// Note: the log severities are used to index into the array of names,
   1.313 +// see log_severity_names.
   1.314 +const LogSeverity LOG_INFO = 0;
   1.315 +const LogSeverity LOG_WARNING = 1;
   1.316 +const LogSeverity LOG_ERROR = 2;
   1.317 +const LogSeverity LOG_ERROR_REPORT = 3;
   1.318 +const LogSeverity LOG_FATAL = 4;
   1.319 +const LogSeverity LOG_NUM_SEVERITIES = 5;
   1.320 +
   1.321 +// LOG_DFATAL is LOG_FATAL in debug mode, ERROR in normal mode
   1.322 +#ifdef NDEBUG
   1.323 +const LogSeverity LOG_DFATAL = LOG_ERROR;
   1.324 +#else
   1.325 +const LogSeverity LOG_DFATAL = LOG_FATAL;
   1.326 +#endif
   1.327 +
   1.328 +// A few definitions of macros that don't generate much code. These are used
   1.329 +// by LOG() and LOG_IF, etc. Since these are used all over our code, it's
   1.330 +// better to have compact code for these operations.
   1.331 +#define COMPACT_GOOGLE_LOG_EX_INFO(ClassName, ...) \
   1.332 +  logging::ClassName(__FILE__, __LINE__, logging::LOG_INFO , ##__VA_ARGS__)
   1.333 +#define COMPACT_GOOGLE_LOG_EX_WARNING(ClassName, ...) \
   1.334 +  logging::ClassName(__FILE__, __LINE__, logging::LOG_WARNING , ##__VA_ARGS__)
   1.335 +#define COMPACT_GOOGLE_LOG_EX_ERROR(ClassName, ...) \
   1.336 +  logging::ClassName(__FILE__, __LINE__, logging::LOG_ERROR , ##__VA_ARGS__)
   1.337 +#define COMPACT_GOOGLE_LOG_EX_ERROR_REPORT(ClassName, ...) \
   1.338 +  logging::ClassName(__FILE__, __LINE__, \
   1.339 +                     logging::LOG_ERROR_REPORT , ##__VA_ARGS__)
   1.340 +#define COMPACT_GOOGLE_LOG_EX_FATAL(ClassName, ...) \
   1.341 +  logging::ClassName(__FILE__, __LINE__, logging::LOG_FATAL , ##__VA_ARGS__)
   1.342 +#define COMPACT_GOOGLE_LOG_EX_DFATAL(ClassName, ...) \
   1.343 +  logging::ClassName(__FILE__, __LINE__, logging::LOG_DFATAL , ##__VA_ARGS__)
   1.344 +
   1.345 +#define COMPACT_GOOGLE_LOG_INFO \
   1.346 +  COMPACT_GOOGLE_LOG_EX_INFO(LogMessage)
   1.347 +#define COMPACT_GOOGLE_LOG_WARNING \
   1.348 +  COMPACT_GOOGLE_LOG_EX_WARNING(LogMessage)
   1.349 +#define COMPACT_GOOGLE_LOG_ERROR \
   1.350 +  COMPACT_GOOGLE_LOG_EX_ERROR(LogMessage)
   1.351 +#define COMPACT_GOOGLE_LOG_ERROR_REPORT \
   1.352 +  COMPACT_GOOGLE_LOG_EX_ERROR_REPORT(LogMessage)
   1.353 +#define COMPACT_GOOGLE_LOG_FATAL \
   1.354 +  COMPACT_GOOGLE_LOG_EX_FATAL(LogMessage)
   1.355 +#define COMPACT_GOOGLE_LOG_DFATAL \
   1.356 +  COMPACT_GOOGLE_LOG_EX_DFATAL(LogMessage)
   1.357 +
   1.358 +#if defined(OS_WIN)
   1.359 +// wingdi.h defines ERROR to be 0. When we call LOG(ERROR), it gets
   1.360 +// substituted with 0, and it expands to COMPACT_GOOGLE_LOG_0. To allow us
   1.361 +// to keep using this syntax, we define this macro to do the same thing
   1.362 +// as COMPACT_GOOGLE_LOG_ERROR, and also define ERROR the same way that
   1.363 +// the Windows SDK does for consistency.
   1.364 +#define ERROR 0
   1.365 +#define COMPACT_GOOGLE_LOG_EX_0(ClassName, ...) \
   1.366 +  COMPACT_GOOGLE_LOG_EX_ERROR(ClassName , ##__VA_ARGS__)
   1.367 +#define COMPACT_GOOGLE_LOG_0 COMPACT_GOOGLE_LOG_ERROR
   1.368 +// Needed for LOG_IS_ON(ERROR).
   1.369 +const LogSeverity LOG_0 = LOG_ERROR;
   1.370 +#endif
   1.371 +
   1.372 +// As special cases, we can assume that LOG_IS_ON(ERROR_REPORT) and
   1.373 +// LOG_IS_ON(FATAL) always hold.  Also, LOG_IS_ON(DFATAL) always holds
   1.374 +// in debug mode.  In particular, CHECK()s will always fire if they
   1.375 +// fail.
   1.376 +#define LOG_IS_ON(severity) \
   1.377 +  ((::logging::LOG_ ## severity) >= ::logging::GetMinLogLevel())
   1.378 +
   1.379 +// We can't do any caching tricks with VLOG_IS_ON() like the
   1.380 +// google-glog version since it requires GCC extensions.  This means
   1.381 +// that using the v-logging functions in conjunction with --vmodule
   1.382 +// may be slow.
   1.383 +#define VLOG_IS_ON(verboselevel) \
   1.384 +  ((verboselevel) <= ::logging::GetVlogLevel(__FILE__))
   1.385 +
   1.386 +// Helper macro which avoids evaluating the arguments to a stream if
   1.387 +// the condition doesn't hold.
   1.388 +#define LAZY_STREAM(stream, condition)                                  \
   1.389 +  !(condition) ? (void) 0 : ::logging::LogMessageVoidify() & (stream)
   1.390 +
   1.391 +// We use the preprocessor's merging operator, "##", so that, e.g.,
   1.392 +// LOG(INFO) becomes the token COMPACT_GOOGLE_LOG_INFO.  There's some funny
   1.393 +// subtle difference between ostream member streaming functions (e.g.,
   1.394 +// ostream::operator<<(int) and ostream non-member streaming functions
   1.395 +// (e.g., ::operator<<(ostream&, string&): it turns out that it's
   1.396 +// impossible to stream something like a string directly to an unnamed
   1.397 +// ostream. We employ a neat hack by calling the stream() member
   1.398 +// function of LogMessage which seems to avoid the problem.
   1.399 +#define LOG_STREAM(severity) COMPACT_GOOGLE_LOG_ ## severity.stream()
   1.400 +
   1.401 +#define LOG(severity) LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity))
   1.402 +#define LOG_IF(severity, condition) \
   1.403 +  LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity) && (condition))
   1.404 +
   1.405 +#define SYSLOG(severity) LOG(severity)
   1.406 +#define SYSLOG_IF(severity, condition) LOG_IF(severity, condition)
   1.407 +
   1.408 +// The VLOG macros log with negative verbosities.
   1.409 +#define VLOG_STREAM(verbose_level) \
   1.410 +  logging::LogMessage(__FILE__, __LINE__, -verbose_level).stream()
   1.411 +
   1.412 +#define VLOG(verbose_level) \
   1.413 +  LAZY_STREAM(VLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level))
   1.414 +
   1.415 +#define VLOG_IF(verbose_level, condition) \
   1.416 +  LAZY_STREAM(VLOG_STREAM(verbose_level), \
   1.417 +      VLOG_IS_ON(verbose_level) && (condition))
   1.418 +
   1.419 +#if defined (OS_WIN)
   1.420 +#define VPLOG_STREAM(verbose_level) \
   1.421 +  logging::Win32ErrorLogMessage(__FILE__, __LINE__, -verbose_level, \
   1.422 +    ::logging::GetLastSystemErrorCode()).stream()
   1.423 +#elif defined(OS_POSIX)
   1.424 +#define VPLOG_STREAM(verbose_level) \
   1.425 +  logging::ErrnoLogMessage(__FILE__, __LINE__, -verbose_level, \
   1.426 +    ::logging::GetLastSystemErrorCode()).stream()
   1.427 +#endif
   1.428 +
   1.429 +#define VPLOG(verbose_level) \
   1.430 +  LAZY_STREAM(VPLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level))
   1.431 +
   1.432 +#define VPLOG_IF(verbose_level, condition) \
   1.433 +  LAZY_STREAM(VPLOG_STREAM(verbose_level), \
   1.434 +    VLOG_IS_ON(verbose_level) && (condition))
   1.435 +
   1.436 +// TODO(akalin): Add more VLOG variants, e.g. VPLOG.
   1.437 +
   1.438 +#define LOG_ASSERT(condition)  \
   1.439 +  LOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". "
   1.440 +#define SYSLOG_ASSERT(condition) \
   1.441 +  SYSLOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". "
   1.442 +
   1.443 +#if defined(OS_WIN)
   1.444 +#define LOG_GETLASTERROR_STREAM(severity) \
   1.445 +  COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \
   1.446 +      ::logging::GetLastSystemErrorCode()).stream()
   1.447 +#define LOG_GETLASTERROR(severity) \
   1.448 +  LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity), LOG_IS_ON(severity))
   1.449 +#define LOG_GETLASTERROR_MODULE_STREAM(severity, module) \
   1.450 +  COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \
   1.451 +      ::logging::GetLastSystemErrorCode(), module).stream()
   1.452 +#define LOG_GETLASTERROR_MODULE(severity, module)                       \
   1.453 +  LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity, module),                \
   1.454 +              LOG_IS_ON(severity))
   1.455 +// PLOG_STREAM is used by PLOG, which is the usual error logging macro
   1.456 +// for each platform.
   1.457 +#define PLOG_STREAM(severity) LOG_GETLASTERROR_STREAM(severity)
   1.458 +#elif defined(OS_POSIX)
   1.459 +#define LOG_ERRNO_STREAM(severity) \
   1.460 +  COMPACT_GOOGLE_LOG_EX_ ## severity(ErrnoLogMessage, \
   1.461 +      ::logging::GetLastSystemErrorCode()).stream()
   1.462 +#define LOG_ERRNO(severity) \
   1.463 +  LAZY_STREAM(LOG_ERRNO_STREAM(severity), LOG_IS_ON(severity))
   1.464 +// PLOG_STREAM is used by PLOG, which is the usual error logging macro
   1.465 +// for each platform.
   1.466 +#define PLOG_STREAM(severity) LOG_ERRNO_STREAM(severity)
   1.467 +#endif
   1.468 +
   1.469 +#define PLOG(severity)                                          \
   1.470 +  LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity))
   1.471 +
   1.472 +#define PLOG_IF(severity, condition) \
   1.473 +  LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity) && (condition))
   1.474 +
   1.475 +#if !defined(NDEBUG)
   1.476 +// Debug builds always include DCHECK and DLOG.
   1.477 +#undef LOGGING_IS_OFFICIAL_BUILD
   1.478 +#define LOGGING_IS_OFFICIAL_BUILD 0
   1.479 +#elif defined(OFFICIAL_BUILD)
   1.480 +// Official release builds always disable and remove DCHECK and DLOG.
   1.481 +#undef LOGGING_IS_OFFICIAL_BUILD
   1.482 +#define LOGGING_IS_OFFICIAL_BUILD 1
   1.483 +#elif !defined(LOGGING_IS_OFFICIAL_BUILD)
   1.484 +// Unless otherwise specified, unofficial release builds include
   1.485 +// DCHECK and DLOG.
   1.486 +#define LOGGING_IS_OFFICIAL_BUILD 0
   1.487 +#endif
   1.488 +
   1.489 +// The actual stream used isn't important.
   1.490 +#define EAT_STREAM_PARAMETERS                                           \
   1.491 +  true ? (void) 0 : ::logging::LogMessageVoidify() & LOG_STREAM(FATAL)
   1.492 +
   1.493 +// CHECK dies with a fatal error if condition is not true.  It is *not*
   1.494 +// controlled by NDEBUG, so the check will be executed regardless of
   1.495 +// compilation mode.
   1.496 +//
   1.497 +// We make sure CHECK et al. always evaluates their arguments, as
   1.498 +// doing CHECK(FunctionWithSideEffect()) is a common idiom.
   1.499 +
   1.500 +#if LOGGING_IS_OFFICIAL_BUILD
   1.501 +
   1.502 +// Make all CHECK functions discard their log strings to reduce code
   1.503 +// bloat for official builds.
   1.504 +
   1.505 +// TODO(akalin): This would be more valuable if there were some way to
   1.506 +// remove BreakDebugger() from the backtrace, perhaps by turning it
   1.507 +// into a macro (like __debugbreak() on Windows).
   1.508 +#define CHECK(condition)                                                \
   1.509 +  !(condition) ? ::base::debug::BreakDebugger() : EAT_STREAM_PARAMETERS
   1.510 +
   1.511 +#define PCHECK(condition) CHECK(condition)
   1.512 +
   1.513 +#define CHECK_OP(name, op, val1, val2) CHECK((val1) op (val2))
   1.514 +
   1.515 +#else
   1.516 +
   1.517 +#define CHECK(condition)                       \
   1.518 +  LAZY_STREAM(LOG_STREAM(FATAL), !(condition)) \
   1.519 +  << "Check failed: " #condition ". "
   1.520 +
   1.521 +#define PCHECK(condition) \
   1.522 +  LAZY_STREAM(PLOG_STREAM(FATAL), !(condition)) \
   1.523 +  << "Check failed: " #condition ". "
   1.524 +
   1.525 +// Helper macro for binary operators.
   1.526 +// Don't use this macro directly in your code, use CHECK_EQ et al below.
   1.527 +//
   1.528 +// TODO(akalin): Rewrite this so that constructs like if (...)
   1.529 +// CHECK_EQ(...) else { ... } work properly.
   1.530 +#define CHECK_OP(name, op, val1, val2)                          \
   1.531 +  if (std::string* _result =                                    \
   1.532 +      logging::Check##name##Impl((val1), (val2),                \
   1.533 +                                 #val1 " " #op " " #val2))      \
   1.534 +    logging::LogMessage(__FILE__, __LINE__, _result).stream()
   1.535 +
   1.536 +#endif
   1.537 +
   1.538 +// Build the error message string.  This is separate from the "Impl"
   1.539 +// function template because it is not performance critical and so can
   1.540 +// be out of line, while the "Impl" code should be inline.  Caller
   1.541 +// takes ownership of the returned string.
   1.542 +template<class t1, class t2>
   1.543 +std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) {
   1.544 +  std::ostringstream ss;
   1.545 +  ss << names << " (" << v1 << " vs. " << v2 << ")";
   1.546 +  std::string* msg = new std::string(ss.str());
   1.547 +  return msg;
   1.548 +}
   1.549 +
   1.550 +// MSVC doesn't like complex extern templates and DLLs.
   1.551 +#if !defined(COMPILER_MSVC)
   1.552 +// Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated
   1.553 +// in logging.cc.
   1.554 +extern template BASE_EXPORT std::string* MakeCheckOpString<int, int>(
   1.555 +    const int&, const int&, const char* names);
   1.556 +extern template BASE_EXPORT
   1.557 +std::string* MakeCheckOpString<unsigned long, unsigned long>(
   1.558 +    const unsigned long&, const unsigned long&, const char* names);
   1.559 +extern template BASE_EXPORT
   1.560 +std::string* MakeCheckOpString<unsigned long, unsigned int>(
   1.561 +    const unsigned long&, const unsigned int&, const char* names);
   1.562 +extern template BASE_EXPORT
   1.563 +std::string* MakeCheckOpString<unsigned int, unsigned long>(
   1.564 +    const unsigned int&, const unsigned long&, const char* names);
   1.565 +extern template BASE_EXPORT
   1.566 +std::string* MakeCheckOpString<std::string, std::string>(
   1.567 +    const std::string&, const std::string&, const char* name);
   1.568 +#endif
   1.569 +
   1.570 +// Helper functions for CHECK_OP macro.
   1.571 +// The (int, int) specialization works around the issue that the compiler
   1.572 +// will not instantiate the template version of the function on values of
   1.573 +// unnamed enum type - see comment below.
   1.574 +#define DEFINE_CHECK_OP_IMPL(name, op) \
   1.575 +  template <class t1, class t2> \
   1.576 +  inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \
   1.577 +                                        const char* names) { \
   1.578 +    if (v1 op v2) return NULL; \
   1.579 +    else return MakeCheckOpString(v1, v2, names); \
   1.580 +  } \
   1.581 +  inline std::string* Check##name##Impl(int v1, int v2, const char* names) { \
   1.582 +    if (v1 op v2) return NULL; \
   1.583 +    else return MakeCheckOpString(v1, v2, names); \
   1.584 +  }
   1.585 +DEFINE_CHECK_OP_IMPL(EQ, ==)
   1.586 +DEFINE_CHECK_OP_IMPL(NE, !=)
   1.587 +DEFINE_CHECK_OP_IMPL(LE, <=)
   1.588 +DEFINE_CHECK_OP_IMPL(LT, < )
   1.589 +DEFINE_CHECK_OP_IMPL(GE, >=)
   1.590 +DEFINE_CHECK_OP_IMPL(GT, > )
   1.591 +#undef DEFINE_CHECK_OP_IMPL
   1.592 +
   1.593 +#define CHECK_EQ(val1, val2) CHECK_OP(EQ, ==, val1, val2)
   1.594 +#define CHECK_NE(val1, val2) CHECK_OP(NE, !=, val1, val2)
   1.595 +#define CHECK_LE(val1, val2) CHECK_OP(LE, <=, val1, val2)
   1.596 +#define CHECK_LT(val1, val2) CHECK_OP(LT, < , val1, val2)
   1.597 +#define CHECK_GE(val1, val2) CHECK_OP(GE, >=, val1, val2)
   1.598 +#define CHECK_GT(val1, val2) CHECK_OP(GT, > , val1, val2)
   1.599 +
   1.600 +#if LOGGING_IS_OFFICIAL_BUILD
   1.601 +// In order to have optimized code for official builds, remove DLOGs and
   1.602 +// DCHECKs.
   1.603 +#define ENABLE_DLOG 0
   1.604 +#define ENABLE_DCHECK 0
   1.605 +
   1.606 +#elif defined(NDEBUG)
   1.607 +// Otherwise, if we're a release build, remove DLOGs but not DCHECKs
   1.608 +// (since those can still be turned on via a command-line flag).
   1.609 +#define ENABLE_DLOG 0
   1.610 +#define ENABLE_DCHECK 1
   1.611 +
   1.612 +#else
   1.613 +// Otherwise, we're a debug build so enable DLOGs and DCHECKs.
   1.614 +#define ENABLE_DLOG 1
   1.615 +#define ENABLE_DCHECK 1
   1.616 +#endif
   1.617 +
   1.618 +// Definitions for DLOG et al.
   1.619 +
   1.620 +#if ENABLE_DLOG
   1.621 +
   1.622 +#define DLOG_IS_ON(severity) LOG_IS_ON(severity)
   1.623 +#define DLOG_IF(severity, condition) LOG_IF(severity, condition)
   1.624 +#define DLOG_ASSERT(condition) LOG_ASSERT(condition)
   1.625 +#define DPLOG_IF(severity, condition) PLOG_IF(severity, condition)
   1.626 +#define DVLOG_IF(verboselevel, condition) VLOG_IF(verboselevel, condition)
   1.627 +#define DVPLOG_IF(verboselevel, condition) VPLOG_IF(verboselevel, condition)
   1.628 +
   1.629 +#else  // ENABLE_DLOG
   1.630 +
   1.631 +// If ENABLE_DLOG is off, we want to avoid emitting any references to
   1.632 +// |condition| (which may reference a variable defined only if NDEBUG
   1.633 +// is not defined).  Contrast this with DCHECK et al., which has
   1.634 +// different behavior.
   1.635 +
   1.636 +#define DLOG_IS_ON(severity) false
   1.637 +#define DLOG_IF(severity, condition) EAT_STREAM_PARAMETERS
   1.638 +#define DLOG_ASSERT(condition) EAT_STREAM_PARAMETERS
   1.639 +#define DPLOG_IF(severity, condition) EAT_STREAM_PARAMETERS
   1.640 +#define DVLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS
   1.641 +#define DVPLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS
   1.642 +
   1.643 +#endif  // ENABLE_DLOG
   1.644 +
   1.645 +// DEBUG_MODE is for uses like
   1.646 +//   if (DEBUG_MODE) foo.CheckThatFoo();
   1.647 +// instead of
   1.648 +//   #ifndef NDEBUG
   1.649 +//     foo.CheckThatFoo();
   1.650 +//   #endif
   1.651 +//
   1.652 +// We tie its state to ENABLE_DLOG.
   1.653 +enum { DEBUG_MODE = ENABLE_DLOG };
   1.654 +
   1.655 +#undef ENABLE_DLOG
   1.656 +
   1.657 +#define DLOG(severity)                                          \
   1.658 +  LAZY_STREAM(LOG_STREAM(severity), DLOG_IS_ON(severity))
   1.659 +
   1.660 +#if defined(OS_WIN)
   1.661 +#define DLOG_GETLASTERROR(severity) \
   1.662 +  LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity), DLOG_IS_ON(severity))
   1.663 +#define DLOG_GETLASTERROR_MODULE(severity, module)                      \
   1.664 +  LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity, module),                \
   1.665 +              DLOG_IS_ON(severity))
   1.666 +#elif defined(OS_POSIX)
   1.667 +#define DLOG_ERRNO(severity)                                    \
   1.668 +  LAZY_STREAM(LOG_ERRNO_STREAM(severity), DLOG_IS_ON(severity))
   1.669 +#endif
   1.670 +
   1.671 +#define DPLOG(severity)                                         \
   1.672 +  LAZY_STREAM(PLOG_STREAM(severity), DLOG_IS_ON(severity))
   1.673 +
   1.674 +#define DVLOG(verboselevel) DVLOG_IF(verboselevel, VLOG_IS_ON(verboselevel))
   1.675 +
   1.676 +#define DVPLOG(verboselevel) DVPLOG_IF(verboselevel, VLOG_IS_ON(verboselevel))
   1.677 +
   1.678 +// Definitions for DCHECK et al.
   1.679 +
   1.680 +#if ENABLE_DCHECK
   1.681 +
   1.682 +#if defined(NDEBUG)
   1.683 +
   1.684 +BASE_EXPORT DcheckState get_dcheck_state();
   1.685 +BASE_EXPORT void set_dcheck_state(DcheckState state);
   1.686 +
   1.687 +#if defined(DCHECK_ALWAYS_ON)
   1.688 +
   1.689 +#define DCHECK_IS_ON() true
   1.690 +#define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \
   1.691 +  COMPACT_GOOGLE_LOG_EX_FATAL(ClassName , ##__VA_ARGS__)
   1.692 +#define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_FATAL
   1.693 +const LogSeverity LOG_DCHECK = LOG_FATAL;
   1.694 +
   1.695 +#else
   1.696 +
   1.697 +#define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \
   1.698 +  COMPACT_GOOGLE_LOG_EX_ERROR_REPORT(ClassName , ##__VA_ARGS__)
   1.699 +#define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_ERROR_REPORT
   1.700 +const LogSeverity LOG_DCHECK = LOG_ERROR_REPORT;
   1.701 +#define DCHECK_IS_ON()                                                  \
   1.702 +  ((::logging::get_dcheck_state() ==                                        \
   1.703 +    ::logging::ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS) &&        \
   1.704 +   LOG_IS_ON(DCHECK))
   1.705 +
   1.706 +#endif  // defined(DCHECK_ALWAYS_ON)
   1.707 +
   1.708 +#else  // defined(NDEBUG)
   1.709 +
   1.710 +// On a regular debug build, we want to have DCHECKs enabled.
   1.711 +#define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \
   1.712 +  COMPACT_GOOGLE_LOG_EX_FATAL(ClassName , ##__VA_ARGS__)
   1.713 +#define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_FATAL
   1.714 +const LogSeverity LOG_DCHECK = LOG_FATAL;
   1.715 +#define DCHECK_IS_ON() true
   1.716 +
   1.717 +#endif  // defined(NDEBUG)
   1.718 +
   1.719 +#else  // ENABLE_DCHECK
   1.720 +
   1.721 +// These are just dummy values since DCHECK_IS_ON() is always false in
   1.722 +// this case.
   1.723 +#define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \
   1.724 +  COMPACT_GOOGLE_LOG_EX_INFO(ClassName , ##__VA_ARGS__)
   1.725 +#define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_INFO
   1.726 +const LogSeverity LOG_DCHECK = LOG_INFO;
   1.727 +#define DCHECK_IS_ON() false
   1.728 +
   1.729 +#endif  // ENABLE_DCHECK
   1.730 +#undef ENABLE_DCHECK
   1.731 +
   1.732 +// DCHECK et al. make sure to reference |condition| regardless of
   1.733 +// whether DCHECKs are enabled; this is so that we don't get unused
   1.734 +// variable warnings if the only use of a variable is in a DCHECK.
   1.735 +// This behavior is different from DLOG_IF et al.
   1.736 +
   1.737 +#define DCHECK(condition)                                           \
   1.738 +  LAZY_STREAM(LOG_STREAM(DCHECK), DCHECK_IS_ON() && !(condition))   \
   1.739 +  << "Check failed: " #condition ". "
   1.740 +
   1.741 +#define DPCHECK(condition)                                          \
   1.742 +  LAZY_STREAM(PLOG_STREAM(DCHECK), DCHECK_IS_ON() && !(condition))  \
   1.743 +  << "Check failed: " #condition ". "
   1.744 +
   1.745 +// Helper macro for binary operators.
   1.746 +// Don't use this macro directly in your code, use DCHECK_EQ et al below.
   1.747 +#define DCHECK_OP(name, op, val1, val2)                         \
   1.748 +  if (DCHECK_IS_ON())                                           \
   1.749 +    if (std::string* _result =                                  \
   1.750 +        logging::Check##name##Impl((val1), (val2),              \
   1.751 +                                   #val1 " " #op " " #val2))    \
   1.752 +      logging::LogMessage(                                      \
   1.753 +          __FILE__, __LINE__, ::logging::LOG_DCHECK,            \
   1.754 +          _result).stream()
   1.755 +
   1.756 +// Equality/Inequality checks - compare two values, and log a
   1.757 +// LOG_DCHECK message including the two values when the result is not
   1.758 +// as expected.  The values must have operator<<(ostream, ...)
   1.759 +// defined.
   1.760 +//
   1.761 +// You may append to the error message like so:
   1.762 +//   DCHECK_NE(1, 2) << ": The world must be ending!";
   1.763 +//
   1.764 +// We are very careful to ensure that each argument is evaluated exactly
   1.765 +// once, and that anything which is legal to pass as a function argument is
   1.766 +// legal here.  In particular, the arguments may be temporary expressions
   1.767 +// which will end up being destroyed at the end of the apparent statement,
   1.768 +// for example:
   1.769 +//   DCHECK_EQ(string("abc")[1], 'b');
   1.770 +//
   1.771 +// WARNING: These may not compile correctly if one of the arguments is a pointer
   1.772 +// and the other is NULL. To work around this, simply static_cast NULL to the
   1.773 +// type of the desired pointer.
   1.774 +
   1.775 +#define DCHECK_EQ(val1, val2) DCHECK_OP(EQ, ==, val1, val2)
   1.776 +#define DCHECK_NE(val1, val2) DCHECK_OP(NE, !=, val1, val2)
   1.777 +#define DCHECK_LE(val1, val2) DCHECK_OP(LE, <=, val1, val2)
   1.778 +#define DCHECK_LT(val1, val2) DCHECK_OP(LT, < , val1, val2)
   1.779 +#define DCHECK_GE(val1, val2) DCHECK_OP(GE, >=, val1, val2)
   1.780 +#define DCHECK_GT(val1, val2) DCHECK_OP(GT, > , val1, val2)
   1.781 +
   1.782 +#define NOTREACHED() DCHECK(false)
   1.783 +
   1.784 +// Redefine the standard assert to use our nice log files
   1.785 +#undef assert
   1.786 +#define assert(x) DLOG_ASSERT(x)
   1.787 +
   1.788 +// This class more or less represents a particular log message.  You
   1.789 +// create an instance of LogMessage and then stream stuff to it.
   1.790 +// When you finish streaming to it, ~LogMessage is called and the
   1.791 +// full message gets streamed to the appropriate destination.
   1.792 +//
   1.793 +// You shouldn't actually use LogMessage's constructor to log things,
   1.794 +// though.  You should use the LOG() macro (and variants thereof)
   1.795 +// above.
   1.796 +class BASE_EXPORT LogMessage {
   1.797 + public:
   1.798 +  LogMessage(const char* file, int line, LogSeverity severity, int ctr);
   1.799 +
   1.800 +  // Two special constructors that generate reduced amounts of code at
   1.801 +  // LOG call sites for common cases.
   1.802 +  //
   1.803 +  // Used for LOG(INFO): Implied are:
   1.804 +  // severity = LOG_INFO, ctr = 0
   1.805 +  //
   1.806 +  // Using this constructor instead of the more complex constructor above
   1.807 +  // saves a couple of bytes per call site.
   1.808 +  LogMessage(const char* file, int line);
   1.809 +
   1.810 +  // Used for LOG(severity) where severity != INFO.  Implied
   1.811 +  // are: ctr = 0
   1.812 +  //
   1.813 +  // Using this constructor instead of the more complex constructor above
   1.814 +  // saves a couple of bytes per call site.
   1.815 +  LogMessage(const char* file, int line, LogSeverity severity);
   1.816 +
   1.817 +  // A special constructor used for check failures.  Takes ownership
   1.818 +  // of the given string.
   1.819 +  // Implied severity = LOG_FATAL
   1.820 +  LogMessage(const char* file, int line, std::string* result);
   1.821 +
   1.822 +  // A special constructor used for check failures, with the option to
   1.823 +  // specify severity.  Takes ownership of the given string.
   1.824 +  LogMessage(const char* file, int line, LogSeverity severity,
   1.825 +             std::string* result);
   1.826 +
   1.827 +  ~LogMessage();
   1.828 +
   1.829 +  std::ostream& stream() { return stream_; }
   1.830 +
   1.831 + private:
   1.832 +  void Init(const char* file, int line);
   1.833 +
   1.834 +  LogSeverity severity_;
   1.835 +  std::ostringstream stream_;
   1.836 +  size_t message_start_;  // Offset of the start of the message (past prefix
   1.837 +                          // info).
   1.838 +  // The file and line information passed in to the constructor.
   1.839 +  const char* file_;
   1.840 +  const int line_;
   1.841 +
   1.842 +#if defined(OS_WIN)
   1.843 +  // Stores the current value of GetLastError in the constructor and restores
   1.844 +  // it in the destructor by calling SetLastError.
   1.845 +  // This is useful since the LogMessage class uses a lot of Win32 calls
   1.846 +  // that will lose the value of GLE and the code that called the log function
   1.847 +  // will have lost the thread error value when the log call returns.
   1.848 +  class SaveLastError {
   1.849 +   public:
   1.850 +    SaveLastError();
   1.851 +    ~SaveLastError();
   1.852 +
   1.853 +    unsigned long get_error() const { return last_error_; }
   1.854 +
   1.855 +   protected:
   1.856 +    unsigned long last_error_;
   1.857 +  };
   1.858 +
   1.859 +  SaveLastError last_error_;
   1.860 +#endif
   1.861 +
   1.862 +  DISALLOW_COPY_AND_ASSIGN(LogMessage);
   1.863 +};
   1.864 +
   1.865 +// A non-macro interface to the log facility; (useful
   1.866 +// when the logging level is not a compile-time constant).
   1.867 +inline void LogAtLevel(int const log_level, std::string const &msg) {
   1.868 +  LogMessage(__FILE__, __LINE__, log_level).stream() << msg;
   1.869 +}
   1.870 +
   1.871 +// This class is used to explicitly ignore values in the conditional
   1.872 +// logging macros.  This avoids compiler warnings like "value computed
   1.873 +// is not used" and "statement has no effect".
   1.874 +class LogMessageVoidify {
   1.875 + public:
   1.876 +  LogMessageVoidify() { }
   1.877 +  // This has to be an operator with a precedence lower than << but
   1.878 +  // higher than ?:
   1.879 +  void operator&(std::ostream&) { }
   1.880 +};
   1.881 +
   1.882 +#if defined(OS_WIN)
   1.883 +typedef unsigned long SystemErrorCode;
   1.884 +#elif defined(OS_POSIX)
   1.885 +typedef int SystemErrorCode;
   1.886 +#endif
   1.887 +
   1.888 +// Alias for ::GetLastError() on Windows and errno on POSIX. Avoids having to
   1.889 +// pull in windows.h just for GetLastError() and DWORD.
   1.890 +BASE_EXPORT SystemErrorCode GetLastSystemErrorCode();
   1.891 +
   1.892 +#if defined(OS_WIN)
   1.893 +// Appends a formatted system message of the GetLastError() type.
   1.894 +class BASE_EXPORT Win32ErrorLogMessage {
   1.895 + public:
   1.896 +  Win32ErrorLogMessage(const char* file,
   1.897 +                       int line,
   1.898 +                       LogSeverity severity,
   1.899 +                       SystemErrorCode err,
   1.900 +                       const char* module);
   1.901 +
   1.902 +  Win32ErrorLogMessage(const char* file,
   1.903 +                       int line,
   1.904 +                       LogSeverity severity,
   1.905 +                       SystemErrorCode err);
   1.906 +
   1.907 +  // Appends the error message before destructing the encapsulated class.
   1.908 +  ~Win32ErrorLogMessage();
   1.909 +
   1.910 +  std::ostream& stream() { return log_message_.stream(); }
   1.911 +
   1.912 + private:
   1.913 +  SystemErrorCode err_;
   1.914 +  // Optional name of the module defining the error.
   1.915 +  const char* module_;
   1.916 +  LogMessage log_message_;
   1.917 +
   1.918 +  DISALLOW_COPY_AND_ASSIGN(Win32ErrorLogMessage);
   1.919 +};
   1.920 +#elif defined(OS_POSIX)
   1.921 +// Appends a formatted system message of the errno type
   1.922 +class BASE_EXPORT ErrnoLogMessage {
   1.923 + public:
   1.924 +  ErrnoLogMessage(const char* file,
   1.925 +                  int line,
   1.926 +                  LogSeverity severity,
   1.927 +                  SystemErrorCode err);
   1.928 +
   1.929 +  // Appends the error message before destructing the encapsulated class.
   1.930 +  ~ErrnoLogMessage();
   1.931 +
   1.932 +  std::ostream& stream() { return log_message_.stream(); }
   1.933 +
   1.934 + private:
   1.935 +  SystemErrorCode err_;
   1.936 +  LogMessage log_message_;
   1.937 +
   1.938 +  DISALLOW_COPY_AND_ASSIGN(ErrnoLogMessage);
   1.939 +};
   1.940 +#endif  // OS_WIN
   1.941 +
   1.942 +// Closes the log file explicitly if open.
   1.943 +// NOTE: Since the log file is opened as necessary by the action of logging
   1.944 +//       statements, there's no guarantee that it will stay closed
   1.945 +//       after this call.
   1.946 +BASE_EXPORT void CloseLogFile();
   1.947 +
   1.948 +// Async signal safe logging mechanism.
   1.949 +BASE_EXPORT void RawLog(int level, const char* message);
   1.950 +
   1.951 +#define RAW_LOG(level, message) logging::RawLog(logging::LOG_ ## level, message)
   1.952 +
   1.953 +#define RAW_CHECK(condition)                                                   \
   1.954 +  do {                                                                         \
   1.955 +    if (!(condition))                                                          \
   1.956 +      logging::RawLog(logging::LOG_FATAL, "Check failed: " #condition "\n");   \
   1.957 +  } while (0)
   1.958 +
   1.959 +#if defined(OS_WIN)
   1.960 +// Returns the default log file path.
   1.961 +BASE_EXPORT std::wstring GetLogFileFullPath();
   1.962 +#endif
   1.963 +
   1.964 +}  // namespace logging
   1.965 +
   1.966 +// These functions are provided as a convenience for logging, which is where we
   1.967 +// use streams (it is against Google style to use streams in other places). It
   1.968 +// is designed to allow you to emit non-ASCII Unicode strings to the log file,
   1.969 +// which is normally ASCII. It is relatively slow, so try not to use it for
   1.970 +// common cases. Non-ASCII characters will be converted to UTF-8 by these
   1.971 +// operators.
   1.972 +BASE_EXPORT std::ostream& operator<<(std::ostream& out, const wchar_t* wstr);
   1.973 +inline std::ostream& operator<<(std::ostream& out, const std::wstring& wstr) {
   1.974 +  return out << wstr.c_str();
   1.975 +}
   1.976 +
   1.977 +// The NOTIMPLEMENTED() macro annotates codepaths which have
   1.978 +// not been implemented yet.
   1.979 +//
   1.980 +// The implementation of this macro is controlled by NOTIMPLEMENTED_POLICY:
   1.981 +//   0 -- Do nothing (stripped by compiler)
   1.982 +//   1 -- Warn at compile time
   1.983 +//   2 -- Fail at compile time
   1.984 +//   3 -- Fail at runtime (DCHECK)
   1.985 +//   4 -- [default] LOG(ERROR) at runtime
   1.986 +//   5 -- LOG(ERROR) at runtime, only once per call-site
   1.987 +
   1.988 +#ifndef NOTIMPLEMENTED_POLICY
   1.989 +#if defined(OS_ANDROID) && defined(OFFICIAL_BUILD)
   1.990 +#define NOTIMPLEMENTED_POLICY 0
   1.991 +#else
   1.992 +// Select default policy: LOG(ERROR)
   1.993 +#define NOTIMPLEMENTED_POLICY 4
   1.994 +#endif
   1.995 +#endif
   1.996 +
   1.997 +#if defined(COMPILER_GCC)
   1.998 +// On Linux, with GCC, we can use __PRETTY_FUNCTION__ to get the demangled name
   1.999 +// of the current function in the NOTIMPLEMENTED message.
  1.1000 +#define NOTIMPLEMENTED_MSG "Not implemented reached in " << __PRETTY_FUNCTION__
  1.1001 +#else
  1.1002 +#define NOTIMPLEMENTED_MSG "NOT IMPLEMENTED"
  1.1003 +#endif
  1.1004 +
  1.1005 +#if NOTIMPLEMENTED_POLICY == 0
  1.1006 +#define NOTIMPLEMENTED() EAT_STREAM_PARAMETERS
  1.1007 +#elif NOTIMPLEMENTED_POLICY == 1
  1.1008 +// TODO, figure out how to generate a warning
  1.1009 +#define NOTIMPLEMENTED() COMPILE_ASSERT(false, NOT_IMPLEMENTED)
  1.1010 +#elif NOTIMPLEMENTED_POLICY == 2
  1.1011 +#define NOTIMPLEMENTED() COMPILE_ASSERT(false, NOT_IMPLEMENTED)
  1.1012 +#elif NOTIMPLEMENTED_POLICY == 3
  1.1013 +#define NOTIMPLEMENTED() NOTREACHED()
  1.1014 +#elif NOTIMPLEMENTED_POLICY == 4
  1.1015 +#define NOTIMPLEMENTED() LOG(ERROR) << NOTIMPLEMENTED_MSG
  1.1016 +#elif NOTIMPLEMENTED_POLICY == 5
  1.1017 +#define NOTIMPLEMENTED() do {\
  1.1018 +  static bool logged_once = false;\
  1.1019 +  LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\
  1.1020 +  logged_once = true;\
  1.1021 +} while(0);\
  1.1022 +EAT_STREAM_PARAMETERS
  1.1023 +#endif
  1.1024 +
  1.1025 +#endif  // BASE_LOGGING_H_

mercurial