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

Side by Side Diff: remoting/host/native_messaging/log_message_handler.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 unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "remoting/host/native_messaging/log_message_handler.h" 5 #include "remoting/host/native_messaging/log_message_handler.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/lazy_instance.h" 8 #include "base/lazy_instance.h"
9 #include "base/single_thread_task_runner.h" 9 #include "base/single_thread_task_runner.h"
10 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
(...skipping 19 matching lines...) Expand all
30 const Delegate& delegate) 30 const Delegate& delegate)
31 : delegate_(delegate), 31 : delegate_(delegate),
32 suppress_logging_(false), 32 suppress_logging_(false),
33 caller_task_runner_(base::ThreadTaskRunnerHandle::Get()), 33 caller_task_runner_(base::ThreadTaskRunnerHandle::Get()),
34 weak_ptr_factory_(this) { 34 weak_ptr_factory_(this) {
35 base::AutoLock lock(g_log_message_handler_lock.Get()); 35 base::AutoLock lock(g_log_message_handler_lock.Get());
36 if (g_log_message_handler) { 36 if (g_log_message_handler) {
37 LOG(FATAL) << "LogMessageHandler is already registered. Only one instance " 37 LOG(FATAL) << "LogMessageHandler is already registered. Only one instance "
38 << "per process is allowed."; 38 << "per process is allowed.";
39 } 39 }
40 previous_log_message_handler_ = logging::GetLogMessageHandler(); 40 logging::AddLogMessageHandler(&LogMessageHandler::OnLogMessage);
41 logging::SetLogMessageHandler(&LogMessageHandler::OnLogMessage);
42 g_log_message_handler = this; 41 g_log_message_handler = this;
43 } 42 }
44 43
45 LogMessageHandler::~LogMessageHandler() { 44 LogMessageHandler::~LogMessageHandler() {
46 base::AutoLock lock(g_log_message_handler_lock.Get()); 45 base::AutoLock lock(g_log_message_handler_lock.Get());
47 if (logging::GetLogMessageHandler() != &LogMessageHandler::OnLogMessage) { 46 logging::RemoveLogMessageHandler(&LogMessageHandler::OnLogMessage);
48 LOG(FATAL) << "LogMessageHandler is not the top-most message handler. "
49 << "Cannot unregister.";
50 }
51 logging::SetLogMessageHandler(previous_log_message_handler_);
52 g_log_message_handler = nullptr; 47 g_log_message_handler = nullptr;
53 } 48 }
54 49
55 // static 50 // static
56 const char* LogMessageHandler::kDebugMessageTypeName = "_debug_log"; 51 const char* LogMessageHandler::kDebugMessageTypeName = "_debug_log";
57 52
58 // static 53 // static
59 bool LogMessageHandler::OnLogMessage( 54 bool LogMessageHandler::OnLogMessage(
60 logging::LogSeverity severity, 55 logging::LogSeverity severity,
61 const char* file, 56 const std::string& file,
62 int line, 57 int line,
63 size_t message_start,
64 const std::string& str) { 58 const std::string& str) {
65 base::AutoLock lock(g_log_message_handler_lock.Get()); 59 base::AutoLock lock(g_log_message_handler_lock.Get());
66 if (g_log_message_handler) { 60 if (g_log_message_handler) {
67 g_log_message_handler->PostLogMessageToCorrectThread( 61 g_log_message_handler->PostLogMessageToCorrectThread(
68 severity, file, line, message_start, str); 62 severity, file, line, str);
69 } 63 }
70 return false; 64 return false;
71 } 65 }
72 66
73 void LogMessageHandler::PostLogMessageToCorrectThread( 67 void LogMessageHandler::PostLogMessageToCorrectThread(
74 logging::LogSeverity severity, 68 logging::LogSeverity severity,
75 const char* file, 69 const std::string& file,
76 int line, 70 int line,
77 size_t message_start,
78 const std::string& str) { 71 const std::string& str) {
79 // Don't process this message if we're already logging and on the caller 72 // Don't process this message if we're already logging and on the caller
80 // thread. This guards against an infinite loop if any code called by this 73 // thread. This guards against an infinite loop if any code called by this
81 // class logs something. 74 // class logs something.
82 if (suppress_logging_ && caller_task_runner_->BelongsToCurrentThread()) { 75 if (suppress_logging_ && caller_task_runner_->BelongsToCurrentThread()) {
83 return; 76 return;
84 } 77 }
85 78
86 // This method is always called under the global lock, so post a task to 79 // This method is always called under the global lock, so post a task to
87 // handle the log message, even if we're already on the correct thread. 80 // handle the log message, even if we're already on the correct thread.
88 // This alows the lock to be released quickly. 81 // This alows the lock to be released quickly.
89 // 82 //
90 // Note that this means that LOG(FATAL) messages will be lost because the 83 // Note that this means that LOG(FATAL) messages will be lost because the
91 // process will exit before the message is sent to the client. 84 // process will exit before the message is sent to the client.
92 caller_task_runner_->PostTask( 85 caller_task_runner_->PostTask(
93 FROM_HERE, 86 FROM_HERE,
94 base::Bind(&LogMessageHandler::SendLogMessageToClient, 87 base::Bind(&LogMessageHandler::SendLogMessageToClient,
95 weak_ptr_factory_.GetWeakPtr(), 88 weak_ptr_factory_.GetWeakPtr(),
96 severity, file, line, message_start, str)); 89 severity, file, line, str));
97 } 90 }
98 91
99 void LogMessageHandler::SendLogMessageToClient( 92 void LogMessageHandler::SendLogMessageToClient(
100 logging::LogSeverity severity, 93 logging::LogSeverity severity,
101 const char* file, 94 const std::string& file,
102 int line, 95 int line,
103 size_t message_start,
104 const std::string& str) { 96 const std::string& str) {
105 suppress_logging_ = true; 97 suppress_logging_ = true;
106 98
107 std::string severity_string = "log"; 99 std::string severity_string = "log";
108 switch (severity) { 100 switch (severity) {
109 case logging::LOG_WARNING: 101 case logging::LOG_WARNING:
110 severity_string = "warn"; 102 severity_string = "warn";
111 break; 103 break;
112 case logging::LOG_FATAL: 104 case logging::LOG_FATAL:
113 case logging::LOG_ERROR: 105 case logging::LOG_ERROR:
(...skipping 10 matching lines...) Expand all
124 dictionary->SetString("message", message); 116 dictionary->SetString("message", message);
125 dictionary->SetString("file", file); 117 dictionary->SetString("file", file);
126 dictionary->SetInteger("line", line); 118 dictionary->SetInteger("line", line);
127 119
128 delegate_.Run(std::move(dictionary)); 120 delegate_.Run(std::move(dictionary));
129 121
130 suppress_logging_ = false; 122 suppress_logging_ = false;
131 } 123 }
132 124
133 } // namespace remoting 125 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/host/native_messaging/log_message_handler.h ('k') | third_party/crashpad/README.chromium » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698