Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include <limits.h> | 7 #include <limits.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "base/callback.h" | |
| 10 #include "base/debug/activity_tracker.h" | 11 #include "base/debug/activity_tracker.h" |
| 11 #include "base/macros.h" | 12 #include "base/macros.h" |
| 12 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 13 | 14 |
| 14 #if defined(OS_WIN) | 15 #if defined(OS_WIN) |
| 15 #include <io.h> | 16 #include <io.h> |
| 16 typedef HANDLE FileHandle; | 17 typedef HANDLE FileHandle; |
| 17 typedef HANDLE MutexHandle; | 18 typedef HANDLE MutexHandle; |
| 18 // Windows warns on using write(). It prefers _write(). | 19 // Windows warns on using write(). It prefers _write(). |
| 19 #define write(fd, buf, count) _write(fd, buf, static_cast<unsigned int>(count)) | 20 #define write(fd, buf, count) _write(fd, buf, static_cast<unsigned int>(count)) |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 114 // What should be prepended to each message? | 115 // What should be prepended to each message? |
| 115 bool g_log_process_id = false; | 116 bool g_log_process_id = false; |
| 116 bool g_log_thread_id = false; | 117 bool g_log_thread_id = false; |
| 117 bool g_log_timestamp = true; | 118 bool g_log_timestamp = true; |
| 118 bool g_log_tickcount = false; | 119 bool g_log_tickcount = false; |
| 119 | 120 |
| 120 // Should we pop up fatal debug messages in a dialog? | 121 // Should we pop up fatal debug messages in a dialog? |
| 121 bool show_error_dialogs = false; | 122 bool show_error_dialogs = false; |
| 122 | 123 |
| 123 // An assert handler override specified by the client to be called instead of | 124 // An assert handler override specified by the client to be called instead of |
| 124 // the debug message dialog and process termination. | 125 // the debug message dialog and process termination. Assert handlers are stored |
| 125 LogAssertHandlerFunction log_assert_handler = nullptr; | 126 // in stack to allow overriding and restoring. |
| 127 struct LogAssertHandlerStackItem { | |
| 128 LogAssertHandlerStackItem* next; | |
| 129 LogAssertHandlerFunction handler; | |
| 130 }; | |
|
dcheng
2017/02/14 23:59:26
Perhaps just use something from STL here? We can j
alex-ac
2017/03/09 14:15:53
We can't use non-pod type for a global variable. I
dcheng
2017/03/09 19:41:02
It could just be a vector in a LazyInstance/functi
| |
| 131 | |
| 132 LogAssertHandlerStackItem* log_assert_handler_stack = nullptr; | |
| 133 | |
| 126 // A log message handler that gets notified of every log message we process. | 134 // A log message handler that gets notified of every log message we process. |
| 127 LogMessageHandlerFunction log_message_handler = nullptr; | 135 LogMessageHandlerFunction log_message_handler = nullptr; |
| 128 | 136 |
| 129 // Helper functions to wrap platform differences. | 137 // Helper functions to wrap platform differences. |
| 130 | 138 |
| 131 int32_t CurrentProcessId() { | 139 int32_t CurrentProcessId() { |
| 132 #if defined(OS_WIN) | 140 #if defined(OS_WIN) |
| 133 return GetCurrentProcessId(); | 141 return GetCurrentProcessId(); |
| 134 #elif defined(OS_POSIX) | 142 #elif defined(OS_POSIX) |
| 135 return getpid(); | 143 return getpid(); |
| (...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 437 g_log_process_id = enable_process_id; | 445 g_log_process_id = enable_process_id; |
| 438 g_log_thread_id = enable_thread_id; | 446 g_log_thread_id = enable_thread_id; |
| 439 g_log_timestamp = enable_timestamp; | 447 g_log_timestamp = enable_timestamp; |
| 440 g_log_tickcount = enable_tickcount; | 448 g_log_tickcount = enable_tickcount; |
| 441 } | 449 } |
| 442 | 450 |
| 443 void SetShowErrorDialogs(bool enable_dialogs) { | 451 void SetShowErrorDialogs(bool enable_dialogs) { |
| 444 show_error_dialogs = enable_dialogs; | 452 show_error_dialogs = enable_dialogs; |
| 445 } | 453 } |
| 446 | 454 |
| 447 void SetLogAssertHandler(LogAssertHandlerFunction handler) { | 455 void PushLogAssertHandler(LogAssertHandlerFunction handler) { |
| 448 log_assert_handler = handler; | 456 LogAssertHandlerStackItem* stack_item = new LogAssertHandlerStackItem; |
| 457 stack_item->handler = handler; | |
| 458 stack_item->next = log_assert_handler_stack; | |
| 459 log_assert_handler_stack = stack_item; | |
| 460 } | |
| 461 | |
| 462 void PopLogAssertHandler() { | |
| 463 LogAssertHandlerStackItem* stack_item = log_assert_handler_stack; | |
| 464 if (stack_item) { | |
| 465 log_assert_handler_stack = stack_item->next; | |
| 466 delete stack_item; | |
| 467 } | |
| 468 } | |
| 469 | |
| 470 LogAssertHandlerFunction GetLogAssertHandler() { | |
| 471 if (log_assert_handler_stack) | |
| 472 return log_assert_handler_stack->handler; | |
| 473 return LogAssertHandlerFunction(); | |
| 449 } | 474 } |
| 450 | 475 |
| 451 void SetLogMessageHandler(LogMessageHandlerFunction handler) { | 476 void SetLogMessageHandler(LogMessageHandlerFunction handler) { |
| 452 log_message_handler = handler; | 477 log_message_handler = handler; |
| 453 } | 478 } |
| 454 | 479 |
| 455 LogMessageHandlerFunction GetLogMessageHandler() { | 480 LogMessageHandlerFunction GetLogMessageHandler() { |
| 456 return log_message_handler; | 481 return log_message_handler; |
| 457 } | 482 } |
| 458 | 483 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 524 | 549 |
| 525 LogMessage::LogMessage(const char* file, int line, LogSeverity severity, | 550 LogMessage::LogMessage(const char* file, int line, LogSeverity severity, |
| 526 std::string* result) | 551 std::string* result) |
| 527 : severity_(severity), file_(file), line_(line) { | 552 : severity_(severity), file_(file), line_(line) { |
| 528 Init(file, line); | 553 Init(file, line); |
| 529 stream_ << "Check failed: " << *result; | 554 stream_ << "Check failed: " << *result; |
| 530 delete result; | 555 delete result; |
| 531 } | 556 } |
| 532 | 557 |
| 533 LogMessage::~LogMessage() { | 558 LogMessage::~LogMessage() { |
| 559 size_t stack_start = stream_.str().length(); | |
| 534 #if !defined(OFFICIAL_BUILD) && !defined(OS_NACL) && !defined(__UCLIBC__) | 560 #if !defined(OFFICIAL_BUILD) && !defined(OS_NACL) && !defined(__UCLIBC__) |
| 535 if (severity_ == LOG_FATAL && !base::debug::BeingDebugged()) { | 561 if (severity_ == LOG_FATAL && !base::debug::BeingDebugged()) { |
| 536 // Include a stack trace on a fatal, unless a debugger is attached. | 562 // Include a stack trace on a fatal, unless a debugger is attached. |
| 537 base::debug::StackTrace trace; | 563 base::debug::StackTrace trace; |
| 538 stream_ << std::endl; // Newline to separate from log message. | 564 stream_ << std::endl; // Newline to separate from log message. |
| 539 trace.OutputToStream(&stream_); | 565 trace.OutputToStream(&stream_); |
| 540 } | 566 } |
| 541 #endif | 567 #endif |
| 542 stream_ << std::endl; | 568 stream_ << std::endl; |
| 543 std::string str_newline(stream_.str()); | 569 std::string str_newline(stream_.str()); |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 732 base::debug::GlobalActivityTracker::Get(); | 758 base::debug::GlobalActivityTracker::Get(); |
| 733 if (tracker) | 759 if (tracker) |
| 734 tracker->RecordLogMessage(str_newline); | 760 tracker->RecordLogMessage(str_newline); |
| 735 | 761 |
| 736 // Ensure the first characters of the string are on the stack so they | 762 // Ensure the first characters of the string are on the stack so they |
| 737 // are contained in minidumps for diagnostic purposes. | 763 // are contained in minidumps for diagnostic purposes. |
| 738 char str_stack[1024]; | 764 char str_stack[1024]; |
| 739 str_newline.copy(str_stack, arraysize(str_stack)); | 765 str_newline.copy(str_stack, arraysize(str_stack)); |
| 740 base::debug::Alias(str_stack); | 766 base::debug::Alias(str_stack); |
| 741 | 767 |
| 768 LogAssertHandlerFunction log_assert_handler = GetLogAssertHandler(); | |
| 742 if (log_assert_handler) { | 769 if (log_assert_handler) { |
| 743 // Make a copy of the string for the handler out of paranoia. | 770 const std::string& full_message = stream_.str(); |
| 744 log_assert_handler(std::string(stream_.str())); | 771 log_assert_handler.Run( |
| 772 file_, line_, | |
| 773 full_message.substr(message_start_, stack_start - message_start_), | |
| 774 full_message.substr(stack_start)); | |
| 745 } else { | 775 } else { |
| 746 // Don't use the string with the newline, get a fresh version to send to | 776 // Don't use the string with the newline, get a fresh version to send to |
| 747 // the debug message process. We also don't display assertions to the | 777 // the debug message process. We also don't display assertions to the |
| 748 // user in release mode. The enduser can't do anything with this | 778 // user in release mode. The enduser can't do anything with this |
| 749 // information, and displaying message boxes when the application is | 779 // information, and displaying message boxes when the application is |
| 750 // hosed can cause additional problems. | 780 // hosed can cause additional problems. |
| 751 #ifndef NDEBUG | 781 #ifndef NDEBUG |
| 752 if (!base::debug::BeingDebugged()) { | 782 if (!base::debug::BeingDebugged()) { |
| 753 // Displaying a dialog is unnecessary when debugging and can complicate | 783 // Displaying a dialog is unnecessary when debugging and can complicate |
| 754 // debugging. | 784 // debugging. |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 947 BASE_EXPORT void LogErrorNotReached(const char* file, int line) { | 977 BASE_EXPORT void LogErrorNotReached(const char* file, int line) { |
| 948 LogMessage(file, line, LOG_ERROR).stream() | 978 LogMessage(file, line, LOG_ERROR).stream() |
| 949 << "NOTREACHED() hit."; | 979 << "NOTREACHED() hit."; |
| 950 } | 980 } |
| 951 | 981 |
| 952 } // namespace logging | 982 } // namespace logging |
| 953 | 983 |
| 954 std::ostream& std::operator<<(std::ostream& out, const wchar_t* wstr) { | 984 std::ostream& std::operator<<(std::ostream& out, const wchar_t* wstr) { |
| 955 return out << (wstr ? base::WideToUTF8(wstr) : std::string()); | 985 return out << (wstr ? base::WideToUTF8(wstr) : std::string()); |
| 956 } | 986 } |
| OLD | NEW |