Chromium Code Reviews| Index: mojo/services/log/cpp/lib/log_client.cc |
| diff --git a/mojo/services/log/cpp/lib/log_client.cc b/mojo/services/log/cpp/lib/log_client.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d79181906bcd0a34b269c24290e88613c76ba3ad |
| --- /dev/null |
| +++ b/mojo/services/log/cpp/lib/log_client.cc |
| @@ -0,0 +1,167 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "mojo/services/log/cpp/log_client.h" |
| + |
| +#include <assert.h> |
| + |
| +#include <atomic> |
| +#include <string> |
| +#include <utility> |
| + |
| +#include "mojo/public/c/environment/logger.h" |
| +#include "mojo/public/cpp/bindings/lib/message_builder.h" |
| +#include "mojo/public/cpp/system/message_pipe.h" |
| +#include "mojo/services/log/interfaces/entry.mojom.h" |
| +#include "mojo/services/log/interfaces/log.mojom.h" |
| + |
| +namespace mojo { |
| +namespace { |
| + |
| +// Forward declare for constructing |g_logclient_logger|. |
| +void LogMessage(MojoLogLevel log_level, |
| + const char* source_file, |
| + uint32_t source_line, |
| + const char* message); |
| +MojoLogLevel GetMinimumLogLevel(); |
| +void SetMinimumLogLevel(MojoLogLevel level); |
| + |
| +// This interface info represents the |mojo::log::Log| service (see log.mojom). |
| +// This doesn't need to be synchronized, since we only allow one LogClient to be |
| +// instantiated, so it is only initialized once, and from one thread. |
| +InterfacePtrInfo<mojo::log::Log>* g_log_interface = nullptr; |
| + |
| +// The minimum logging level. |
| +std::atomic<MojoLogLevel> g_min_log_level; |
|
viettrungluu
2015/12/03 19:13:25
Hmmm. This may lead to a static initializer, proba
vardhan
2015/12/15 01:55:06
Done. I have a global |LogClient* g_log_client| th
|
| + |
| +MojoLogger g_logclient_logger = {&LogMessage, &GetMinimumLogLevel, |
|
viettrungluu
2015/12/03 19:13:25
const
vardhan
2015/12/15 01:55:06
Done.
|
| + &SetMinimumLogLevel}; |
| + |
| +// This fallback logger is also thread-safe. |
| +const MojoLogger* g_fallback_logger = nullptr; |
| + |
| +void LogMessage(MojoLogLevel log_level, |
| + const char* source_file, |
| + uint32_t source_line, |
| + const char* message) { |
| + // We avoid the use of C++ bindings to do interface calls in order to be |
| + // thread-safe (as of this writing, the bindings are not). Because the |
| + // AddEntry message of the Log interface does not have a response message, we |
| + // can fire-and-forget the message: construct the params for the call, framing |
| + // it inside a Message and writing the Message to the message pipe connecting |
| + // to the log service. |
| + assert(g_log_interface); |
| + if (!g_log_interface->is_valid()) { |
| + assert(g_fallback_logger); |
| + return g_fallback_logger->LogMessage(log_level, source_file, source_line, |
| + message); |
| + } |
| + |
| + if (log_level < g_min_log_level.load(std::memory_order_relaxed)) |
| + return; |
| + |
| + assert(g_log_interface->is_valid()); |
| + |
| + mojo::log::Log_AddEntry_Params request_params; |
| + request_params.entry = mojo::log::Entry::New(); |
| + request_params.entry->timestamp = GetTimeTicksNow(); |
| + request_params.entry->log_level = log_level; |
| + request_params.entry->source_file = source_file; |
| + request_params.entry->source_line = source_line; |
| + request_params.entry->message = message; |
| + |
| + size_t params_size = request_params.GetSerializedSize(); |
| + MessageBuilder builder( |
| + static_cast<uint32_t>(mojo::log::Log::MessageOrdinals::AddEntry), |
| + params_size); |
| + |
| + request_params.Serialize( |
| + static_cast<void*>(builder.message()->mutable_payload()), params_size); |
| + |
| + auto retval = WriteMessageRaw(g_log_interface->handle().get(), |
| + builder.message()->data(), |
| + builder.message()->data_num_bytes(), nullptr, 0, |
| + MOJO_WRITE_MESSAGE_FLAG_NONE); |
| + switch (retval) { |
| + case MOJO_RESULT_OK: |
| + break; |
| + |
| + // TODO(vardhan): Are any of these error cases recoverable (in which case |
| + // we shouldn't close our handle)? Maybe MOJO_RESULT_RESOURCE_EXHAUSTED? |
| + case MOJO_RESULT_INVALID_ARGUMENT: |
| + case MOJO_RESULT_RESOURCE_EXHAUSTED: |
| + case MOJO_RESULT_FAILED_PRECONDITION: |
| + case MOJO_RESULT_UNIMPLEMENTED: |
| + case MOJO_RESULT_BUSY: { |
| + g_log_interface->PassHandle(); |
| + return g_fallback_logger->LogMessage(log_level, source_file, source_line, |
| + message); |
| + } |
| + |
| + default: |
| + // Should not reach here. |
| + assert(false); |
| + } |
| + |
| + if (log_level >= MOJO_LOG_LEVEL_FATAL) |
| + abort(); |
| +} |
| + |
| +MojoLogLevel GetMinimumLogLevel() { |
| + assert(g_log_interface); |
| + |
| + if (!g_log_interface->is_valid()) { |
| + assert(g_fallback_logger); |
| + return g_fallback_logger->GetMinimumLogLevel(); |
| + } |
| + return g_min_log_level.load(std::memory_order_relaxed); |
| +} |
| + |
| +// TODO(vardhan): Should we keep the our log level consistent with the fallback |
| +// logger's? If we do, and we also require the fallback logger be thread-safe, |
| +// then we could get rid of |g_min_log_level| altogether and use the fallback |
| +// logger's (Get|Set)MinimumLogLevel functions. |
| +void SetMinimumLogLevel(MojoLogLevel level) { |
| + assert(g_log_interface); |
| + |
| + if (!g_log_interface->is_valid()) { |
| + assert(g_fallback_logger); |
| + g_fallback_logger->SetMinimumLogLevel(level); |
| + return; |
| + } |
| + |
| + g_min_log_level.store(std::min(level, MOJO_LOG_LEVEL_FATAL), |
| + std::memory_order_relaxed); |
| +} |
| + |
| +} // namespace |
| + |
| +namespace log { |
| + |
| +LogClient::LogClient(LogPtr log, const MojoLogger* fallback_logger) { |
| + assert(!g_log_interface); |
| + assert(log.is_bound()); |
| + assert(fallback_logger); |
| + |
| + g_min_log_level.store(MOJO_LOG_LEVEL_INFO, std::memory_order_relaxed); |
| + g_log_interface = new InterfacePtrInfo<Log>(log.PassInterface()); |
| + |
| + g_fallback_logger = fallback_logger; |
| +} |
| + |
| +LogClient::~LogClient() { |
| + if (g_log_interface) { |
| + g_log_interface->PassHandle(); |
| + delete g_log_interface; |
| + g_log_interface = nullptr; |
| + } |
| +} |
| + |
| +// static |
| +const MojoLogger* LogClient::GetLogger() { |
| + return &g_logclient_logger; |
| +} |
| + |
| +} // namespace log |
| +} // namespace mojo |