blob: 68a88a84509aca39f08ce44375f52c181636ac9f [file] [log] [blame]
Brian Silvermanb0893882014-02-10 14:48:30 -08001#include "aos/common/logging/logging_impl.h"
2
3#include <assert.h>
4#include <stdarg.h>
5#include <string.h>
6
7#include "aos/common/die.h"
8
9// This file only contains the code necessary to link (ie no implementations).
10// See logging_impl.h for why this is necessary.
11
12namespace aos {
13namespace logging {
14namespace internal {
15
16LogImplementation *global_top_implementation(NULL);
17
18Context::Context()
19 : implementation(global_top_implementation),
20 sequence(0) {
21 cork_data.Reset();
22}
23
24void ExecuteFormat(char *output, size_t output_size,
25 const char *format, va_list ap) {
26 static const char *continued = "...\n";
27 const size_t size = output_size - strlen(continued);
28 const int ret = vsnprintf(output, size, format, ap);
29 if (ret < 0) {
30 LOG(FATAL, "vsnprintf(%p, %zd, %s, args) failed with %d (%s)\n",
31 output, size, format, errno, strerror(errno));
32 } else if (static_cast<uintmax_t>(ret) >= static_cast<uintmax_t>(size)) {
33 // Overwrite the '\0' at the end of the existing data and
34 // copy in the one on the end of continued.
35 memcpy(&output[size - 1], continued, strlen(continued) + 1);
36 }
37}
38
Brian Silvermand6974f42014-02-14 13:39:21 -080039void RunWithCurrentImplementation(
40 int levels, ::std::function<void(LogImplementation *)> function) {
Brian Silvermanb0893882014-02-10 14:48:30 -080041 Context *context = Context::Get();
42
Brian Silvermand6974f42014-02-14 13:39:21 -080043 LogImplementation *const top_implementation = context->implementation;
Brian Silvermanb0893882014-02-10 14:48:30 -080044 LogImplementation *new_implementation = top_implementation;
45 LogImplementation *implementation = NULL;
46 assert(levels >= 1);
47 for (int i = 0; i < levels; ++i) {
48 implementation = new_implementation;
49 if (new_implementation == NULL) {
50 Die("no logging implementation to use\n");
51 }
52 new_implementation = new_implementation->next();
53 }
54 context->implementation = new_implementation;
Brian Silvermand6974f42014-02-14 13:39:21 -080055 function(implementation);
Brian Silvermanb0893882014-02-10 14:48:30 -080056 context->implementation = top_implementation;
Brian Silvermand6974f42014-02-14 13:39:21 -080057}
Brian Silvermanb0893882014-02-10 14:48:30 -080058
Brian Silvermand6974f42014-02-14 13:39:21 -080059} // namespace internal
60
61using internal::Context;
62
Brian Silvermand6974f42014-02-14 13:39:21 -080063void LogImplementation::DoVLog(log_level level, const char *format, va_list ap,
64 int levels) {
65 internal::RunWithCurrentImplementation(
66 levels, [&](LogImplementation * implementation) {
67 implementation->DoLog(level, format, ap);
68
69 if (level == FATAL) {
70 VDie(format, ap);
71 }
72 });
Brian Silvermanb0893882014-02-10 14:48:30 -080073}
74
75void VLog(log_level level, const char *format, va_list ap) {
76 LogImplementation::DoVLog(level, format, ap, 1);
77}
78
79void VCork(int line, const char *function, const char *format, va_list ap) {
80 Context *context = Context::Get();
81
82 const size_t message_length = strlen(context->cork_data.message);
83 if (line > context->cork_data.line_max) context->cork_data.line_max = line;
84 if (line < context->cork_data.line_min) context->cork_data.line_min = line;
85
86 if (context->cork_data.function == NULL) {
87 context->cork_data.function = function;
88 } else {
89 if (strcmp(context->cork_data.function, function) != 0) {
90 LOG(FATAL, "started corking data in function %s but then moved to %s\n",
91 context->cork_data.function, function);
92 }
93 }
94
95 internal::ExecuteFormat(context->cork_data.message + message_length,
96 sizeof(context->cork_data.message) - message_length,
97 format, ap);
98}
99
100void VUnCork(int line, const char *function, log_level level, const char *file,
101 const char *format, va_list ap) {
102 Context *context = Context::Get();
103
104 VCork(line, function, format, ap);
105
106 log_do(level, "%s: %d-%d: %s: %s", file,
107 context->cork_data.line_min, context->cork_data.line_max, function,
108 context->cork_data.message);
109
110 context->cork_data.Reset();
111}
112
113} // namespace logging
114} // namespace aos
115
116void log_do(log_level level, const char *format, ...) {
117 va_list ap;
118 va_start(ap, format);
119 aos::logging::VLog(level, format, ap);
120 va_end(ap);
121}
122
123void log_cork(int line, const char *function, const char *format, ...) {
124 va_list ap;
125 va_start(ap, format);
126 aos::logging::VCork(line, function, format, ap);
127 va_end(ap);
128}
129
130void log_uncork(int line, const char *function, log_level level,
131 const char *file, const char *format, ...) {
132 va_list ap;
133 va_start(ap, format);
134 aos::logging::VUnCork(line, function, level, file, format, ap);
135 va_end(ap);
136}