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

Unified Diff: base/logging.cc

Issue 2497833002: support multiple log message handlers in base/logging.h (Closed)
Patch Set: Created 4 years, 1 month 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/logging.h ('k') | base/logging_win.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/logging.cc
diff --git a/base/logging.cc b/base/logging.cc
index 0771b47c182e5c18c868f1124a0784bf411d74a9..316843bba603ded5ab16c72fcd20c5e2db75789f 100644
--- a/base/logging.cc
+++ b/base/logging.cc
@@ -52,6 +52,7 @@ typedef pthread_mutex_t* MutexHandle;
#include <iomanip>
#include <ostream>
#include <string>
+#include <vector>
#include "base/base_switches.h"
#include "base/command_line.h"
@@ -123,8 +124,8 @@ 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 handlers that get notified of every log message we process.
+std::vector<LogMessageHandlerFunction>* log_message_handlers = nullptr;
// Helper functions to wrap platform differences.
@@ -409,7 +410,8 @@ 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 ||
+ return g_logging_destination != LOG_NONE ||
+ (log_message_handlers && log_message_handlers->size() > 0) ||
severity >= kAlwaysPrintErrorLevel;
}
@@ -443,12 +445,24 @@ void SetLogAssertHandler(LogAssertHandlerFunction handler) {
log_assert_handler = handler;
}
-void SetLogMessageHandler(LogMessageHandlerFunction handler) {
- log_message_handler = handler;
+void AddLogMessageHandler(LogMessageHandlerFunction handler) {
+ if (!log_message_handlers)
+ log_message_handlers = new std::vector<LogMessageHandlerFunction>;
+ DCHECK_EQ(0, std::count(log_message_handlers->begin(),
+ log_message_handlers->end(), handler));
+ log_message_handlers->push_back(handler);
}
-LogMessageHandlerFunction GetLogMessageHandler() {
- return log_message_handler;
+void RemoveLogMessageHandler(LogMessageHandlerFunction handler) {
+ if (log_message_handlers) {
+ auto it = std::find(log_message_handlers->begin(),
+ log_message_handlers->end(), handler);
+ if (it != log_message_handlers->end()) {
+ log_message_handlers->erase(it);
+ return;
+ }
+ }
+ NOTREACHED();
}
// Explicit instantiations for commonly used comparisons.
@@ -538,11 +552,16 @@ LogMessage::~LogMessage() {
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;
+ if (log_message_handlers) {
+ const std::string message = str_newline.substr(message_start_);
+ if (std::count_if(log_message_handlers->begin(),
+ log_message_handlers->end(),
+ [this, &message](auto handler) {
+ return handler(severity_, file_, line_, message);
+ }) > 0) {
+ // At least one of the handlers took care of it, no further processing.
+ return;
+ }
}
if ((g_logging_destination & LOG_TO_SYSTEM_DEBUG_LOG) != 0) {
« no previous file with comments | « base/logging.h ('k') | base/logging_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698