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