John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #ifndef AOS_LOGGING_IMPLEMENTATIONS_H_ |
| 2 | #define AOS_LOGGING_IMPLEMENTATIONS_H_ |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 3 | |
| 4 | #include <sys/types.h> |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 5 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 6 | #include <cstdint> |
Tyler Chatow | 5febd8f | 2020-01-05 18:25:31 -0800 | [diff] [blame] | 7 | #include <functional> |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 8 | #include <memory> |
Tyler Chatow | 5febd8f | 2020-01-05 18:25:31 -0800 | [diff] [blame] | 9 | #include <string> |
Austin Schuh | ad9e5eb | 2021-11-19 20:33:55 -0800 | [diff] [blame] | 10 | #include <string_view> |
Stephan Pleines | 0960c26 | 2024-05-31 20:29:24 -0700 | [diff] [blame] | 11 | #include <utility> |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 12 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 13 | #include "aos/logging/context.h" |
| 14 | #include "aos/logging/interface.h" |
| 15 | #include "aos/logging/logging.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 16 | #include "aos/macros.h" |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 17 | #include "aos/time/time.h" |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 18 | |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 19 | // This file has various concrete LogImplementations. |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 20 | |
| 21 | namespace aos { |
| 22 | namespace logging { |
| 23 | |
| 24 | // Unless explicitly stated otherwise, format must always be a string constant, |
| 25 | // args are printf-style arguments for format, and ap is a va_list of args. |
Brian Silverman | 1a572cc | 2013-03-05 19:58:01 -0800 | [diff] [blame] | 26 | // The validity of format and args together will be checked at compile time |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 27 | // using a function attribute. |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 28 | |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 29 | // Contains all of the information about a given logging call. |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 30 | struct LogMessage { |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 31 | int32_t seconds, nseconds; |
Brian Silverman | ff12c9f | 2014-03-19 17:53:29 -0700 | [diff] [blame] | 32 | // message_length is just the length of the actual data (which member depends |
| 33 | // on the type). |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 34 | size_t message_length, name_length; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 35 | pid_t source; |
| 36 | static_assert(sizeof(source) == 4, "that's how they get printed"); |
| 37 | // Per task/thread. |
| 38 | uint16_t sequence; |
| 39 | log_level level; |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 40 | char name[LOG_MESSAGE_NAME_LEN]; |
Austin Schuh | 5619643 | 2020-10-24 20:15:21 -0700 | [diff] [blame] | 41 | char message[LOG_MESSAGE_LEN]; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 42 | }; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 43 | |
| 44 | // Returns left > right. LOG_UNKNOWN is most important. |
| 45 | static inline bool log_gt_important(log_level left, log_level right) { |
| 46 | if (left == ERROR) left = 3; |
| 47 | if (right == ERROR) right = 3; |
| 48 | return left > right; |
| 49 | } |
| 50 | |
| 51 | // Returns a string representing level or "unknown". |
| 52 | static inline const char *log_str(log_level level) { |
Tyler Chatow | 5febd8f | 2020-01-05 18:25:31 -0800 | [diff] [blame] | 53 | #define DECL_LEVEL(name, value) \ |
| 54 | if (level == name) return #name; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 55 | DECL_LEVELS; |
| 56 | #undef DECL_LEVEL |
| 57 | return "unknown"; |
| 58 | } |
| 59 | // Returns the log level represented by str or LOG_UNKNOWN. |
| 60 | static inline log_level str_log(const char *str) { |
Tyler Chatow | 5febd8f | 2020-01-05 18:25:31 -0800 | [diff] [blame] | 61 | #define DECL_LEVEL(name, value) \ |
| 62 | if (!strcmp(str, #name)) return name; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 63 | DECL_LEVELS; |
| 64 | #undef DECL_LEVEL |
| 65 | return LOG_UNKNOWN; |
| 66 | } |
| 67 | |
Brian Silverman | be858a1 | 2014-04-30 17:37:28 -0700 | [diff] [blame] | 68 | // Implements all of the DoLog* methods in terms of a (pure virtual in this |
| 69 | // class) HandleMessage method that takes a pointer to the message. |
| 70 | class HandleMessageLogImplementation : public LogImplementation { |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 71 | protected: |
| 72 | virtual ::aos::monotonic_clock::time_point monotonic_now() const { |
| 73 | return ::aos::monotonic_clock::now(); |
| 74 | } |
| 75 | |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 76 | private: |
Tyler Chatow | 5febd8f | 2020-01-05 18:25:31 -0800 | [diff] [blame] | 77 | __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 0))) void DoLog( |
| 78 | log_level level, const char *format, va_list ap) override; |
Brian Silverman | be858a1 | 2014-04-30 17:37:28 -0700 | [diff] [blame] | 79 | |
| 80 | virtual void HandleMessage(const LogMessage &message) = 0; |
| 81 | }; |
| 82 | |
Brian Silverman | 1e8ddfe | 2013-12-19 16:20:53 -0800 | [diff] [blame] | 83 | // A log implementation that dumps all messages to a C stdio stream. |
Brian Silverman | be858a1 | 2014-04-30 17:37:28 -0700 | [diff] [blame] | 84 | class StreamLogImplementation : public HandleMessageLogImplementation { |
Brian Silverman | 1e8ddfe | 2013-12-19 16:20:53 -0800 | [diff] [blame] | 85 | public: |
| 86 | StreamLogImplementation(FILE *stream); |
| 87 | |
Austin Schuh | ad9e5eb | 2021-11-19 20:33:55 -0800 | [diff] [blame] | 88 | // Returns the name of this actual thread as the name. |
| 89 | std::string_view MyName() override { |
| 90 | internal::Context *context = internal::Context::Get(); |
| 91 | return context->MyName(); |
| 92 | } |
| 93 | |
Brian Silverman | 1e8ddfe | 2013-12-19 16:20:53 -0800 | [diff] [blame] | 94 | private: |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 95 | void HandleMessage(const LogMessage &message) override; |
Brian Silverman | 1e8ddfe | 2013-12-19 16:20:53 -0800 | [diff] [blame] | 96 | |
| 97 | FILE *const stream_; |
| 98 | }; |
| 99 | |
Austin Schuh | 5619643 | 2020-10-24 20:15:21 -0700 | [diff] [blame] | 100 | // Returns the current implementation. |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 101 | std::shared_ptr<LogImplementation> GetImplementation(); |
Tyler Chatow | 4b471e1 | 2020-01-05 20:19:36 -0800 | [diff] [blame] | 102 | |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 103 | // Sets the current implementation. |
| 104 | void SetImplementation(std::shared_ptr<LogImplementation> implementation); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 105 | |
Austin Schuh | 5619643 | 2020-10-24 20:15:21 -0700 | [diff] [blame] | 106 | // A logging implementation which just uses a callback. |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 107 | class CallbackLogImplementation : public HandleMessageLogImplementation { |
| 108 | public: |
| 109 | CallbackLogImplementation( |
Austin Schuh | ad9e5eb | 2021-11-19 20:33:55 -0800 | [diff] [blame] | 110 | const ::std::function<void(const LogMessage &)> &callback, |
| 111 | const std::string *name) |
| 112 | : callback_(callback), name_(name) {} |
| 113 | |
| 114 | // Returns the provided name. This is most likely the event loop name. |
| 115 | std::string_view MyName() override { return *name_; } |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 116 | |
| 117 | private: |
| 118 | void HandleMessage(const LogMessage &message) override { callback_(message); } |
| 119 | |
| 120 | ::std::function<void(const LogMessage &)> callback_; |
Austin Schuh | ad9e5eb | 2021-11-19 20:33:55 -0800 | [diff] [blame] | 121 | const std::string *name_; |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 122 | }; |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 123 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 124 | class ScopedLogRestorer { |
| 125 | public: |
Austin Schuh | 5619643 | 2020-10-24 20:15:21 -0700 | [diff] [blame] | 126 | ScopedLogRestorer() : prev_impl_(GetImplementation()) {} |
| 127 | ~ScopedLogRestorer() { SetImplementation(std::move(prev_impl_)); } |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 128 | |
| 129 | void Swap(std::shared_ptr<LogImplementation> new_impl) { |
Austin Schuh | 5619643 | 2020-10-24 20:15:21 -0700 | [diff] [blame] | 130 | SetImplementation(std::move(new_impl)); |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 131 | } |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 132 | |
| 133 | private: |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 134 | std::shared_ptr<LogImplementation> prev_impl_; |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 135 | }; |
Tyler Chatow | 5febd8f | 2020-01-05 18:25:31 -0800 | [diff] [blame] | 136 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 137 | // This is where all of the code that is only used by actual LogImplementations |
| 138 | // goes. |
| 139 | namespace internal { |
| 140 | |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 141 | // Fills in *message according to the given inputs (with type kString). |
| 142 | // Used for implementing LogImplementation::DoLog. |
Austin Schuh | ad9e5eb | 2021-11-19 20:33:55 -0800 | [diff] [blame] | 143 | void FillInMessage(log_level level, std::string_view name, |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 144 | ::aos::monotonic_clock::time_point monotonic_now, |
| 145 | const char *format, va_list ap, LogMessage *message) |
Austin Schuh | ad9e5eb | 2021-11-19 20:33:55 -0800 | [diff] [blame] | 146 | __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 4, 0))); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 147 | |
Austin Schuh | ad9e5eb | 2021-11-19 20:33:55 -0800 | [diff] [blame] | 148 | __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 5, 6))) static inline void |
| 149 | FillInMessageVarargs(log_level level, std::string_view name, |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 150 | ::aos::monotonic_clock::time_point monotonic_now, |
| 151 | LogMessage *message, const char *format, ...) { |
Brian Silverman | 7896854 | 2014-03-05 17:03:43 -0800 | [diff] [blame] | 152 | va_list ap; |
| 153 | va_start(ap, format); |
Austin Schuh | ad9e5eb | 2021-11-19 20:33:55 -0800 | [diff] [blame] | 154 | FillInMessage(level, name, monotonic_now, format, ap, message); |
Brian Silverman | 7896854 | 2014-03-05 17:03:43 -0800 | [diff] [blame] | 155 | va_end(ap); |
| 156 | } |
| 157 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 158 | // Prints message to output. |
| 159 | void PrintMessage(FILE *output, const LogMessage &message); |
| 160 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 161 | } // namespace internal |
| 162 | } // namespace logging |
| 163 | } // namespace aos |
| 164 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 165 | #endif // AOS_LOGGING_IMPLEMENTATIONS_H_ |