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/debug/activity_tracker.h" | |
11 #include "base/macros.h" | 10 #include "base/macros.h" |
12 #include "build/build_config.h" | 11 #include "build/build_config.h" |
13 | 12 |
14 #if defined(OS_WIN) | 13 #if defined(OS_WIN) |
15 #include <io.h> | 14 #include <io.h> |
| 15 #include <windows.h> |
16 typedef HANDLE FileHandle; | 16 typedef HANDLE FileHandle; |
17 typedef HANDLE MutexHandle; | 17 typedef HANDLE MutexHandle; |
18 // Windows warns on using write(). It prefers _write(). | 18 // Windows warns on using write(). It prefers _write(). |
19 #define write(fd, buf, count) _write(fd, buf, static_cast<unsigned int>(count)) | 19 #define write(fd, buf, count) _write(fd, buf, static_cast<unsigned int>(count)) |
20 // Windows doesn't define STDERR_FILENO. Define it here. | 20 // Windows doesn't define STDERR_FILENO. Define it here. |
21 #define STDERR_FILENO 2 | 21 #define STDERR_FILENO 2 |
22 #elif defined(OS_MACOSX) | 22 #elif defined(OS_MACOSX) |
23 #include <asl.h> | 23 #include <asl.h> |
24 #include <CoreFoundation/CoreFoundation.h> | 24 #include <CoreFoundation/CoreFoundation.h> |
25 #include <mach/mach.h> | 25 #include <mach/mach.h> |
(...skipping 18 matching lines...) Expand all Loading... |
44 #define MAX_PATH PATH_MAX | 44 #define MAX_PATH PATH_MAX |
45 typedef FILE* FileHandle; | 45 typedef FILE* FileHandle; |
46 typedef pthread_mutex_t* MutexHandle; | 46 typedef pthread_mutex_t* MutexHandle; |
47 #endif | 47 #endif |
48 | 48 |
49 #include <algorithm> | 49 #include <algorithm> |
50 #include <cstring> | 50 #include <cstring> |
51 #include <ctime> | 51 #include <ctime> |
52 #include <iomanip> | 52 #include <iomanip> |
53 #include <ostream> | 53 #include <ostream> |
| 54 #include <stack> |
54 #include <string> | 55 #include <string> |
| 56 #include <utility> |
55 | 57 |
56 #include "base/base_switches.h" | 58 #include "base/base_switches.h" |
| 59 #include "base/callback.h" |
57 #include "base/command_line.h" | 60 #include "base/command_line.h" |
| 61 #include "base/debug/activity_tracker.h" |
58 #include "base/debug/alias.h" | 62 #include "base/debug/alias.h" |
59 #include "base/debug/debugger.h" | 63 #include "base/debug/debugger.h" |
60 #include "base/debug/stack_trace.h" | 64 #include "base/debug/stack_trace.h" |
| 65 #include "base/lazy_instance.h" |
61 #include "base/posix/eintr_wrapper.h" | 66 #include "base/posix/eintr_wrapper.h" |
62 #include "base/strings/string_piece.h" | 67 #include "base/strings/string_piece.h" |
63 #include "base/strings/string_util.h" | 68 #include "base/strings/string_util.h" |
64 #include "base/strings/stringprintf.h" | 69 #include "base/strings/stringprintf.h" |
65 #include "base/strings/sys_string_conversions.h" | 70 #include "base/strings/sys_string_conversions.h" |
66 #include "base/strings/utf_string_conversions.h" | 71 #include "base/strings/utf_string_conversions.h" |
67 #include "base/synchronization/lock_impl.h" | 72 #include "base/synchronization/lock_impl.h" |
68 #include "base/threading/platform_thread.h" | 73 #include "base/threading/platform_thread.h" |
69 #include "base/vlog.h" | 74 #include "base/vlog.h" |
70 #if defined(OS_POSIX) | 75 #if defined(OS_POSIX) |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 // What should be prepended to each message? | 119 // What should be prepended to each message? |
115 bool g_log_process_id = false; | 120 bool g_log_process_id = false; |
116 bool g_log_thread_id = false; | 121 bool g_log_thread_id = false; |
117 bool g_log_timestamp = true; | 122 bool g_log_timestamp = true; |
118 bool g_log_tickcount = false; | 123 bool g_log_tickcount = false; |
119 | 124 |
120 // Should we pop up fatal debug messages in a dialog? | 125 // Should we pop up fatal debug messages in a dialog? |
121 bool show_error_dialogs = false; | 126 bool show_error_dialogs = false; |
122 | 127 |
123 // An assert handler override specified by the client to be called instead of | 128 // An assert handler override specified by the client to be called instead of |
124 // the debug message dialog and process termination. | 129 // the debug message dialog and process termination. Assert handlers are stored |
125 LogAssertHandlerFunction log_assert_handler = nullptr; | 130 // in stack to allow overriding and restoring. |
| 131 base::LazyInstance<std::stack<LogAssertHandlerFunction>>::Leaky |
| 132 log_assert_handler_stack = LAZY_INSTANCE_INITIALIZER; |
| 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 ScopedLogAssertHandler::ScopedLogAssertHandler( |
448 log_assert_handler = handler; | 456 LogAssertHandlerFunction handler) { |
| 457 log_assert_handler_stack.Get().push(std::move(handler)); |
| 458 } |
| 459 |
| 460 ScopedLogAssertHandler::~ScopedLogAssertHandler() { |
| 461 log_assert_handler_stack.Get().pop(); |
449 } | 462 } |
450 | 463 |
451 void SetLogMessageHandler(LogMessageHandlerFunction handler) { | 464 void SetLogMessageHandler(LogMessageHandlerFunction handler) { |
452 log_message_handler = handler; | 465 log_message_handler = handler; |
453 } | 466 } |
454 | 467 |
455 LogMessageHandlerFunction GetLogMessageHandler() { | 468 LogMessageHandlerFunction GetLogMessageHandler() { |
456 return log_message_handler; | 469 return log_message_handler; |
457 } | 470 } |
458 | 471 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
524 | 537 |
525 LogMessage::LogMessage(const char* file, int line, LogSeverity severity, | 538 LogMessage::LogMessage(const char* file, int line, LogSeverity severity, |
526 std::string* result) | 539 std::string* result) |
527 : severity_(severity), file_(file), line_(line) { | 540 : severity_(severity), file_(file), line_(line) { |
528 Init(file, line); | 541 Init(file, line); |
529 stream_ << "Check failed: " << *result; | 542 stream_ << "Check failed: " << *result; |
530 delete result; | 543 delete result; |
531 } | 544 } |
532 | 545 |
533 LogMessage::~LogMessage() { | 546 LogMessage::~LogMessage() { |
| 547 size_t stack_start = stream_.tellp(); |
534 #if !defined(OFFICIAL_BUILD) && !defined(OS_NACL) && !defined(__UCLIBC__) | 548 #if !defined(OFFICIAL_BUILD) && !defined(OS_NACL) && !defined(__UCLIBC__) |
535 if (severity_ == LOG_FATAL && !base::debug::BeingDebugged()) { | 549 if (severity_ == LOG_FATAL && !base::debug::BeingDebugged()) { |
536 // Include a stack trace on a fatal, unless a debugger is attached. | 550 // Include a stack trace on a fatal, unless a debugger is attached. |
537 base::debug::StackTrace trace; | 551 base::debug::StackTrace trace; |
538 stream_ << std::endl; // Newline to separate from log message. | 552 stream_ << std::endl; // Newline to separate from log message. |
539 trace.OutputToStream(&stream_); | 553 trace.OutputToStream(&stream_); |
540 } | 554 } |
541 #endif | 555 #endif |
542 stream_ << std::endl; | 556 stream_ << std::endl; |
543 std::string str_newline(stream_.str()); | 557 std::string str_newline(stream_.str()); |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
732 base::debug::GlobalActivityTracker::Get(); | 746 base::debug::GlobalActivityTracker::Get(); |
733 if (tracker) | 747 if (tracker) |
734 tracker->RecordLogMessage(str_newline); | 748 tracker->RecordLogMessage(str_newline); |
735 | 749 |
736 // Ensure the first characters of the string are on the stack so they | 750 // Ensure the first characters of the string are on the stack so they |
737 // are contained in minidumps for diagnostic purposes. | 751 // are contained in minidumps for diagnostic purposes. |
738 char str_stack[1024]; | 752 char str_stack[1024]; |
739 str_newline.copy(str_stack, arraysize(str_stack)); | 753 str_newline.copy(str_stack, arraysize(str_stack)); |
740 base::debug::Alias(str_stack); | 754 base::debug::Alias(str_stack); |
741 | 755 |
742 if (log_assert_handler) { | 756 if (!(log_assert_handler_stack == nullptr) && |
743 // Make a copy of the string for the handler out of paranoia. | 757 !log_assert_handler_stack.Get().empty()) { |
744 log_assert_handler(std::string(stream_.str())); | 758 LogAssertHandlerFunction log_assert_handler = |
| 759 log_assert_handler_stack.Get().top(); |
| 760 |
| 761 if (log_assert_handler) { |
| 762 log_assert_handler.Run( |
| 763 file_, line_, |
| 764 base::StringPiece(str_newline.c_str() + message_start_, |
| 765 stack_start - message_start_), |
| 766 base::StringPiece(str_newline.c_str() + stack_start)); |
| 767 } |
745 } else { | 768 } else { |
746 // Don't use the string with the newline, get a fresh version to send to | 769 // 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 | 770 // 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 | 771 // user in release mode. The enduser can't do anything with this |
749 // information, and displaying message boxes when the application is | 772 // information, and displaying message boxes when the application is |
750 // hosed can cause additional problems. | 773 // hosed can cause additional problems. |
751 #ifndef NDEBUG | 774 #ifndef NDEBUG |
752 if (!base::debug::BeingDebugged()) { | 775 if (!base::debug::BeingDebugged()) { |
753 // Displaying a dialog is unnecessary when debugging and can complicate | 776 // Displaying a dialog is unnecessary when debugging and can complicate |
754 // debugging. | 777 // debugging. |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
947 BASE_EXPORT void LogErrorNotReached(const char* file, int line) { | 970 BASE_EXPORT void LogErrorNotReached(const char* file, int line) { |
948 LogMessage(file, line, LOG_ERROR).stream() | 971 LogMessage(file, line, LOG_ERROR).stream() |
949 << "NOTREACHED() hit."; | 972 << "NOTREACHED() hit."; |
950 } | 973 } |
951 | 974 |
952 } // namespace logging | 975 } // namespace logging |
953 | 976 |
954 std::ostream& std::operator<<(std::ostream& out, const wchar_t* wstr) { | 977 std::ostream& std::operator<<(std::ostream& out, const wchar_t* wstr) { |
955 return out << (wstr ? base::WideToUTF8(wstr) : std::string()); | 978 return out << (wstr ? base::WideToUTF8(wstr) : std::string()); |
956 } | 979 } |
OLD | NEW |