| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_LOGGING_H_ | 5 #ifndef BASE_LOGGING_H_ |
| 6 #define BASE_LOGGING_H_ | 6 #define BASE_LOGGING_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <cstring> | 10 #include <cstring> |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 // directory may not be writable on an enduser's system. | 202 // directory may not be writable on an enduser's system. |
| 203 inline bool InitLogging(const PathChar* log_file, | 203 inline bool InitLogging(const PathChar* log_file, |
| 204 LoggingDestination logging_dest, | 204 LoggingDestination logging_dest, |
| 205 LogLockingState lock_log, | 205 LogLockingState lock_log, |
| 206 OldFileDeletionState delete_old) { | 206 OldFileDeletionState delete_old) { |
| 207 return BaseInitLoggingImpl(log_file, logging_dest, lock_log, delete_old); | 207 return BaseInitLoggingImpl(log_file, logging_dest, lock_log, delete_old); |
| 208 } | 208 } |
| 209 | 209 |
| 210 // Sets the log level. Anything at or above this level will be written to the | 210 // Sets the log level. Anything at or above this level will be written to the |
| 211 // log file/displayed to the user (if applicable). Anything below this level | 211 // log file/displayed to the user (if applicable). Anything below this level |
| 212 // will be silently ignored. The log level defaults to 0 (everything is logged) | 212 // will be silently ignored. The log level defaults to 0 (everything is logged |
| 213 // if this function is not called. | 213 // up to level INFO) if this function is not called. |
| 214 // Note that log messages for VLOG(x) are logged at level -x, so setting |
| 215 // the min log level to negative values enables verbose logging. |
| 214 void SetMinLogLevel(int level); | 216 void SetMinLogLevel(int level); |
| 215 | 217 |
| 216 // Gets the current log level. | 218 // Gets the current log level. |
| 217 int GetMinLogLevel(); | 219 int GetMinLogLevel(); |
| 218 | 220 |
| 221 // Gets the VLOG default verbosity level. |
| 222 int GetVlogVerbosity(); |
| 223 |
| 219 // Gets the current vlog level for the given file (usually taken from | 224 // Gets the current vlog level for the given file (usually taken from |
| 220 // __FILE__). | 225 // __FILE__). |
| 221 | 226 |
| 222 // Note that |N| is the size *with* the null terminator. | 227 // Note that |N| is the size *with* the null terminator. |
| 223 int GetVlogLevelHelper(const char* file_start, size_t N); | 228 int GetVlogLevelHelper(const char* file_start, size_t N); |
| 224 | 229 |
| 225 template <size_t N> | 230 template <size_t N> |
| 226 int GetVlogLevel(const char (&file)[N]) { | 231 int GetVlogLevel(const char (&file)[N]) { |
| 227 return GetVlogLevelHelper(file, N); | 232 return GetVlogLevelHelper(file, N); |
| 228 } | 233 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 250 // in non-debug mode. The default handler shows a dialog box and continues | 255 // in non-debug mode. The default handler shows a dialog box and continues |
| 251 // the execution, however clients can use this function to override with their | 256 // the execution, however clients can use this function to override with their |
| 252 // own handling. | 257 // own handling. |
| 253 typedef void (*LogReportHandlerFunction)(const std::string& str); | 258 typedef void (*LogReportHandlerFunction)(const std::string& str); |
| 254 void SetLogReportHandler(LogReportHandlerFunction handler); | 259 void SetLogReportHandler(LogReportHandlerFunction handler); |
| 255 | 260 |
| 256 // Sets the Log Message Handler that gets passed every log message before | 261 // Sets the Log Message Handler that gets passed every log message before |
| 257 // it's sent to other log destinations (if any). | 262 // it's sent to other log destinations (if any). |
| 258 // Returns true to signal that it handled the message and the message | 263 // Returns true to signal that it handled the message and the message |
| 259 // should not be sent to other log destinations. | 264 // should not be sent to other log destinations. |
| 260 typedef bool (*LogMessageHandlerFunction)(int severity, const std::string& str); | 265 typedef bool (*LogMessageHandlerFunction)(int severity, |
| 266 const char* file, int line, size_t message_start, const std::string& str); |
| 261 void SetLogMessageHandler(LogMessageHandlerFunction handler); | 267 void SetLogMessageHandler(LogMessageHandlerFunction handler); |
| 262 LogMessageHandlerFunction GetLogMessageHandler(); | 268 LogMessageHandlerFunction GetLogMessageHandler(); |
| 263 | 269 |
| 264 typedef int LogSeverity; | 270 typedef int LogSeverity; |
| 271 const LogSeverity LOG_VERBOSE = -1; // This is level 1 verbosity |
| 272 // Note: the log severities are used to index into the array of names, |
| 273 // see log_severity_names. |
| 265 const LogSeverity LOG_INFO = 0; | 274 const LogSeverity LOG_INFO = 0; |
| 266 const LogSeverity LOG_WARNING = 1; | 275 const LogSeverity LOG_WARNING = 1; |
| 267 const LogSeverity LOG_ERROR = 2; | 276 const LogSeverity LOG_ERROR = 2; |
| 268 const LogSeverity LOG_ERROR_REPORT = 3; | 277 const LogSeverity LOG_ERROR_REPORT = 3; |
| 269 const LogSeverity LOG_FATAL = 4; | 278 const LogSeverity LOG_FATAL = 4; |
| 270 const LogSeverity LOG_NUM_SEVERITIES = 5; | 279 const LogSeverity LOG_NUM_SEVERITIES = 5; |
| 271 | 280 |
| 272 // LOG_DFATAL is LOG_FATAL in debug mode, ERROR in normal mode | 281 // LOG_DFATAL is LOG_FATAL in debug mode, ERROR in normal mode |
| 273 #ifdef NDEBUG | 282 #ifdef NDEBUG |
| 274 const LogSeverity LOG_DFATAL = LOG_ERROR; | 283 const LogSeverity LOG_DFATAL = LOG_ERROR; |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 347 // function of LogMessage which seems to avoid the problem. | 356 // function of LogMessage which seems to avoid the problem. |
| 348 #define LOG_STREAM(severity) COMPACT_GOOGLE_LOG_ ## severity.stream() | 357 #define LOG_STREAM(severity) COMPACT_GOOGLE_LOG_ ## severity.stream() |
| 349 | 358 |
| 350 #define LOG(severity) LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity)) | 359 #define LOG(severity) LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity)) |
| 351 #define LOG_IF(severity, condition) \ | 360 #define LOG_IF(severity, condition) \ |
| 352 LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity) && (condition)) | 361 LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity) && (condition)) |
| 353 | 362 |
| 354 #define SYSLOG(severity) LOG(severity) | 363 #define SYSLOG(severity) LOG(severity) |
| 355 #define SYSLOG_IF(severity, condition) LOG_IF(severity, condition) | 364 #define SYSLOG_IF(severity, condition) LOG_IF(severity, condition) |
| 356 | 365 |
| 357 #define VLOG(verboselevel) LOG_IF(INFO, VLOG_IS_ON(verboselevel)) | 366 // The VLOG macros log with negative verbosities. |
| 358 #define VLOG_IF(verboselevel, condition) \ | 367 #define VLOG_STREAM(verbose_level) \ |
| 359 LOG_IF(INFO, VLOG_IS_ON(verboselevel) && (condition)) | 368 logging::LogMessage(__FILE__, __LINE__, -verbose_level).stream() |
| 369 |
| 370 #define VLOG(verbose_level) \ |
| 371 LAZY_STREAM(VLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level)) |
| 372 |
| 373 #define VLOG_IF(verbose_level, condition) \ |
| 374 LAZY_STREAM(VLOG_STREAM(verbose_level), \ |
| 375 VLOG_IS_ON(verbose_level) && (condition)) |
| 360 | 376 |
| 361 // TODO(akalin): Add more VLOG variants, e.g. VPLOG. | 377 // TODO(akalin): Add more VLOG variants, e.g. VPLOG. |
| 362 | 378 |
| 363 #define LOG_ASSERT(condition) \ | 379 #define LOG_ASSERT(condition) \ |
| 364 LOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". " | 380 LOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". " |
| 365 #define SYSLOG_ASSERT(condition) \ | 381 #define SYSLOG_ASSERT(condition) \ |
| 366 SYSLOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". " | 382 SYSLOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". " |
| 367 | 383 |
| 368 #if defined(OS_WIN) | 384 #if defined(OS_WIN) |
| 369 #define LOG_GETLASTERROR_STREAM(severity) \ | 385 #define LOG_GETLASTERROR_STREAM(severity) \ |
| (...skipping 341 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 711 | 727 |
| 712 std::ostream& stream() { return stream_; } | 728 std::ostream& stream() { return stream_; } |
| 713 | 729 |
| 714 private: | 730 private: |
| 715 void Init(const char* file, int line); | 731 void Init(const char* file, int line); |
| 716 | 732 |
| 717 LogSeverity severity_; | 733 LogSeverity severity_; |
| 718 std::ostringstream stream_; | 734 std::ostringstream stream_; |
| 719 size_t message_start_; // Offset of the start of the message (past prefix | 735 size_t message_start_; // Offset of the start of the message (past prefix |
| 720 // info). | 736 // info). |
| 737 // The file and line information passed in to the constructor. |
| 738 const char* file_; |
| 739 const int line_; |
| 740 |
| 721 #if defined(OS_WIN) | 741 #if defined(OS_WIN) |
| 722 // Stores the current value of GetLastError in the constructor and restores | 742 // Stores the current value of GetLastError in the constructor and restores |
| 723 // it in the destructor by calling SetLastError. | 743 // it in the destructor by calling SetLastError. |
| 724 // This is useful since the LogMessage class uses a lot of Win32 calls | 744 // This is useful since the LogMessage class uses a lot of Win32 calls |
| 725 // that will lose the value of GLE and the code that called the log function | 745 // that will lose the value of GLE and the code that called the log function |
| 726 // will have lost the thread error value when the log call returns. | 746 // will have lost the thread error value when the log call returns. |
| 727 class SaveLastError { | 747 class SaveLastError { |
| 728 public: | 748 public: |
| 729 SaveLastError(); | 749 SaveLastError(); |
| 730 ~SaveLastError(); | 750 ~SaveLastError(); |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 884 #elif NOTIMPLEMENTED_POLICY == 4 | 904 #elif NOTIMPLEMENTED_POLICY == 4 |
| 885 #define NOTIMPLEMENTED() LOG(ERROR) << NOTIMPLEMENTED_MSG | 905 #define NOTIMPLEMENTED() LOG(ERROR) << NOTIMPLEMENTED_MSG |
| 886 #elif NOTIMPLEMENTED_POLICY == 5 | 906 #elif NOTIMPLEMENTED_POLICY == 5 |
| 887 #define NOTIMPLEMENTED() do {\ | 907 #define NOTIMPLEMENTED() do {\ |
| 888 static int count = 0;\ | 908 static int count = 0;\ |
| 889 LOG_IF(ERROR, 0 == count++) << NOTIMPLEMENTED_MSG;\ | 909 LOG_IF(ERROR, 0 == count++) << NOTIMPLEMENTED_MSG;\ |
| 890 } while(0) | 910 } while(0) |
| 891 #endif | 911 #endif |
| 892 | 912 |
| 893 #endif // BASE_LOGGING_H_ | 913 #endif // BASE_LOGGING_H_ |
| OLD | NEW |