Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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 "mojo/services/log/cpp/log_client.h" | |
| 6 | |
| 7 #include <assert.h> | |
| 8 | |
| 9 #include <atomic> | |
| 10 #include <string> | |
| 11 #include <utility> | |
| 12 | |
| 13 #include "mojo/public/c/environment/logger.h" | |
| 14 #include "mojo/public/cpp/bindings/lib/message_builder.h" | |
| 15 #include "mojo/public/cpp/system/message_pipe.h" | |
| 16 #include "mojo/services/log/interfaces/entry.mojom.h" | |
| 17 #include "mojo/services/log/interfaces/log.mojom.h" | |
| 18 | |
| 19 namespace mojo { | |
| 20 namespace { | |
| 21 | |
| 22 // Forward declare for constructing |g_logclient_logger|. | |
| 23 void LogMessage(MojoLogLevel log_level, | |
| 24 const char* source_file, | |
| 25 uint32_t source_line, | |
| 26 const char* message); | |
| 27 MojoLogLevel GetMinimumLogLevel(); | |
| 28 void SetMinimumLogLevel(MojoLogLevel level); | |
| 29 | |
| 30 // This interface info represents the |mojo::log::Log| service (see log.mojom). | |
| 31 // This doesn't need to be synchronized, since we only allow one LogClient to be | |
| 32 // instantiated, so it is only initialized once, and from one thread. | |
| 33 InterfacePtrInfo<mojo::log::Log>* g_log_interface = nullptr; | |
| 34 | |
| 35 // The minimum logging level. | |
| 36 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
| |
| 37 | |
| 38 MojoLogger g_logclient_logger = {&LogMessage, &GetMinimumLogLevel, | |
|
viettrungluu
2015/12/03 19:13:25
const
vardhan
2015/12/15 01:55:06
Done.
| |
| 39 &SetMinimumLogLevel}; | |
| 40 | |
| 41 // This fallback logger is also thread-safe. | |
| 42 const MojoLogger* g_fallback_logger = nullptr; | |
| 43 | |
| 44 void LogMessage(MojoLogLevel log_level, | |
| 45 const char* source_file, | |
| 46 uint32_t source_line, | |
| 47 const char* message) { | |
| 48 // We avoid the use of C++ bindings to do interface calls in order to be | |
| 49 // thread-safe (as of this writing, the bindings are not). Because the | |
| 50 // AddEntry message of the Log interface does not have a response message, we | |
| 51 // can fire-and-forget the message: construct the params for the call, framing | |
| 52 // it inside a Message and writing the Message to the message pipe connecting | |
| 53 // to the log service. | |
| 54 assert(g_log_interface); | |
| 55 if (!g_log_interface->is_valid()) { | |
| 56 assert(g_fallback_logger); | |
| 57 return g_fallback_logger->LogMessage(log_level, source_file, source_line, | |
| 58 message); | |
| 59 } | |
| 60 | |
| 61 if (log_level < g_min_log_level.load(std::memory_order_relaxed)) | |
| 62 return; | |
| 63 | |
| 64 assert(g_log_interface->is_valid()); | |
| 65 | |
| 66 mojo::log::Log_AddEntry_Params request_params; | |
| 67 request_params.entry = mojo::log::Entry::New(); | |
| 68 request_params.entry->timestamp = GetTimeTicksNow(); | |
| 69 request_params.entry->log_level = log_level; | |
| 70 request_params.entry->source_file = source_file; | |
| 71 request_params.entry->source_line = source_line; | |
| 72 request_params.entry->message = message; | |
| 73 | |
| 74 size_t params_size = request_params.GetSerializedSize(); | |
| 75 MessageBuilder builder( | |
| 76 static_cast<uint32_t>(mojo::log::Log::MessageOrdinals::AddEntry), | |
| 77 params_size); | |
| 78 | |
| 79 request_params.Serialize( | |
| 80 static_cast<void*>(builder.message()->mutable_payload()), params_size); | |
| 81 | |
| 82 auto retval = WriteMessageRaw(g_log_interface->handle().get(), | |
| 83 builder.message()->data(), | |
| 84 builder.message()->data_num_bytes(), nullptr, 0, | |
| 85 MOJO_WRITE_MESSAGE_FLAG_NONE); | |
| 86 switch (retval) { | |
| 87 case MOJO_RESULT_OK: | |
| 88 break; | |
| 89 | |
| 90 // TODO(vardhan): Are any of these error cases recoverable (in which case | |
| 91 // we shouldn't close our handle)? Maybe MOJO_RESULT_RESOURCE_EXHAUSTED? | |
| 92 case MOJO_RESULT_INVALID_ARGUMENT: | |
| 93 case MOJO_RESULT_RESOURCE_EXHAUSTED: | |
| 94 case MOJO_RESULT_FAILED_PRECONDITION: | |
| 95 case MOJO_RESULT_UNIMPLEMENTED: | |
| 96 case MOJO_RESULT_BUSY: { | |
| 97 g_log_interface->PassHandle(); | |
| 98 return g_fallback_logger->LogMessage(log_level, source_file, source_line, | |
| 99 message); | |
| 100 } | |
| 101 | |
| 102 default: | |
| 103 // Should not reach here. | |
| 104 assert(false); | |
| 105 } | |
| 106 | |
| 107 if (log_level >= MOJO_LOG_LEVEL_FATAL) | |
| 108 abort(); | |
| 109 } | |
| 110 | |
| 111 MojoLogLevel GetMinimumLogLevel() { | |
| 112 assert(g_log_interface); | |
| 113 | |
| 114 if (!g_log_interface->is_valid()) { | |
| 115 assert(g_fallback_logger); | |
| 116 return g_fallback_logger->GetMinimumLogLevel(); | |
| 117 } | |
| 118 return g_min_log_level.load(std::memory_order_relaxed); | |
| 119 } | |
| 120 | |
| 121 // TODO(vardhan): Should we keep the our log level consistent with the fallback | |
| 122 // logger's? If we do, and we also require the fallback logger be thread-safe, | |
| 123 // then we could get rid of |g_min_log_level| altogether and use the fallback | |
| 124 // logger's (Get|Set)MinimumLogLevel functions. | |
| 125 void SetMinimumLogLevel(MojoLogLevel level) { | |
| 126 assert(g_log_interface); | |
| 127 | |
| 128 if (!g_log_interface->is_valid()) { | |
| 129 assert(g_fallback_logger); | |
| 130 g_fallback_logger->SetMinimumLogLevel(level); | |
| 131 return; | |
| 132 } | |
| 133 | |
| 134 g_min_log_level.store(std::min(level, MOJO_LOG_LEVEL_FATAL), | |
| 135 std::memory_order_relaxed); | |
| 136 } | |
| 137 | |
| 138 } // namespace | |
| 139 | |
| 140 namespace log { | |
| 141 | |
| 142 LogClient::LogClient(LogPtr log, const MojoLogger* fallback_logger) { | |
| 143 assert(!g_log_interface); | |
| 144 assert(log.is_bound()); | |
| 145 assert(fallback_logger); | |
| 146 | |
| 147 g_min_log_level.store(MOJO_LOG_LEVEL_INFO, std::memory_order_relaxed); | |
| 148 g_log_interface = new InterfacePtrInfo<Log>(log.PassInterface()); | |
| 149 | |
| 150 g_fallback_logger = fallback_logger; | |
| 151 } | |
| 152 | |
| 153 LogClient::~LogClient() { | |
| 154 if (g_log_interface) { | |
| 155 g_log_interface->PassHandle(); | |
| 156 delete g_log_interface; | |
| 157 g_log_interface = nullptr; | |
| 158 } | |
| 159 } | |
| 160 | |
| 161 // static | |
| 162 const MojoLogger* LogClient::GetLogger() { | |
| 163 return &g_logclient_logger; | |
| 164 } | |
| 165 | |
| 166 } // namespace log | |
| 167 } // namespace mojo | |
| OLD | NEW |