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/debug/activity_tracker.h" | 10 #include "base/debug/activity_tracker.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 42 #include <sys/stat.h> | 42 #include <sys/stat.h> |
| 43 #include <unistd.h> | 43 #include <unistd.h> |
| 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 <deque> | |
| 52 #include <iomanip> | 53 #include <iomanip> |
| 53 #include <ostream> | 54 #include <ostream> |
| 54 #include <string> | 55 #include <string> |
| 55 | 56 |
| 56 #include "base/base_switches.h" | 57 #include "base/base_switches.h" |
| 57 #include "base/command_line.h" | 58 #include "base/command_line.h" |
| 58 #include "base/debug/alias.h" | 59 #include "base/debug/alias.h" |
| 59 #include "base/debug/debugger.h" | 60 #include "base/debug/debugger.h" |
| 60 #include "base/debug/stack_trace.h" | 61 #include "base/debug/stack_trace.h" |
| 62 #include "base/lazy_instance.h" | |
| 61 #include "base/posix/eintr_wrapper.h" | 63 #include "base/posix/eintr_wrapper.h" |
| 62 #include "base/strings/string_piece.h" | 64 #include "base/strings/string_piece.h" |
| 63 #include "base/strings/string_util.h" | 65 #include "base/strings/string_util.h" |
| 64 #include "base/strings/stringprintf.h" | 66 #include "base/strings/stringprintf.h" |
| 65 #include "base/strings/sys_string_conversions.h" | 67 #include "base/strings/sys_string_conversions.h" |
| 66 #include "base/strings/utf_string_conversions.h" | 68 #include "base/strings/utf_string_conversions.h" |
| 67 #include "base/synchronization/lock_impl.h" | 69 #include "base/synchronization/lock_impl.h" |
| 70 #include "base/synchronization/read_write_lock.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) |
| 71 #include "base/posix/safe_strerror.h" | 74 #include "base/posix/safe_strerror.h" |
| 72 #endif | 75 #endif |
| 73 | 76 |
| 74 #if defined(OS_ANDROID) | 77 #if defined(OS_ANDROID) |
| 75 #include <android/log.h> | 78 #include <android/log.h> |
| 76 #endif | 79 #endif |
| 77 | 80 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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. |
| 125 LogAssertHandlerFunction log_assert_handler = nullptr; | 128 LogAssertHandlerFunction log_assert_handler = nullptr; |
| 126 // A log message handler that gets notified of every log message we process. | 129 // Log message handlers that get notified of every log message we process. |
| 127 LogMessageHandlerFunction log_message_handler = nullptr; | 130 base::LazyInstance<std::deque<LogMessageHandler*>>::Leaky log_message_handlers = |
| 131 LAZY_INSTANCE_INITIALIZER; | |
| 132 base::LazyInstance<base::subtle::ReadWriteLock>::Leaky | |
| 133 log_message_handler_lock = LAZY_INSTANCE_INITIALIZER; | |
| 134 // Log message listeners that get notified of every log message we process | |
| 135 // before log message handlers. | |
| 136 base::LazyInstance<std::deque<LogMessageListener*>>::Leaky | |
| 137 log_message_listeners = LAZY_INSTANCE_INITIALIZER; | |
| 138 base::LazyInstance<base::subtle::ReadWriteLock>::Leaky | |
| 139 log_message_listener_lock = LAZY_INSTANCE_INITIALIZER; | |
| 128 | 140 |
| 129 // Helper functions to wrap platform differences. | 141 // Helper functions to wrap platform differences. |
| 130 | 142 |
| 131 int32_t CurrentProcessId() { | 143 int32_t CurrentProcessId() { |
| 132 #if defined(OS_WIN) | 144 #if defined(OS_WIN) |
| 133 return GetCurrentProcessId(); | 145 return GetCurrentProcessId(); |
| 134 #elif defined(OS_POSIX) | 146 #elif defined(OS_POSIX) |
| 135 return getpid(); | 147 return getpid(); |
| 136 #endif | 148 #endif |
| 137 } | 149 } |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 335 void CloseLogFileUnlocked() { | 347 void CloseLogFileUnlocked() { |
| 336 if (!g_log_file) | 348 if (!g_log_file) |
| 337 return; | 349 return; |
| 338 | 350 |
| 339 CloseFile(g_log_file); | 351 CloseFile(g_log_file); |
| 340 g_log_file = nullptr; | 352 g_log_file = nullptr; |
| 341 } | 353 } |
| 342 | 354 |
| 343 } // namespace | 355 } // namespace |
| 344 | 356 |
| 357 LogMessageHandler::LogMessageHandler() { | |
| 358 base::subtle::AutoWriteLock lock(log_message_handler_lock.Get()); | |
| 359 log_message_handlers.Get().push_front(this); | |
| 360 } | |
| 361 | |
| 362 LogMessageHandler::~LogMessageHandler() { | |
| 363 base::subtle::AutoWriteLock lock(log_message_handler_lock.Get()); | |
| 364 auto& handlers = log_message_handlers.Get(); | |
| 365 size_t count = handlers.size(); | |
| 366 handlers.erase(std::remove(handlers.begin(), handlers.end(), this), | |
| 367 handlers.end()); | |
| 368 DCHECK_EQ(count - 1, handlers.size()); | |
|
grt (UTC plus 2)
2017/01/04 09:12:18
this check is a bit bogus. if you think it's possi
| |
| 369 } | |
| 370 | |
| 371 size_t LogMessageHandlerCountForTesting() { | |
| 372 base::subtle::AutoReadLock lock(log_message_handler_lock.Get()); | |
| 373 return log_message_handlers.Get().size(); | |
| 374 } | |
| 375 | |
| 376 LogMessageListener::LogMessageListener() { | |
| 377 base::subtle::AutoWriteLock lock(log_message_listener_lock.Get()); | |
| 378 log_message_listeners.Get().push_front(this); | |
| 379 } | |
| 380 | |
| 381 LogMessageListener::~LogMessageListener() { | |
| 382 base::subtle::AutoWriteLock lock(log_message_listener_lock.Get()); | |
| 383 auto& listeners = log_message_listeners.Get(); | |
| 384 size_t count = listeners.size(); | |
| 385 listeners.erase(std::remove(listeners.begin(), listeners.end(), this), | |
| 386 listeners.end()); | |
| 387 DCHECK_EQ(count - 1, listeners.size()); | |
| 388 } | |
| 389 | |
| 390 size_t LogMessageListenerCountForTesting() { | |
| 391 base::subtle::AutoReadLock lock(log_message_listener_lock.Get()); | |
| 392 return log_message_listeners.Get().size(); | |
| 393 } | |
| 394 | |
| 345 // This is never instantiated, it's just used for EAT_STREAM_PARAMETERS to have | 395 // 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 | 396 // an object of the correct type on the LHS of the unused part of the ternary |
| 347 // operator. | 397 // operator. |
| 348 std::ostream* g_swallow_stream; | 398 std::ostream* g_swallow_stream; |
| 349 | 399 |
| 350 LoggingSettings::LoggingSettings() | 400 LoggingSettings::LoggingSettings() |
| 351 : logging_dest(LOG_DEFAULT), | 401 : logging_dest(LOG_DEFAULT), |
| 352 log_file(nullptr), | 402 log_file(nullptr), |
| 353 lock_log(LOCK_LOG_FILE), | 403 lock_log(LOCK_LOG_FILE), |
| 354 delete_old(APPEND_TO_OLD_LOG_FILE) {} | 404 delete_old(APPEND_TO_OLD_LOG_FILE) {} |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 407 return g_min_log_level; | 457 return g_min_log_level; |
| 408 } | 458 } |
| 409 | 459 |
| 410 bool ShouldCreateLogMessage(int severity) { | 460 bool ShouldCreateLogMessage(int severity) { |
| 411 if (severity < g_min_log_level) | 461 if (severity < g_min_log_level) |
| 412 return false; | 462 return false; |
| 413 | 463 |
| 414 // Return true here unless we know ~LogMessage won't do anything. Note that | 464 // Return true here unless we know ~LogMessage won't do anything. Note that |
| 415 // ~LogMessage writes to stderr if severity_ >= kAlwaysPrintErrorLevel, even | 465 // ~LogMessage writes to stderr if severity_ >= kAlwaysPrintErrorLevel, even |
| 416 // when g_logging_destination is LOG_NONE. | 466 // when g_logging_destination is LOG_NONE. |
| 417 return g_logging_destination != LOG_NONE || log_message_handler || | 467 bool handlers_empty, listeners_empty; |
| 418 severity >= kAlwaysPrintErrorLevel; | 468 { |
| 469 base::subtle::AutoReadLock lock(log_message_handler_lock.Get()); | |
| 470 handlers_empty = log_message_handlers.Get().empty(); | |
| 471 } | |
| 472 { | |
| 473 base::subtle::AutoReadLock lock(log_message_listener_lock.Get()); | |
| 474 listeners_empty = log_message_listeners.Get().empty(); | |
| 475 } | |
| 476 return g_logging_destination != LOG_NONE || !handlers_empty || | |
| 477 !listeners_empty || severity >= kAlwaysPrintErrorLevel; | |
| 419 } | 478 } |
| 420 | 479 |
| 421 int GetVlogVerbosity() { | 480 int GetVlogVerbosity() { |
| 422 return std::max(-1, LOG_INFO - GetMinLogLevel()); | 481 return std::max(-1, LOG_INFO - GetMinLogLevel()); |
| 423 } | 482 } |
| 424 | 483 |
| 425 int GetVlogLevelHelper(const char* file, size_t N) { | 484 int GetVlogLevelHelper(const char* file, size_t N) { |
| 426 DCHECK_GT(N, 0U); | 485 DCHECK_GT(N, 0U); |
| 427 // Note: |g_vlog_info| may change on a different thread during startup | 486 // Note: |g_vlog_info| may change on a different thread during startup |
| 428 // (but will always be valid or nullptr). | 487 // (but will always be valid or nullptr). |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 441 } | 500 } |
| 442 | 501 |
| 443 void SetShowErrorDialogs(bool enable_dialogs) { | 502 void SetShowErrorDialogs(bool enable_dialogs) { |
| 444 show_error_dialogs = enable_dialogs; | 503 show_error_dialogs = enable_dialogs; |
| 445 } | 504 } |
| 446 | 505 |
| 447 void SetLogAssertHandler(LogAssertHandlerFunction handler) { | 506 void SetLogAssertHandler(LogAssertHandlerFunction handler) { |
| 448 log_assert_handler = handler; | 507 log_assert_handler = handler; |
| 449 } | 508 } |
| 450 | 509 |
| 451 void SetLogMessageHandler(LogMessageHandlerFunction handler) { | |
| 452 log_message_handler = handler; | |
| 453 } | |
| 454 | |
| 455 LogMessageHandlerFunction GetLogMessageHandler() { | |
| 456 return log_message_handler; | |
| 457 } | |
| 458 | |
| 459 // Explicit instantiations for commonly used comparisons. | 510 // Explicit instantiations for commonly used comparisons. |
| 460 template std::string* MakeCheckOpString<int, int>( | 511 template std::string* MakeCheckOpString<int, int>( |
| 461 const int&, const int&, const char* names); | 512 const int&, const int&, const char* names); |
| 462 template std::string* MakeCheckOpString<unsigned long, unsigned long>( | 513 template std::string* MakeCheckOpString<unsigned long, unsigned long>( |
| 463 const unsigned long&, const unsigned long&, const char* names); | 514 const unsigned long&, const unsigned long&, const char* names); |
| 464 template std::string* MakeCheckOpString<unsigned long, unsigned int>( | 515 template std::string* MakeCheckOpString<unsigned long, unsigned int>( |
| 465 const unsigned long&, const unsigned int&, const char* names); | 516 const unsigned long&, const unsigned int&, const char* names); |
| 466 template std::string* MakeCheckOpString<unsigned int, unsigned long>( | 517 template std::string* MakeCheckOpString<unsigned int, unsigned long>( |
| 467 const unsigned int&, const unsigned long&, const char* names); | 518 const unsigned int&, const unsigned long&, const char* names); |
| 468 template std::string* MakeCheckOpString<std::string, std::string>( | 519 template std::string* MakeCheckOpString<std::string, std::string>( |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 535 if (severity_ == LOG_FATAL && !base::debug::BeingDebugged()) { | 586 if (severity_ == LOG_FATAL && !base::debug::BeingDebugged()) { |
| 536 // Include a stack trace on a fatal, unless a debugger is attached. | 587 // Include a stack trace on a fatal, unless a debugger is attached. |
| 537 base::debug::StackTrace trace; | 588 base::debug::StackTrace trace; |
| 538 stream_ << std::endl; // Newline to separate from log message. | 589 stream_ << std::endl; // Newline to separate from log message. |
| 539 trace.OutputToStream(&stream_); | 590 trace.OutputToStream(&stream_); |
| 540 } | 591 } |
| 541 #endif | 592 #endif |
| 542 stream_ << std::endl; | 593 stream_ << std::endl; |
| 543 std::string str_newline(stream_.str()); | 594 std::string str_newline(stream_.str()); |
| 544 | 595 |
| 545 // Give any log message handler first dibs on the message. | 596 { |
| 546 if (log_message_handler && | 597 base::subtle::AutoReadLock lock(log_message_listener_lock.Get()); |
| 547 log_message_handler(severity_, file_, line_, | 598 // Broadcast to log message listeners first. |
| 548 message_start_, str_newline)) { | 599 for (auto* listener : log_message_listeners.Get()) { |
| 549 // The handler took care of it, no further processing. | 600 listener->OnMessage(severity_, file_, line_, message_start_, str_newline); |
| 550 return; | 601 } |
| 602 } | |
| 603 | |
| 604 { | |
| 605 base::subtle::AutoReadLock lock(log_message_handler_lock.Get()); | |
| 606 // Give log message handlers first dibs on the message. | |
| 607 for (auto* handler : log_message_handlers.Get()) { | |
| 608 if (handler->OnMessage(severity_, file_, line_, message_start_, | |
| 609 str_newline)) { | |
| 610 // The handler took care of it, no further processing. | |
| 611 return; | |
| 612 } | |
| 613 } | |
| 551 } | 614 } |
| 552 | 615 |
| 553 if ((g_logging_destination & LOG_TO_SYSTEM_DEBUG_LOG) != 0) { | 616 if ((g_logging_destination & LOG_TO_SYSTEM_DEBUG_LOG) != 0) { |
| 554 #if defined(OS_WIN) | 617 #if defined(OS_WIN) |
| 555 OutputDebugStringA(str_newline.c_str()); | 618 OutputDebugStringA(str_newline.c_str()); |
| 556 #elif defined(OS_MACOSX) | 619 #elif defined(OS_MACOSX) |
| 557 // In LOG_TO_SYSTEM_DEBUG_LOG mode, log messages are always written to | 620 // In LOG_TO_SYSTEM_DEBUG_LOG mode, log messages are always written to |
| 558 // stderr. If stderr is /dev/null, also log via ASL (Apple System Log). If | 621 // stderr. If stderr is /dev/null, also log via ASL (Apple System Log). If |
| 559 // there's something weird about stderr, assume that log messages are going | 622 // there's something weird about stderr, assume that log messages are going |
| 560 // nowhere and log via ASL too. Messages logged via ASL show up in | 623 // nowhere and log via ASL too. Messages logged via ASL show up in |
| (...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 947 BASE_EXPORT void LogErrorNotReached(const char* file, int line) { | 1010 BASE_EXPORT void LogErrorNotReached(const char* file, int line) { |
| 948 LogMessage(file, line, LOG_ERROR).stream() | 1011 LogMessage(file, line, LOG_ERROR).stream() |
| 949 << "NOTREACHED() hit."; | 1012 << "NOTREACHED() hit."; |
| 950 } | 1013 } |
| 951 | 1014 |
| 952 } // namespace logging | 1015 } // namespace logging |
| 953 | 1016 |
| 954 std::ostream& std::operator<<(std::ostream& out, const wchar_t* wstr) { | 1017 std::ostream& std::operator<<(std::ostream& out, const wchar_t* wstr) { |
| 955 return out << (wstr ? base::WideToUTF8(wstr) : std::string()); | 1018 return out << (wstr ? base::WideToUTF8(wstr) : std::string()); |
| 956 } | 1019 } |
| OLD | NEW |