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

Side by Side Diff: remoting/host/plugin/host_log_handler.cc

Issue 7648042: Change Chromoting logger to be setup in plugin's NP_Initialize. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: move client log changes into separate cl Created 9 years, 3 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 | Annotate | Revision Log
OLDNEW
(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.
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::RegisterLoggingScriptObject(
45 HostNPScriptObject* script_object) {
46 base::AutoLock lock(g_logging_lock);
47
48 LOG(INFO) << "Registering log handler scriptable object";
49
50 // Register this script object as the one that will handle all logging calls
51 // and display them to the user.
52 // If multiple plugins are run, then the last one registered will handle all
53 // logging for all instances.
54 g_logging_scriptable_object = script_object;
55 g_has_logging_scriptable_object = true;
56 }
57
58 // static
59 void HostLogHandler::UnregisterLoggingScriptObject(
60 HostNPScriptObject* script_object) {
61 base::AutoLock lock(g_logging_lock);
62
63 // Ignore unless we're the currently registered script object.
64 if (script_object != g_logging_scriptable_object)
65 return;
66
67 // Unregister this script object for logging.
68 g_has_logging_scriptable_object = false;
69 g_logging_scriptable_object = NULL;
70
71 LOG(INFO) << "Unregistering log handler scriptable object";
72 }
73
74 // static
75 bool HostLogHandler::LogToUI(int severity, const char* file, int line,
76 size_t message_start,
77 const std::string& str) {
78 // Note: We're reading |g_has_logging_scriptable_object| outside of a lock.
79 // This lockless read is done so that we don't needlessly slow down global
80 // logging with a lock for each log message.
81 //
82 // This lockless read is safe because:
83 //
84 // Misreading a false value (when it should be true) means that we'll simply
85 // skip processing a few log messages.
86 //
87 // Misreading a true value (when it should be false) means that we'll take
88 // the lock and check |g_logging_scriptable_object| unnecessarily. This is not
89 // problematic because we always set |g_logging_scriptable_object| inside a
90 // lock.
91 //
92 // Misreading an old cached value is also not problematic for the same
93 // reasons: a mis-read either skips a log message or causes us to take a lock
94 // unnecessarily.
95 if (g_has_logging_scriptable_object) {
96 base::AutoLock lock(g_logging_lock);
97
98 if (g_logging_scriptable_object) {
99 std::string message = remoting::GetTimestampString();
100 message += (str.c_str() + message_start);
101 g_logging_scriptable_object->PostLogDebugInfo(message);
102 }
103 }
104
105 // Call the next log handler in the chain.
106 if (g_logging_old_handler)
107 return (g_logging_old_handler)(severity, file, line, message_start, str);
108
109 return false;
110 }
111
112 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698