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 | |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 6 | #include <functional> |
Tyler Chatow | 4b471e1 | 2020-01-05 20:19:36 -0800 | [diff] [blame^] | 7 | #include <string> |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 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 { |
| 19 | |
Brian Silverman | 17291d8 | 2015-10-24 22:30:57 -0400 | [diff] [blame] | 20 | // Takes a message and logs it. It will set everything up and then call DoLog |
| 21 | // for the current LogImplementation. |
| 22 | void VLog(log_level level, const char *format, va_list ap) |
| 23 | __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 2, 0))); |
| 24 | // Adds to the saved up message. |
| 25 | void VCork(int line, const char *function, const char *format, va_list ap) |
| 26 | __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 0))); |
| 27 | // Actually logs the saved up message. |
| 28 | void VUnCork(int line, const char *function, log_level level, const char *file, |
| 29 | const char *format, va_list ap) |
| 30 | __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 5, 0))); |
| 31 | |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 32 | // Represents a system that can actually take log messages and do something |
| 33 | // useful with them. |
| 34 | // All of the code (transitively too!) in the DoLog here can make |
| 35 | // normal LOG and LOG_DYNAMIC calls but can NOT call LOG_CORK/LOG_UNCORK. These |
| 36 | // calls will not result in DoLog recursing. However, implementations must be |
| 37 | // safe to call from multiple threads/tasks at the same time. Also, any other |
| 38 | // overriden methods may end up logging through a given implementation's DoLog. |
| 39 | class LogImplementation { |
| 40 | public: |
Tyler Chatow | 4b471e1 | 2020-01-05 20:19:36 -0800 | [diff] [blame^] | 41 | LogImplementation() {} |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 42 | |
Tyler Chatow | 4b471e1 | 2020-01-05 20:19:36 -0800 | [diff] [blame^] | 43 | virtual ~LogImplementation() {} |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 44 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 45 | virtual bool fill_type_cache() { return true; } |
| 46 | |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 47 | protected: |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 48 | // Actually logs the given message. Implementations should somehow create a |
| 49 | // LogMessage and then call internal::FillInMessage. |
Tyler Chatow | 4b471e1 | 2020-01-05 20:19:36 -0800 | [diff] [blame^] | 50 | __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 0))) virtual void DoLog( |
| 51 | log_level level, const char *format, va_list ap) = 0; |
| 52 | __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 4))) void DoLogVariadic( |
| 53 | log_level level, const char *format, ...) { |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 54 | va_list ap; |
| 55 | va_start(ap, format); |
| 56 | DoLog(level, format, ap); |
| 57 | va_end(ap); |
| 58 | } |
| 59 | |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 60 | private: |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 61 | // These functions call similar methods on the "current" LogImplementation or |
| 62 | // Die if they can't find one. |
| 63 | // levels is how many LogImplementations to not use off the stack. |
Tyler Chatow | 4b471e1 | 2020-01-05 20:19:36 -0800 | [diff] [blame^] | 64 | static void DoVLog(log_level, const char *format, va_list ap) |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 65 | __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 2, 0))); |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 66 | |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 67 | friend void VLog(log_level, const char *, va_list); |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
Brian Silverman | 17291d8 | 2015-10-24 22:30:57 -0400 | [diff] [blame] | 70 | namespace internal { |
| 71 | |
| 72 | // Prints format (with ap) into output and correctly deals with the result |
| 73 | // being too long etc. |
| 74 | size_t ExecuteFormat(char *output, size_t output_size, const char *format, |
| 75 | va_list ap) |
| 76 | __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 0))); |
| 77 | |
| 78 | // Runs the given function with the current LogImplementation (handles switching |
| 79 | // it out while running function etc). |
| 80 | // levels is how many LogImplementations to not use off the stack. |
| 81 | void RunWithCurrentImplementation( |
| 82 | int levels, ::std::function<void(LogImplementation *)> function); |
| 83 | |
| 84 | } // namespace internal |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 85 | } // namespace logging |
| 86 | } // namespace aos |
| 87 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 88 | #endif // AOS_LOGGING_INTERFACE_H_ |