Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(299)

Side by Side Diff: mojo/services/log/cpp/lib/log_client.cc

Issue 1447273002: Mojo Log service and a thread-safe client library. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: oops: restore vtl's TODO, fix LogClient::GetLogger() Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/services/log/cpp/BUILD.gn ('k') | mojo/services/log/cpp/log_client.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 <utility>
11
12 #include "mojo/public/c/environment/logger.h"
13 #include "mojo/public/cpp/bindings/lib/message_builder.h"
14 #include "mojo/public/cpp/system/message_pipe.h"
15 #include "mojo/services/log/interfaces/entry.mojom.h"
16 #include "mojo/services/log/interfaces/log.mojom.h"
17
18 namespace mojo {
19 namespace {
20
21 // Fwd declare for constructing |g_logclient_logger|.
22 void LogMessage(MojoLogLevel log_level,
23 const char* source_file,
24 uint32_t source_line,
25 const char* message);
26 MojoLogLevel GetMinimumLogLevel();
27 void SetMinimumLogLevel(MojoLogLevel level);
28
29 // This interface info represents the mojo::log::Log service (see log.mojom).
30 // This doesn't need to be synchronized, since we only allow one LogClient to be
31 // instantiated, so it is only initialized once, and from one thread.
32 InterfacePtrInfo<mojo::log::Log> g_log_interface;
jamesr 2015/11/17 00:34:17 this will introduce a static destructor, which we
vardhan 2015/12/02 00:06:13 crap. I will make this a pointer, and have the Lo
33
34 // The minimum logging level.
35 std::atomic<MojoLogLevel> g_min_log_level;
36
37 MojoLogger g_logclient_logger = {&LogMessage, &GetMinimumLogLevel,
38 &SetMinimumLogLevel};
39
40 const MojoLogger* g_fallback_logger = nullptr;
41
42 // We avoid the use of C++ bindings to do interface calls in order to be
43 // thread-safe (as of this writing, the bindings are not). Because the AddEntry
44 // method of the Log interface does not have a return type, we can easily do
45 // this by constructing the params for the call
46 // (mojo::log::Log_AddEntry_Params), framing it inside a Message using
47 // MessageBuilder, writing to the message pipe connecting to the log service.
48 void LogMessage(MojoLogLevel log_level,
49 const char* source_file,
50 uint32_t source_line,
51 const char* message) {
52 if (!g_log_interface.is_valid()) {
53 assert(g_fallback_logger);
54 return g_fallback_logger->LogMessage(log_level, source_file, source_line,
55 message);
56 }
57
58 if (log_level < g_min_log_level.load(std::memory_order_relaxed))
59 return;
60
61 assert(g_log_interface.is_valid());
62
63 mojo::log::Log_AddEntry_Params request_params;
64 request_params.entry = mojo::log::Entry::New();
65 request_params.entry->timestamp = GetTimeTicksNow();
66 request_params.entry->log_level = log_level;
67 request_params.entry->source_file = source_file;
68 request_params.entry->source_line = source_line;
69 request_params.entry->message = message;
70
71 size_t params_size = request_params.GetSerializedSize();
72 MessageBuilder builder(
73 static_cast<uint32_t>(mojo::log::Log::MessageOrdinals::AddEntry),
74 params_size);
75
76 assert(request_params.Serialize(
77 static_cast<void*>(builder.message()->mutable_payload()), params_size));
78
79 auto retval =
80 WriteMessageRaw(g_log_interface.handle().get(), builder.message()->data(),
81 builder.message()->data_num_bytes(), nullptr, 0,
82 MOJO_WRITE_MESSAGE_FLAG_NONE);
83 switch (retval) {
84 case MOJO_RESULT_OK:
85 break;
86
87 // This means that the other end of the pipe no longer exists, so it 's time
88 // to fall back.
89 case MOJO_RESULT_FAILED_PRECONDITION: {
90 g_log_interface.PassHandle();
91 return g_fallback_logger->LogMessage(log_level, source_file, source_line,
92 message);
93 }
94
95 default:
96 // TODO(vardhan): What other cases are there?
97 break;
98 }
99
100 if (log_level >= MOJO_LOG_LEVEL_FATAL)
101 abort();
102 }
103
104 MojoLogLevel GetMinimumLogLevel() {
105 if (!g_log_interface.is_valid()) {
106 assert(g_fallback_logger);
107 return g_fallback_logger->GetMinimumLogLevel();
108 }
109 return g_min_log_level.load(std::memory_order_relaxed);
110 }
111
112 void SetMinimumLogLevel(MojoLogLevel level) {
113 if (!g_log_interface.is_valid()) {
114 assert(g_fallback_logger);
115 g_fallback_logger->SetMinimumLogLevel(level);
116 }
117 g_min_log_level.store(std::min(level, MOJO_LOG_LEVEL_FATAL),
118 std::memory_order_relaxed);
119
120 // Keep the fallback logger's level consistent with ours.
121 g_fallback_logger->SetMinimumLogLevel(level);
122 }
123
124 } // namespace
125
126 namespace log {
127
128 LogClient::LogClient(LogPtr log, const MojoLogger* fallback_logger) {
129 assert(!g_log_interface.is_valid());
130 assert(log.is_bound());
131 assert(fallback_logger);
132
133 g_min_log_level.store(MOJO_LOG_LEVEL_INFO, std::memory_order_relaxed);
134 g_log_interface = log.PassInterface();
135
136 g_fallback_logger = fallback_logger;
137 }
138
139 LogClient::~LogClient() {
140 g_log_interface.PassHandle();
141 }
142
143 // static
144 const MojoLogger* LogClient::GetLogger() {
145 return &g_logclient_logger;
146 }
147
148 } // namespace log
149 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/services/log/cpp/BUILD.gn ('k') | mojo/services/log/cpp/log_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698