Chromium Code Reviews| 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. | |
| 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 // Set to true when we register our global log handler so that we don't try | |
| 28 // to register it twice. | |
| 29 static bool g_has_registered_log_handler = false; | |
| 30 | |
| 31 // static | |
| 32 void HostLogHandler::RegisterLogMessageHandler() { | |
| 33 base::AutoLock lock(g_logging_lock); | |
| 34 | |
| 35 if (!g_has_registered_log_handler) { | |
| 36 LOG(INFO) << "Already have global log handler"; | |
|
Wez
2011/09/01 01:21:23
It doesn't seem necessary to log something here, s
garykac
2011/09/01 01:30:55
Done.
| |
| 37 return; | |
| 38 } | |
| 39 | |
| 40 LOG(INFO) << "Registering global log handler"; | |
| 41 | |
| 42 // Record previous handler so we can call it in a chain. | |
| 43 g_logging_old_handler = logging::GetLogMessageHandler(); | |
| 44 | |
| 45 // Set up log message handler. | |
| 46 // This is not thread-safe so we need it within our lock. | |
| 47 // Note that this will not log anything until a scriptable object instance | |
| 48 // has been created to handle the log message display. | |
| 49 logging::SetLogMessageHandler(&LogToUI); | |
| 50 } | |
| 51 | |
| 52 // static | |
| 53 void HostLogHandler::RegisterLoggingScriptObject( | |
| 54 HostNPScriptObject* script_object) { | |
| 55 base::AutoLock lock(g_logging_lock); | |
| 56 | |
| 57 LOG(INFO) << "Registering log handler scriptable object"; | |
| 58 | |
| 59 // Register this script object as the one that will handle all logging calls | |
| 60 // and display them to the user. | |
| 61 // If multiple plugins are run, then the last one registered will handle all | |
| 62 // logging for all instances. | |
| 63 g_logging_scriptable_object = script_object; | |
| 64 g_has_logging_scriptable_object = true; | |
| 65 } | |
| 66 | |
| 67 // static | |
| 68 void HostLogHandler::UnregisterLoggingScriptObject( | |
| 69 HostNPScriptObject* script_object) { | |
| 70 base::AutoLock lock(g_logging_lock); | |
| 71 | |
| 72 // Ignore unless we're the currently registered script object. | |
| 73 if (script_object != g_logging_scriptable_object) | |
| 74 return; | |
| 75 | |
| 76 // Unregister this script object for logging. | |
| 77 g_has_logging_scriptable_object = false; | |
| 78 g_logging_scriptable_object = NULL; | |
| 79 | |
| 80 LOG(INFO) << "Unregistering log handler scriptable object"; | |
| 81 } | |
| 82 | |
| 83 // static | |
| 84 bool HostLogHandler::LogToUI(int severity, const char* file, int line, | |
| 85 size_t message_start, | |
| 86 const std::string& str) { | |
| 87 // Note: We're reading |g_has_logging_scriptable_object| outside of a lock. | |
| 88 // This lockless read is done so that we don't needlessly slow down global | |
| 89 // logging with a lock for each log message. | |
| 90 // | |
| 91 // This lockless read is safe because: | |
| 92 // | |
| 93 // Misreading a false value (when it should be true) means that we'll simply | |
| 94 // skip processing a few log messages. | |
| 95 // | |
| 96 // Misreading a true value (when it should be false) means that we'll take | |
| 97 // the lock and check |g_logging_scriptable_object| unnecessarily. This is not | |
| 98 // problematic because we always set |g_logging_scriptable_object| inside a | |
| 99 // lock. | |
| 100 // | |
| 101 // Misreading an old cached value is also not problematic for the same | |
| 102 // reasons: a mis-read either skips a log message or causes us to take a lock | |
| 103 // unnecessarily. | |
| 104 if (g_has_logging_scriptable_object) { | |
| 105 base::AutoLock lock(g_logging_lock); | |
| 106 | |
| 107 if (g_logging_scriptable_object) { | |
| 108 std::string message = remoting::GetTimestampString(); | |
| 109 message += (str.c_str() + message_start); | |
| 110 g_logging_scriptable_object->PostLogDebugInfo(message); | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 // Call the next log handler in the chain. | |
| 115 if (g_logging_old_handler) | |
| 116 return (g_logging_old_handler)(severity, file, line, message_start, str); | |
| 117 | |
| 118 return false; | |
| 119 } | |
| 120 | |
| 121 } // namespace remoting | |
| OLD | NEW |