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