Home | History | Annotate | Download | only in plugin
      1 // Copyright (c) 2012 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 "base/lazy_instance.h"
      8 #include "remoting/base/util.h"
      9 #include "remoting/host/plugin/host_script_object.h"
     10 
     11 namespace remoting {
     12 
     13 // Records whether or not we have a scriptable object registered for logging.
     14 // This is set inside the lock, but is read (in LogToUI) outside of a lock so
     15 // that we don't needlessly slow down the system when we log.
     16 static bool g_has_logging_scriptable_object = false;
     17 
     18 // The lock that protects the logging globals.
     19 static base::LazyInstance<base::Lock>::Leaky
     20     g_logging_lock = LAZY_INSTANCE_INITIALIZER;
     21 
     22 // The scriptable object that will display the log information to the user.
     23 static HostNPScriptObject* g_logging_scriptable_object = NULL;
     24 
     25 // The previously registered LogMessageHandler. If not NULL, we call this after
     26 // we're doing processing the log message.
     27 static logging::LogMessageHandlerFunction g_logging_old_handler = NULL;
     28 
     29 // Set to true when we register our global log handler so that we don't try
     30 // to register it twice.
     31 static bool g_has_registered_log_handler = false;
     32 
     33 // static
     34 void HostLogHandler::RegisterLogMessageHandler() {
     35   base::AutoLock lock(g_logging_lock.Get());
     36 
     37   if (g_has_registered_log_handler)
     38     return;
     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   g_has_registered_log_handler = true;
     51 }
     52 
     53 // static
     54 void HostLogHandler::RegisterLoggingScriptObject(
     55     HostNPScriptObject* script_object) {
     56   base::AutoLock lock(g_logging_lock.Get());
     57 
     58   VLOG(1) << "Registering log handler scriptable object";
     59 
     60   // Register this script object as the one that will handle all logging calls
     61   // and display them to the user.
     62   // If multiple plugins are run, then the last one registered will handle all
     63   // logging for all instances.
     64   g_logging_scriptable_object = script_object;
     65   g_has_logging_scriptable_object = true;
     66 }
     67 
     68 // static
     69 void HostLogHandler::UnregisterLoggingScriptObject(
     70     HostNPScriptObject* script_object) {
     71   base::AutoLock lock(g_logging_lock.Get());
     72 
     73   // Ignore unless we're the currently registered script object.
     74   if (script_object != g_logging_scriptable_object)
     75     return;
     76 
     77   // Unregister this script object for logging.
     78   g_has_logging_scriptable_object = false;
     79   g_logging_scriptable_object = NULL;
     80 
     81   VLOG(1) << "Unregistering log handler scriptable object";
     82 }
     83 
     84 // static
     85 bool HostLogHandler::LogToUI(int severity, const char* file, int line,
     86                              size_t message_start,
     87                              const std::string& str) {
     88   // Note: We're reading |g_has_logging_scriptable_object| outside of a lock.
     89   // This lockless read is done so that we don't needlessly slow down global
     90   // logging with a lock for each log message.
     91   //
     92   // This lockless read is safe because:
     93   //
     94   // Misreading a false value (when it should be true) means that we'll simply
     95   // skip processing a few log messages.
     96   //
     97   // Misreading a true value (when it should be false) means that we'll take
     98   // the lock and check |g_logging_scriptable_object| unnecessarily. This is not
     99   // problematic because we always set |g_logging_scriptable_object| inside a
    100   // lock.
    101   //
    102   // Misreading an old cached value is also not problematic for the same
    103   // reasons: a mis-read either skips a log message or causes us to take a lock
    104   // unnecessarily.
    105   if (g_has_logging_scriptable_object) {
    106     base::AutoLock lock(g_logging_lock.Get());
    107 
    108     if (g_logging_scriptable_object) {
    109       std::string message = remoting::GetTimestampString();
    110       message += (str.c_str() + message_start);
    111       g_logging_scriptable_object->PostLogDebugInfo(message);
    112     }
    113   }
    114 
    115   // Call the next log handler in the chain.
    116   if (g_logging_old_handler)
    117     return (g_logging_old_handler)(severity, file, line, message_start, str);
    118 
    119   return false;
    120 }
    121 
    122 }  // namespace remoting
    123