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 | |
| 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 | |
| 12 | namespace aos { |
| 13 | namespace logging { |
| 14 | namespace internal { |
| 15 | |
| 16 | LogImplementation *global_top_implementation(NULL); |
| 17 | |
| 18 | Context::Context() |
| 19 | : implementation(global_top_implementation), |
| 20 | sequence(0) { |
| 21 | cork_data.Reset(); |
| 22 | } |
| 23 | |
| 24 | void 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 | |
| 39 | } // namespace internal |
| 40 | |
| 41 | using internal::Context; |
| 42 | |
| 43 | void LogImplementation::DoVLog(log_level level, const char *format, va_list ap, |
| 44 | int levels) { |
| 45 | Context *context = Context::Get(); |
| 46 | |
| 47 | LogImplementation *top_implementation = context->implementation; |
| 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; |
| 59 | implementation->DoLog(level, format, ap); |
| 60 | context->implementation = top_implementation; |
| 61 | |
| 62 | if (level == FATAL) { |
| 63 | VDie(format, ap); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | void VLog(log_level level, const char *format, va_list ap) { |
| 68 | LogImplementation::DoVLog(level, format, ap, 1); |
| 69 | } |
| 70 | |
| 71 | void VCork(int line, const char *function, const char *format, va_list ap) { |
| 72 | Context *context = Context::Get(); |
| 73 | |
| 74 | const size_t message_length = strlen(context->cork_data.message); |
| 75 | if (line > context->cork_data.line_max) context->cork_data.line_max = line; |
| 76 | if (line < context->cork_data.line_min) context->cork_data.line_min = line; |
| 77 | |
| 78 | if (context->cork_data.function == NULL) { |
| 79 | context->cork_data.function = function; |
| 80 | } else { |
| 81 | if (strcmp(context->cork_data.function, function) != 0) { |
| 82 | LOG(FATAL, "started corking data in function %s but then moved to %s\n", |
| 83 | context->cork_data.function, function); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | internal::ExecuteFormat(context->cork_data.message + message_length, |
| 88 | sizeof(context->cork_data.message) - message_length, |
| 89 | format, ap); |
| 90 | } |
| 91 | |
| 92 | void VUnCork(int line, const char *function, log_level level, const char *file, |
| 93 | const char *format, va_list ap) { |
| 94 | Context *context = Context::Get(); |
| 95 | |
| 96 | VCork(line, function, format, ap); |
| 97 | |
| 98 | log_do(level, "%s: %d-%d: %s: %s", file, |
| 99 | context->cork_data.line_min, context->cork_data.line_max, function, |
| 100 | context->cork_data.message); |
| 101 | |
| 102 | context->cork_data.Reset(); |
| 103 | } |
| 104 | |
| 105 | } // namespace logging |
| 106 | } // namespace aos |
| 107 | |
| 108 | void log_do(log_level level, const char *format, ...) { |
| 109 | va_list ap; |
| 110 | va_start(ap, format); |
| 111 | aos::logging::VLog(level, format, ap); |
| 112 | va_end(ap); |
| 113 | } |
| 114 | |
| 115 | void log_cork(int line, const char *function, const char *format, ...) { |
| 116 | va_list ap; |
| 117 | va_start(ap, format); |
| 118 | aos::logging::VCork(line, function, format, ap); |
| 119 | va_end(ap); |
| 120 | } |
| 121 | |
| 122 | void log_uncork(int line, const char *function, log_level level, |
| 123 | const char *file, const char *format, ...) { |
| 124 | va_list ap; |
| 125 | va_start(ap, format); |
| 126 | aos::logging::VUnCork(line, function, level, file, format, ap); |
| 127 | va_end(ap); |
| 128 | } |