Index: base/logging.cc |
diff --git a/base/logging.cc b/base/logging.cc |
index cea6edae4a37cef2519ff32045c46c0a3c73e4d0..6ac4cb28402ce3466467f683e2da5c638783869a 100644 |
--- a/base/logging.cc |
+++ b/base/logging.cc |
@@ -49,6 +49,7 @@ typedef pthread_mutex_t* MutexHandle; |
#include <algorithm> |
#include <cstring> |
#include <ctime> |
+#include <deque> |
#include <iomanip> |
#include <ostream> |
#include <string> |
@@ -58,6 +59,7 @@ typedef pthread_mutex_t* MutexHandle; |
#include "base/debug/alias.h" |
#include "base/debug/debugger.h" |
#include "base/debug/stack_trace.h" |
+#include "base/lazy_instance.h" |
#include "base/posix/eintr_wrapper.h" |
#include "base/strings/string_piece.h" |
#include "base/strings/string_util.h" |
@@ -65,6 +67,7 @@ typedef pthread_mutex_t* MutexHandle; |
#include "base/strings/sys_string_conversions.h" |
#include "base/strings/utf_string_conversions.h" |
#include "base/synchronization/lock_impl.h" |
+#include "base/synchronization/read_write_lock.h" |
#include "base/threading/platform_thread.h" |
#include "base/vlog.h" |
#if defined(OS_POSIX) |
@@ -123,8 +126,11 @@ bool show_error_dialogs = false; |
// An assert handler override specified by the client to be called instead of |
// the debug message dialog and process termination. |
LogAssertHandlerFunction log_assert_handler = nullptr; |
-// A log message handler that gets notified of every log message we process. |
-LogMessageHandlerFunction log_message_handler = nullptr; |
+// Log message listeners that get notified of every log message we process. |
+base::LazyInstance<std::deque<LogMessageListener*>>::Leaky |
+ log_message_listeners = LAZY_INSTANCE_INITIALIZER; |
+base::LazyInstance<base::subtle::ReadWriteLock>::Leaky |
+ log_message_listener_lock = LAZY_INSTANCE_INITIALIZER; |
// Helper functions to wrap platform differences. |
@@ -342,6 +348,25 @@ void CloseLogFileUnlocked() { |
} // namespace |
+LogMessageListener::LogMessageListener() { |
+ base::subtle::AutoWriteLock lock(log_message_listener_lock.Get()); |
+ log_message_listeners.Get().push_front(this); |
+} |
+ |
+LogMessageListener::~LogMessageListener() { |
+ base::subtle::AutoWriteLock lock(log_message_listener_lock.Get()); |
+ auto& listeners = log_message_listeners.Get(); |
+ size_t count = listeners.size(); |
+ listeners.erase(std::remove(listeners.begin(), listeners.end(), this), |
+ listeners.end()); |
+ DCHECK_EQ(count - 1, listeners.size()); |
+} |
+ |
+size_t LogMessageListenerCountForTesting() { |
+ base::subtle::AutoReadLock lock(log_message_listener_lock.Get()); |
+ return log_message_listeners.Get().size(); |
+} |
+ |
// This is never instantiated, it's just used for EAT_STREAM_PARAMETERS to have |
// an object of the correct type on the LHS of the unused part of the ternary |
// operator. |
@@ -414,7 +439,12 @@ bool ShouldCreateLogMessage(int severity) { |
// Return true here unless we know ~LogMessage won't do anything. Note that |
// ~LogMessage writes to stderr if severity_ >= kAlwaysPrintErrorLevel, even |
// when g_logging_destination is LOG_NONE. |
- return g_logging_destination != LOG_NONE || log_message_handler || |
+ bool listeners_empty; |
+ { |
+ base::subtle::AutoReadLock lock(log_message_listener_lock.Get()); |
+ listeners_empty = log_message_listeners.Get().empty(); |
+ } |
+ return g_logging_destination != LOG_NONE || !listeners_empty || |
severity >= kAlwaysPrintErrorLevel; |
} |
@@ -448,14 +478,6 @@ void SetLogAssertHandler(LogAssertHandlerFunction handler) { |
log_assert_handler = handler; |
} |
-void SetLogMessageHandler(LogMessageHandlerFunction handler) { |
- log_message_handler = handler; |
-} |
- |
-LogMessageHandlerFunction GetLogMessageHandler() { |
- return log_message_handler; |
-} |
- |
// Explicit instantiations for commonly used comparisons. |
template std::string* MakeCheckOpString<int, int>( |
const int&, const int&, const char* names); |
@@ -542,12 +564,12 @@ LogMessage::~LogMessage() { |
stream_ << std::endl; |
std::string str_newline(stream_.str()); |
- // Give any log message handler first dibs on the message. |
- if (log_message_handler && |
- log_message_handler(severity_, file_, line_, |
- message_start_, str_newline)) { |
- // The handler took care of it, no further processing. |
- return; |
+ { |
+ base::subtle::AutoReadLock lock(log_message_listener_lock.Get()); |
+ // Broadcast to log message listeners first. |
+ for (auto* listener : log_message_listeners.Get()) { |
+ listener->OnMessage(severity_, file_, line_, message_start_, str_newline); |
+ } |
} |
if ((g_logging_destination & LOG_TO_SYSTEM_DEBUG_LOG) != 0) { |