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> |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame^] | 8 | #include <mutex> |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 9 | |
Tyler Chatow | 5febd8f | 2020-01-05 18:25:31 -0800 | [diff] [blame] | 10 | #include "absl/base/call_once.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 11 | #include "aos/die.h" |
| 12 | #include "aos/logging/printf_formats.h" |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame^] | 13 | #include "aos/stl_mutex/stl_mutex.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 14 | #include "aos/time/time.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 | |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame^] | 22 | struct GlobalState { |
| 23 | std::shared_ptr<LogImplementation> implementation; |
| 24 | aos::stl_mutex lock; |
| 25 | static GlobalState *Get() { |
| 26 | static GlobalState r; |
| 27 | return &r; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 28 | } |
| 29 | }; |
| 30 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 31 | } // namespace |
| 32 | namespace internal { |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 33 | namespace { |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 34 | |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 35 | void FillInMessageBase(log_level level, |
| 36 | monotonic_clock::time_point monotonic_now, |
| 37 | LogMessage *message) { |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 38 | Context *context = Context::Get(); |
| 39 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 40 | message->level = level; |
| 41 | message->source = context->source; |
Austin Schuh | aebbc34 | 2015-01-25 02:25:13 -0800 | [diff] [blame] | 42 | memcpy(message->name, context->name, context->name_size); |
| 43 | message->name_length = context->name_size; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 44 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 45 | message->seconds = |
| 46 | chrono::duration_cast<chrono::seconds>(monotonic_now.time_since_epoch()) |
| 47 | .count(); |
| 48 | message->nseconds = |
| 49 | chrono::duration_cast<chrono::nanoseconds>( |
| 50 | monotonic_now.time_since_epoch() - chrono::seconds(message->seconds)) |
| 51 | .count(); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 52 | |
| 53 | message->sequence = context->sequence++; |
| 54 | } |
| 55 | |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 56 | } // namespace |
| 57 | |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 58 | void FillInMessage(log_level level, monotonic_clock::time_point monotonic_now, |
| 59 | const char *format, va_list ap, LogMessage *message) { |
| 60 | FillInMessageBase(level, monotonic_now, message); |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 61 | |
| 62 | message->message_length = |
| 63 | ExecuteFormat(message->message, sizeof(message->message), format, ap); |
| 64 | message->type = LogMessage::Type::kString; |
| 65 | } |
| 66 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 67 | void PrintMessage(FILE *output, const LogMessage &message) { |
Brian Silverman | a7234c6 | 2014-03-24 20:23:25 -0700 | [diff] [blame] | 68 | #define BASE_ARGS \ |
| 69 | AOS_LOGGING_BASE_ARGS( \ |
| 70 | message.name_length, message.name, static_cast<int32_t>(message.source), \ |
| 71 | message.sequence, message.level, message.seconds, message.nseconds) |
| 72 | switch (message.type) { |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 73 | case LogMessage::Type::kString: |
Brian Silverman | a7234c6 | 2014-03-24 20:23:25 -0700 | [diff] [blame] | 74 | fprintf(output, AOS_LOGGING_BASE_FORMAT "%.*s", BASE_ARGS, |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 75 | static_cast<int>(message.message_length), message.message); |
| 76 | break; |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 77 | } |
Brian Silverman | c1a244e | 2014-02-20 14:12:39 -0800 | [diff] [blame] | 78 | #undef BASE_ARGS |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | } // namespace internal |
| 82 | |
Brian Silverman | be858a1 | 2014-04-30 17:37:28 -0700 | [diff] [blame] | 83 | void HandleMessageLogImplementation::DoLog(log_level level, const char *format, |
| 84 | va_list ap) { |
| 85 | LogMessage message; |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 86 | internal::FillInMessage(level, monotonic_now(), format, ap, &message); |
Brian Silverman | be858a1 | 2014-04-30 17:37:28 -0700 | [diff] [blame] | 87 | HandleMessage(message); |
| 88 | } |
| 89 | |
Brian Silverman | 1e8ddfe | 2013-12-19 16:20:53 -0800 | [diff] [blame] | 90 | StreamLogImplementation::StreamLogImplementation(FILE *stream) |
| 91 | : stream_(stream) {} |
| 92 | |
Brian Silverman | be858a1 | 2014-04-30 17:37:28 -0700 | [diff] [blame] | 93 | void StreamLogImplementation::HandleMessage(const LogMessage &message) { |
Brian Silverman | 1e8ddfe | 2013-12-19 16:20:53 -0800 | [diff] [blame] | 94 | internal::PrintMessage(stream_, message); |
| 95 | } |
| 96 | |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame^] | 97 | void SetImplementation(std::shared_ptr<LogImplementation> implementation) { |
| 98 | Init(); |
| 99 | GlobalState *const global = GlobalState::Get(); |
| 100 | std::unique_lock<aos::stl_mutex> locker(global->lock); |
| 101 | global->implementation = std::move(implementation); |
| 102 | } |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 103 | |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame^] | 104 | std::shared_ptr<LogImplementation> SwapImplementation( |
| 105 | std::shared_ptr<LogImplementation> implementation) { |
| 106 | std::shared_ptr<LogImplementation> result; |
| 107 | { |
| 108 | GlobalState *const global = GlobalState::Get(); |
| 109 | std::unique_lock<aos::stl_mutex> locker(global->lock); |
| 110 | result = std::move(global->implementation); |
| 111 | global->implementation = std::move(implementation); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 112 | } |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame^] | 113 | Cleanup(); |
| 114 | return result; |
Tyler Chatow | 4b471e1 | 2020-01-05 20:19:36 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame^] | 117 | std::shared_ptr<LogImplementation> GetImplementation() { |
| 118 | GlobalState *const global = GlobalState::Get(); |
| 119 | std::unique_lock<aos::stl_mutex> locker(global->lock); |
| 120 | CHECK(global->implementation); |
| 121 | return global->implementation; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame^] | 124 | namespace { |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 125 | |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame^] | 126 | struct DoInit { |
| 127 | DoInit() { |
| 128 | GlobalState *const global = GlobalState::Get(); |
| 129 | std::unique_lock<aos::stl_mutex> locker(global->lock); |
| 130 | CHECK(!global->implementation); |
| 131 | global->implementation = std::make_shared<StreamLogImplementation>(stdout); |
| 132 | } |
| 133 | }; |
| 134 | |
| 135 | } // namespace |
| 136 | |
| 137 | void Init() { static DoInit do_init; } |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 138 | |
Tyler Chatow | 5febd8f | 2020-01-05 18:25:31 -0800 | [diff] [blame] | 139 | void Load() { internal::Context::Get(); } |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 140 | |
Tyler Chatow | 5febd8f | 2020-01-05 18:25:31 -0800 | [diff] [blame] | 141 | void Cleanup() { internal::Context::Delete(); } |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 142 | |
Tyler Chatow | 5febd8f | 2020-01-05 18:25:31 -0800 | [diff] [blame] | 143 | void RegisterCallbackImplementation( |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame^] | 144 | const ::std::function<void(const LogMessage &)> &callback) { |
Tyler Chatow | 5febd8f | 2020-01-05 18:25:31 -0800 | [diff] [blame] | 145 | Init(); |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame^] | 146 | SetImplementation(std::make_shared<CallbackLogImplementation>(callback)); |
Tyler Chatow | 5febd8f | 2020-01-05 18:25:31 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 149 | } // namespace logging |
| 150 | } // namespace aos |