blob: 0413072aca71950ef6721f9224a7776e28f216b0 [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:
124 char buffer[LOG_MESSAGE_LEN];
125 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,
142 static_cast<int>(message.message_length), message.message,
143 static_cast<int>(output_length), buffer);
144 break;
145 }
Brian Silvermanf665d692013-02-17 22:11:39 -0800146}
147
148} // namespace internal
149
Brian Silverman669669f2014-02-14 16:32:56 -0800150void LogImplementation::LogStruct(
151 log_level level, const ::std::string &message, size_t size,
152 const MessageType *type, const ::std::function<size_t(char *)> &serialize) {
153 char serialized[1024];
154 if (size > sizeof(serialized)) {
155 LOG(FATAL, "structure of type %s too big to serialize\n",
156 type->name.c_str());
157 }
158 size_t used = serialize(serialized);
159 char printed[LOG_MESSAGE_LEN];
160 size_t printed_bytes = sizeof(printed);
161 if (!PrintMessage(printed, &printed_bytes, serialized, &used, *type)) {
162 LOG(FATAL, "PrintMessage(%p, %p(=%zd), %p, %p(=%zd), %p(name=%s)) failed\n",
163 printed, &printed_bytes, printed_bytes, serialized, &used, used, type,
164 type->name.c_str());
165 }
166 DoLogVariadic(level, "%.*s: %.*s\n", static_cast<int>(message.size()),
167 message.data(), static_cast<int>(printed_bytes), printed);
168}
169
Brian Silverman1e8ddfe2013-12-19 16:20:53 -0800170StreamLogImplementation::StreamLogImplementation(FILE *stream)
171 : stream_(stream) {}
172
173void StreamLogImplementation::DoLog(log_level level, const char *format,
174 va_list ap) {
175 LogMessage message;
176 internal::FillInMessage(level, format, ap, &message);
177 internal::PrintMessage(stream_, message);
178}
179
Brian Silvermanf665d692013-02-17 22:11:39 -0800180void LogNext(log_level level, const char *format, ...) {
181 va_list ap;
182 va_start(ap, format);
183 LogImplementation::DoVLog(level, format, ap, 2);
184 va_end(ap);
185}
186
187void AddImplementation(LogImplementation *implementation) {
188 Context *context = Context::Get();
189
190 if (implementation->next() != NULL) {
191 LOG(FATAL, "%p already has a next implementation, but it's not"
192 " being used yet\n", implementation);
193 }
194
195 LogImplementation *old = context->implementation;
196 if (old != NULL) {
197 implementation->set_next(old);
198 }
199 SetGlobalImplementation(implementation);
200}
201
202void Init() {
203 static Once<void> once(DoInit);
204 once.Get();
205}
206
207void Load() {
208 Context::Get();
209}
210
211void Cleanup() {
212 Context::Delete();
213}
214
215} // namespace logging
216} // namespace aos