blob: a0c4e5639371a44372aba0aad51c4a12b2a61ae8 [file] [log] [blame]
Brian Silvermanf665d692013-02-17 22:11:39 -08001#include "aos/common/logging/logging_impl.h"
2
3#include <assert.h>
Brian Silvermanb0893882014-02-10 14:48:30 -08004#include <stdarg.h>
Brian Silvermanf665d692013-02-17 22:11:39 -08005
Brian Silvermanf665d692013-02-17 22:11:39 -08006#include "aos/common/time.h"
7#include "aos/common/inttypes.h"
8#include "aos/common/once.h"
Brian Silverman669669f2014-02-14 16:32:56 -08009#include "aos/common/queue_types.h"
Brian Silvermanf665d692013-02-17 22:11:39 -080010
11namespace aos {
12namespace logging {
13namespace {
14
15using internal::Context;
Brian Silvermanb0893882014-02-10 14:48:30 -080016using internal::global_top_implementation;
Brian Silvermanf665d692013-02-17 22:11:39 -080017
Brian Silvermanf665d692013-02-17 22:11:39 -080018// The root LogImplementation. It only logs to stderr/stdout.
19// Some of the things specified in the LogImplementation documentation doesn't
20// apply here (mostly the parts about being able to use LOG) because this is the
21// root one.
22class RootLogImplementation : public LogImplementation {
23 virtual void set_next(LogImplementation *) {
24 LOG(FATAL, "can't have a next logger from here\n");
25 }
26
27 virtual void DoLog(log_level level, const char *format, va_list ap) {
28 LogMessage message;
29 internal::FillInMessage(level, format, ap, &message);
30 internal::PrintMessage(stderr, message);
31 fputs("root logger got used, see stderr for message\n", stdout);
32 }
33};
34
35void SetGlobalImplementation(LogImplementation *implementation) {
36 Context *context = Context::Get();
37
38 context->implementation = implementation;
Brian Silvermanb0893882014-02-10 14:48:30 -080039 global_top_implementation = implementation;
Brian Silvermanf665d692013-02-17 22:11:39 -080040}
41
Brian Silvermane5d65692013-02-28 15:15:03 -080042void NewContext() {
43 Context::Delete();
44}
45
Brian Silvermanf665d692013-02-17 22:11:39 -080046void *DoInit() {
47 SetGlobalImplementation(new RootLogImplementation());
Brian Silvermane5d65692013-02-28 15:15:03 -080048
Brian Silvermane5d65692013-02-28 15:15:03 -080049 if (pthread_atfork(NULL /*prepare*/, NULL /*parent*/,
50 NewContext /*child*/) != 0) {
51 LOG(FATAL, "pthread_atfork(NULL, NULL, %p) failed\n",
52 NewContext);
53 }
Brian Silvermane5d65692013-02-28 15:15:03 -080054
Brian Silvermanf665d692013-02-17 22:11:39 -080055 return NULL;
56}
57
58} // namespace
59namespace internal {
Brian Silverman88471dc2014-02-15 22:35:42 -080060namespace {
Brian Silvermanf665d692013-02-17 22:11:39 -080061
Brian Silverman88471dc2014-02-15 22:35:42 -080062void FillInMessageBase(log_level level, LogMessage *message) {
Brian Silvermanf665d692013-02-17 22:11:39 -080063 Context *context = Context::Get();
64
Brian Silvermanf665d692013-02-17 22:11:39 -080065 message->level = level;
66 message->source = context->source;
Brian Silverman88471dc2014-02-15 22:35:42 -080067 memcpy(message->name, context->name.c_str(), context->name.size());
68 message->name_length = context->name.size();
Brian Silvermanf665d692013-02-17 22:11:39 -080069
70 time::Time now = time::Time::Now();
71 message->seconds = now.sec();
72 message->nseconds = now.nsec();
73
74 message->sequence = context->sequence++;
75}
76
Brian Silverman88471dc2014-02-15 22:35:42 -080077} // namespace
78
79void FillInMessageStructure(log_level level,
80 const ::std::string &message_string, size_t size,
81 const MessageType *type,
82 const ::std::function<size_t(char *)> &serialize,
83 LogMessage *message) {
84 type_cache::AddShm(type->id);
85 message->structure.type_id = type->id;
86
87 FillInMessageBase(level, message);
88
89 if (message_string.size() + size > sizeof(message->structure.serialized)) {
90 LOG(FATAL, "serialized struct %s (size %zd) and message %s too big\n",
91 type->name.c_str(), size, message_string.c_str());
92 }
93 message->structure.string_length = message_string.size();
94 memcpy(message->structure.serialized, message_string.data(),
95 message->structure.string_length);
96
97 message->message_length = serialize(
98 &message->structure.serialized[message->structure.string_length]);
99 message->type = LogMessage::Type::kStruct;
100}
101
102void FillInMessage(log_level level, const char *format, va_list ap,
103 LogMessage *message) {
104 FillInMessageBase(level, message);
105
106 message->message_length =
107 ExecuteFormat(message->message, sizeof(message->message), format, ap);
108 message->type = LogMessage::Type::kString;
109}
110
Brian Silvermanf665d692013-02-17 22:11:39 -0800111void PrintMessage(FILE *output, const LogMessage &message) {
Brian Silverman88471dc2014-02-15 22:35:42 -0800112#define BASE_FORMAT \
113 "%.*s(%" PRId32 ")(%05" PRIu16 "): %s at %010" PRId32 ".%09" PRId32 "s: "
114#define BASE_ARGS \
115 static_cast<int>(message.name_length), message.name, \
116 static_cast<int32_t>(message.source), message.sequence, \
117 log_str(message.level), message.seconds, message.nseconds
118 switch (message.type) {
119 case LogMessage::Type::kString:
120 fprintf(output, BASE_FORMAT "%.*s", BASE_ARGS,
121 static_cast<int>(message.message_length), message.message);
122 break;
123 case LogMessage::Type::kStruct:
Brian Silverman2508c082014-02-17 15:45:02 -0800124 char buffer[1024];
Brian Silverman88471dc2014-02-15 22:35:42 -0800125 size_t output_length = sizeof(buffer);
126 size_t input_length = message.message_length;
127 if (!PrintMessage(
128 buffer, &output_length,
129 message.structure.serialized + message.structure.string_length,
130 &input_length, type_cache::Get(message.structure.type_id))) {
131 LOG(FATAL,
132 "printing message (%.*s) of type %s into %zu-byte buffer failed\n",
133 static_cast<int>(message.message_length), message.message,
134 type_cache::Get(message.structure.type_id).name.c_str(),
135 sizeof(buffer));
136 }
137 if (input_length > 0) {
138 LOG(WARNING, "%zu extra bytes on message of type %s\n", input_length,
139 type_cache::Get(message.structure.type_id).name.c_str());
140 }
141 fprintf(output, BASE_FORMAT "%.*s: %.*s\n", BASE_ARGS,
Brian Silvermanb263d302014-02-16 00:01:43 -0800142 static_cast<int>(message.structure.string_length),
143 message.structure.serialized,
144 static_cast<int>(sizeof(buffer) - output_length), buffer);
Brian Silverman88471dc2014-02-15 22:35:42 -0800145 break;
146 }
Brian Silvermanf665d692013-02-17 22:11:39 -0800147}
148
149} // namespace internal
150
Brian Silverman669669f2014-02-14 16:32:56 -0800151void LogImplementation::LogStruct(
152 log_level level, const ::std::string &message, size_t size,
153 const MessageType *type, const ::std::function<size_t(char *)> &serialize) {
154 char serialized[1024];
155 if (size > sizeof(serialized)) {
156 LOG(FATAL, "structure of type %s too big to serialize\n",
157 type->name.c_str());
158 }
159 size_t used = serialize(serialized);
Brian Silverman2508c082014-02-17 15:45:02 -0800160 char printed[1024];
Brian Silverman669669f2014-02-14 16:32:56 -0800161 size_t printed_bytes = sizeof(printed);
162 if (!PrintMessage(printed, &printed_bytes, serialized, &used, *type)) {
163 LOG(FATAL, "PrintMessage(%p, %p(=%zd), %p, %p(=%zd), %p(name=%s)) failed\n",
164 printed, &printed_bytes, printed_bytes, serialized, &used, used, type,
165 type->name.c_str());
166 }
167 DoLogVariadic(level, "%.*s: %.*s\n", static_cast<int>(message.size()),
Brian Silverman1c7b8192014-02-16 22:37:36 -0800168 message.data(),
169 static_cast<int>(sizeof(printed) - printed_bytes), printed);
Brian Silverman669669f2014-02-14 16:32:56 -0800170}
171
Brian Silverman1e8ddfe2013-12-19 16:20:53 -0800172StreamLogImplementation::StreamLogImplementation(FILE *stream)
173 : stream_(stream) {}
174
175void StreamLogImplementation::DoLog(log_level level, const char *format,
176 va_list ap) {
177 LogMessage message;
178 internal::FillInMessage(level, format, ap, &message);
179 internal::PrintMessage(stream_, message);
180}
181
Brian Silvermanf665d692013-02-17 22:11:39 -0800182void LogNext(log_level level, const char *format, ...) {
183 va_list ap;
184 va_start(ap, format);
185 LogImplementation::DoVLog(level, format, ap, 2);
186 va_end(ap);
187}
188
189void AddImplementation(LogImplementation *implementation) {
190 Context *context = Context::Get();
191
192 if (implementation->next() != NULL) {
193 LOG(FATAL, "%p already has a next implementation, but it's not"
194 " being used yet\n", implementation);
195 }
196
197 LogImplementation *old = context->implementation;
198 if (old != NULL) {
199 implementation->set_next(old);
200 }
201 SetGlobalImplementation(implementation);
202}
203
204void Init() {
205 static Once<void> once(DoInit);
206 once.Get();
207}
208
209void Load() {
210 Context::Get();
211}
212
213void Cleanup() {
214 Context::Delete();
215}
216
217} // namespace logging
218} // namespace aos