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