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

Side by Side Diff: remoting/host/native_messaging/log_message_handler.cc

Issue 2034393004: Allow multiple logging::LogMessage{Handler,Listener}s Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 3 years, 11 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 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"
11 #include "base/synchronization/lock.h" 11 #include "base/synchronization/lock.h"
12 #include "base/threading/thread_task_runner_handle.h" 12 #include "base/threading/thread_task_runner_handle.h"
13 #include "base/tracked_objects.h" 13 #include "base/tracked_objects.h"
14 14
15 namespace remoting { 15 namespace remoting {
16 16
17 namespace { 17 namespace {
18 // Log message handler registration and deregistration is not thread-safe. 18 // Log message listener registration and deregistration is not thread-safe.
19 // This lock must be held in order to set or restore a log message handler, 19 // This lock must be held in order to set or restore a log message listener,
20 // and when accessing the pointer to the LogMessageHandler instance. 20 // and when accessing the pointer to the LogMessageListener instance.
21 base::LazyInstance<base::Lock>::Leaky g_log_message_handler_lock = 21 base::LazyInstance<base::Lock>::Leaky g_log_message_listener_lock =
22 LAZY_INSTANCE_INITIALIZER; 22 LAZY_INSTANCE_INITIALIZER;
23 23
24 // The singleton LogMessageHandler instance, or null if log messages are not 24 // The singleton LogMessageListener instance, or null if log messages are not
25 // being intercepted. It must be dereferenced and updated under the above lock. 25 // being intercepted. It must be dereferenced and updated under the above lock.
26 LogMessageHandler* g_log_message_handler = nullptr; 26 LogMessageListener* g_log_message_listener = nullptr;
27 } // namespace 27 } // namespace
28 28
29 LogMessageHandler::LogMessageHandler( 29 LogMessageListener::LogMessageListener(const Delegate& delegate)
30 const Delegate& delegate)
31 : delegate_(delegate), 30 : delegate_(delegate),
32 suppress_logging_(false), 31 suppress_logging_(false),
33 caller_task_runner_(base::ThreadTaskRunnerHandle::Get()), 32 caller_task_runner_(base::ThreadTaskRunnerHandle::Get()),
34 weak_ptr_factory_(this) { 33 weak_ptr_factory_(this) {
35 base::AutoLock lock(g_log_message_handler_lock.Get()); 34 base::AutoLock lock(g_log_message_listener_lock.Get());
36 if (g_log_message_handler) { 35 if (g_log_message_listener) {
37 LOG(FATAL) << "LogMessageHandler is already registered. Only one instance " 36 LOG(FATAL) << "LogMessageListener is already registered. Only one instance "
38 << "per process is allowed."; 37 << "per process is allowed.";
39 } 38 }
40 previous_log_message_handler_ = logging::GetLogMessageHandler(); 39 g_log_message_listener = this;
41 logging::SetLogMessageHandler(&LogMessageHandler::OnLogMessage);
42 g_log_message_handler = this;
43 } 40 }
44 41
45 LogMessageHandler::~LogMessageHandler() { 42 LogMessageListener::~LogMessageListener() {
46 base::AutoLock lock(g_log_message_handler_lock.Get()); 43 base::AutoLock lock(g_log_message_listener_lock.Get());
47 if (logging::GetLogMessageHandler() != &LogMessageHandler::OnLogMessage) { 44 g_log_message_listener = nullptr;
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;
53 } 45 }
54 46
55 // static 47 // static
56 const char* LogMessageHandler::kDebugMessageTypeName = "_debug_log"; 48 const char* LogMessageListener::kDebugMessageTypeName = "_debug_log";
57 49
58 // static 50 void LogMessageListener::OnMessage(logging::LogSeverity severity,
59 bool LogMessageHandler::OnLogMessage( 51 const char* file,
52 int line,
53 size_t message_start,
54 const std::string& str) {
55 base::AutoLock lock(g_log_message_listener_lock.Get());
56 if (g_log_message_listener) {
57 g_log_message_listener->PostLogMessageToCorrectThread(severity, file, line,
58 message_start, str);
59 }
60 }
61
62 void LogMessageListener::PostLogMessageToCorrectThread(
60 logging::LogSeverity severity, 63 logging::LogSeverity severity,
61 const char* file, 64 const char* file,
62 int line, 65 int line,
63 size_t message_start,
64 const std::string& str) {
65 base::AutoLock lock(g_log_message_handler_lock.Get());
66 if (g_log_message_handler) {
67 g_log_message_handler->PostLogMessageToCorrectThread(
68 severity, file, line, message_start, str);
69 }
70 return false;
71 }
72
73 void LogMessageHandler::PostLogMessageToCorrectThread(
74 logging::LogSeverity severity,
75 const char* file,
76 int line,
77 size_t message_start, 66 size_t message_start,
78 const std::string& str) { 67 const std::string& str) {
79 // Don't process this message if we're already logging and on the caller 68 // 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 69 // thread. This guards against an infinite loop if any code called by this
81 // class logs something. 70 // class logs something.
82 if (suppress_logging_ && caller_task_runner_->BelongsToCurrentThread()) { 71 if (suppress_logging_ && caller_task_runner_->BelongsToCurrentThread()) {
83 return; 72 return;
84 } 73 }
85 74
86 // This method is always called under the global lock, so post a task to 75 // 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. 76 // handle the log message, even if we're already on the correct thread.
88 // This alows the lock to be released quickly. 77 // This alows the lock to be released quickly.
89 // 78 //
90 // Note that this means that LOG(FATAL) messages will be lost because the 79 // 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. 80 // process will exit before the message is sent to the client.
92 caller_task_runner_->PostTask( 81 caller_task_runner_->PostTask(
93 FROM_HERE, 82 FROM_HERE, base::Bind(&LogMessageListener::SendLogMessageToClient,
94 base::Bind(&LogMessageHandler::SendLogMessageToClient, 83 weak_ptr_factory_.GetWeakPtr(), severity, file,
95 weak_ptr_factory_.GetWeakPtr(), 84 line, message_start, str));
96 severity, file, line, message_start, str));
97 } 85 }
98 86
99 void LogMessageHandler::SendLogMessageToClient( 87 void LogMessageListener::SendLogMessageToClient(logging::LogSeverity severity,
100 logging::LogSeverity severity, 88 const char* file,
101 const char* file, 89 int line,
102 int line, 90 size_t message_start,
103 size_t message_start, 91 const std::string& str) {
104 const std::string& str) {
105 suppress_logging_ = true; 92 suppress_logging_ = true;
106 93
107 std::string severity_string = "log"; 94 std::string severity_string = "log";
108 switch (severity) { 95 switch (severity) {
109 case logging::LOG_WARNING: 96 case logging::LOG_WARNING:
110 severity_string = "warn"; 97 severity_string = "warn";
111 break; 98 break;
112 case logging::LOG_FATAL: 99 case logging::LOG_FATAL:
113 case logging::LOG_ERROR: 100 case logging::LOG_ERROR:
114 severity_string = "error"; 101 severity_string = "error";
115 break; 102 break;
116 } 103 }
117 104
118 std::string message = str.substr(message_start); 105 std::string message = str.substr(message_start);
119 base::TrimWhitespaceASCII(message, base::TRIM_ALL, &message); 106 base::TrimWhitespaceASCII(message, base::TRIM_ALL, &message);
120 107
121 std::unique_ptr<base::DictionaryValue> dictionary(new base::DictionaryValue); 108 std::unique_ptr<base::DictionaryValue> dictionary(new base::DictionaryValue);
122 dictionary->SetString("type", kDebugMessageTypeName); 109 dictionary->SetString("type", kDebugMessageTypeName);
123 dictionary->SetString("severity", severity_string); 110 dictionary->SetString("severity", severity_string);
124 dictionary->SetString("message", message); 111 dictionary->SetString("message", message);
125 dictionary->SetString("file", file); 112 dictionary->SetString("file", file);
126 dictionary->SetInteger("line", line); 113 dictionary->SetInteger("line", line);
127 114
128 delegate_.Run(std::move(dictionary)); 115 delegate_.Run(std::move(dictionary));
129 116
130 suppress_logging_ = false; 117 suppress_logging_ = false;
131 } 118 }
132 119
133 } // namespace remoting 120 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/host/native_messaging/log_message_handler.h ('k') | remoting/host/policy_watcher_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698