OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "remoting/host/plugin/host_log_handler.h" | |
6 | |
7 #include "remoting/base/util.h" | |
8 #include "remoting/host/plugin/host_script_object.h" | |
9 | |
10 namespace remoting { | |
11 | |
12 // Records whether or not we have a scriptable object registered for logging. | |
13 // This is set inside the lock, but is read (in LogToUI) outside of a lock so | |
14 // that we don't needlessly slow down the system when we log. | |
15 static bool g_has_logging_scriptable_object = false; | |
16 | |
17 // The lock that protects the logging globals. | |
Wez
2011/08/30 05:20:02
nit: Only protects the scriptable object global?
garykac
2011/08/31 00:59:00
It also protects writing to (but not reading from)
| |
18 static base::Lock g_logging_lock; | |
19 | |
20 // The scriptable object that will display the log information to the user. | |
21 static HostNPScriptObject* g_logging_scriptable_object = NULL; | |
22 | |
23 // The previously registered LogMessageHandler. If not NULL, we call this after | |
24 // we're doing processing the log message. | |
25 static logging::LogMessageHandlerFunction g_logging_old_handler = NULL; | |
26 | |
27 // static | |
28 void HostLogHandler::RegisterLogMessageHandler() { | |
29 base::AutoLock lock(g_logging_lock); | |
30 | |
31 LOG(INFO) << "Registering global log handler"; | |
32 | |
33 // Record previous handler so we can call it in a chain. | |
34 g_logging_old_handler = logging::GetLogMessageHandler(); | |
35 | |
36 // Set up log message handler. | |
37 // This is not thread-safe so we need it within our lock. | |
38 // Note that this will not log anything until a scriptable object instance | |
39 // has been created to handle the log message display. | |
40 logging::SetLogMessageHandler(&LogToUI); | |
41 } | |
42 | |
43 // static | |
44 void HostLogHandler::UnregisterLogMessageHandler() { | |
45 base::AutoLock lock(g_logging_lock); | |
46 | |
47 // Restore previous handler. | |
48 logging::SetLogMessageHandler(g_logging_old_handler); | |
49 g_logging_old_handler = NULL; | |
50 | |
51 LOG(INFO) << "Unregistering global log handler"; | |
52 } | |
53 | |
54 // static | |
55 void HostLogHandler::RegisterLoggingScriptObject( | |
56 HostNPScriptObject* script_object) { | |
57 base::AutoLock lock(g_logging_lock); | |
58 | |
59 LOG(INFO) << "Registering log handler scriptable object"; | |
60 | |
61 // Register this script object as the one that will handle all logging calls | |
62 // and display them to the user. | |
63 // If multiple plugins are run, then the last one registered will handle all | |
64 // logging for all instances. | |
65 g_logging_scriptable_object = script_object; | |
66 g_has_logging_scriptable_object = true; | |
67 } | |
68 | |
69 // static | |
70 void HostLogHandler::UnregisterLoggingScriptObject( | |
71 HostNPScriptObject* script_object) { | |
72 base::AutoLock lock(g_logging_lock); | |
73 | |
74 // Ignore unless we're the currently registered script object. | |
75 if (script_object != g_logging_scriptable_object) | |
76 return; | |
77 | |
78 // Unregister this script object for logging. | |
79 g_has_logging_scriptable_object = false; | |
80 g_logging_scriptable_object = NULL; | |
81 | |
82 LOG(INFO) << "Unregistering log handler scriptable object"; | |
83 } | |
84 | |
85 // static | |
86 bool HostLogHandler::LogToUI(int severity, const char* file, int line, | |
87 size_t message_start, | |
88 const std::string& str) { | |
89 // Note: We're reading |g_has_logging_scriptable_object| outside of a lock. | |
90 // This lockless read is done so that we don't needlessly slow down global | |
91 // logging with a lock for each log message. | |
92 // | |
93 // This lockless read is safe because: | |
94 // | |
95 // Misreading a false value (when it should be true) means that we'll simply | |
96 // skip processing a few log messages. | |
97 // | |
98 // Misreading a true value (when it should be false) means that we'll take | |
99 // the lock and check |g_logging_scriptable_object| unnecessarily. This is not | |
100 // problematic because we always set |g_logging_scriptable_object| inside a | |
101 // lock. | |
Wez
2011/08/30 05:20:02
Also worth noting that we're not worried about the
garykac
2011/08/31 00:59:00
Done.
| |
102 if (g_has_logging_scriptable_object) { | |
103 base::AutoLock lock(g_logging_lock); | |
104 | |
105 if (g_logging_scriptable_object) { | |
106 std::string message = remoting::GetTimestampString(); | |
107 message += (str.c_str() + message_start); | |
108 g_logging_scriptable_object->PostLogDebugInfo(message); | |
109 } | |
110 } | |
111 | |
112 // Call the next log handler in the chain. | |
113 if (g_logging_old_handler) | |
114 return (g_logging_old_handler)(severity, file, line, message_start, str); | |
115 | |
116 return false; | |
117 } | |
118 | |
119 } // namespace remoting | |
OLD | NEW |