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