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 return; |
| 37 |
| 38 LOG(INFO) << "Registering global log handler"; |
| 39 |
| 40 // Record previous handler so we can call it in a chain. |
| 41 g_logging_old_handler = logging::GetLogMessageHandler(); |
| 42 |
| 43 // Set up log message handler. |
| 44 // This is not thread-safe so we need it within our lock. |
| 45 // Note that this will not log anything until a scriptable object instance |
| 46 // has been created to handle the log message display. |
| 47 logging::SetLogMessageHandler(&LogToUI); |
| 48 } |
| 49 |
| 50 // static |
| 51 void HostLogHandler::RegisterLoggingScriptObject( |
| 52 HostNPScriptObject* script_object) { |
| 53 base::AutoLock lock(g_logging_lock); |
| 54 |
| 55 LOG(INFO) << "Registering log handler scriptable object"; |
| 56 |
| 57 // Register this script object as the one that will handle all logging calls |
| 58 // and display them to the user. |
| 59 // If multiple plugins are run, then the last one registered will handle all |
| 60 // logging for all instances. |
| 61 g_logging_scriptable_object = script_object; |
| 62 g_has_logging_scriptable_object = true; |
| 63 } |
| 64 |
| 65 // static |
| 66 void HostLogHandler::UnregisterLoggingScriptObject( |
| 67 HostNPScriptObject* script_object) { |
| 68 base::AutoLock lock(g_logging_lock); |
| 69 |
| 70 // Ignore unless we're the currently registered script object. |
| 71 if (script_object != g_logging_scriptable_object) |
| 72 return; |
| 73 |
| 74 // Unregister this script object for logging. |
| 75 g_has_logging_scriptable_object = false; |
| 76 g_logging_scriptable_object = NULL; |
| 77 |
| 78 LOG(INFO) << "Unregistering log handler scriptable object"; |
| 79 } |
| 80 |
| 81 // static |
| 82 bool HostLogHandler::LogToUI(int severity, const char* file, int line, |
| 83 size_t message_start, |
| 84 const std::string& str) { |
| 85 // Note: We're reading |g_has_logging_scriptable_object| outside of a lock. |
| 86 // This lockless read is done so that we don't needlessly slow down global |
| 87 // logging with a lock for each log message. |
| 88 // |
| 89 // This lockless read is safe because: |
| 90 // |
| 91 // Misreading a false value (when it should be true) means that we'll simply |
| 92 // skip processing a few log messages. |
| 93 // |
| 94 // Misreading a true value (when it should be false) means that we'll take |
| 95 // the lock and check |g_logging_scriptable_object| unnecessarily. This is not |
| 96 // problematic because we always set |g_logging_scriptable_object| inside a |
| 97 // lock. |
| 98 // |
| 99 // Misreading an old cached value is also not problematic for the same |
| 100 // reasons: a mis-read either skips a log message or causes us to take a lock |
| 101 // unnecessarily. |
| 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 |