This source file includes following definitions.
- RegisterLogMessageHandler
- RegisterLoggingScriptObject
- UnregisterLoggingScriptObject
- LogToUI
#include "remoting/host/plugin/host_log_handler.h"
#include "base/lazy_instance.h"
#include "remoting/base/logging.h"
#include "remoting/base/util.h"
#include "remoting/host/plugin/host_script_object.h"
namespace remoting {
static bool g_has_logging_scriptable_object = false;
static base::LazyInstance<base::Lock>::Leaky
g_logging_lock = LAZY_INSTANCE_INITIALIZER;
static HostNPScriptObject* g_logging_scriptable_object = NULL;
static logging::LogMessageHandlerFunction g_logging_old_handler = NULL;
static bool g_has_registered_log_handler = false;
void HostLogHandler::RegisterLogMessageHandler() {
base::AutoLock lock(g_logging_lock.Get());
if (g_has_registered_log_handler)
return;
HOST_LOG << "Registering global log handler";
g_logging_old_handler = logging::GetLogMessageHandler();
logging::SetLogMessageHandler(&LogToUI);
g_has_registered_log_handler = true;
}
void HostLogHandler::RegisterLoggingScriptObject(
HostNPScriptObject* script_object) {
base::AutoLock lock(g_logging_lock.Get());
VLOG(1) << "Registering log handler scriptable object";
g_logging_scriptable_object = script_object;
g_has_logging_scriptable_object = true;
}
void HostLogHandler::UnregisterLoggingScriptObject(
HostNPScriptObject* script_object) {
base::AutoLock lock(g_logging_lock.Get());
if (script_object != g_logging_scriptable_object)
return;
g_has_logging_scriptable_object = false;
g_logging_scriptable_object = NULL;
VLOG(1) << "Unregistering log handler scriptable object";
}
bool HostLogHandler::LogToUI(int severity, const char* file, int line,
size_t message_start,
const std::string& str) {
if (g_has_logging_scriptable_object) {
base::AutoLock lock(g_logging_lock.Get());
if (g_logging_scriptable_object) {
std::string message = remoting::GetTimestampString();
message += (str.c_str() + message_start);
g_logging_scriptable_object->PostLogDebugInfo(message);
}
}
if (g_logging_old_handler)
return (g_logging_old_handler)(severity, file, line, message_start, str);
return false;
}
}