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