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