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