blob: fb3b60e6813bcb6a3d70a57ea85f4d4de5083c40 [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#include "aos/logging/interface.h"
Brian Silvermanb0893882014-02-10 14:48:30 -08002
Brian Silvermanb0893882014-02-10 14:48:30 -08003#include <stdarg.h>
Austin Schuh044e18b2015-10-21 20:17:09 -07004#include <stdio.h>
Brian Silvermanb0893882014-02-10 14:48:30 -08005#include <string.h>
6
Brian Silverman88471dc2014-02-15 22:35:42 -08007#include <type_traits>
Austin Schuh044e18b2015-10-21 20:17:09 -07008#include <functional>
Brian Silverman88471dc2014-02-15 22:35:42 -08009
John Park33858a32018-09-28 23:05:48 -070010#include "aos/die.h"
11#include "aos/logging/context.h"
Brian Silvermanb0893882014-02-10 14:48:30 -080012
Brian Silvermanb0893882014-02-10 14:48:30 -080013namespace aos {
14namespace logging {
15namespace internal {
16
Brian Silverman88471dc2014-02-15 22:35:42 -080017size_t ExecuteFormat(char *output, size_t output_size, const char *format,
18 va_list ap) {
19 static const char *const continued = "...\n";
Brian Silvermanb0893882014-02-10 14:48:30 -080020 const size_t size = output_size - strlen(continued);
21 const int ret = vsnprintf(output, size, format, ap);
Brian Silverman88471dc2014-02-15 22:35:42 -080022 typedef ::std::common_type<typeof(ret), typeof(size)>::type RetType;
Brian Silvermanb0893882014-02-10 14:48:30 -080023 if (ret < 0) {
Brian Silverman01be0002014-05-10 15:44:38 -070024 PLOG(FATAL, "vsnprintf(%p, %zd, %s, args) failed",
25 output, size, format);
Brian Silverman88471dc2014-02-15 22:35:42 -080026 } else if (static_cast<RetType>(ret) >= static_cast<RetType>(size)) {
Brian Silvermanb0893882014-02-10 14:48:30 -080027 // Overwrite the '\0' at the end of the existing data and
28 // copy in the one on the end of continued.
29 memcpy(&output[size - 1], continued, strlen(continued) + 1);
30 }
Brian Silverman88471dc2014-02-15 22:35:42 -080031 return ::std::min<RetType>(ret, size);
Brian Silvermanb0893882014-02-10 14:48:30 -080032}
33
Brian Silvermand6974f42014-02-14 13:39:21 -080034void RunWithCurrentImplementation(
35 int levels, ::std::function<void(LogImplementation *)> function) {
Brian Silvermanb0893882014-02-10 14:48:30 -080036 Context *context = Context::Get();
37
Brian Silvermand6974f42014-02-14 13:39:21 -080038 LogImplementation *const top_implementation = context->implementation;
Brian Silvermanb0893882014-02-10 14:48:30 -080039 LogImplementation *new_implementation = top_implementation;
40 LogImplementation *implementation = NULL;
Brian Silvermanb0893882014-02-10 14:48:30 -080041 for (int i = 0; i < levels; ++i) {
42 implementation = new_implementation;
43 if (new_implementation == NULL) {
44 Die("no logging implementation to use\n");
45 }
46 new_implementation = new_implementation->next();
47 }
48 context->implementation = new_implementation;
Brian Silvermand6974f42014-02-14 13:39:21 -080049 function(implementation);
Brian Silvermanb0893882014-02-10 14:48:30 -080050 context->implementation = top_implementation;
Brian Silvermand6974f42014-02-14 13:39:21 -080051}
Brian Silvermanb0893882014-02-10 14:48:30 -080052
Brian Silvermand6974f42014-02-14 13:39:21 -080053} // namespace internal
54
55using internal::Context;
56
Brian Silvermand6974f42014-02-14 13:39:21 -080057void LogImplementation::DoVLog(log_level level, const char *format, va_list ap,
58 int levels) {
Austin Schuh39700802016-11-26 21:23:56 -080059 auto log_impl = [&](LogImplementation *implementation) {
Brian Silvermane6335e42014-02-20 20:53:06 -080060 va_list ap1;
61 va_copy(ap1, ap);
62 implementation->DoLog(level, format, ap1);
63 va_end(ap1);
Brian Silvermand6974f42014-02-14 13:39:21 -080064
65 if (level == FATAL) {
66 VDie(format, ap);
67 }
Austin Schuh39700802016-11-26 21:23:56 -080068 };
69 internal::RunWithCurrentImplementation(levels, ::std::ref(log_impl));
Brian Silvermanb0893882014-02-10 14:48:30 -080070}
71
72void VLog(log_level level, const char *format, va_list ap) {
73 LogImplementation::DoVLog(level, format, ap, 1);
74}
75
76void VCork(int line, const char *function, const char *format, va_list ap) {
77 Context *context = Context::Get();
78
79 const size_t message_length = strlen(context->cork_data.message);
80 if (line > context->cork_data.line_max) context->cork_data.line_max = line;
81 if (line < context->cork_data.line_min) context->cork_data.line_min = line;
82
83 if (context->cork_data.function == NULL) {
84 context->cork_data.function = function;
85 } else {
86 if (strcmp(context->cork_data.function, function) != 0) {
87 LOG(FATAL, "started corking data in function %s but then moved to %s\n",
88 context->cork_data.function, function);
89 }
90 }
91
92 internal::ExecuteFormat(context->cork_data.message + message_length,
93 sizeof(context->cork_data.message) - message_length,
94 format, ap);
95}
96
97void VUnCork(int line, const char *function, log_level level, const char *file,
98 const char *format, va_list ap) {
99 Context *context = Context::Get();
100
101 VCork(line, function, format, ap);
102
103 log_do(level, "%s: %d-%d: %s: %s", file,
104 context->cork_data.line_min, context->cork_data.line_max, function,
105 context->cork_data.message);
106
107 context->cork_data.Reset();
108}
109
110} // namespace logging
111} // namespace aos
112
113void log_do(log_level level, const char *format, ...) {
114 va_list ap;
115 va_start(ap, format);
116 aos::logging::VLog(level, format, ap);
117 va_end(ap);
118}
119
120void log_cork(int line, const char *function, const char *format, ...) {
121 va_list ap;
122 va_start(ap, format);
123 aos::logging::VCork(line, function, format, ap);
124 va_end(ap);
125}
126
127void log_uncork(int line, const char *function, log_level level,
128 const char *file, const char *format, ...) {
129 va_list ap;
130 va_start(ap, format);
131 aos::logging::VUnCork(line, function, level, file, format, ap);
132 va_end(ap);
133}