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> |
| 10 | |
| 11 | #include <string> |
| 12 | |
| 13 | #include "aos/common/logging/logging.h" |
| 14 | #include "aos/common/type_traits.h" |
| 15 | #include "aos/common/mutex.h" |
| 16 | |
| 17 | // This file has all of the logging implementation. It can't be #included by C |
| 18 | // code like logging.h can. |
Brian Silverman | 1a572cc | 2013-03-05 19:58:01 -0800 | [diff] [blame] | 19 | // It is useful for the rest of the logging implementation and other C++ code |
| 20 | // that needs to do special things with logging. |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 21 | |
| 22 | namespace aos { |
| 23 | namespace logging { |
| 24 | |
| 25 | // Unless explicitly stated otherwise, format must always be a string constant, |
| 26 | // 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] | 27 | // 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] | 28 | // using a gcc function attribute. |
| 29 | |
| 30 | // The struct that the code uses for making logging calls. |
| 31 | // Packed so that it ends up the same under both linux and vxworks. |
| 32 | struct __attribute__((packed)) LogMessage { |
| 33 | #ifdef __VXWORKS__ |
| 34 | static_assert(sizeof(pid_t) == sizeof(int), |
| 35 | "we use task IDs (aka ints) and pid_t interchangeably"); |
| 36 | #endif |
| 37 | // Actually the task ID (aka a pointer to the TCB) on the cRIO. |
| 38 | pid_t source; |
| 39 | static_assert(sizeof(source) == 4, "that's how they get printed"); |
| 40 | // Per task/thread. |
| 41 | uint16_t sequence; |
| 42 | log_level level; |
| 43 | int32_t seconds, nseconds; |
| 44 | char name[100]; |
| 45 | char message[LOG_MESSAGE_LEN]; |
| 46 | }; |
| 47 | static_assert(shm_ok<LogMessage>::value, "it's going in a queue"); |
| 48 | |
| 49 | // Returns left > right. LOG_UNKNOWN is most important. |
| 50 | static inline bool log_gt_important(log_level left, log_level right) { |
| 51 | if (left == ERROR) left = 3; |
| 52 | if (right == ERROR) right = 3; |
| 53 | return left > right; |
| 54 | } |
| 55 | |
| 56 | // Returns a string representing level or "unknown". |
| 57 | static inline const char *log_str(log_level level) { |
| 58 | #define DECL_LEVEL(name, value) if (level == name) return #name; |
| 59 | DECL_LEVELS; |
| 60 | #undef DECL_LEVEL |
| 61 | return "unknown"; |
| 62 | } |
| 63 | // Returns the log level represented by str or LOG_UNKNOWN. |
| 64 | static inline log_level str_log(const char *str) { |
| 65 | #define DECL_LEVEL(name, value) if (!strcmp(str, #name)) return name; |
| 66 | DECL_LEVELS; |
| 67 | #undef DECL_LEVEL |
| 68 | return LOG_UNKNOWN; |
| 69 | } |
| 70 | |
| 71 | // Takes a message and logs it. It will set everything up and then call DoLog |
| 72 | // for the current LogImplementation. |
| 73 | void VLog(log_level level, const char *format, va_list ap); |
| 74 | // Adds to the saved up message. |
| 75 | void VCork(int line, const char *format, va_list ap); |
| 76 | // Actually logs the saved up message. |
| 77 | void VUnCork(int line, log_level level, const char *file, |
| 78 | const char *format, va_list ap); |
| 79 | |
| 80 | // Will call VLog with the given arguments for the next logger in the chain. |
| 81 | void LogNext(log_level level, const char *format, ...) |
| 82 | __attribute__((format(LOG_PRINTF_FORMAT_TYPE, 2, 3))); |
| 83 | |
| 84 | // Represents a system that can actually take log messages and do something |
| 85 | // useful with them. |
| 86 | // All of the code (transitively too!) in the DoLog here can make |
| 87 | // normal LOG and LOG_DYNAMIC calls but can NOT call LOG_CORK/LOG_UNCORK. These |
| 88 | // calls will not result in DoLog recursing. However, implementations must be |
| 89 | // safe to call from multiple threads/tasks at the same time. Also, any other |
| 90 | // overriden methods may end up logging through a given implementation's DoLog. |
| 91 | class LogImplementation { |
| 92 | public: |
| 93 | LogImplementation() : next_(NULL) {} |
| 94 | |
| 95 | // The one that this one's implementation logs to. |
| 96 | // NULL means that there is no next one. |
| 97 | LogImplementation *next() { return next_; } |
| 98 | // Virtual in case a subclass wants to perform checks. There will be a valid |
| 99 | // logger other than this one available while this is called. |
| 100 | virtual void set_next(LogImplementation *next) { next_ = next; } |
| 101 | |
| 102 | private: |
| 103 | // Actually logs the given message. Implementations should somehow create a |
| 104 | // LogMessage and then call internal::FillInMessage. |
| 105 | virtual void DoLog(log_level level, const char *format, va_list ap) = 0; |
| 106 | |
| 107 | // Function of this class so that it can access DoLog. |
| 108 | // Levels is how many LogImplementations to not use off the stack. |
| 109 | static void DoVLog(log_level, const char *format, va_list ap, int levels); |
| 110 | // Friends so that they can access DoVLog. |
| 111 | friend void VLog(log_level, const char *, va_list); |
| 112 | friend void LogNext(log_level, const char *, ...); |
| 113 | |
| 114 | LogImplementation *next_; |
| 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. |
| 118 | class StreamLogImplementation : public LogImplementation { |
| 119 | public: |
| 120 | StreamLogImplementation(FILE *stream); |
| 121 | |
| 122 | private: |
| 123 | virtual void DoLog(log_level level, const char *format, va_list ap); |
| 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. |
| 135 | void AddImplementation(LogImplementation *implementation); |
| 136 | |
| 137 | // Must be called at least once per process/load before anything else is |
| 138 | // called. This function is safe to call multiple times from multiple |
| 139 | // tasks/threads. |
| 140 | void Init(); |
| 141 | |
| 142 | // Forces all of the state that is usually lazily created when first needed to |
| 143 | // be created when called. Cleanup() will delete it. |
| 144 | void Load(); |
| 145 | // Resets all information in this task/thread to its initial state. |
| 146 | // NOTE: This is not the opposite of Init(). The state that this deletes is |
| 147 | // lazily created when needed. It is actually the opposite of Load(). |
| 148 | void Cleanup(); |
| 149 | |
| 150 | // This is where all of the code that is only used by actual LogImplementations |
| 151 | // goes. |
| 152 | namespace internal { |
| 153 | |
| 154 | // An separate instance of this class is accessible from each task/thread. |
Brian Silverman | 1a572cc | 2013-03-05 19:58:01 -0800 | [diff] [blame] | 155 | // NOTE: It will get deleted in the child of a fork. |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 156 | struct Context { |
| 157 | Context(); |
| 158 | |
| 159 | // Gets the Context object for this task/thread. Will create one the first |
| 160 | // time it is called. |
| 161 | // |
| 162 | // The implementation for each platform will lazily instantiate a new instance |
| 163 | // and then initialize name the first time. |
| 164 | // IMPORTANT: The implementation of this can not use logging. |
| 165 | static Context *Get(); |
| 166 | // Deletes the Context object for this task/thread so that the next Get() is |
| 167 | // called it will create a new one. |
| 168 | // It is valid to call this when Get() has never been called. |
| 169 | static void Delete(); |
| 170 | |
| 171 | // Which one to log to right now. |
| 172 | // Will be NULL if there is no logging implementation to use right now. |
| 173 | LogImplementation *implementation; |
| 174 | |
Brian Silverman | ab6615c | 2013-03-05 20:29:29 -0800 | [diff] [blame] | 175 | // A name representing this task/(process and thread). |
| 176 | // strlen(name.c_str()) must be <= sizeof(LogMessage::name). |
| 177 | ::std::string name; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 178 | |
| 179 | // What to assign LogMessage::source to in this task/thread. |
| 180 | pid_t source; |
| 181 | |
| 182 | // The sequence value to send out with the next message. |
| 183 | uint16_t sequence; |
| 184 | |
| 185 | // Contains all of the information related to implementing LOG_CORK and |
| 186 | // LOG_UNCORK. |
| 187 | struct { |
| 188 | char message[LOG_MESSAGE_LEN]; |
| 189 | int line_min, line_max; |
| 190 | // Sets the data up to record a new series of corked logs. |
| 191 | void Reset() { |
| 192 | message[0] = '\0'; // make strlen of it 0 |
| 193 | line_min = INT_MAX; |
| 194 | line_max = -1; |
| 195 | function = NULL; |
| 196 | } |
| 197 | // The function that the calls are in. |
| 198 | // REMEMBER: While the compiler/linker will probably optimize all of the |
| 199 | // identical strings to point to the same data, it might not, so using == to |
| 200 | // compare this with another value is a bad idea. |
| 201 | const char *function; |
| 202 | } cork_data; |
| 203 | }; |
| 204 | |
| 205 | // Fills in *message according to the given inputs. Used for implementing |
| 206 | // LogImplementation::DoLog. |
| 207 | void FillInMessage(log_level level, const char *format, va_list ap, |
| 208 | LogMessage *message); |
| 209 | |
| 210 | // Prints message to output. |
| 211 | void PrintMessage(FILE *output, const LogMessage &message); |
| 212 | |
| 213 | } // namespace internal |
| 214 | } // namespace logging |
| 215 | } // namespace aos |
| 216 | |
| 217 | #endif // AOS_COMMON_LOGGING_LOGGING_IMPL_H_ |