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 }; | |
| 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 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 333 } | 341 } |
| 334 | 342 |
| 335 void CloseLogFileUnlocked() { | 343 void CloseLogFileUnlocked() { |
| 336 if (!g_log_file) | 344 if (!g_log_file) |
| 337 return; | 345 return; |
| 338 | 346 |
| 339 CloseFile(g_log_file); | 347 CloseFile(g_log_file); |
| 340 g_log_file = nullptr; | 348 g_log_file = nullptr; |
| 341 } | 349 } |
| 342 | 350 |
| 351 void PushLogAssertHandler(LogAssertHandlerFunction handler) { | |
| 352 LogAssertHandlerStackItem* stack_item = new LogAssertHandlerStackItem; | |
| 353 stack_item->handler = handler; | |
| 354 stack_item->next = log_assert_handler_stack; | |
| 355 log_assert_handler_stack = stack_item; | |
| 356 } | |
| 357 | |
| 358 void PopLogAssertHandler() { | |
| 359 LogAssertHandlerStackItem* stack_item = log_assert_handler_stack; | |
| 360 if (stack_item) { | |
| 361 log_assert_handler_stack = stack_item->next; | |
| 362 delete stack_item; | |
| 363 } | |
| 364 } | |
| 365 | |
| 366 LogAssertHandlerFunction GetLogAssertHandler() { | |
| 367 if (log_assert_handler_stack) | |
| 368 return log_assert_handler_stack->handler; | |
| 369 return LogAssertHandlerFunction(); | |
| 370 } | |
| 371 | |
| 343 } // namespace | 372 } // namespace |
| 344 | 373 |
| 345 // This is never instantiated, it's just used for EAT_STREAM_PARAMETERS to have | 374 // This is never instantiated, it's just used for EAT_STREAM_PARAMETERS to have |
| 346 // an object of the correct type on the LHS of the unused part of the ternary | 375 // an object of the correct type on the LHS of the unused part of the ternary |
| 347 // operator. | 376 // operator. |
| 348 std::ostream* g_swallow_stream; | 377 std::ostream* g_swallow_stream; |
| 349 | 378 |
| 350 LoggingSettings::LoggingSettings() | 379 LoggingSettings::LoggingSettings() |
| 351 : logging_dest(LOG_DEFAULT), | 380 : logging_dest(LOG_DEFAULT), |
| 352 log_file(nullptr), | 381 log_file(nullptr), |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 438 g_log_thread_id = enable_thread_id; | 467 g_log_thread_id = enable_thread_id; |
| 439 g_log_timestamp = enable_timestamp; | 468 g_log_timestamp = enable_timestamp; |
| 440 g_log_tickcount = enable_tickcount; | 469 g_log_tickcount = enable_tickcount; |
| 441 } | 470 } |
| 442 | 471 |
| 443 void SetShowErrorDialogs(bool enable_dialogs) { | 472 void SetShowErrorDialogs(bool enable_dialogs) { |
| 444 show_error_dialogs = enable_dialogs; | 473 show_error_dialogs = enable_dialogs; |
| 445 } | 474 } |
| 446 | 475 |
| 447 void SetLogAssertHandler(LogAssertHandlerFunction handler) { | 476 void SetLogAssertHandler(LogAssertHandlerFunction handler) { |
| 448 log_assert_handler = handler; | 477 PushLogAssertHandler(handler); |
|
Paweł Hajdan Jr.
2017/01/27 17:19:43
Why create separate functions instead of putting t
alex-ac
2017/02/11 20:12:19
Done.
| |
| 478 } | |
| 479 | |
| 480 void ResetLogAssertHandler() { | |
| 481 PopLogAssertHandler(); | |
| 449 } | 482 } |
| 450 | 483 |
| 451 void SetLogMessageHandler(LogMessageHandlerFunction handler) { | 484 void SetLogMessageHandler(LogMessageHandlerFunction handler) { |
| 452 log_message_handler = handler; | 485 log_message_handler = handler; |
| 453 } | 486 } |
| 454 | 487 |
| 455 LogMessageHandlerFunction GetLogMessageHandler() { | 488 LogMessageHandlerFunction GetLogMessageHandler() { |
| 456 return log_message_handler; | 489 return log_message_handler; |
| 457 } | 490 } |
| 458 | 491 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 524 | 557 |
| 525 LogMessage::LogMessage(const char* file, int line, LogSeverity severity, | 558 LogMessage::LogMessage(const char* file, int line, LogSeverity severity, |
| 526 std::string* result) | 559 std::string* result) |
| 527 : severity_(severity), file_(file), line_(line) { | 560 : severity_(severity), file_(file), line_(line) { |
| 528 Init(file, line); | 561 Init(file, line); |
| 529 stream_ << "Check failed: " << *result; | 562 stream_ << "Check failed: " << *result; |
| 530 delete result; | 563 delete result; |
| 531 } | 564 } |
| 532 | 565 |
| 533 LogMessage::~LogMessage() { | 566 LogMessage::~LogMessage() { |
| 567 size_t stack_start = stream_.str().length(); | |
| 534 #if !defined(OFFICIAL_BUILD) && !defined(OS_NACL) && !defined(__UCLIBC__) | 568 #if !defined(OFFICIAL_BUILD) && !defined(OS_NACL) && !defined(__UCLIBC__) |
| 535 if (severity_ == LOG_FATAL && !base::debug::BeingDebugged()) { | 569 if (severity_ == LOG_FATAL && !base::debug::BeingDebugged()) { |
| 536 // Include a stack trace on a fatal, unless a debugger is attached. | 570 // Include a stack trace on a fatal, unless a debugger is attached. |
| 537 base::debug::StackTrace trace; | 571 base::debug::StackTrace trace; |
| 538 stream_ << std::endl; // Newline to separate from log message. | 572 stream_ << std::endl; // Newline to separate from log message. |
| 539 trace.OutputToStream(&stream_); | 573 trace.OutputToStream(&stream_); |
| 540 } | 574 } |
| 541 #endif | 575 #endif |
| 542 stream_ << std::endl; | 576 stream_ << std::endl; |
| 543 std::string str_newline(stream_.str()); | 577 std::string str_newline(stream_.str()); |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 732 base::debug::GlobalActivityTracker::Get(); | 766 base::debug::GlobalActivityTracker::Get(); |
| 733 if (tracker) | 767 if (tracker) |
| 734 tracker->RecordLogMessage(str_newline); | 768 tracker->RecordLogMessage(str_newline); |
| 735 | 769 |
| 736 // Ensure the first characters of the string are on the stack so they | 770 // Ensure the first characters of the string are on the stack so they |
| 737 // are contained in minidumps for diagnostic purposes. | 771 // are contained in minidumps for diagnostic purposes. |
| 738 char str_stack[1024]; | 772 char str_stack[1024]; |
| 739 str_newline.copy(str_stack, arraysize(str_stack)); | 773 str_newline.copy(str_stack, arraysize(str_stack)); |
| 740 base::debug::Alias(str_stack); | 774 base::debug::Alias(str_stack); |
| 741 | 775 |
| 776 LogAssertHandlerFunction log_assert_handler = GetLogAssertHandler(); | |
| 742 if (log_assert_handler) { | 777 if (log_assert_handler) { |
| 743 // Make a copy of the string for the handler out of paranoia. | 778 // Make a copy of the string for the handler out of paranoia. |
| 744 log_assert_handler(std::string(stream_.str())); | 779 log_assert_handler.Run(file_, line_, message_start_, stack_start, |
|
Paweł Hajdan Jr.
2017/01/27 17:19:44
Would it make sense to execute all handlers on the
alex-ac
2017/02/11 20:12:19
No there's some code that adds new handler to prev
| |
| 780 std::string(stream_.str())); | |
| 745 } else { | 781 } else { |
| 746 // Don't use the string with the newline, get a fresh version to send to | 782 // 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 | 783 // 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 | 784 // user in release mode. The enduser can't do anything with this |
| 749 // information, and displaying message boxes when the application is | 785 // information, and displaying message boxes when the application is |
| 750 // hosed can cause additional problems. | 786 // hosed can cause additional problems. |
| 751 #ifndef NDEBUG | 787 #ifndef NDEBUG |
| 752 if (!base::debug::BeingDebugged()) { | 788 if (!base::debug::BeingDebugged()) { |
| 753 // Displaying a dialog is unnecessary when debugging and can complicate | 789 // Displaying a dialog is unnecessary when debugging and can complicate |
| 754 // debugging. | 790 // debugging. |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 947 BASE_EXPORT void LogErrorNotReached(const char* file, int line) { | 983 BASE_EXPORT void LogErrorNotReached(const char* file, int line) { |
| 948 LogMessage(file, line, LOG_ERROR).stream() | 984 LogMessage(file, line, LOG_ERROR).stream() |
| 949 << "NOTREACHED() hit."; | 985 << "NOTREACHED() hit."; |
| 950 } | 986 } |
| 951 | 987 |
| 952 } // namespace logging | 988 } // namespace logging |
| 953 | 989 |
| 954 std::ostream& std::operator<<(std::ostream& out, const wchar_t* wstr) { | 990 std::ostream& std::operator<<(std::ostream& out, const wchar_t* wstr) { |
| 955 return out << (wstr ? base::WideToUTF8(wstr) : std::string()); | 991 return out << (wstr ? base::WideToUTF8(wstr) : std::string()); |
| 956 } | 992 } |
| OLD | NEW |