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