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> |
| 14 | #include <string> |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 15 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 16 | #include "aos/logging/context.h" |
| 17 | #include "aos/logging/interface.h" |
| 18 | #include "aos/logging/logging.h" |
| 19 | #include "aos/logging/sizes.h" |
| 20 | #include "aos/macros.h" |
| 21 | #include "aos/mutex/mutex.h" |
Sabina Davis | 2ed5ea2 | 2017-09-26 22:27:42 -0700 | [diff] [blame] | 22 | #include "aos/once.h" |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 23 | #include "aos/time/time.h" |
| 24 | #include "aos/type_traits/type_traits.h" |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 25 | |
Brian Silverman | d6974f4 | 2014-02-14 13:39:21 -0800 | [diff] [blame] | 26 | namespace aos { |
| 27 | |
Brian Silverman | f798614 | 2014-04-21 17:42:35 -0700 | [diff] [blame] | 28 | struct MessageType; |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 29 | class RawQueue; |
Brian Silverman | d6974f4 | 2014-02-14 13:39:21 -0800 | [diff] [blame] | 30 | |
| 31 | } // namespace aos |
| 32 | |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 33 | // This file has various concrete LogImplementations. |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 34 | |
| 35 | namespace aos { |
| 36 | namespace logging { |
| 37 | |
| 38 | // Unless explicitly stated otherwise, format must always be a string constant, |
| 39 | // 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] | 40 | // 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] | 41 | // using a function attribute. |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 42 | |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 43 | // Contains all of the information about a given logging call. |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 44 | struct LogMessage { |
Tyler Chatow | 5febd8f | 2020-01-05 18:25:31 -0800 | [diff] [blame] | 45 | enum class Type : uint8_t { kString }; |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 46 | |
| 47 | int32_t seconds, nseconds; |
Brian Silverman | ff12c9f | 2014-03-19 17:53:29 -0700 | [diff] [blame] | 48 | // message_length is just the length of the actual data (which member depends |
| 49 | // on the type). |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 50 | size_t message_length, name_length; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 51 | pid_t source; |
| 52 | static_assert(sizeof(source) == 4, "that's how they get printed"); |
| 53 | // Per task/thread. |
| 54 | uint16_t sequence; |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 55 | Type type; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 56 | log_level level; |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 57 | char name[LOG_MESSAGE_NAME_LEN]; |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 58 | union { |
| 59 | char message[LOG_MESSAGE_LEN]; |
| 60 | struct { |
| 61 | uint32_t type_id; |
| 62 | size_t string_length; |
| 63 | // The message string and then the serialized structure. |
| 64 | char serialized[LOG_MESSAGE_LEN - sizeof(type) - sizeof(string_length)]; |
| 65 | } structure; |
Brian Silverman | fd5e2a3 | 2014-02-22 20:02:39 -0800 | [diff] [blame] | 66 | struct { |
| 67 | // The type ID of the element type. |
| 68 | uint32_t type; |
Brian Silverman | ff12c9f | 2014-03-19 17:53:29 -0700 | [diff] [blame] | 69 | int rows, cols; |
| 70 | size_t string_length; |
| 71 | // The message string and then the serialized matrix. |
Tyler Chatow | 5febd8f | 2020-01-05 18:25:31 -0800 | [diff] [blame] | 72 | char data[LOG_MESSAGE_LEN - sizeof(type) - sizeof(rows) - sizeof(cols)]; |
Brian Silverman | fd5e2a3 | 2014-02-22 20:02:39 -0800 | [diff] [blame] | 73 | } matrix; |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 74 | }; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 75 | }; |
| 76 | static_assert(shm_ok<LogMessage>::value, "it's going in a queue"); |
| 77 | |
| 78 | // Returns left > right. LOG_UNKNOWN is most important. |
| 79 | static inline bool log_gt_important(log_level left, log_level right) { |
| 80 | if (left == ERROR) left = 3; |
| 81 | if (right == ERROR) right = 3; |
| 82 | return left > right; |
| 83 | } |
| 84 | |
| 85 | // Returns a string representing level or "unknown". |
| 86 | static inline const char *log_str(log_level level) { |
Tyler Chatow | 5febd8f | 2020-01-05 18:25:31 -0800 | [diff] [blame] | 87 | #define DECL_LEVEL(name, value) \ |
| 88 | if (level == name) return #name; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 89 | DECL_LEVELS; |
| 90 | #undef DECL_LEVEL |
| 91 | return "unknown"; |
| 92 | } |
| 93 | // Returns the log level represented by str or LOG_UNKNOWN. |
| 94 | static inline log_level str_log(const char *str) { |
Tyler Chatow | 5febd8f | 2020-01-05 18:25:31 -0800 | [diff] [blame] | 95 | #define DECL_LEVEL(name, value) \ |
| 96 | if (!strcmp(str, #name)) return name; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 97 | DECL_LEVELS; |
| 98 | #undef DECL_LEVEL |
| 99 | return LOG_UNKNOWN; |
| 100 | } |
| 101 | |
Brian Silverman | be858a1 | 2014-04-30 17:37:28 -0700 | [diff] [blame] | 102 | // Implements all of the DoLog* methods in terms of a (pure virtual in this |
| 103 | // class) HandleMessage method that takes a pointer to the message. |
| 104 | class HandleMessageLogImplementation : public LogImplementation { |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 105 | protected: |
| 106 | virtual ::aos::monotonic_clock::time_point monotonic_now() const { |
| 107 | return ::aos::monotonic_clock::now(); |
| 108 | } |
| 109 | |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 110 | private: |
Tyler Chatow | 5febd8f | 2020-01-05 18:25:31 -0800 | [diff] [blame] | 111 | __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 0))) void DoLog( |
| 112 | log_level level, const char *format, va_list ap) override; |
Brian Silverman | be858a1 | 2014-04-30 17:37:28 -0700 | [diff] [blame] | 113 | |
| 114 | virtual void HandleMessage(const LogMessage &message) = 0; |
| 115 | }; |
| 116 | |
Brian Silverman | 1e8ddfe | 2013-12-19 16:20:53 -0800 | [diff] [blame] | 117 | // A log implementation that dumps all messages to a C stdio stream. |
Brian Silverman | be858a1 | 2014-04-30 17:37:28 -0700 | [diff] [blame] | 118 | class StreamLogImplementation : public HandleMessageLogImplementation { |
Brian Silverman | 1e8ddfe | 2013-12-19 16:20:53 -0800 | [diff] [blame] | 119 | public: |
| 120 | StreamLogImplementation(FILE *stream); |
| 121 | |
| 122 | private: |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 123 | void HandleMessage(const LogMessage &message) override; |
Brian Silverman | 1e8ddfe | 2013-12-19 16:20:53 -0800 | [diff] [blame] | 124 | |
| 125 | FILE *const stream_; |
| 126 | }; |
| 127 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 128 | // Adds another implementation to the stack of implementations in this |
| 129 | // task/thread. |
| 130 | // Any tasks/threads created after this call will also use this implementation. |
| 131 | // The cutoff is when the state in a given task/thread is created (either lazily |
| 132 | // when needed or by calling Load()). |
| 133 | // The logging system takes ownership of implementation. It will delete it if |
| 134 | // necessary, so it must be created with new. |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame^] | 135 | // TODO: Log implementations are never deleted. Need means to safely deregister. |
Tyler Chatow | 4b471e1 | 2020-01-05 20:19:36 -0800 | [diff] [blame] | 136 | void SetImplementation(LogImplementation *implementation, |
| 137 | bool update_global = true); |
| 138 | |
| 139 | // Updates the log implementation for the current thread, returning the current |
| 140 | // implementation. |
| 141 | LogImplementation *SwapImplementation(LogImplementation *implementation); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 142 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame^] | 143 | LogImplementation *GetImplementation(); |
| 144 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 145 | // Must be called at least once per process/load before anything else is |
| 146 | // called. This function is safe to call multiple times from multiple |
| 147 | // tasks/threads. |
| 148 | void Init(); |
| 149 | |
| 150 | // Forces all of the state that is usually lazily created when first needed to |
| 151 | // be created when called. Cleanup() will delete it. |
| 152 | void Load(); |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 153 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 154 | // Resets all information in this task/thread to its initial state. |
| 155 | // NOTE: This is not the opposite of Init(). The state that this deletes is |
| 156 | // lazily created when needed. It is actually the opposite of Load(). |
| 157 | void Cleanup(); |
| 158 | |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 159 | // Returns a queue which deals with LogMessage-sized messages. |
| 160 | // The caller takes ownership. |
| 161 | RawQueue *GetLoggingQueue(); |
| 162 | |
Tyler Chatow | 4b471e1 | 2020-01-05 20:19:36 -0800 | [diff] [blame] | 163 | // Calls SetImplementation to register the standard linux logging implementation |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 164 | // which sends the messages through a queue. This implementation relies on |
| 165 | // another process(es) to read the log messages that it puts into the queue. |
| 166 | // This function is usually called by aos::Init*. |
| 167 | void RegisterQueueImplementation(); |
| 168 | |
Tyler Chatow | 5febd8f | 2020-01-05 18:25:31 -0800 | [diff] [blame] | 169 | void RegisterCallbackImplementation( |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame^] | 170 | const ::std::function<void(const LogMessage &)> &callback, |
| 171 | bool update_global = true); |
| 172 | |
| 173 | class ScopedLogRestorer { |
| 174 | public: |
| 175 | ScopedLogRestorer() { prev_impl_ = GetImplementation(); } |
| 176 | |
| 177 | ~ScopedLogRestorer() { SetImplementation(prev_impl_); } |
| 178 | |
| 179 | private: |
| 180 | LogImplementation *prev_impl_; |
| 181 | }; |
Tyler Chatow | 5febd8f | 2020-01-05 18:25:31 -0800 | [diff] [blame] | 182 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 183 | // This is where all of the code that is only used by actual LogImplementations |
| 184 | // goes. |
| 185 | namespace internal { |
| 186 | |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 187 | // Fills in *message according to the given inputs (with type kString). |
| 188 | // Used for implementing LogImplementation::DoLog. |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 189 | void FillInMessage(log_level level, |
| 190 | ::aos::monotonic_clock::time_point monotonic_now, |
| 191 | const char *format, va_list ap, LogMessage *message) |
| 192 | __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 0))); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 193 | |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 194 | __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 4, 5))) static inline void |
| 195 | FillInMessageVarargs(log_level level, |
| 196 | ::aos::monotonic_clock::time_point monotonic_now, |
| 197 | LogMessage *message, const char *format, ...) { |
Brian Silverman | 7896854 | 2014-03-05 17:03:43 -0800 | [diff] [blame] | 198 | va_list ap; |
| 199 | va_start(ap, format); |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 200 | FillInMessage(level, monotonic_now, format, ap, message); |
Brian Silverman | 7896854 | 2014-03-05 17:03:43 -0800 | [diff] [blame] | 201 | va_end(ap); |
| 202 | } |
| 203 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 204 | // Prints message to output. |
| 205 | void PrintMessage(FILE *output, const LogMessage &message); |
| 206 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 207 | } // namespace internal |
| 208 | } // namespace logging |
| 209 | } // namespace aos |
| 210 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 211 | #endif // AOS_LOGGING_IMPLEMENTATIONS_H_ |