John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #include "aos/logging/implementations.h" |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 2 | |
Brian Silverman | b089388 | 2014-02-10 14:48:30 -0800 | [diff] [blame] | 3 | #include <stdarg.h> |
Brian | 4a424a2 | 2014-04-02 11:52:45 -0700 | [diff] [blame] | 4 | #include <inttypes.h> |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 5 | |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 6 | #include <algorithm> |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 7 | #include <chrono> |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 8 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 9 | #include "aos/die.h" |
| 10 | #include "aos/logging/printf_formats.h" |
| 11 | #include "aos/queue_types.h" |
| 12 | #include "aos/time/time.h" |
John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 13 | #include "aos/ipc_lib/queue.h" |
Sabina Davis | 2ed5ea2 | 2017-09-26 22:27:42 -0700 | [diff] [blame] | 14 | #include "aos/once.h" |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 15 | |
| 16 | namespace aos { |
| 17 | namespace logging { |
| 18 | namespace { |
| 19 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 20 | namespace chrono = ::std::chrono; |
| 21 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 22 | // The root LogImplementation. It only logs to stderr/stdout. |
| 23 | // Some of the things specified in the LogImplementation documentation doesn't |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 24 | // apply here (mostly the parts about being able to use AOS_LOG) because this is |
| 25 | // the root one. |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 26 | class RootLogImplementation : public SimpleLogImplementation { |
Brian Silverman | 2e79973 | 2014-06-05 21:50:19 -0700 | [diff] [blame] | 27 | public: |
| 28 | void have_other_implementation() { only_implementation_ = false; } |
| 29 | |
| 30 | private: |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 31 | void set_next(LogImplementation *) override { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 32 | AOS_LOG(FATAL, "can't have a next logger from here\n"); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 33 | } |
| 34 | |
Brian Silverman | f798614 | 2014-04-21 17:42:35 -0700 | [diff] [blame] | 35 | __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 0))) |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 36 | void DoLog(log_level level, const char *format, va_list ap) override { |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 37 | LogMessage message; |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 38 | internal::FillInMessage(level, monotonic_now(), format, ap, &message); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 39 | internal::PrintMessage(stderr, message); |
Brian Silverman | 2e79973 | 2014-06-05 21:50:19 -0700 | [diff] [blame] | 40 | if (!only_implementation_) { |
| 41 | fputs("root logger got used, see stderr for message\n", stdout); |
| 42 | } |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 43 | } |
Brian Silverman | 2e79973 | 2014-06-05 21:50:19 -0700 | [diff] [blame] | 44 | |
| 45 | bool only_implementation_ = true; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 46 | }; |
| 47 | |
Brian Silverman | 2e79973 | 2014-06-05 21:50:19 -0700 | [diff] [blame] | 48 | RootLogImplementation *root_implementation = nullptr; |
| 49 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 50 | void SetGlobalImplementation(LogImplementation *implementation) { |
Brian Silverman | 5c201e2 | 2014-06-12 22:40:28 -0700 | [diff] [blame] | 51 | if (root_implementation == nullptr) { |
| 52 | fputs("Somebody didn't call logging::Init()!\n", stderr); |
| 53 | abort(); |
| 54 | } |
| 55 | |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 56 | internal::Context *context = internal::Context::Get(); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 57 | |
| 58 | context->implementation = implementation; |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 59 | internal::global_top_implementation.store(implementation); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Brian Silverman | e5d6569 | 2013-02-28 15:15:03 -0800 | [diff] [blame] | 62 | void NewContext() { |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 63 | internal::Context::Delete(); |
Brian Silverman | e5d6569 | 2013-02-28 15:15:03 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 66 | void *DoInit() { |
Brian Silverman | 2e79973 | 2014-06-05 21:50:19 -0700 | [diff] [blame] | 67 | SetGlobalImplementation(root_implementation = new RootLogImplementation()); |
Brian Silverman | e5d6569 | 2013-02-28 15:15:03 -0800 | [diff] [blame] | 68 | |
Brian Silverman | e5d6569 | 2013-02-28 15:15:03 -0800 | [diff] [blame] | 69 | if (pthread_atfork(NULL /*prepare*/, NULL /*parent*/, |
| 70 | NewContext /*child*/) != 0) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 71 | AOS_LOG(FATAL, "pthread_atfork(NULL, NULL, %p) failed\n", NewContext); |
Brian Silverman | e5d6569 | 2013-02-28 15:15:03 -0800 | [diff] [blame] | 72 | } |
Brian Silverman | e5d6569 | 2013-02-28 15:15:03 -0800 | [diff] [blame] | 73 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 74 | return NULL; |
| 75 | } |
| 76 | |
| 77 | } // namespace |
| 78 | namespace internal { |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 79 | namespace { |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 80 | |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 81 | void FillInMessageBase(log_level level, |
| 82 | monotonic_clock::time_point monotonic_now, |
| 83 | LogMessage *message) { |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 84 | Context *context = Context::Get(); |
| 85 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 86 | message->level = level; |
| 87 | message->source = context->source; |
Austin Schuh | aebbc34 | 2015-01-25 02:25:13 -0800 | [diff] [blame] | 88 | memcpy(message->name, context->name, context->name_size); |
| 89 | message->name_length = context->name_size; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 90 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 91 | message->seconds = |
| 92 | chrono::duration_cast<chrono::seconds>(monotonic_now.time_since_epoch()) |
| 93 | .count(); |
| 94 | message->nseconds = |
| 95 | chrono::duration_cast<chrono::nanoseconds>( |
| 96 | monotonic_now.time_since_epoch() - chrono::seconds(message->seconds)) |
| 97 | .count(); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 98 | |
| 99 | message->sequence = context->sequence++; |
| 100 | } |
| 101 | |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 102 | } // namespace |
| 103 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 104 | void FillInMessageStructure(bool add_to_type_cache, log_level level, |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 105 | monotonic_clock::time_point monotonic_now, |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 106 | const ::std::string &message_string, size_t size, |
| 107 | const MessageType *type, |
| 108 | const ::std::function<size_t(char *)> &serialize, |
| 109 | LogMessage *message) { |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 110 | if (add_to_type_cache) { |
| 111 | type_cache::AddShm(type->id); |
| 112 | } |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 113 | message->structure.type_id = type->id; |
| 114 | |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 115 | FillInMessageBase(level, monotonic_now, message); |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 116 | |
| 117 | if (message_string.size() + size > sizeof(message->structure.serialized)) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 118 | AOS_LOG( |
| 119 | FATAL, |
Austin Schuh | c804fd1 | 2019-03-22 21:15:37 -0700 | [diff] [blame] | 120 | "serialized struct %s (size %zd + %zd > %zd) and message %s too big\n", |
| 121 | type->name.c_str(), message_string.size(), size, |
| 122 | sizeof(message->structure.serialized), message_string.c_str()); |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 123 | } |
| 124 | message->structure.string_length = message_string.size(); |
| 125 | memcpy(message->structure.serialized, message_string.data(), |
| 126 | message->structure.string_length); |
| 127 | |
| 128 | message->message_length = serialize( |
| 129 | &message->structure.serialized[message->structure.string_length]); |
| 130 | message->type = LogMessage::Type::kStruct; |
| 131 | } |
| 132 | |
Brian Silverman | 664db1a | 2014-03-20 17:06:29 -0700 | [diff] [blame] | 133 | void FillInMessageMatrix(log_level level, |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 134 | monotonic_clock::time_point monotonic_now, |
Brian Silverman | 664db1a | 2014-03-20 17:06:29 -0700 | [diff] [blame] | 135 | const ::std::string &message_string, uint32_t type_id, |
| 136 | int rows, int cols, const void *data, |
| 137 | LogMessage *message) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 138 | AOS_CHECK(MessageType::IsPrimitive(type_id)); |
Brian Silverman | 664db1a | 2014-03-20 17:06:29 -0700 | [diff] [blame] | 139 | message->matrix.type = type_id; |
| 140 | |
| 141 | const auto element_size = MessageType::Sizeof(type_id); |
| 142 | |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 143 | FillInMessageBase(level, monotonic_now, message); |
Brian Silverman | 664db1a | 2014-03-20 17:06:29 -0700 | [diff] [blame] | 144 | |
| 145 | message->message_length = rows * cols * element_size; |
| 146 | if (message_string.size() + message->message_length > |
| 147 | sizeof(message->matrix.data)) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 148 | AOS_LOG(FATAL, |
| 149 | "%dx%d matrix of type %" PRIu32 |
| 150 | " (size %u) and message %s is too big\n", |
| 151 | rows, cols, type_id, element_size, message_string.c_str()); |
Brian Silverman | 664db1a | 2014-03-20 17:06:29 -0700 | [diff] [blame] | 152 | } |
| 153 | message->matrix.string_length = message_string.size(); |
| 154 | memcpy(message->matrix.data, message_string.data(), |
| 155 | message->matrix.string_length); |
| 156 | |
| 157 | message->matrix.rows = rows; |
| 158 | message->matrix.cols = cols; |
| 159 | SerializeMatrix(type_id, &message->matrix.data[message->matrix.string_length], |
| 160 | data, rows, cols); |
| 161 | message->type = LogMessage::Type::kMatrix; |
| 162 | } |
| 163 | |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 164 | void FillInMessage(log_level level, monotonic_clock::time_point monotonic_now, |
| 165 | const char *format, va_list ap, LogMessage *message) { |
| 166 | FillInMessageBase(level, monotonic_now, message); |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 167 | |
| 168 | message->message_length = |
| 169 | ExecuteFormat(message->message, sizeof(message->message), format, ap); |
| 170 | message->type = LogMessage::Type::kString; |
| 171 | } |
| 172 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 173 | void PrintMessage(FILE *output, const LogMessage &message) { |
Brian Silverman | a7234c6 | 2014-03-24 20:23:25 -0700 | [diff] [blame] | 174 | #define BASE_ARGS \ |
| 175 | AOS_LOGGING_BASE_ARGS( \ |
| 176 | message.name_length, message.name, static_cast<int32_t>(message.source), \ |
| 177 | message.sequence, message.level, message.seconds, message.nseconds) |
| 178 | switch (message.type) { |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 179 | case LogMessage::Type::kString: |
Brian Silverman | a7234c6 | 2014-03-24 20:23:25 -0700 | [diff] [blame] | 180 | fprintf(output, AOS_LOGGING_BASE_FORMAT "%.*s", BASE_ARGS, |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 181 | static_cast<int>(message.message_length), message.message); |
| 182 | break; |
Brian Silverman | ff12c9f | 2014-03-19 17:53:29 -0700 | [diff] [blame] | 183 | case LogMessage::Type::kStruct: { |
Austin Schuh | eee8c96 | 2017-04-12 22:32:44 -0700 | [diff] [blame] | 184 | char buffer[4096]; |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 185 | size_t output_length = sizeof(buffer); |
| 186 | size_t input_length = message.message_length; |
| 187 | if (!PrintMessage( |
| 188 | buffer, &output_length, |
| 189 | message.structure.serialized + message.structure.string_length, |
| 190 | &input_length, type_cache::Get(message.structure.type_id))) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 191 | AOS_LOG( |
| 192 | FATAL, |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 193 | "printing message (%.*s) of type %s into %zu-byte buffer failed\n", |
| 194 | static_cast<int>(message.message_length), message.message, |
| 195 | type_cache::Get(message.structure.type_id).name.c_str(), |
| 196 | sizeof(buffer)); |
| 197 | } |
| 198 | if (input_length > 0) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 199 | AOS_LOG(WARNING, "%zu extra bytes on message of type %s\n", |
| 200 | input_length, |
| 201 | type_cache::Get(message.structure.type_id).name.c_str()); |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 202 | } |
Brian Silverman | a7234c6 | 2014-03-24 20:23:25 -0700 | [diff] [blame] | 203 | fprintf(output, AOS_LOGGING_BASE_FORMAT "%.*s: %.*s\n", BASE_ARGS, |
Brian Silverman | b263d30 | 2014-02-16 00:01:43 -0800 | [diff] [blame] | 204 | static_cast<int>(message.structure.string_length), |
| 205 | message.structure.serialized, |
| 206 | static_cast<int>(sizeof(buffer) - output_length), buffer); |
Brian Silverman | ff12c9f | 2014-03-19 17:53:29 -0700 | [diff] [blame] | 207 | } break; |
| 208 | case LogMessage::Type::kMatrix: { |
| 209 | char buffer[1024]; |
| 210 | size_t output_length = sizeof(buffer); |
| 211 | if (message.message_length != |
| 212 | static_cast<size_t>(message.matrix.rows * message.matrix.cols * |
| 213 | MessageType::Sizeof(message.matrix.type))) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 214 | AOS_LOG(FATAL, "expected %d bytes of matrix data but have %zu\n", |
| 215 | message.matrix.rows * message.matrix.cols * |
| 216 | MessageType::Sizeof(message.matrix.type), |
| 217 | message.message_length); |
Brian Silverman | ff12c9f | 2014-03-19 17:53:29 -0700 | [diff] [blame] | 218 | } |
| 219 | if (!PrintMatrix(buffer, &output_length, |
| 220 | message.matrix.data + message.matrix.string_length, |
| 221 | message.matrix.type, message.matrix.rows, |
| 222 | message.matrix.cols)) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 223 | AOS_LOG(FATAL, "printing %dx%d matrix of type %" PRIu32 " failed\n", |
| 224 | message.matrix.rows, message.matrix.cols, message.matrix.type); |
Brian Silverman | ff12c9f | 2014-03-19 17:53:29 -0700 | [diff] [blame] | 225 | } |
Brian Silverman | a7234c6 | 2014-03-24 20:23:25 -0700 | [diff] [blame] | 226 | fprintf(output, AOS_LOGGING_BASE_FORMAT "%.*s: %.*s\n", BASE_ARGS, |
Brian Silverman | ff12c9f | 2014-03-19 17:53:29 -0700 | [diff] [blame] | 227 | static_cast<int>(message.matrix.string_length), |
Brian Silverman | 664db1a | 2014-03-20 17:06:29 -0700 | [diff] [blame] | 228 | message.matrix.data, |
Brian Silverman | ff12c9f | 2014-03-19 17:53:29 -0700 | [diff] [blame] | 229 | static_cast<int>(sizeof(buffer) - output_length), buffer); |
| 230 | } break; |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 231 | } |
Brian Silverman | c1a244e | 2014-02-20 14:12:39 -0800 | [diff] [blame] | 232 | #undef BASE_ARGS |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | } // namespace internal |
| 236 | |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 237 | void SimpleLogImplementation::LogStruct( |
Brian Silverman | 669669f | 2014-02-14 16:32:56 -0800 | [diff] [blame] | 238 | log_level level, const ::std::string &message, size_t size, |
| 239 | const MessageType *type, const ::std::function<size_t(char *)> &serialize) { |
| 240 | char serialized[1024]; |
| 241 | if (size > sizeof(serialized)) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 242 | AOS_LOG(FATAL, "structure of type %s too big to serialize\n", |
| 243 | type->name.c_str()); |
Brian Silverman | 669669f | 2014-02-14 16:32:56 -0800 | [diff] [blame] | 244 | } |
| 245 | size_t used = serialize(serialized); |
Brian Silverman | 2508c08 | 2014-02-17 15:45:02 -0800 | [diff] [blame] | 246 | char printed[1024]; |
Brian Silverman | 669669f | 2014-02-14 16:32:56 -0800 | [diff] [blame] | 247 | size_t printed_bytes = sizeof(printed); |
| 248 | if (!PrintMessage(printed, &printed_bytes, serialized, &used, *type)) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 249 | AOS_LOG(FATAL, |
| 250 | "PrintMessage(%p, %p(=%zd), %p, %p(=%zd), %p(name=%s)) failed\n", |
| 251 | printed, &printed_bytes, printed_bytes, serialized, &used, used, |
| 252 | type, type->name.c_str()); |
Brian Silverman | 669669f | 2014-02-14 16:32:56 -0800 | [diff] [blame] | 253 | } |
| 254 | DoLogVariadic(level, "%.*s: %.*s\n", static_cast<int>(message.size()), |
Brian Silverman | 1c7b819 | 2014-02-16 22:37:36 -0800 | [diff] [blame] | 255 | message.data(), |
| 256 | static_cast<int>(sizeof(printed) - printed_bytes), printed); |
Brian Silverman | 669669f | 2014-02-14 16:32:56 -0800 | [diff] [blame] | 257 | } |
| 258 | |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 259 | void SimpleLogImplementation::LogMatrix( |
Brian Silverman | ff12c9f | 2014-03-19 17:53:29 -0700 | [diff] [blame] | 260 | log_level level, const ::std::string &message, uint32_t type_id, |
| 261 | int rows, int cols, const void *data) { |
| 262 | char serialized[1024]; |
| 263 | if (static_cast<size_t>(rows * cols * MessageType::Sizeof(type_id)) > |
| 264 | sizeof(serialized)) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 265 | AOS_LOG(FATAL, "matrix of size %u too big to serialize\n", |
| 266 | rows * cols * MessageType::Sizeof(type_id)); |
Brian Silverman | ff12c9f | 2014-03-19 17:53:29 -0700 | [diff] [blame] | 267 | } |
| 268 | SerializeMatrix(type_id, serialized, data, rows, cols); |
| 269 | char printed[1024]; |
| 270 | size_t printed_bytes = sizeof(printed); |
| 271 | if (!PrintMatrix(printed, &printed_bytes, serialized, type_id, rows, cols)) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 272 | AOS_LOG(FATAL, |
| 273 | "PrintMatrix(%p, %p(=%zd), %p, %" PRIu32 ", %d, %d) failed\n", |
| 274 | printed, &printed_bytes, printed_bytes, serialized, type_id, rows, |
| 275 | cols); |
Brian Silverman | ff12c9f | 2014-03-19 17:53:29 -0700 | [diff] [blame] | 276 | } |
| 277 | DoLogVariadic(level, "%.*s: %.*s\n", static_cast<int>(message.size()), |
| 278 | message.data(), |
| 279 | static_cast<int>(sizeof(printed) - printed_bytes), printed); |
| 280 | } |
| 281 | |
Brian Silverman | be858a1 | 2014-04-30 17:37:28 -0700 | [diff] [blame] | 282 | void HandleMessageLogImplementation::DoLog(log_level level, const char *format, |
| 283 | va_list ap) { |
| 284 | LogMessage message; |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 285 | internal::FillInMessage(level, monotonic_now(), format, ap, &message); |
Brian Silverman | be858a1 | 2014-04-30 17:37:28 -0700 | [diff] [blame] | 286 | HandleMessage(message); |
| 287 | } |
| 288 | |
| 289 | void HandleMessageLogImplementation::LogStruct( |
| 290 | log_level level, const ::std::string &message_string, size_t size, |
| 291 | const MessageType *type, const ::std::function<size_t(char *)> &serialize) { |
| 292 | LogMessage message; |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 293 | internal::FillInMessageStructure(fill_type_cache(), level, monotonic_now(), |
| 294 | message_string, size, type, serialize, |
| 295 | &message); |
Brian Silverman | be858a1 | 2014-04-30 17:37:28 -0700 | [diff] [blame] | 296 | HandleMessage(message); |
| 297 | } |
| 298 | |
| 299 | void HandleMessageLogImplementation::LogMatrix( |
| 300 | log_level level, const ::std::string &message_string, uint32_t type_id, |
| 301 | int rows, int cols, const void *data) { |
| 302 | LogMessage message; |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 303 | internal::FillInMessageMatrix(level, monotonic_now(), message_string, type_id, |
| 304 | rows, cols, data, &message); |
Brian Silverman | be858a1 | 2014-04-30 17:37:28 -0700 | [diff] [blame] | 305 | HandleMessage(message); |
| 306 | } |
| 307 | |
Brian Silverman | 1e8ddfe | 2013-12-19 16:20:53 -0800 | [diff] [blame] | 308 | StreamLogImplementation::StreamLogImplementation(FILE *stream) |
| 309 | : stream_(stream) {} |
| 310 | |
Brian Silverman | be858a1 | 2014-04-30 17:37:28 -0700 | [diff] [blame] | 311 | void StreamLogImplementation::HandleMessage(const LogMessage &message) { |
Brian Silverman | 1e8ddfe | 2013-12-19 16:20:53 -0800 | [diff] [blame] | 312 | internal::PrintMessage(stream_, message); |
| 313 | } |
| 314 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 315 | void AddImplementation(LogImplementation *implementation) { |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 316 | internal::Context *context = internal::Context::Get(); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 317 | |
| 318 | if (implementation->next() != NULL) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 319 | AOS_LOG(FATAL, |
| 320 | "%p already has a next implementation, but it's not" |
| 321 | " being used yet\n", |
| 322 | implementation); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | LogImplementation *old = context->implementation; |
| 326 | if (old != NULL) { |
| 327 | implementation->set_next(old); |
| 328 | } |
| 329 | SetGlobalImplementation(implementation); |
Brian Silverman | 2e79973 | 2014-06-05 21:50:19 -0700 | [diff] [blame] | 330 | root_implementation->have_other_implementation(); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | void Init() { |
| 334 | static Once<void> once(DoInit); |
| 335 | once.Get(); |
| 336 | } |
| 337 | |
| 338 | void Load() { |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 339 | internal::Context::Get(); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | void Cleanup() { |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 343 | internal::Context::Delete(); |
| 344 | } |
| 345 | |
| 346 | namespace { |
| 347 | |
| 348 | RawQueue *queue = NULL; |
| 349 | |
| 350 | int dropped_messages = 0; |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 351 | monotonic_clock::time_point dropped_start, backoff_start; |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 352 | // Wait this long after dropping a message before even trying to write any more. |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 353 | constexpr chrono::milliseconds kDropBackoff = chrono::milliseconds(100); |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 354 | |
| 355 | LogMessage *GetMessageOrDie() { |
| 356 | LogMessage *message = static_cast<LogMessage *>(queue->GetMessage()); |
| 357 | if (message == NULL) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 358 | AOS_LOG(FATAL, "%p->GetMessage() failed\n", queue); |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 359 | } else { |
| 360 | return message; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | void Write(LogMessage *msg) { |
| 365 | if (__builtin_expect(dropped_messages > 0, false)) { |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 366 | monotonic_clock::time_point message_time( |
| 367 | chrono::seconds(msg->seconds) + chrono::nanoseconds(msg->nseconds)); |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 368 | if (message_time - backoff_start < kDropBackoff) { |
| 369 | ++dropped_messages; |
| 370 | queue->FreeMessage(msg); |
| 371 | return; |
| 372 | } |
| 373 | |
| 374 | LogMessage *dropped_message = GetMessageOrDie(); |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 375 | chrono::seconds dropped_start_sec = chrono::duration_cast<chrono::seconds>( |
| 376 | dropped_start.time_since_epoch()); |
| 377 | chrono::nanoseconds dropped_start_nsec = |
| 378 | chrono::duration_cast<chrono::nanoseconds>( |
| 379 | dropped_start.time_since_epoch() - dropped_start_sec); |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 380 | internal::FillInMessageVarargs( |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 381 | ERROR, message_time, dropped_message, |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 382 | "%d logs starting at %" PRId32 ".%" PRId32 " dropped\n", |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 383 | dropped_messages, static_cast<int32_t>(dropped_start_sec.count()), |
| 384 | static_cast<int32_t>(dropped_start_nsec.count())); |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 385 | if (queue->WriteMessage(dropped_message, RawQueue::kNonBlock)) { |
| 386 | dropped_messages = 0; |
| 387 | } else { |
| 388 | // Don't even bother trying to write this message because it's not likely |
| 389 | // to work and it would be confusing to have one log in the middle of a |
| 390 | // string of failures get through. |
| 391 | ++dropped_messages; |
| 392 | backoff_start = message_time; |
| 393 | queue->FreeMessage(msg); |
| 394 | return; |
| 395 | } |
| 396 | } |
| 397 | if (!queue->WriteMessage(msg, RawQueue::kNonBlock)) { |
| 398 | if (dropped_messages == 0) { |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 399 | monotonic_clock::time_point message_time( |
| 400 | chrono::seconds(msg->seconds) + chrono::nanoseconds(msg->nseconds)); |
| 401 | dropped_start = backoff_start = message_time; |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 402 | } |
| 403 | ++dropped_messages; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | class LinuxQueueLogImplementation : public LogImplementation { |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 408 | virtual ::aos::monotonic_clock::time_point monotonic_now() const { |
| 409 | return ::aos::monotonic_clock::now(); |
| 410 | } |
| 411 | |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 412 | __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 0))) |
| 413 | void DoLog(log_level level, const char *format, va_list ap) override { |
| 414 | LogMessage *message = GetMessageOrDie(); |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 415 | internal::FillInMessage(level, monotonic_now(), format, ap, message); |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 416 | Write(message); |
| 417 | } |
| 418 | |
| 419 | void LogStruct(log_level level, const ::std::string &message_string, |
| 420 | size_t size, const MessageType *type, |
| 421 | const ::std::function<size_t(char *)> &serialize) override { |
| 422 | LogMessage *message = GetMessageOrDie(); |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 423 | internal::FillInMessageStructure(fill_type_cache(), level, monotonic_now(), |
| 424 | message_string, size, type, serialize, |
| 425 | message); |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 426 | Write(message); |
| 427 | } |
| 428 | |
| 429 | void LogMatrix(log_level level, const ::std::string &message_string, |
| 430 | uint32_t type_id, int rows, int cols, |
| 431 | const void *data) override { |
| 432 | LogMessage *message = GetMessageOrDie(); |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 433 | internal::FillInMessageMatrix(level, monotonic_now(), message_string, |
| 434 | type_id, rows, cols, data, message); |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 435 | Write(message); |
| 436 | } |
| 437 | }; |
| 438 | |
| 439 | } // namespace |
| 440 | |
| 441 | RawQueue *GetLoggingQueue() { |
| 442 | return RawQueue::Fetch("LoggingQueue", sizeof(LogMessage), 1323, 40000); |
| 443 | } |
| 444 | |
| 445 | void RegisterQueueImplementation() { |
| 446 | Init(); |
| 447 | |
| 448 | queue = GetLoggingQueue(); |
| 449 | if (queue == NULL) { |
| 450 | Die("logging: couldn't fetch queue\n"); |
| 451 | } |
| 452 | |
| 453 | AddImplementation(new LinuxQueueLogImplementation()); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | } // namespace logging |
| 457 | } // namespace aos |