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