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 | 4a424a2 | 2014-04-02 11:52:45 -0700 | [diff] [blame] | 3 | #include <inttypes.h> |
Tyler Chatow | 5febd8f | 2020-01-05 18:25:31 -0800 | [diff] [blame] | 4 | #include <stdarg.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 | |
Tyler Chatow | 5febd8f | 2020-01-05 18:25:31 -0800 | [diff] [blame] | 9 | #include "absl/base/call_once.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 10 | #include "aos/die.h" |
Tyler Chatow | 5febd8f | 2020-01-05 18:25:31 -0800 | [diff] [blame] | 11 | #include "aos/ipc_lib/queue.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 12 | #include "aos/logging/printf_formats.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 13 | #include "aos/time/time.h" |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 14 | |
| 15 | namespace aos { |
| 16 | namespace logging { |
| 17 | namespace { |
| 18 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 19 | namespace chrono = ::std::chrono; |
| 20 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 21 | // The root LogImplementation. It only logs to stderr/stdout. |
| 22 | // Some of the things specified in the LogImplementation documentation doesn't |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 23 | // apply here (mostly the parts about being able to use AOS_LOG) because this is |
| 24 | // the root one. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 25 | class RootLogImplementation : public LogImplementation { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 26 | protected: |
| 27 | virtual ::aos::monotonic_clock::time_point monotonic_now() const { |
| 28 | return ::aos::monotonic_clock::now(); |
| 29 | } |
| 30 | |
Brian Silverman | 2e79973 | 2014-06-05 21:50:19 -0700 | [diff] [blame] | 31 | private: |
Tyler Chatow | 5febd8f | 2020-01-05 18:25:31 -0800 | [diff] [blame] | 32 | __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 0))) void DoLog( |
| 33 | log_level level, const char *format, va_list ap) override { |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 34 | LogMessage message; |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 35 | internal::FillInMessage(level, monotonic_now(), format, ap, &message); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 36 | internal::PrintMessage(stderr, message); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 37 | } |
| 38 | }; |
| 39 | |
Brian Silverman | 2e79973 | 2014-06-05 21:50:19 -0700 | [diff] [blame] | 40 | RootLogImplementation *root_implementation = nullptr; |
| 41 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 42 | void SetGlobalImplementation(LogImplementation *implementation) { |
Brian Silverman | 5c201e2 | 2014-06-12 22:40:28 -0700 | [diff] [blame] | 43 | if (root_implementation == nullptr) { |
| 44 | fputs("Somebody didn't call logging::Init()!\n", stderr); |
| 45 | abort(); |
| 46 | } |
| 47 | |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 48 | internal::Context *context = internal::Context::Get(); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 49 | |
| 50 | context->implementation = implementation; |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 51 | internal::global_top_implementation.store(implementation); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Tyler Chatow | 5febd8f | 2020-01-05 18:25:31 -0800 | [diff] [blame] | 54 | void NewContext() { internal::Context::Delete(); } |
Brian Silverman | e5d6569 | 2013-02-28 15:15:03 -0800 | [diff] [blame] | 55 | |
John Park | 06da045 | 2019-12-02 22:19:03 -0800 | [diff] [blame] | 56 | void DoInit() { |
Brian Silverman | 2e79973 | 2014-06-05 21:50:19 -0700 | [diff] [blame] | 57 | SetGlobalImplementation(root_implementation = new RootLogImplementation()); |
Brian Silverman | e5d6569 | 2013-02-28 15:15:03 -0800 | [diff] [blame] | 58 | |
Tyler Chatow | 5febd8f | 2020-01-05 18:25:31 -0800 | [diff] [blame] | 59 | if (pthread_atfork(NULL /*prepare*/, NULL /*parent*/, NewContext /*child*/) != |
| 60 | 0) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 61 | AOS_LOG(FATAL, "pthread_atfork(NULL, NULL, %p) failed\n", NewContext); |
Brian Silverman | e5d6569 | 2013-02-28 15:15:03 -0800 | [diff] [blame] | 62 | } |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | } // namespace |
| 66 | namespace internal { |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 67 | namespace { |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 68 | |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 69 | void FillInMessageBase(log_level level, |
| 70 | monotonic_clock::time_point monotonic_now, |
| 71 | LogMessage *message) { |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 72 | Context *context = Context::Get(); |
| 73 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 74 | message->level = level; |
| 75 | message->source = context->source; |
Austin Schuh | aebbc34 | 2015-01-25 02:25:13 -0800 | [diff] [blame] | 76 | memcpy(message->name, context->name, context->name_size); |
| 77 | message->name_length = context->name_size; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 78 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 79 | message->seconds = |
| 80 | chrono::duration_cast<chrono::seconds>(monotonic_now.time_since_epoch()) |
| 81 | .count(); |
| 82 | message->nseconds = |
| 83 | chrono::duration_cast<chrono::nanoseconds>( |
| 84 | monotonic_now.time_since_epoch() - chrono::seconds(message->seconds)) |
| 85 | .count(); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 86 | |
| 87 | message->sequence = context->sequence++; |
| 88 | } |
| 89 | |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 90 | } // namespace |
| 91 | |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 92 | void FillInMessage(log_level level, monotonic_clock::time_point monotonic_now, |
| 93 | const char *format, va_list ap, LogMessage *message) { |
| 94 | FillInMessageBase(level, monotonic_now, message); |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 95 | |
| 96 | message->message_length = |
| 97 | ExecuteFormat(message->message, sizeof(message->message), format, ap); |
| 98 | message->type = LogMessage::Type::kString; |
| 99 | } |
| 100 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 101 | void PrintMessage(FILE *output, const LogMessage &message) { |
Brian Silverman | a7234c6 | 2014-03-24 20:23:25 -0700 | [diff] [blame] | 102 | #define BASE_ARGS \ |
| 103 | AOS_LOGGING_BASE_ARGS( \ |
| 104 | message.name_length, message.name, static_cast<int32_t>(message.source), \ |
| 105 | message.sequence, message.level, message.seconds, message.nseconds) |
| 106 | switch (message.type) { |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 107 | case LogMessage::Type::kString: |
Brian Silverman | a7234c6 | 2014-03-24 20:23:25 -0700 | [diff] [blame] | 108 | fprintf(output, AOS_LOGGING_BASE_FORMAT "%.*s", BASE_ARGS, |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 109 | static_cast<int>(message.message_length), message.message); |
| 110 | break; |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 111 | } |
Brian Silverman | c1a244e | 2014-02-20 14:12:39 -0800 | [diff] [blame] | 112 | #undef BASE_ARGS |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | } // namespace internal |
| 116 | |
Brian Silverman | be858a1 | 2014-04-30 17:37:28 -0700 | [diff] [blame] | 117 | void HandleMessageLogImplementation::DoLog(log_level level, const char *format, |
| 118 | va_list ap) { |
| 119 | LogMessage message; |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 120 | internal::FillInMessage(level, monotonic_now(), format, ap, &message); |
Brian Silverman | be858a1 | 2014-04-30 17:37:28 -0700 | [diff] [blame] | 121 | HandleMessage(message); |
| 122 | } |
| 123 | |
Brian Silverman | 1e8ddfe | 2013-12-19 16:20:53 -0800 | [diff] [blame] | 124 | StreamLogImplementation::StreamLogImplementation(FILE *stream) |
| 125 | : stream_(stream) {} |
| 126 | |
Brian Silverman | be858a1 | 2014-04-30 17:37:28 -0700 | [diff] [blame] | 127 | void StreamLogImplementation::HandleMessage(const LogMessage &message) { |
Brian Silverman | 1e8ddfe | 2013-12-19 16:20:53 -0800 | [diff] [blame] | 128 | internal::PrintMessage(stream_, message); |
| 129 | } |
| 130 | |
Tyler Chatow | 4b471e1 | 2020-01-05 20:19:36 -0800 | [diff] [blame] | 131 | void SetImplementation(LogImplementation *implementation, bool update_global) { |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 132 | internal::Context *context = internal::Context::Get(); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 133 | |
Tyler Chatow | 4b471e1 | 2020-01-05 20:19:36 -0800 | [diff] [blame] | 134 | context->implementation = implementation; |
| 135 | if (update_global) { |
| 136 | SetGlobalImplementation(implementation); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 137 | } |
Tyler Chatow | 4b471e1 | 2020-01-05 20:19:36 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | LogImplementation *SwapImplementation(LogImplementation *implementation) { |
| 141 | internal::Context *context = internal::Context::Get(); |
| 142 | |
| 143 | LogImplementation *old = context->implementation; |
| 144 | context->implementation = implementation; |
| 145 | |
| 146 | return old; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame^] | 149 | LogImplementation *GetImplementation() { |
| 150 | return internal::Context::Get()->implementation; |
| 151 | } |
| 152 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 153 | void Init() { |
John Park | 06da045 | 2019-12-02 22:19:03 -0800 | [diff] [blame] | 154 | static absl::once_flag once; |
| 155 | absl::call_once(once, DoInit); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Tyler Chatow | 5febd8f | 2020-01-05 18:25:31 -0800 | [diff] [blame] | 158 | void Load() { internal::Context::Get(); } |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 159 | |
Tyler Chatow | 5febd8f | 2020-01-05 18:25:31 -0800 | [diff] [blame] | 160 | void Cleanup() { internal::Context::Delete(); } |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 161 | |
| 162 | namespace { |
| 163 | |
| 164 | RawQueue *queue = NULL; |
| 165 | |
| 166 | int dropped_messages = 0; |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 167 | monotonic_clock::time_point dropped_start, backoff_start; |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 168 | // 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] | 169 | constexpr chrono::milliseconds kDropBackoff = chrono::milliseconds(100); |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 170 | |
| 171 | LogMessage *GetMessageOrDie() { |
| 172 | LogMessage *message = static_cast<LogMessage *>(queue->GetMessage()); |
| 173 | if (message == NULL) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 174 | AOS_LOG(FATAL, "%p->GetMessage() failed\n", queue); |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 175 | } else { |
| 176 | return message; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | void Write(LogMessage *msg) { |
| 181 | if (__builtin_expect(dropped_messages > 0, false)) { |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 182 | monotonic_clock::time_point message_time( |
| 183 | chrono::seconds(msg->seconds) + chrono::nanoseconds(msg->nseconds)); |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 184 | if (message_time - backoff_start < kDropBackoff) { |
| 185 | ++dropped_messages; |
| 186 | queue->FreeMessage(msg); |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | LogMessage *dropped_message = GetMessageOrDie(); |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 191 | chrono::seconds dropped_start_sec = chrono::duration_cast<chrono::seconds>( |
| 192 | dropped_start.time_since_epoch()); |
| 193 | chrono::nanoseconds dropped_start_nsec = |
| 194 | chrono::duration_cast<chrono::nanoseconds>( |
| 195 | dropped_start.time_since_epoch() - dropped_start_sec); |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 196 | internal::FillInMessageVarargs( |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 197 | ERROR, message_time, dropped_message, |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 198 | "%d logs starting at %" PRId32 ".%" PRId32 " dropped\n", |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 199 | dropped_messages, static_cast<int32_t>(dropped_start_sec.count()), |
| 200 | static_cast<int32_t>(dropped_start_nsec.count())); |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 201 | if (queue->WriteMessage(dropped_message, RawQueue::kNonBlock)) { |
| 202 | dropped_messages = 0; |
| 203 | } else { |
| 204 | // Don't even bother trying to write this message because it's not likely |
| 205 | // to work and it would be confusing to have one log in the middle of a |
| 206 | // string of failures get through. |
| 207 | ++dropped_messages; |
| 208 | backoff_start = message_time; |
| 209 | queue->FreeMessage(msg); |
| 210 | return; |
| 211 | } |
| 212 | } |
| 213 | if (!queue->WriteMessage(msg, RawQueue::kNonBlock)) { |
| 214 | if (dropped_messages == 0) { |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 215 | monotonic_clock::time_point message_time( |
| 216 | chrono::seconds(msg->seconds) + chrono::nanoseconds(msg->nseconds)); |
| 217 | dropped_start = backoff_start = message_time; |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 218 | } |
| 219 | ++dropped_messages; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | class LinuxQueueLogImplementation : public LogImplementation { |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 224 | virtual ::aos::monotonic_clock::time_point monotonic_now() const { |
| 225 | return ::aos::monotonic_clock::now(); |
| 226 | } |
| 227 | |
Tyler Chatow | 5febd8f | 2020-01-05 18:25:31 -0800 | [diff] [blame] | 228 | __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 0))) void DoLog( |
| 229 | log_level level, const char *format, va_list ap) override { |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 230 | LogMessage *message = GetMessageOrDie(); |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 231 | internal::FillInMessage(level, monotonic_now(), format, ap, message); |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 232 | Write(message); |
| 233 | } |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 234 | }; |
| 235 | |
Tyler Chatow | 5febd8f | 2020-01-05 18:25:31 -0800 | [diff] [blame] | 236 | class CallbackLogImplementation : public HandleMessageLogImplementation { |
| 237 | public: |
| 238 | CallbackLogImplementation( |
| 239 | const ::std::function<void(const LogMessage &)> &callback) |
| 240 | : callback_(callback) {} |
| 241 | |
| 242 | private: |
| 243 | void HandleMessage(const LogMessage &message) override { callback_(message); } |
| 244 | |
| 245 | ::std::function<void(const LogMessage &)> callback_; |
| 246 | }; |
| 247 | |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 248 | } // namespace |
| 249 | |
| 250 | RawQueue *GetLoggingQueue() { |
Austin Schuh | 3e252e7 | 2019-12-29 16:25:58 -0800 | [diff] [blame] | 251 | return RawQueue::Fetch("LoggingQueue", sizeof(LogMessage), 1323, 10000); |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | void RegisterQueueImplementation() { |
| 255 | Init(); |
| 256 | |
| 257 | queue = GetLoggingQueue(); |
| 258 | if (queue == NULL) { |
| 259 | Die("logging: couldn't fetch queue\n"); |
| 260 | } |
| 261 | |
Tyler Chatow | 4b471e1 | 2020-01-05 20:19:36 -0800 | [diff] [blame] | 262 | SetImplementation(new LinuxQueueLogImplementation()); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 263 | } |
| 264 | |
Tyler Chatow | 5febd8f | 2020-01-05 18:25:31 -0800 | [diff] [blame] | 265 | void RegisterCallbackImplementation( |
Tyler Chatow | 4b471e1 | 2020-01-05 20:19:36 -0800 | [diff] [blame] | 266 | const ::std::function<void(const LogMessage &)> &callback, |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame^] | 267 | bool update_global) { |
Tyler Chatow | 5febd8f | 2020-01-05 18:25:31 -0800 | [diff] [blame] | 268 | Init(); |
Tyler Chatow | 4b471e1 | 2020-01-05 20:19:36 -0800 | [diff] [blame] | 269 | SetImplementation(new CallbackLogImplementation(callback), update_global); |
Tyler Chatow | 5febd8f | 2020-01-05 18:25:31 -0800 | [diff] [blame] | 270 | } |
| 271 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 272 | } // namespace logging |
| 273 | } // namespace aos |