Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(531)

Side by Side Diff: base/logging.cc

Issue 2034393004: Allow multiple logging::LogMessage{Handler,Listener}s Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: clean up, MockLog uses listener Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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"
67 #include "base/synchronization/lock_impl.h" 70 #include "base/synchronization/lock_impl.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)
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
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<LogMessageHandlerFunction>>::Leaky
131 log_message_handlers = LAZY_INSTANCE_INITIALIZER;
132 // Log message listeners that get notified of every log message we process
133 // before log message handlers.
134 base::LazyInstance<std::unordered_set<LogMessageListenerFunction>>::Leaky
135 log_message_listeners = LAZY_INSTANCE_INITIALIZER;
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();
136 #endif 144 #endif
137 } 145 }
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
343 } // namespace 351 } // namespace
344 352
353 ScopedLogMessageListener::ScopedLogMessageListener(
354 LogMessageListenerFunction listener)
355 : listener_(listener) {
356 AddLogMessageListener(listener);
357 }
358
359 ScopedLogMessageListener::~ScopedLogMessageListener() {
360 RemoveLogMessageListener(listener_);
361 }
362
345 LoggingSettings::LoggingSettings() 363 LoggingSettings::LoggingSettings()
346 : logging_dest(LOG_DEFAULT), 364 : logging_dest(LOG_DEFAULT),
347 log_file(nullptr), 365 log_file(nullptr),
348 lock_log(LOCK_LOG_FILE), 366 lock_log(LOCK_LOG_FILE),
349 delete_old(APPEND_TO_OLD_LOG_FILE) {} 367 delete_old(APPEND_TO_OLD_LOG_FILE) {}
350 368
351 bool BaseInitLoggingImpl(const LoggingSettings& settings) { 369 bool BaseInitLoggingImpl(const LoggingSettings& settings) {
352 #if defined(OS_NACL) 370 #if defined(OS_NACL)
353 // Can log only to the system debug log. 371 // Can log only to the system debug log.
354 CHECK_EQ(settings.logging_dest & ~LOG_TO_SYSTEM_DEBUG_LOG, 0); 372 CHECK_EQ(settings.logging_dest & ~LOG_TO_SYSTEM_DEBUG_LOG, 0);
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
402 return g_min_log_level; 420 return g_min_log_level;
403 } 421 }
404 422
405 bool ShouldCreateLogMessage(int severity) { 423 bool ShouldCreateLogMessage(int severity) {
406 if (severity < g_min_log_level) 424 if (severity < g_min_log_level)
407 return false; 425 return false;
408 426
409 // Return true here unless we know ~LogMessage won't do anything. Note that 427 // Return true here unless we know ~LogMessage won't do anything. Note that
410 // ~LogMessage writes to stderr if severity_ >= kAlwaysPrintErrorLevel, even 428 // ~LogMessage writes to stderr if severity_ >= kAlwaysPrintErrorLevel, even
411 // when g_logging_destination is LOG_NONE. 429 // when g_logging_destination is LOG_NONE.
412 return g_logging_destination != LOG_NONE || log_message_handler || 430 return g_logging_destination != LOG_NONE ||
431 !log_message_handlers.Get().empty() ||
432 !log_message_listeners.Get().empty() ||
413 severity >= kAlwaysPrintErrorLevel; 433 severity >= kAlwaysPrintErrorLevel;
414 } 434 }
415 435
416 int GetVlogVerbosity() { 436 int GetVlogVerbosity() {
417 return std::max(-1, LOG_INFO - GetMinLogLevel()); 437 return std::max(-1, LOG_INFO - GetMinLogLevel());
418 } 438 }
419 439
420 int GetVlogLevelHelper(const char* file, size_t N) { 440 int GetVlogLevelHelper(const char* file, size_t N) {
421 DCHECK_GT(N, 0U); 441 DCHECK_GT(N, 0U);
422 // Note: |g_vlog_info| may change on a different thread during startup 442 // Note: |g_vlog_info| may change on a different thread during startup
(...skipping 13 matching lines...) Expand all
436 } 456 }
437 457
438 void SetShowErrorDialogs(bool enable_dialogs) { 458 void SetShowErrorDialogs(bool enable_dialogs) {
439 show_error_dialogs = enable_dialogs; 459 show_error_dialogs = enable_dialogs;
440 } 460 }
441 461
442 void SetLogAssertHandler(LogAssertHandlerFunction handler) { 462 void SetLogAssertHandler(LogAssertHandlerFunction handler) {
443 log_assert_handler = handler; 463 log_assert_handler = handler;
444 } 464 }
445 465
446 void SetLogMessageHandler(LogMessageHandlerFunction handler) { 466 void PushLogMessageHandler(LogMessageHandlerFunction handler) {
447 log_message_handler = handler; 467 log_message_handlers.Get().push_front(handler);
448 } 468 }
449 469
450 LogMessageHandlerFunction GetLogMessageHandler() { 470 void PopLogMessageHandler(LogMessageHandlerFunction expected) {
451 return log_message_handler; 471 CHECK_EQ(GetTopLogMessageHandler(), expected);
472 log_message_handlers.Get().pop_front();
473 }
474
475 LogMessageHandlerFunction GetTopLogMessageHandler() {
476 if (log_message_handlers.Get().empty())
477 return nullptr;
478 return log_message_handlers.Get().front();
479 }
480
481 void AddLogMessageListener(LogMessageListenerFunction listener) {
482 log_message_listeners.Get().insert(listener);
483 }
484
485 void RemoveLogMessageListener(LogMessageListenerFunction listener) {
486 CHECK_EQ(1u, log_message_listeners.Get().erase(listener));
452 } 487 }
453 488
454 // Explicit instantiations for commonly used comparisons. 489 // Explicit instantiations for commonly used comparisons.
455 template std::string* MakeCheckOpString<int, int>( 490 template std::string* MakeCheckOpString<int, int>(
456 const int&, const int&, const char* names); 491 const int&, const int&, const char* names);
457 template std::string* MakeCheckOpString<unsigned long, unsigned long>( 492 template std::string* MakeCheckOpString<unsigned long, unsigned long>(
458 const unsigned long&, const unsigned long&, const char* names); 493 const unsigned long&, const unsigned long&, const char* names);
459 template std::string* MakeCheckOpString<unsigned long, unsigned int>( 494 template std::string* MakeCheckOpString<unsigned long, unsigned int>(
460 const unsigned long&, const unsigned int&, const char* names); 495 const unsigned long&, const unsigned int&, const char* names);
461 template std::string* MakeCheckOpString<unsigned int, unsigned long>( 496 template std::string* MakeCheckOpString<unsigned int, unsigned long>(
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
530 if (severity_ == LOG_FATAL && !base::debug::BeingDebugged()) { 565 if (severity_ == LOG_FATAL && !base::debug::BeingDebugged()) {
531 // Include a stack trace on a fatal, unless a debugger is attached. 566 // Include a stack trace on a fatal, unless a debugger is attached.
532 base::debug::StackTrace trace; 567 base::debug::StackTrace trace;
533 stream_ << std::endl; // Newline to separate from log message. 568 stream_ << std::endl; // Newline to separate from log message.
534 trace.OutputToStream(&stream_); 569 trace.OutputToStream(&stream_);
535 } 570 }
536 #endif 571 #endif
537 stream_ << std::endl; 572 stream_ << std::endl;
538 std::string str_newline(stream_.str()); 573 std::string str_newline(stream_.str());
539 574
540 // Give any log message handler first dibs on the message. 575 // Broadcast to log message listeners first.
541 if (log_message_handler && 576 for (auto* listeners : log_message_listeners.Get()) {
542 log_message_handler(severity_, file_, line_, 577 listeners(severity_, file_, line_, message_start_, str_newline);
543 message_start_, str_newline)) { 578 }
544 // The handler took care of it, no further processing. 579
545 return; 580 // Give log message handlers first dibs on the message.
581 for (auto* handler : log_message_handlers.Get()) {
582 if (handler(severity_, file_, line_, message_start_, str_newline)) {
583 // The handler took care of it, no further processing.
584 return;
585 }
546 } 586 }
547 587
548 if ((g_logging_destination & LOG_TO_SYSTEM_DEBUG_LOG) != 0) { 588 if ((g_logging_destination & LOG_TO_SYSTEM_DEBUG_LOG) != 0) {
549 #if defined(OS_WIN) 589 #if defined(OS_WIN)
550 OutputDebugStringA(str_newline.c_str()); 590 OutputDebugStringA(str_newline.c_str());
551 #elif defined(OS_MACOSX) 591 #elif defined(OS_MACOSX)
552 // In LOG_TO_SYSTEM_DEBUG_LOG mode, log messages are always written to 592 // 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 593 // 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 594 // 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 595 // 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
921 BASE_EXPORT void LogErrorNotReached(const char* file, int line) { 961 BASE_EXPORT void LogErrorNotReached(const char* file, int line) {
922 LogMessage(file, line, LOG_ERROR).stream() 962 LogMessage(file, line, LOG_ERROR).stream()
923 << "NOTREACHED() hit."; 963 << "NOTREACHED() hit.";
924 } 964 }
925 965
926 } // namespace logging 966 } // namespace logging
927 967
928 std::ostream& std::operator<<(std::ostream& out, const wchar_t* wstr) { 968 std::ostream& std::operator<<(std::ostream& out, const wchar_t* wstr) {
929 return out << (wstr ? base::WideToUTF8(wstr) : std::string()); 969 return out << (wstr ? base::WideToUTF8(wstr) : std::string());
930 } 970 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698