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