| 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 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> |
| 56 #include <unordered_set> |
| 55 | 57 |
| 56 #include "base/base_switches.h" | 58 #include "base/base_switches.h" |
| 57 #include "base/command_line.h" | 59 #include "base/command_line.h" |
| 58 #include "base/debug/alias.h" | 60 #include "base/debug/alias.h" |
| 59 #include "base/debug/debugger.h" | 61 #include "base/debug/debugger.h" |
| 60 #include "base/debug/stack_trace.h" | 62 #include "base/debug/stack_trace.h" |
| 63 #include "base/lazy_instance.h" |
| 61 #include "base/posix/eintr_wrapper.h" | 64 #include "base/posix/eintr_wrapper.h" |
| 62 #include "base/strings/string_piece.h" | 65 #include "base/strings/string_piece.h" |
| 63 #include "base/strings/string_util.h" | 66 #include "base/strings/string_util.h" |
| 64 #include "base/strings/stringprintf.h" | 67 #include "base/strings/stringprintf.h" |
| 65 #include "base/strings/sys_string_conversions.h" | 68 #include "base/strings/sys_string_conversions.h" |
| 66 #include "base/strings/utf_string_conversions.h" | 69 #include "base/strings/utf_string_conversions.h" |
| 70 #include "base/synchronization/lock.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) |
| 71 #include "base/posix/safe_strerror.h" | 75 #include "base/posix/safe_strerror.h" |
| 72 #endif | 76 #endif |
| 73 | 77 |
| 74 #if defined(OS_ANDROID) | 78 #if defined(OS_ANDROID) |
| 75 #include <android/log.h> | 79 #include <android/log.h> |
| 76 #endif | 80 #endif |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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. |
| 125 LogAssertHandlerFunction log_assert_handler = nullptr; | 129 LogAssertHandlerFunction log_assert_handler = nullptr; |
| 126 // 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. |
| 127 LogMessageHandlerFunction log_message_handler = nullptr; | 131 base::LazyInstance<std::deque<LogMessageHandler*>>::Leaky |
| 132 log_message_handlers = LAZY_INSTANCE_INITIALIZER; |
| 133 base::LazyInstance<base::Lock>::Leaky log_message_handler_lock; |
| 134 // Log message listeners that get notified of every log message we process |
| 135 // before log message handlers. |
| 136 base::LazyInstance<std::unordered_set<LogMessageListener*>>::Leaky |
| 137 log_message_listeners = LAZY_INSTANCE_INITIALIZER; |
| 138 base::LazyInstance<base::Lock>::Leaky log_message_listener_lock; |
| 128 | 139 |
| 129 // Helper functions to wrap platform differences. | 140 // Helper functions to wrap platform differences. |
| 130 | 141 |
| 131 int32_t CurrentProcessId() { | 142 int32_t CurrentProcessId() { |
| 132 #if defined(OS_WIN) | 143 #if defined(OS_WIN) |
| 133 return GetCurrentProcessId(); | 144 return GetCurrentProcessId(); |
| 134 #elif defined(OS_POSIX) | 145 #elif defined(OS_POSIX) |
| 135 return getpid(); | 146 return getpid(); |
| 136 #endif | 147 #endif |
| 137 } | 148 } |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 335 void CloseLogFileUnlocked() { | 346 void CloseLogFileUnlocked() { |
| 336 if (!g_log_file) | 347 if (!g_log_file) |
| 337 return; | 348 return; |
| 338 | 349 |
| 339 CloseFile(g_log_file); | 350 CloseFile(g_log_file); |
| 340 g_log_file = nullptr; | 351 g_log_file = nullptr; |
| 341 } | 352 } |
| 342 | 353 |
| 343 } // namespace | 354 } // namespace |
| 344 | 355 |
| 356 LogMessageHandler::LogMessageHandler() { |
| 357 base::AutoLock lock(log_message_handler_lock.Get()); |
| 358 log_message_handlers.Get().push_front(this); |
| 359 } |
| 360 |
| 361 LogMessageHandler::~LogMessageHandler() { |
| 362 base::AutoLock lock(log_message_handler_lock.Get()); |
| 363 auto& handlers = log_message_handlers.Get(); |
| 364 size_t count = handlers.size(); |
| 365 handlers.erase(std::remove(handlers.begin(), handlers.end(), this), |
| 366 handlers.end()); |
| 367 DCHECK_EQ(count - 1, handlers.size()); |
| 368 } |
| 369 |
| 370 size_t LogMessageHandlerCountForTesting() { |
| 371 base::AutoLock lock(log_message_handler_lock.Get()); |
| 372 return log_message_handlers.Get().size(); |
| 373 } |
| 374 |
| 375 LogMessageListener::LogMessageListener() { |
| 376 base::AutoLock lock(log_message_listener_lock.Get()); |
| 377 log_message_listeners.Get().insert(this); |
| 378 } |
| 379 |
| 380 LogMessageListener::~LogMessageListener() { |
| 381 base::AutoLock lock(log_message_listener_lock.Get()); |
| 382 size_t erased_count = log_message_listeners.Get().erase(this); |
| 383 DCHECK_EQ(1u, erased_count); |
| 384 } |
| 385 |
| 386 size_t LogMessageListenerCountForTesting() { |
| 387 base::AutoLock lock(log_message_listener_lock.Get()); |
| 388 return log_message_listeners.Get().size(); |
| 389 } |
| 390 |
| 345 LoggingSettings::LoggingSettings() | 391 LoggingSettings::LoggingSettings() |
| 346 : logging_dest(LOG_DEFAULT), | 392 : logging_dest(LOG_DEFAULT), |
| 347 log_file(nullptr), | 393 log_file(nullptr), |
| 348 lock_log(LOCK_LOG_FILE), | 394 lock_log(LOCK_LOG_FILE), |
| 349 delete_old(APPEND_TO_OLD_LOG_FILE) {} | 395 delete_old(APPEND_TO_OLD_LOG_FILE) {} |
| 350 | 396 |
| 351 bool BaseInitLoggingImpl(const LoggingSettings& settings) { | 397 bool BaseInitLoggingImpl(const LoggingSettings& settings) { |
| 352 #if defined(OS_NACL) | 398 #if defined(OS_NACL) |
| 353 // Can log only to the system debug log. | 399 // Can log only to the system debug log. |
| 354 CHECK_EQ(settings.logging_dest & ~LOG_TO_SYSTEM_DEBUG_LOG, 0); | 400 CHECK_EQ(settings.logging_dest & ~LOG_TO_SYSTEM_DEBUG_LOG, 0); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 402 return g_min_log_level; | 448 return g_min_log_level; |
| 403 } | 449 } |
| 404 | 450 |
| 405 bool ShouldCreateLogMessage(int severity) { | 451 bool ShouldCreateLogMessage(int severity) { |
| 406 if (severity < g_min_log_level) | 452 if (severity < g_min_log_level) |
| 407 return false; | 453 return false; |
| 408 | 454 |
| 409 // Return true here unless we know ~LogMessage won't do anything. Note that | 455 // Return true here unless we know ~LogMessage won't do anything. Note that |
| 410 // ~LogMessage writes to stderr if severity_ >= kAlwaysPrintErrorLevel, even | 456 // ~LogMessage writes to stderr if severity_ >= kAlwaysPrintErrorLevel, even |
| 411 // when g_logging_destination is LOG_NONE. | 457 // when g_logging_destination is LOG_NONE. |
| 412 return g_logging_destination != LOG_NONE || log_message_handler || | 458 return g_logging_destination != LOG_NONE || |
| 459 !log_message_handlers.Get().empty() || |
| 460 !log_message_listeners.Get().empty() || |
| 413 severity >= kAlwaysPrintErrorLevel; | 461 severity >= kAlwaysPrintErrorLevel; |
| 414 } | 462 } |
| 415 | 463 |
| 416 int GetVlogVerbosity() { | 464 int GetVlogVerbosity() { |
| 417 return std::max(-1, LOG_INFO - GetMinLogLevel()); | 465 return std::max(-1, LOG_INFO - GetMinLogLevel()); |
| 418 } | 466 } |
| 419 | 467 |
| 420 int GetVlogLevelHelper(const char* file, size_t N) { | 468 int GetVlogLevelHelper(const char* file, size_t N) { |
| 421 DCHECK_GT(N, 0U); | 469 DCHECK_GT(N, 0U); |
| 422 // Note: |g_vlog_info| may change on a different thread during startup | 470 // Note: |g_vlog_info| may change on a different thread during startup |
| (...skipping 13 matching lines...) Expand all Loading... |
| 436 } | 484 } |
| 437 | 485 |
| 438 void SetShowErrorDialogs(bool enable_dialogs) { | 486 void SetShowErrorDialogs(bool enable_dialogs) { |
| 439 show_error_dialogs = enable_dialogs; | 487 show_error_dialogs = enable_dialogs; |
| 440 } | 488 } |
| 441 | 489 |
| 442 void SetLogAssertHandler(LogAssertHandlerFunction handler) { | 490 void SetLogAssertHandler(LogAssertHandlerFunction handler) { |
| 443 log_assert_handler = handler; | 491 log_assert_handler = handler; |
| 444 } | 492 } |
| 445 | 493 |
| 446 void SetLogMessageHandler(LogMessageHandlerFunction handler) { | |
| 447 log_message_handler = handler; | |
| 448 } | |
| 449 | |
| 450 LogMessageHandlerFunction GetLogMessageHandler() { | |
| 451 return log_message_handler; | |
| 452 } | |
| 453 | |
| 454 // Explicit instantiations for commonly used comparisons. | 494 // Explicit instantiations for commonly used comparisons. |
| 455 template std::string* MakeCheckOpString<int, int>( | 495 template std::string* MakeCheckOpString<int, int>( |
| 456 const int&, const int&, const char* names); | 496 const int&, const int&, const char* names); |
| 457 template std::string* MakeCheckOpString<unsigned long, unsigned long>( | 497 template std::string* MakeCheckOpString<unsigned long, unsigned long>( |
| 458 const unsigned long&, const unsigned long&, const char* names); | 498 const unsigned long&, const unsigned long&, const char* names); |
| 459 template std::string* MakeCheckOpString<unsigned long, unsigned int>( | 499 template std::string* MakeCheckOpString<unsigned long, unsigned int>( |
| 460 const unsigned long&, const unsigned int&, const char* names); | 500 const unsigned long&, const unsigned int&, const char* names); |
| 461 template std::string* MakeCheckOpString<unsigned int, unsigned long>( | 501 template std::string* MakeCheckOpString<unsigned int, unsigned long>( |
| 462 const unsigned int&, const unsigned long&, const char* names); | 502 const unsigned int&, const unsigned long&, const char* names); |
| 463 template std::string* MakeCheckOpString<std::string, std::string>( | 503 template std::string* MakeCheckOpString<std::string, std::string>( |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 530 if (severity_ == LOG_FATAL && !base::debug::BeingDebugged()) { | 570 if (severity_ == LOG_FATAL && !base::debug::BeingDebugged()) { |
| 531 // Include a stack trace on a fatal, unless a debugger is attached. | 571 // Include a stack trace on a fatal, unless a debugger is attached. |
| 532 base::debug::StackTrace trace; | 572 base::debug::StackTrace trace; |
| 533 stream_ << std::endl; // Newline to separate from log message. | 573 stream_ << std::endl; // Newline to separate from log message. |
| 534 trace.OutputToStream(&stream_); | 574 trace.OutputToStream(&stream_); |
| 535 } | 575 } |
| 536 #endif | 576 #endif |
| 537 stream_ << std::endl; | 577 stream_ << std::endl; |
| 538 std::string str_newline(stream_.str()); | 578 std::string str_newline(stream_.str()); |
| 539 | 579 |
| 540 // Give any log message handler first dibs on the message. | 580 { |
| 541 if (log_message_handler && | 581 base::AutoLock lock(log_message_listener_lock.Get()); |
| 542 log_message_handler(severity_, file_, line_, | 582 // Broadcast to log message listeners first. |
| 543 message_start_, str_newline)) { | 583 for (auto* listener : log_message_listeners.Get()) { |
| 544 // The handler took care of it, no further processing. | 584 listener->OnMessage(severity_, file_, line_, message_start_, str_newline); |
| 545 return; | 585 } |
| 586 } |
| 587 |
| 588 { |
| 589 base::AutoLock lock(log_message_handler_lock.Get()); |
| 590 // Give log message handlers first dibs on the message. |
| 591 for (auto* handler : log_message_handlers.Get()) { |
| 592 if (handler->OnMessage(severity_, file_, line_, message_start_, |
| 593 str_newline)) { |
| 594 // The handler took care of it, no further processing. |
| 595 return; |
| 596 } |
| 597 } |
| 546 } | 598 } |
| 547 | 599 |
| 548 if ((g_logging_destination & LOG_TO_SYSTEM_DEBUG_LOG) != 0) { | 600 if ((g_logging_destination & LOG_TO_SYSTEM_DEBUG_LOG) != 0) { |
| 549 #if defined(OS_WIN) | 601 #if defined(OS_WIN) |
| 550 OutputDebugStringA(str_newline.c_str()); | 602 OutputDebugStringA(str_newline.c_str()); |
| 551 #elif defined(OS_MACOSX) | 603 #elif defined(OS_MACOSX) |
| 552 // In LOG_TO_SYSTEM_DEBUG_LOG mode, log messages are always written to | 604 // In LOG_TO_SYSTEM_DEBUG_LOG mode, log messages are always written to |
| 553 // stderr. If stderr is /dev/null, also log via ASL (Apple System Log). If | 605 // stderr. If stderr is /dev/null, also log via ASL (Apple System Log). If |
| 554 // there's something weird about stderr, assume that log messages are going | 606 // there's something weird about stderr, assume that log messages are going |
| 555 // nowhere and log via ASL too. Messages logged via ASL show up in | 607 // 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... |
| 921 BASE_EXPORT void LogErrorNotReached(const char* file, int line) { | 973 BASE_EXPORT void LogErrorNotReached(const char* file, int line) { |
| 922 LogMessage(file, line, LOG_ERROR).stream() | 974 LogMessage(file, line, LOG_ERROR).stream() |
| 923 << "NOTREACHED() hit."; | 975 << "NOTREACHED() hit."; |
| 924 } | 976 } |
| 925 | 977 |
| 926 } // namespace logging | 978 } // namespace logging |
| 927 | 979 |
| 928 std::ostream& std::operator<<(std::ostream& out, const wchar_t* wstr) { | 980 std::ostream& std::operator<<(std::ostream& out, const wchar_t* wstr) { |
| 929 return out << (wstr ? base::WideToUTF8(wstr) : std::string()); | 981 return out << (wstr ? base::WideToUTF8(wstr) : std::string()); |
| 930 } | 982 } |
| OLD | NEW |