John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #ifndef AOS_LOGGING_INTERFACE_H_ |
| 2 | #define AOS_LOGGING_INTERFACE_H_ |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 3 | |
| 4 | #include <stdarg.h> |
| 5 | |
| 6 | #include <string> |
| 7 | #include <functional> |
| 8 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 9 | #include "aos/logging/logging.h" |
| 10 | #include "aos/macros.h" |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 11 | |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 12 | // This file has the non-C-compatible parts of the logging client interface. |
| 13 | |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 14 | namespace aos { |
| 15 | |
| 16 | struct MessageType; |
| 17 | |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 18 | namespace logging { |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 19 | namespace internal { |
| 20 | |
| 21 | // Defined in queue_logging.cc. |
| 22 | void DoLogStruct(log_level level, const ::std::string &message, size_t size, |
| 23 | const MessageType *type, |
| 24 | const ::std::function<size_t(char *)> &serialize, int levels); |
| 25 | |
| 26 | // Defined in matrix_logging.cc. |
| 27 | void DoLogMatrix(log_level level, const ::std::string &message, |
| 28 | uint32_t type_id, int rows, int cols, const void *data, |
| 29 | int levels); |
| 30 | |
| 31 | } // namespace internal |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 32 | |
Brian Silverman | 17291d8 | 2015-10-24 22:30:57 -0400 | [diff] [blame] | 33 | // Takes a message and logs it. It will set everything up and then call DoLog |
| 34 | // for the current LogImplementation. |
| 35 | void VLog(log_level level, const char *format, va_list ap) |
| 36 | __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 2, 0))); |
| 37 | // Adds to the saved up message. |
| 38 | void VCork(int line, const char *function, const char *format, va_list ap) |
| 39 | __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 0))); |
| 40 | // Actually logs the saved up message. |
| 41 | void VUnCork(int line, const char *function, log_level level, const char *file, |
| 42 | const char *format, va_list ap) |
| 43 | __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 5, 0))); |
| 44 | |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 45 | // Represents a system that can actually take log messages and do something |
| 46 | // useful with them. |
| 47 | // All of the code (transitively too!) in the DoLog here can make |
| 48 | // normal LOG and LOG_DYNAMIC calls but can NOT call LOG_CORK/LOG_UNCORK. These |
| 49 | // calls will not result in DoLog recursing. However, implementations must be |
| 50 | // safe to call from multiple threads/tasks at the same time. Also, any other |
| 51 | // overriden methods may end up logging through a given implementation's DoLog. |
| 52 | class LogImplementation { |
| 53 | public: |
| 54 | LogImplementation() : next_(NULL) {} |
| 55 | |
| 56 | // The one that this one's implementation logs to. |
| 57 | // NULL means that there is no next one. |
| 58 | LogImplementation *next() { return next_; } |
| 59 | // Virtual in case a subclass wants to perform checks. There will be a valid |
| 60 | // logger other than this one available while this is called. |
| 61 | virtual void set_next(LogImplementation *next) { next_ = next; } |
| 62 | |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 63 | protected: |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 64 | // Actually logs the given message. Implementations should somehow create a |
| 65 | // LogMessage and then call internal::FillInMessage. |
| 66 | __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 0))) |
| 67 | virtual void DoLog(log_level level, const char *format, va_list ap) = 0; |
| 68 | __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 4))) |
| 69 | void DoLogVariadic(log_level level, const char *format, ...) { |
| 70 | va_list ap; |
| 71 | va_start(ap, format); |
| 72 | DoLog(level, format, ap); |
| 73 | va_end(ap); |
| 74 | } |
| 75 | |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 76 | // Logs the contents of an auto-generated structure. |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 77 | // size and type are the result of calling Size() and Type() on the type of |
| 78 | // the message. |
| 79 | // serialize will call Serialize on the message. |
| 80 | virtual void LogStruct(log_level level, const ::std::string &message, |
| 81 | size_t size, const MessageType *type, |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 82 | const ::std::function<size_t(char *)> &serialize) = 0; |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 83 | // Similiar to LogStruct, except for matrixes. |
| 84 | // type_id is the type of the elements of the matrix. |
| 85 | // data points to rows*cols*type_id.Size() bytes of data in row-major order. |
| 86 | virtual void LogMatrix(log_level level, const ::std::string &message, |
| 87 | uint32_t type_id, int rows, int cols, |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 88 | const void *data) = 0; |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 89 | |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 90 | private: |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 91 | // These functions call similar methods on the "current" LogImplementation or |
| 92 | // Die if they can't find one. |
| 93 | // levels is how many LogImplementations to not use off the stack. |
| 94 | static void DoVLog(log_level, const char *format, va_list ap, int levels) |
| 95 | __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 2, 0))); |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 96 | |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 97 | friend void VLog(log_level, const char *, va_list); |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 98 | friend void internal::DoLogStruct( |
| 99 | log_level level, const ::std::string &message, size_t size, |
| 100 | const MessageType *type, const ::std::function<size_t(char *)> &serialize, |
| 101 | int levels); |
| 102 | friend void internal::DoLogMatrix(log_level level, |
| 103 | const ::std::string &message, |
| 104 | uint32_t type_id, int rows, int cols, |
| 105 | const void *data, int levels); |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 106 | |
| 107 | LogImplementation *next_; |
| 108 | }; |
| 109 | |
Brian Silverman | 17291d8 | 2015-10-24 22:30:57 -0400 | [diff] [blame] | 110 | namespace internal { |
| 111 | |
| 112 | // Prints format (with ap) into output and correctly deals with the result |
| 113 | // being too long etc. |
| 114 | size_t ExecuteFormat(char *output, size_t output_size, const char *format, |
| 115 | va_list ap) |
| 116 | __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 0))); |
| 117 | |
| 118 | // Runs the given function with the current LogImplementation (handles switching |
| 119 | // it out while running function etc). |
| 120 | // levels is how many LogImplementations to not use off the stack. |
| 121 | void RunWithCurrentImplementation( |
| 122 | int levels, ::std::function<void(LogImplementation *)> function); |
| 123 | |
| 124 | } // namespace internal |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 125 | } // namespace logging |
| 126 | } // namespace aos |
| 127 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 128 | #endif // AOS_LOGGING_INTERFACE_H_ |