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