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