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/macros.h" | 10 #include "base/macros.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 #include <sys/stat.h> | 44 #include <sys/stat.h> |
45 #include <unistd.h> | 45 #include <unistd.h> |
46 #define MAX_PATH PATH_MAX | 46 #define MAX_PATH PATH_MAX |
47 typedef FILE* FileHandle; | 47 typedef FILE* FileHandle; |
48 typedef pthread_mutex_t* MutexHandle; | 48 typedef pthread_mutex_t* MutexHandle; |
49 #endif | 49 #endif |
50 | 50 |
51 #include <algorithm> | 51 #include <algorithm> |
52 #include <cstring> | 52 #include <cstring> |
53 #include <ctime> | 53 #include <ctime> |
| 54 #include <deque> |
54 #include <iomanip> | 55 #include <iomanip> |
55 #include <ostream> | 56 #include <ostream> |
56 #include <string> | 57 #include <string> |
57 | 58 |
58 #include "base/base_switches.h" | 59 #include "base/base_switches.h" |
59 #include "base/command_line.h" | 60 #include "base/command_line.h" |
60 #include "base/debug/alias.h" | 61 #include "base/debug/alias.h" |
61 #include "base/debug/debugger.h" | 62 #include "base/debug/debugger.h" |
62 #include "base/debug/stack_trace.h" | 63 #include "base/debug/stack_trace.h" |
| 64 #include "base/lazy_instance.h" |
63 #include "base/posix/eintr_wrapper.h" | 65 #include "base/posix/eintr_wrapper.h" |
64 #include "base/strings/string_piece.h" | 66 #include "base/strings/string_piece.h" |
65 #include "base/strings/string_util.h" | 67 #include "base/strings/string_util.h" |
66 #include "base/strings/stringprintf.h" | 68 #include "base/strings/stringprintf.h" |
67 #include "base/strings/sys_string_conversions.h" | 69 #include "base/strings/sys_string_conversions.h" |
68 #include "base/strings/utf_string_conversions.h" | 70 #include "base/strings/utf_string_conversions.h" |
69 #include "base/synchronization/lock_impl.h" | 71 #include "base/synchronization/lock_impl.h" |
70 #include "base/threading/platform_thread.h" | 72 #include "base/threading/platform_thread.h" |
71 #include "base/vlog.h" | 73 #include "base/vlog.h" |
72 #if defined(OS_POSIX) | 74 #if defined(OS_POSIX) |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 bool g_log_thread_id = false; | 120 bool g_log_thread_id = false; |
119 bool g_log_timestamp = true; | 121 bool g_log_timestamp = true; |
120 bool g_log_tickcount = false; | 122 bool g_log_tickcount = false; |
121 | 123 |
122 // Should we pop up fatal debug messages in a dialog? | 124 // Should we pop up fatal debug messages in a dialog? |
123 bool show_error_dialogs = false; | 125 bool show_error_dialogs = false; |
124 | 126 |
125 // 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 |
126 // the debug message dialog and process termination. | 128 // the debug message dialog and process termination. |
127 LogAssertHandlerFunction log_assert_handler = nullptr; | 129 LogAssertHandlerFunction log_assert_handler = nullptr; |
128 // A log message handler that gets notified of every log message we process. | 130 // Log message handlers that get notified of every log message we process. |
129 LogMessageHandlerFunction log_message_handler = nullptr; | 131 base::LazyInstance<std::deque<LogMessageHandlerFunction>>::Leaky |
| 132 log_message_handlers = LAZY_INSTANCE_INITIALIZER; |
130 | 133 |
131 // Helper functions to wrap platform differences. | 134 // Helper functions to wrap platform differences. |
132 | 135 |
133 int32_t CurrentProcessId() { | 136 int32_t CurrentProcessId() { |
134 #if defined(OS_WIN) | 137 #if defined(OS_WIN) |
135 return GetCurrentProcessId(); | 138 return GetCurrentProcessId(); |
136 #elif defined(OS_POSIX) | 139 #elif defined(OS_POSIX) |
137 return getpid(); | 140 return getpid(); |
138 #endif | 141 #endif |
139 } | 142 } |
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
393 return g_min_log_level; | 396 return g_min_log_level; |
394 } | 397 } |
395 | 398 |
396 bool ShouldCreateLogMessage(int severity) { | 399 bool ShouldCreateLogMessage(int severity) { |
397 if (severity < g_min_log_level) | 400 if (severity < g_min_log_level) |
398 return false; | 401 return false; |
399 | 402 |
400 // Return true here unless we know ~LogMessage won't do anything. Note that | 403 // Return true here unless we know ~LogMessage won't do anything. Note that |
401 // ~LogMessage writes to stderr if severity_ >= kAlwaysPrintErrorLevel, even | 404 // ~LogMessage writes to stderr if severity_ >= kAlwaysPrintErrorLevel, even |
402 // when g_logging_destination is LOG_NONE. | 405 // when g_logging_destination is LOG_NONE. |
403 return g_logging_destination != LOG_NONE || log_message_handler || | 406 return g_logging_destination != LOG_NONE || |
| 407 !log_message_handlers.Get().empty() || |
404 severity >= kAlwaysPrintErrorLevel; | 408 severity >= kAlwaysPrintErrorLevel; |
405 } | 409 } |
406 | 410 |
407 int GetVlogVerbosity() { | 411 int GetVlogVerbosity() { |
408 return std::max(-1, LOG_INFO - GetMinLogLevel()); | 412 return std::max(-1, LOG_INFO - GetMinLogLevel()); |
409 } | 413 } |
410 | 414 |
411 int GetVlogLevelHelper(const char* file, size_t N) { | 415 int GetVlogLevelHelper(const char* file, size_t N) { |
412 DCHECK_GT(N, 0U); | 416 DCHECK_GT(N, 0U); |
413 // Note: |g_vlog_info| may change on a different thread during startup | 417 // Note: |g_vlog_info| may change on a different thread during startup |
(...skipping 13 matching lines...) Expand all Loading... |
427 } | 431 } |
428 | 432 |
429 void SetShowErrorDialogs(bool enable_dialogs) { | 433 void SetShowErrorDialogs(bool enable_dialogs) { |
430 show_error_dialogs = enable_dialogs; | 434 show_error_dialogs = enable_dialogs; |
431 } | 435 } |
432 | 436 |
433 void SetLogAssertHandler(LogAssertHandlerFunction handler) { | 437 void SetLogAssertHandler(LogAssertHandlerFunction handler) { |
434 log_assert_handler = handler; | 438 log_assert_handler = handler; |
435 } | 439 } |
436 | 440 |
437 void SetLogMessageHandler(LogMessageHandlerFunction handler) { | 441 void PushLogMessageHandler(LogMessageHandlerFunction handler) { |
438 log_message_handler = handler; | 442 log_message_handlers.Get().push_front(handler); |
439 } | 443 } |
440 | 444 |
441 LogMessageHandlerFunction GetLogMessageHandler() { | 445 void PopLogMessageHandler() { |
442 return log_message_handler; | 446 log_message_handlers.Get().pop_front(); |
| 447 } |
| 448 |
| 449 LogMessageHandlerFunction GetTopLogMessageHandler() { |
| 450 if (log_message_handlers.Get().empty()) |
| 451 return nullptr; |
| 452 return log_message_handlers.Get().front(); |
443 } | 453 } |
444 | 454 |
445 // Explicit instantiations for commonly used comparisons. | 455 // Explicit instantiations for commonly used comparisons. |
446 template std::string* MakeCheckOpString<int, int>( | 456 template std::string* MakeCheckOpString<int, int>( |
447 const int&, const int&, const char* names); | 457 const int&, const int&, const char* names); |
448 template std::string* MakeCheckOpString<unsigned long, unsigned long>( | 458 template std::string* MakeCheckOpString<unsigned long, unsigned long>( |
449 const unsigned long&, const unsigned long&, const char* names); | 459 const unsigned long&, const unsigned long&, const char* names); |
450 template std::string* MakeCheckOpString<unsigned long, unsigned int>( | 460 template std::string* MakeCheckOpString<unsigned long, unsigned int>( |
451 const unsigned long&, const unsigned int&, const char* names); | 461 const unsigned long&, const unsigned int&, const char* names); |
452 template std::string* MakeCheckOpString<unsigned int, unsigned long>( | 462 template std::string* MakeCheckOpString<unsigned int, unsigned long>( |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
521 if (severity_ == LOG_FATAL && !base::debug::BeingDebugged()) { | 531 if (severity_ == LOG_FATAL && !base::debug::BeingDebugged()) { |
522 // Include a stack trace on a fatal, unless a debugger is attached. | 532 // Include a stack trace on a fatal, unless a debugger is attached. |
523 base::debug::StackTrace trace; | 533 base::debug::StackTrace trace; |
524 stream_ << std::endl; // Newline to separate from log message. | 534 stream_ << std::endl; // Newline to separate from log message. |
525 trace.OutputToStream(&stream_); | 535 trace.OutputToStream(&stream_); |
526 } | 536 } |
527 #endif | 537 #endif |
528 stream_ << std::endl; | 538 stream_ << std::endl; |
529 std::string str_newline(stream_.str()); | 539 std::string str_newline(stream_.str()); |
530 | 540 |
531 // Give any log message handler first dibs on the message. | 541 // Give log message handlers first dibs on the message. |
532 if (log_message_handler && | 542 for (auto* handler : log_message_handlers.Get()) { |
533 log_message_handler(severity_, file_, line_, | 543 if (handler(severity_, file_, line_, message_start_, str_newline)) { |
534 message_start_, str_newline)) { | 544 // The handler took care of it, no further processing. |
535 // The handler took care of it, no further processing. | 545 return; |
536 return; | 546 } |
537 } | 547 } |
538 | 548 |
539 if ((g_logging_destination & LOG_TO_SYSTEM_DEBUG_LOG) != 0) { | 549 if ((g_logging_destination & LOG_TO_SYSTEM_DEBUG_LOG) != 0) { |
540 #if defined(OS_WIN) | 550 #if defined(OS_WIN) |
541 OutputDebugStringA(str_newline.c_str()); | 551 OutputDebugStringA(str_newline.c_str()); |
542 #elif defined(OS_MACOSX) | 552 #elif defined(OS_MACOSX) |
543 // In LOG_TO_SYSTEM_DEBUG_LOG mode, log messages are always written to | 553 // In LOG_TO_SYSTEM_DEBUG_LOG mode, log messages are always written to |
544 // stderr. If stderr is /dev/null, also log via ASL (Apple System Log). If | 554 // stderr. If stderr is /dev/null, also log via ASL (Apple System Log). If |
545 // there's something weird about stderr, assume that log messages are going | 555 // there's something weird about stderr, assume that log messages are going |
546 // nowhere and log via ASL too. Messages logged via ASL show up in | 556 // nowhere and log via ASL too. Messages logged via ASL show up in |
(...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
912 BASE_EXPORT void LogErrorNotReached(const char* file, int line) { | 922 BASE_EXPORT void LogErrorNotReached(const char* file, int line) { |
913 LogMessage(file, line, LOG_ERROR).stream() | 923 LogMessage(file, line, LOG_ERROR).stream() |
914 << "NOTREACHED() hit."; | 924 << "NOTREACHED() hit."; |
915 } | 925 } |
916 | 926 |
917 } // namespace logging | 927 } // namespace logging |
918 | 928 |
919 std::ostream& std::operator<<(std::ostream& out, const wchar_t* wstr) { | 929 std::ostream& std::operator<<(std::ostream& out, const wchar_t* wstr) { |
920 return out << (wstr ? base::WideToUTF8(wstr) : std::string()); | 930 return out << (wstr ? base::WideToUTF8(wstr) : std::string()); |
921 } | 931 } |
OLD | NEW |