Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 #include "base/logging.h" | 5 #include "base/logging.h" |
| 6 | 6 |
| 7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
| 8 #include <windows.h> | 8 #include <windows.h> |
| 9 typedef HANDLE FileHandle; | 9 typedef HANDLE FileHandle; |
| 10 typedef HANDLE MutexHandle; | 10 typedef HANDLE MutexHandle; |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 39 #include "base/lock_impl.h" | 39 #include "base/lock_impl.h" |
| 40 #include "base/string_piece.h" | 40 #include "base/string_piece.h" |
| 41 #include "base/string_util.h" | 41 #include "base/string_util.h" |
| 42 #include "base/sys_string_conversions.h" | 42 #include "base/sys_string_conversions.h" |
| 43 | 43 |
| 44 namespace logging { | 44 namespace logging { |
| 45 | 45 |
| 46 bool g_enable_dcheck = false; | 46 bool g_enable_dcheck = false; |
| 47 | 47 |
| 48 const char* const log_severity_names[LOG_NUM_SEVERITIES] = { | 48 const char* const log_severity_names[LOG_NUM_SEVERITIES] = { |
| 49 "INFO", "WARNING", "ERROR", "FATAL" }; | 49 "INFO", "WARNING", "ERROR", "ERROR_REPORT", "FATAL" }; |
| 50 | 50 |
| 51 int min_log_level = 0; | 51 int min_log_level = 0; |
| 52 LogLockingState lock_log_file = LOCK_LOG_FILE; | 52 LogLockingState lock_log_file = LOCK_LOG_FILE; |
| 53 | 53 |
| 54 // The default set here for logging_destination will only be used if | 54 // The default set here for logging_destination will only be used if |
| 55 // InitLogging is not called. On Windows, use a file next to the exe; | 55 // InitLogging is not called. On Windows, use a file next to the exe; |
| 56 // on POSIX platforms, where it may not even be possible to locate the | 56 // on POSIX platforms, where it may not even be possible to locate the |
| 57 // executable on disk, use stderr. | 57 // executable on disk, use stderr. |
| 58 #if defined(OS_WIN) | 58 #if defined(OS_WIN) |
| 59 LoggingDestination logging_destination = LOG_ONLY_TO_FILE; | 59 LoggingDestination logging_destination = LOG_ONLY_TO_FILE; |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 82 // this file is lazily opened and the handle may be NULL | 82 // this file is lazily opened and the handle may be NULL |
| 83 FileHandle log_file = NULL; | 83 FileHandle log_file = NULL; |
| 84 | 84 |
| 85 // what should be prepended to each message? | 85 // what should be prepended to each message? |
| 86 bool log_process_id = false; | 86 bool log_process_id = false; |
| 87 bool log_thread_id = false; | 87 bool log_thread_id = false; |
| 88 bool log_timestamp = true; | 88 bool log_timestamp = true; |
| 89 bool log_tickcount = false; | 89 bool log_tickcount = false; |
| 90 | 90 |
| 91 // An assert handler override specified by the client to be called instead of | 91 // An assert handler override specified by the client to be called instead of |
| 92 // the debug message dialog and process termination. | |
| 93 LogAssertHandlerFunction log_assert_handler = NULL; | |
| 94 // An report handler override specified by the client to be called instead of | |
|
wtc
2009/02/19 01:32:04
Nit: add a blank line above this line.
| |
| 92 // the debug message dialog. | 95 // the debug message dialog. |
| 93 LogAssertHandlerFunction log_assert_handler = NULL; | 96 LogReportHandlerFunction log_report_handler = NULL; |
| 94 | 97 |
| 95 // The lock is used if log file locking is false. It helps us avoid problems | 98 // The lock is used if log file locking is false. It helps us avoid problems |
| 96 // with multiple threads writing to the log file at the same time. Use | 99 // with multiple threads writing to the log file at the same time. Use |
| 97 // LockImpl directly instead of using Lock, because Lock makes logging calls. | 100 // LockImpl directly instead of using Lock, because Lock makes logging calls. |
| 98 static LockImpl* log_lock = NULL; | 101 static LockImpl* log_lock = NULL; |
| 99 | 102 |
| 100 // When we don't use a lock, we are using a global mutex. We need to do this | 103 // When we don't use a lock, we are using a global mutex. We need to do this |
| 101 // because LockFileEx is not thread safe. | 104 // because LockFileEx is not thread safe. |
| 102 #if defined(OS_WIN) | 105 #if defined(OS_WIN) |
| 103 MutexHandle log_mutex = NULL; | 106 MutexHandle log_mutex = NULL; |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 284 log_process_id = enable_process_id; | 287 log_process_id = enable_process_id; |
| 285 log_thread_id = enable_thread_id; | 288 log_thread_id = enable_thread_id; |
| 286 log_timestamp = enable_timestamp; | 289 log_timestamp = enable_timestamp; |
| 287 log_tickcount = enable_tickcount; | 290 log_tickcount = enable_tickcount; |
| 288 } | 291 } |
| 289 | 292 |
| 290 void SetLogAssertHandler(LogAssertHandlerFunction handler) { | 293 void SetLogAssertHandler(LogAssertHandlerFunction handler) { |
| 291 log_assert_handler = handler; | 294 log_assert_handler = handler; |
| 292 } | 295 } |
| 293 | 296 |
| 297 void SetLogReportHandler(LogReportHandlerFunction handler) { | |
| 298 log_report_handler = handler; | |
| 299 } | |
| 300 | |
| 294 // Displays a message box to the user with the error message in it. For | 301 // Displays a message box to the user with the error message in it. For |
| 295 // Windows programs, it's possible that the message loop is messed up on | 302 // Windows programs, it's possible that the message loop is messed up on |
| 296 // a fatal error, and creating a MessageBox will cause that message loop | 303 // a fatal error, and creating a MessageBox will cause that message loop |
| 297 // to be run. Instead, we try to spawn another process that displays its | 304 // to be run. Instead, we try to spawn another process that displays its |
| 298 // command line. We look for "Debug Message.exe" in the same directory as | 305 // command line. We look for "Debug Message.exe" in the same directory as |
| 299 // the application. If it exists, we use it, otherwise, we use a regular | 306 // the application. If it exists, we use it, otherwise, we use a regular |
| 300 // message box. | 307 // message box. |
| 301 void DisplayDebugMessage(const std::string& str) { | 308 void DisplayDebugMessage(const std::string& str) { |
| 302 if (str.empty()) | 309 if (str.empty()) |
| 303 return; | 310 return; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 341 : severity_(severity) { | 348 : severity_(severity) { |
| 342 Init(file, line); | 349 Init(file, line); |
| 343 } | 350 } |
| 344 | 351 |
| 345 LogMessage::LogMessage(const char* file, int line, const CheckOpString& result) | 352 LogMessage::LogMessage(const char* file, int line, const CheckOpString& result) |
| 346 : severity_(LOG_FATAL) { | 353 : severity_(LOG_FATAL) { |
| 347 Init(file, line); | 354 Init(file, line); |
| 348 stream_ << "Check failed: " << (*result.str_); | 355 stream_ << "Check failed: " << (*result.str_); |
| 349 } | 356 } |
| 350 | 357 |
| 358 LogMessage::LogMessage(const char* file, int line, LogSeverity severity, | |
| 359 const CheckOpString& result) | |
| 360 : severity_(severity) { | |
| 361 Init(file, line); | |
| 362 stream_ << "Check failed: " << (*result.str_); | |
| 363 } | |
| 364 | |
| 351 LogMessage::LogMessage(const char* file, int line) | 365 LogMessage::LogMessage(const char* file, int line) |
| 352 : severity_(LOG_INFO) { | 366 : severity_(LOG_INFO) { |
| 353 Init(file, line); | 367 Init(file, line); |
| 354 } | 368 } |
| 355 | 369 |
| 356 LogMessage::LogMessage(const char* file, int line, LogSeverity severity) | 370 LogMessage::LogMessage(const char* file, int line, LogSeverity severity) |
| 357 : severity_(severity) { | 371 : severity_(severity) { |
| 358 Init(file, line); | 372 Init(file, line); |
| 359 } | 373 } |
| 360 | 374 |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 504 // hosed can cause additional problems. | 518 // hosed can cause additional problems. |
| 505 #ifndef NDEBUG | 519 #ifndef NDEBUG |
| 506 DisplayDebugMessage(stream_.str()); | 520 DisplayDebugMessage(stream_.str()); |
| 507 #endif | 521 #endif |
| 508 // Crash the process to generate a dump. | 522 // Crash the process to generate a dump. |
| 509 DebugUtil::BreakDebugger(); | 523 DebugUtil::BreakDebugger(); |
| 510 // TODO(mmentovai): when we have breakpad support, generate a breakpad | 524 // TODO(mmentovai): when we have breakpad support, generate a breakpad |
| 511 // dump, but until then, do not invoke the Apple crash reporter. | 525 // dump, but until then, do not invoke the Apple crash reporter. |
| 512 } | 526 } |
| 513 } | 527 } |
| 528 } else if (severity_ == LOG_ERROR_REPORT) { | |
| 529 // We are here only if the user runs with --enable-dcheck in release mode. | |
| 530 if (log_report_handler) { | |
| 531 log_report_handler(std::string(stream_.str())); | |
| 532 } else { | |
| 533 DisplayDebugMessage(stream_.str()); | |
| 534 } | |
| 514 } | 535 } |
| 515 } | 536 } |
| 516 | 537 |
| 517 void CloseLogFile() { | 538 void CloseLogFile() { |
| 518 if (!log_file) | 539 if (!log_file) |
| 519 return; | 540 return; |
| 520 | 541 |
| 521 CloseFile(log_file); | 542 CloseFile(log_file); |
| 522 log_file = NULL; | 543 log_file = NULL; |
| 523 } | 544 } |
| 524 | 545 |
| 525 } // namespace logging | 546 } // namespace logging |
| 526 | 547 |
| 527 std::ostream& operator<<(std::ostream& out, const wchar_t* wstr) { | 548 std::ostream& operator<<(std::ostream& out, const wchar_t* wstr) { |
| 528 return out << base::SysWideToUTF8(std::wstring(wstr)); | 549 return out << base::SysWideToUTF8(std::wstring(wstr)); |
| 529 } | 550 } |
| OLD | NEW |