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