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 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 4 | #include <cstdarg> |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 5 | #include <functional> |
Tyler Chatow | 4b471e1 | 2020-01-05 20:19:36 -0800 | [diff] [blame] | 6 | #include <string> |
Austin Schuh | ad9e5eb | 2021-11-19 20:33:55 -0800 | [diff] [blame^] | 7 | #include <string_view> |
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))); |
Brian Silverman | 17291d8 | 2015-10-24 22:30:57 -0400 | [diff] [blame] | 24 | |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 25 | // Represents a system that can actually take log messages and do something |
| 26 | // useful with them. |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 27 | class LogImplementation { |
| 28 | public: |
Tyler Chatow | 4b471e1 | 2020-01-05 20:19:36 -0800 | [diff] [blame] | 29 | LogImplementation() {} |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 30 | |
Tyler Chatow | 4b471e1 | 2020-01-05 20:19:36 -0800 | [diff] [blame] | 31 | virtual ~LogImplementation() {} |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 32 | |
Austin Schuh | ad9e5eb | 2021-11-19 20:33:55 -0800 | [diff] [blame^] | 33 | // Returns the identifying name to be used when logging. This could be the |
| 34 | // event loop name or the thread name. |
| 35 | virtual std::string_view MyName() = 0; |
| 36 | |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 37 | // Actually logs the given message. Implementations should somehow create a |
| 38 | // LogMessage and then call internal::FillInMessage. |
Tyler Chatow | 4b471e1 | 2020-01-05 20:19:36 -0800 | [diff] [blame] | 39 | __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 0))) virtual void DoLog( |
| 40 | log_level level, const char *format, va_list ap) = 0; |
| 41 | __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 4))) void DoLogVariadic( |
| 42 | log_level level, const char *format, ...) { |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 43 | va_list ap; |
| 44 | va_start(ap, format); |
| 45 | DoLog(level, format, ap); |
| 46 | va_end(ap); |
| 47 | } |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
Brian Silverman | 17291d8 | 2015-10-24 22:30:57 -0400 | [diff] [blame] | 50 | namespace internal { |
| 51 | |
| 52 | // Prints format (with ap) into output and correctly deals with the result |
| 53 | // being too long etc. |
| 54 | size_t ExecuteFormat(char *output, size_t output_size, const char *format, |
| 55 | va_list ap) |
| 56 | __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 0))); |
| 57 | |
Brian Silverman | 17291d8 | 2015-10-24 22:30:57 -0400 | [diff] [blame] | 58 | } // namespace internal |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 59 | } // namespace logging |
| 60 | } // namespace aos |
| 61 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 62 | #endif // AOS_LOGGING_INTERFACE_H_ |