blob: e6e92035531bf30031a701dc022aafc71caf88ae [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
Austin Schuh044e18b2015-10-21 20:17:09 -07007#include <functional>
Tyler Chatow4b471e12020-01-05 20:19:36 -08008#include <type_traits>
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);
Alex Perrycb7da4b2019-08-28 19:35:56 -070022 typedef ::std::common_type<int, size_t>::type RetType;
Brian Silvermanb0893882014-02-10 14:48:30 -080023 if (ret < 0) {
Tyler Chatow4b471e12020-01-05 20:19:36 -080024 AOS_PLOG(FATAL, "vsnprintf(%p, %zd, %s, args) failed", output, size,
25 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(
Tyler Chatow4b471e12020-01-05 20:19:36 -080035 ::std::function<void(LogImplementation *)> function) {
Brian Silvermanb0893882014-02-10 14:48:30 -080036 Context *context = Context::Get();
37
Tyler Chatow4b471e12020-01-05 20:19:36 -080038 LogImplementation *const implementation = context->implementation;
39 if (implementation == NULL) {
40 Die("no logging implementation to use\n");
Brian Silvermanb0893882014-02-10 14:48:30 -080041 }
Brian Silvermand6974f42014-02-14 13:39:21 -080042 function(implementation);
Brian Silvermand6974f42014-02-14 13:39:21 -080043}
Brian Silvermanb0893882014-02-10 14:48:30 -080044
Brian Silvermand6974f42014-02-14 13:39:21 -080045} // namespace internal
46
47using internal::Context;
48
Tyler Chatow4b471e12020-01-05 20:19:36 -080049void LogImplementation::DoVLog(log_level level, const char *format,
50 va_list ap) {
Austin Schuh39700802016-11-26 21:23:56 -080051 auto log_impl = [&](LogImplementation *implementation) {
Brian Silvermane6335e42014-02-20 20:53:06 -080052 va_list ap1;
53 va_copy(ap1, ap);
54 implementation->DoLog(level, format, ap1);
55 va_end(ap1);
Brian Silvermand6974f42014-02-14 13:39:21 -080056
57 if (level == FATAL) {
58 VDie(format, ap);
59 }
Austin Schuh39700802016-11-26 21:23:56 -080060 };
Tyler Chatow4b471e12020-01-05 20:19:36 -080061 internal::RunWithCurrentImplementation(::std::ref(log_impl));
Brian Silvermanb0893882014-02-10 14:48:30 -080062}
63
64void VLog(log_level level, const char *format, va_list ap) {
Tyler Chatow4b471e12020-01-05 20:19:36 -080065 LogImplementation::DoVLog(level, format, ap);
Brian Silvermanb0893882014-02-10 14:48:30 -080066}
67
68void VCork(int line, const char *function, const char *format, va_list ap) {
69 Context *context = Context::Get();
70
71 const size_t message_length = strlen(context->cork_data.message);
72 if (line > context->cork_data.line_max) context->cork_data.line_max = line;
73 if (line < context->cork_data.line_min) context->cork_data.line_min = line;
74
75 if (context->cork_data.function == NULL) {
76 context->cork_data.function = function;
77 } else {
78 if (strcmp(context->cork_data.function, function) != 0) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070079 AOS_LOG(FATAL,
80 "started corking data in function %s but then moved to %s\n",
81 context->cork_data.function, function);
Brian Silvermanb0893882014-02-10 14:48:30 -080082 }
83 }
84
85 internal::ExecuteFormat(context->cork_data.message + message_length,
86 sizeof(context->cork_data.message) - message_length,
87 format, ap);
88}
89
90void VUnCork(int line, const char *function, log_level level, const char *file,
91 const char *format, va_list ap) {
92 Context *context = Context::Get();
93
94 VCork(line, function, format, ap);
95
Tyler Chatow4b471e12020-01-05 20:19:36 -080096 log_do(level, "%s: %d-%d: %s: %s", file, context->cork_data.line_min,
97 context->cork_data.line_max, function, context->cork_data.message);
Brian Silvermanb0893882014-02-10 14:48:30 -080098
99 context->cork_data.Reset();
100}
101
102} // namespace logging
103} // namespace aos
104
105void log_do(log_level level, const char *format, ...) {
106 va_list ap;
107 va_start(ap, format);
108 aos::logging::VLog(level, format, ap);
109 va_end(ap);
110}
111
112void log_cork(int line, const char *function, const char *format, ...) {
113 va_list ap;
114 va_start(ap, format);
115 aos::logging::VCork(line, function, format, ap);
116 va_end(ap);
117}
118
119void log_uncork(int line, const char *function, log_level level,
120 const char *file, const char *format, ...) {
121 va_list ap;
122 va_start(ap, format);
123 aos::logging::VUnCork(line, function, level, file, format, ap);
124 va_end(ap);
125}