blob: f37f0ca906ccf71388150fe18b4790b480b09655 [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#include "aos/logging/implementations.h"
Brian Silvermanf665d692013-02-17 22:11:39 -08002
Brian Silvermanb0893882014-02-10 14:48:30 -08003#include <stdarg.h>
Brian4a424a22014-04-02 11:52:45 -07004#include <inttypes.h>
Austin Schuh044e18b2015-10-21 20:17:09 -07005
Brian Silvermancb5da1f2015-12-05 22:19:58 -05006#include <algorithm>
Austin Schuhf2a50ba2016-12-24 16:16:26 -08007#include <chrono>
Brian Silvermancb5da1f2015-12-05 22:19:58 -05008
John Park33858a32018-09-28 23:05:48 -07009#include "aos/die.h"
10#include "aos/logging/printf_formats.h"
11#include "aos/queue_types.h"
12#include "aos/time/time.h"
John Park398c74a2018-10-20 21:17:39 -070013#include "aos/ipc_lib/queue.h"
Sabina Davis2ed5ea22017-09-26 22:27:42 -070014#include "aos/once.h"
Brian Silvermanf665d692013-02-17 22:11:39 -080015
16namespace aos {
17namespace logging {
18namespace {
19
Austin Schuhf2a50ba2016-12-24 16:16:26 -080020namespace chrono = ::std::chrono;
21
Brian Silvermanf665d692013-02-17 22:11:39 -080022// The root LogImplementation. It only logs to stderr/stdout.
23// Some of the things specified in the LogImplementation documentation doesn't
24// apply here (mostly the parts about being able to use LOG) because this is the
25// root one.
Brian Silvermancb5da1f2015-12-05 22:19:58 -050026class RootLogImplementation : public SimpleLogImplementation {
Brian Silverman2e799732014-06-05 21:50:19 -070027 public:
28 void have_other_implementation() { only_implementation_ = false; }
29
30 private:
Brian Silvermancb5da1f2015-12-05 22:19:58 -050031 void set_next(LogImplementation *) override {
Brian Silvermanf665d692013-02-17 22:11:39 -080032 LOG(FATAL, "can't have a next logger from here\n");
33 }
34
Brian Silvermanf7986142014-04-21 17:42:35 -070035 __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 0)))
Brian Silvermancb5da1f2015-12-05 22:19:58 -050036 void DoLog(log_level level, const char *format, va_list ap) override {
Brian Silvermanf665d692013-02-17 22:11:39 -080037 LogMessage message;
38 internal::FillInMessage(level, format, ap, &message);
39 internal::PrintMessage(stderr, message);
Brian Silverman2e799732014-06-05 21:50:19 -070040 if (!only_implementation_) {
41 fputs("root logger got used, see stderr for message\n", stdout);
42 }
Brian Silvermanf665d692013-02-17 22:11:39 -080043 }
Brian Silverman2e799732014-06-05 21:50:19 -070044
45 bool only_implementation_ = true;
Brian Silvermanf665d692013-02-17 22:11:39 -080046};
47
Brian Silverman2e799732014-06-05 21:50:19 -070048RootLogImplementation *root_implementation = nullptr;
49
Brian Silvermanf665d692013-02-17 22:11:39 -080050void SetGlobalImplementation(LogImplementation *implementation) {
Brian Silverman5c201e22014-06-12 22:40:28 -070051 if (root_implementation == nullptr) {
52 fputs("Somebody didn't call logging::Init()!\n", stderr);
53 abort();
54 }
55
Brian Silvermancb5da1f2015-12-05 22:19:58 -050056 internal::Context *context = internal::Context::Get();
Brian Silvermanf665d692013-02-17 22:11:39 -080057
58 context->implementation = implementation;
Brian Silvermancb5da1f2015-12-05 22:19:58 -050059 internal::global_top_implementation.store(implementation);
Brian Silvermanf665d692013-02-17 22:11:39 -080060}
61
Brian Silvermane5d65692013-02-28 15:15:03 -080062void NewContext() {
Brian Silvermancb5da1f2015-12-05 22:19:58 -050063 internal::Context::Delete();
Brian Silvermane5d65692013-02-28 15:15:03 -080064}
65
Brian Silvermanf665d692013-02-17 22:11:39 -080066void *DoInit() {
Brian Silverman2e799732014-06-05 21:50:19 -070067 SetGlobalImplementation(root_implementation = new RootLogImplementation());
Brian Silvermane5d65692013-02-28 15:15:03 -080068
Brian Silvermane5d65692013-02-28 15:15:03 -080069 if (pthread_atfork(NULL /*prepare*/, NULL /*parent*/,
70 NewContext /*child*/) != 0) {
71 LOG(FATAL, "pthread_atfork(NULL, NULL, %p) failed\n",
72 NewContext);
73 }
Brian Silvermane5d65692013-02-28 15:15:03 -080074
Brian Silvermanf665d692013-02-17 22:11:39 -080075 return NULL;
76}
77
78} // namespace
79namespace internal {
Brian Silverman88471dc2014-02-15 22:35:42 -080080namespace {
Brian Silvermanf665d692013-02-17 22:11:39 -080081
Brian Silverman88471dc2014-02-15 22:35:42 -080082void FillInMessageBase(log_level level, LogMessage *message) {
Brian Silvermanf665d692013-02-17 22:11:39 -080083 Context *context = Context::Get();
84
Brian Silvermanf665d692013-02-17 22:11:39 -080085 message->level = level;
86 message->source = context->source;
Austin Schuhaebbc342015-01-25 02:25:13 -080087 memcpy(message->name, context->name, context->name_size);
88 message->name_length = context->name_size;
Brian Silvermanf665d692013-02-17 22:11:39 -080089
Austin Schuhf2a50ba2016-12-24 16:16:26 -080090 monotonic_clock::time_point monotonic_now = monotonic_clock::now();
91 message->seconds =
92 chrono::duration_cast<chrono::seconds>(monotonic_now.time_since_epoch())
93 .count();
94 message->nseconds =
95 chrono::duration_cast<chrono::nanoseconds>(
96 monotonic_now.time_since_epoch() - chrono::seconds(message->seconds))
97 .count();
Brian Silvermanf665d692013-02-17 22:11:39 -080098
99 message->sequence = context->sequence++;
100}
101
Brian Silverman88471dc2014-02-15 22:35:42 -0800102} // namespace
103
Austin Schuh1bf8a212019-05-26 22:13:14 -0700104void FillInMessageStructure(bool add_to_type_cache, log_level level,
Brian Silverman88471dc2014-02-15 22:35:42 -0800105 const ::std::string &message_string, size_t size,
106 const MessageType *type,
107 const ::std::function<size_t(char *)> &serialize,
108 LogMessage *message) {
Austin Schuh1bf8a212019-05-26 22:13:14 -0700109 if (add_to_type_cache) {
110 type_cache::AddShm(type->id);
111 }
Brian Silverman88471dc2014-02-15 22:35:42 -0800112 message->structure.type_id = type->id;
113
114 FillInMessageBase(level, message);
115
116 if (message_string.size() + size > sizeof(message->structure.serialized)) {
Austin Schuhc804fd12019-03-22 21:15:37 -0700117 LOG(FATAL,
118 "serialized struct %s (size %zd + %zd > %zd) and message %s too big\n",
119 type->name.c_str(), message_string.size(), size,
120 sizeof(message->structure.serialized), message_string.c_str());
Brian Silverman88471dc2014-02-15 22:35:42 -0800121 }
122 message->structure.string_length = message_string.size();
123 memcpy(message->structure.serialized, message_string.data(),
124 message->structure.string_length);
125
126 message->message_length = serialize(
127 &message->structure.serialized[message->structure.string_length]);
128 message->type = LogMessage::Type::kStruct;
129}
130
Brian Silverman664db1a2014-03-20 17:06:29 -0700131void FillInMessageMatrix(log_level level,
132 const ::std::string &message_string, uint32_t type_id,
133 int rows, int cols, const void *data,
134 LogMessage *message) {
135 CHECK(MessageType::IsPrimitive(type_id));
136 message->matrix.type = type_id;
137
138 const auto element_size = MessageType::Sizeof(type_id);
139
140 FillInMessageBase(level, message);
141
142 message->message_length = rows * cols * element_size;
143 if (message_string.size() + message->message_length >
144 sizeof(message->matrix.data)) {
145 LOG(FATAL, "%dx%d matrix of type %" PRIu32
146 " (size %u) and message %s is too big\n",
147 rows, cols, type_id, element_size, message_string.c_str());
148 }
149 message->matrix.string_length = message_string.size();
150 memcpy(message->matrix.data, message_string.data(),
151 message->matrix.string_length);
152
153 message->matrix.rows = rows;
154 message->matrix.cols = cols;
155 SerializeMatrix(type_id, &message->matrix.data[message->matrix.string_length],
156 data, rows, cols);
157 message->type = LogMessage::Type::kMatrix;
158}
159
Brian Silverman88471dc2014-02-15 22:35:42 -0800160void FillInMessage(log_level level, const char *format, va_list ap,
161 LogMessage *message) {
162 FillInMessageBase(level, message);
163
164 message->message_length =
165 ExecuteFormat(message->message, sizeof(message->message), format, ap);
166 message->type = LogMessage::Type::kString;
167}
168
Brian Silvermanf665d692013-02-17 22:11:39 -0800169void PrintMessage(FILE *output, const LogMessage &message) {
Brian Silvermana7234c62014-03-24 20:23:25 -0700170#define BASE_ARGS \
171 AOS_LOGGING_BASE_ARGS( \
172 message.name_length, message.name, static_cast<int32_t>(message.source), \
173 message.sequence, message.level, message.seconds, message.nseconds)
174 switch (message.type) {
Brian Silverman88471dc2014-02-15 22:35:42 -0800175 case LogMessage::Type::kString:
Brian Silvermana7234c62014-03-24 20:23:25 -0700176 fprintf(output, AOS_LOGGING_BASE_FORMAT "%.*s", BASE_ARGS,
Brian Silverman88471dc2014-02-15 22:35:42 -0800177 static_cast<int>(message.message_length), message.message);
178 break;
Brian Silvermanff12c9f2014-03-19 17:53:29 -0700179 case LogMessage::Type::kStruct: {
Austin Schuheee8c962017-04-12 22:32:44 -0700180 char buffer[4096];
Brian Silverman88471dc2014-02-15 22:35:42 -0800181 size_t output_length = sizeof(buffer);
182 size_t input_length = message.message_length;
183 if (!PrintMessage(
184 buffer, &output_length,
185 message.structure.serialized + message.structure.string_length,
186 &input_length, type_cache::Get(message.structure.type_id))) {
187 LOG(FATAL,
188 "printing message (%.*s) of type %s into %zu-byte buffer failed\n",
189 static_cast<int>(message.message_length), message.message,
190 type_cache::Get(message.structure.type_id).name.c_str(),
191 sizeof(buffer));
192 }
193 if (input_length > 0) {
194 LOG(WARNING, "%zu extra bytes on message of type %s\n", input_length,
195 type_cache::Get(message.structure.type_id).name.c_str());
196 }
Brian Silvermana7234c62014-03-24 20:23:25 -0700197 fprintf(output, AOS_LOGGING_BASE_FORMAT "%.*s: %.*s\n", BASE_ARGS,
Brian Silvermanb263d302014-02-16 00:01:43 -0800198 static_cast<int>(message.structure.string_length),
199 message.structure.serialized,
200 static_cast<int>(sizeof(buffer) - output_length), buffer);
Brian Silvermanff12c9f2014-03-19 17:53:29 -0700201 } break;
202 case LogMessage::Type::kMatrix: {
203 char buffer[1024];
204 size_t output_length = sizeof(buffer);
205 if (message.message_length !=
206 static_cast<size_t>(message.matrix.rows * message.matrix.cols *
207 MessageType::Sizeof(message.matrix.type))) {
208 LOG(FATAL, "expected %d bytes of matrix data but have %zu\n",
209 message.matrix.rows * message.matrix.cols *
210 MessageType::Sizeof(message.matrix.type),
211 message.message_length);
212 }
213 if (!PrintMatrix(buffer, &output_length,
214 message.matrix.data + message.matrix.string_length,
215 message.matrix.type, message.matrix.rows,
216 message.matrix.cols)) {
217 LOG(FATAL, "printing %dx%d matrix of type %" PRIu32 " failed\n",
218 message.matrix.rows, message.matrix.cols, message.matrix.type);
219 }
Brian Silvermana7234c62014-03-24 20:23:25 -0700220 fprintf(output, AOS_LOGGING_BASE_FORMAT "%.*s: %.*s\n", BASE_ARGS,
Brian Silvermanff12c9f2014-03-19 17:53:29 -0700221 static_cast<int>(message.matrix.string_length),
Brian Silverman664db1a2014-03-20 17:06:29 -0700222 message.matrix.data,
Brian Silvermanff12c9f2014-03-19 17:53:29 -0700223 static_cast<int>(sizeof(buffer) - output_length), buffer);
224 } break;
Brian Silverman88471dc2014-02-15 22:35:42 -0800225 }
Brian Silvermanc1a244e2014-02-20 14:12:39 -0800226#undef BASE_ARGS
Brian Silvermanf665d692013-02-17 22:11:39 -0800227}
228
229} // namespace internal
230
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500231void SimpleLogImplementation::LogStruct(
Brian Silverman669669f2014-02-14 16:32:56 -0800232 log_level level, const ::std::string &message, size_t size,
233 const MessageType *type, const ::std::function<size_t(char *)> &serialize) {
234 char serialized[1024];
235 if (size > sizeof(serialized)) {
236 LOG(FATAL, "structure of type %s too big to serialize\n",
237 type->name.c_str());
238 }
239 size_t used = serialize(serialized);
Brian Silverman2508c082014-02-17 15:45:02 -0800240 char printed[1024];
Brian Silverman669669f2014-02-14 16:32:56 -0800241 size_t printed_bytes = sizeof(printed);
242 if (!PrintMessage(printed, &printed_bytes, serialized, &used, *type)) {
243 LOG(FATAL, "PrintMessage(%p, %p(=%zd), %p, %p(=%zd), %p(name=%s)) failed\n",
244 printed, &printed_bytes, printed_bytes, serialized, &used, used, type,
245 type->name.c_str());
246 }
247 DoLogVariadic(level, "%.*s: %.*s\n", static_cast<int>(message.size()),
Brian Silverman1c7b8192014-02-16 22:37:36 -0800248 message.data(),
249 static_cast<int>(sizeof(printed) - printed_bytes), printed);
Brian Silverman669669f2014-02-14 16:32:56 -0800250}
251
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500252void SimpleLogImplementation::LogMatrix(
Brian Silvermanff12c9f2014-03-19 17:53:29 -0700253 log_level level, const ::std::string &message, uint32_t type_id,
254 int rows, int cols, const void *data) {
255 char serialized[1024];
256 if (static_cast<size_t>(rows * cols * MessageType::Sizeof(type_id)) >
257 sizeof(serialized)) {
258 LOG(FATAL, "matrix of size %u too big to serialize\n",
259 rows * cols * MessageType::Sizeof(type_id));
260 }
261 SerializeMatrix(type_id, serialized, data, rows, cols);
262 char printed[1024];
263 size_t printed_bytes = sizeof(printed);
264 if (!PrintMatrix(printed, &printed_bytes, serialized, type_id, rows, cols)) {
265 LOG(FATAL, "PrintMatrix(%p, %p(=%zd), %p, %" PRIu32 ", %d, %d) failed\n",
266 printed, &printed_bytes, printed_bytes, serialized, type_id, rows,
267 cols);
268 }
269 DoLogVariadic(level, "%.*s: %.*s\n", static_cast<int>(message.size()),
270 message.data(),
271 static_cast<int>(sizeof(printed) - printed_bytes), printed);
272}
273
Brian Silvermanbe858a12014-04-30 17:37:28 -0700274void HandleMessageLogImplementation::DoLog(log_level level, const char *format,
275 va_list ap) {
276 LogMessage message;
277 internal::FillInMessage(level, format, ap, &message);
278 HandleMessage(message);
279}
280
281void HandleMessageLogImplementation::LogStruct(
282 log_level level, const ::std::string &message_string, size_t size,
283 const MessageType *type, const ::std::function<size_t(char *)> &serialize) {
284 LogMessage message;
Austin Schuh1bf8a212019-05-26 22:13:14 -0700285 internal::FillInMessageStructure(fill_type_cache(), level, message_string,
286 size, type, serialize, &message);
Brian Silvermanbe858a12014-04-30 17:37:28 -0700287 HandleMessage(message);
288}
289
290void HandleMessageLogImplementation::LogMatrix(
291 log_level level, const ::std::string &message_string, uint32_t type_id,
292 int rows, int cols, const void *data) {
293 LogMessage message;
294 internal::FillInMessageMatrix(level, message_string, type_id, rows, cols,
295 data, &message);
296 HandleMessage(message);
297}
298
Brian Silverman1e8ddfe2013-12-19 16:20:53 -0800299StreamLogImplementation::StreamLogImplementation(FILE *stream)
300 : stream_(stream) {}
301
Brian Silvermanbe858a12014-04-30 17:37:28 -0700302void StreamLogImplementation::HandleMessage(const LogMessage &message) {
Brian Silverman1e8ddfe2013-12-19 16:20:53 -0800303 internal::PrintMessage(stream_, message);
304}
305
Brian Silvermanf665d692013-02-17 22:11:39 -0800306void AddImplementation(LogImplementation *implementation) {
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500307 internal::Context *context = internal::Context::Get();
Brian Silvermanf665d692013-02-17 22:11:39 -0800308
309 if (implementation->next() != NULL) {
310 LOG(FATAL, "%p already has a next implementation, but it's not"
311 " being used yet\n", implementation);
312 }
313
314 LogImplementation *old = context->implementation;
315 if (old != NULL) {
316 implementation->set_next(old);
317 }
318 SetGlobalImplementation(implementation);
Brian Silverman2e799732014-06-05 21:50:19 -0700319 root_implementation->have_other_implementation();
Brian Silvermanf665d692013-02-17 22:11:39 -0800320}
321
322void Init() {
323 static Once<void> once(DoInit);
324 once.Get();
325}
326
327void Load() {
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500328 internal::Context::Get();
Brian Silvermanf665d692013-02-17 22:11:39 -0800329}
330
331void Cleanup() {
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500332 internal::Context::Delete();
333}
334
335namespace {
336
337RawQueue *queue = NULL;
338
339int dropped_messages = 0;
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800340monotonic_clock::time_point dropped_start, backoff_start;
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500341// Wait this long after dropping a message before even trying to write any more.
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800342constexpr chrono::milliseconds kDropBackoff = chrono::milliseconds(100);
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500343
344LogMessage *GetMessageOrDie() {
345 LogMessage *message = static_cast<LogMessage *>(queue->GetMessage());
346 if (message == NULL) {
347 LOG(FATAL, "%p->GetMessage() failed\n", queue);
348 } else {
349 return message;
350 }
351}
352
353void Write(LogMessage *msg) {
354 if (__builtin_expect(dropped_messages > 0, false)) {
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800355 monotonic_clock::time_point message_time(
356 chrono::seconds(msg->seconds) + chrono::nanoseconds(msg->nseconds));
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500357 if (message_time - backoff_start < kDropBackoff) {
358 ++dropped_messages;
359 queue->FreeMessage(msg);
360 return;
361 }
362
363 LogMessage *dropped_message = GetMessageOrDie();
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800364 chrono::seconds dropped_start_sec = chrono::duration_cast<chrono::seconds>(
365 dropped_start.time_since_epoch());
366 chrono::nanoseconds dropped_start_nsec =
367 chrono::duration_cast<chrono::nanoseconds>(
368 dropped_start.time_since_epoch() - dropped_start_sec);
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500369 internal::FillInMessageVarargs(
370 ERROR, dropped_message,
371 "%d logs starting at %" PRId32 ".%" PRId32 " dropped\n",
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800372 dropped_messages, static_cast<int32_t>(dropped_start_sec.count()),
373 static_cast<int32_t>(dropped_start_nsec.count()));
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500374 if (queue->WriteMessage(dropped_message, RawQueue::kNonBlock)) {
375 dropped_messages = 0;
376 } else {
377 // Don't even bother trying to write this message because it's not likely
378 // to work and it would be confusing to have one log in the middle of a
379 // string of failures get through.
380 ++dropped_messages;
381 backoff_start = message_time;
382 queue->FreeMessage(msg);
383 return;
384 }
385 }
386 if (!queue->WriteMessage(msg, RawQueue::kNonBlock)) {
387 if (dropped_messages == 0) {
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800388 monotonic_clock::time_point message_time(
389 chrono::seconds(msg->seconds) + chrono::nanoseconds(msg->nseconds));
390 dropped_start = backoff_start = message_time;
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500391 }
392 ++dropped_messages;
393 }
394}
395
396class LinuxQueueLogImplementation : public LogImplementation {
397 __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 0)))
398 void DoLog(log_level level, const char *format, va_list ap) override {
399 LogMessage *message = GetMessageOrDie();
400 internal::FillInMessage(level, format, ap, message);
401 Write(message);
402 }
403
404 void LogStruct(log_level level, const ::std::string &message_string,
405 size_t size, const MessageType *type,
406 const ::std::function<size_t(char *)> &serialize) override {
407 LogMessage *message = GetMessageOrDie();
Austin Schuh1bf8a212019-05-26 22:13:14 -0700408 internal::FillInMessageStructure(fill_type_cache(), level, message_string,
409 size, type, serialize, message);
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500410 Write(message);
411 }
412
413 void LogMatrix(log_level level, const ::std::string &message_string,
414 uint32_t type_id, int rows, int cols,
415 const void *data) override {
416 LogMessage *message = GetMessageOrDie();
417 internal::FillInMessageMatrix(level, message_string, type_id, rows, cols,
418 data, message);
419 Write(message);
420 }
421};
422
423} // namespace
424
425RawQueue *GetLoggingQueue() {
426 return RawQueue::Fetch("LoggingQueue", sizeof(LogMessage), 1323, 40000);
427}
428
429void RegisterQueueImplementation() {
430 Init();
431
432 queue = GetLoggingQueue();
433 if (queue == NULL) {
434 Die("logging: couldn't fetch queue\n");
435 }
436
437 AddImplementation(new LinuxQueueLogImplementation());
Brian Silvermanf665d692013-02-17 22:11:39 -0800438}
439
440} // namespace logging
441} // namespace aos