blob: d2663e7e57e794374aedc63554d14a5ba2f6ab1c [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#ifndef AOS_LOGGING_INTERFACE_H_
2#define AOS_LOGGING_INTERFACE_H_
Austin Schuh044e18b2015-10-21 20:17:09 -07003
Tyler Chatowbf0609c2021-07-31 16:13:27 -07004#include <cstdarg>
Austin Schuh044e18b2015-10-21 20:17:09 -07005#include <functional>
Tyler Chatow4b471e12020-01-05 20:19:36 -08006#include <string>
Austin Schuhad9e5eb2021-11-19 20:33:55 -08007#include <string_view>
Austin Schuh044e18b2015-10-21 20:17:09 -07008
John Park33858a32018-09-28 23:05:48 -07009#include "aos/logging/logging.h"
10#include "aos/macros.h"
Austin Schuh044e18b2015-10-21 20:17:09 -070011
Brian Silvermancb5da1f2015-12-05 22:19:58 -050012// This file has the non-C-compatible parts of the logging client interface.
13
Austin Schuh044e18b2015-10-21 20:17:09 -070014namespace aos {
15
16struct MessageType;
17
Austin Schuh044e18b2015-10-21 20:17:09 -070018namespace logging {
19
Brian Silverman17291d82015-10-24 22:30:57 -040020// Takes a message and logs it. It will set everything up and then call DoLog
21// for the current LogImplementation.
22void VLog(log_level level, const char *format, va_list ap)
23 __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 2, 0)));
Brian Silverman17291d82015-10-24 22:30:57 -040024
Austin Schuh044e18b2015-10-21 20:17:09 -070025// Represents a system that can actually take log messages and do something
26// useful with them.
Austin Schuh044e18b2015-10-21 20:17:09 -070027class LogImplementation {
28 public:
Tyler Chatow4b471e12020-01-05 20:19:36 -080029 LogImplementation() {}
Austin Schuh044e18b2015-10-21 20:17:09 -070030
Tyler Chatow4b471e12020-01-05 20:19:36 -080031 virtual ~LogImplementation() {}
Austin Schuh044e18b2015-10-21 20:17:09 -070032
Austin Schuhad9e5eb2021-11-19 20:33:55 -080033 // 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 Schuh044e18b2015-10-21 20:17:09 -070037 // Actually logs the given message. Implementations should somehow create a
38 // LogMessage and then call internal::FillInMessage.
Tyler Chatow4b471e12020-01-05 20:19:36 -080039 __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 Schuh044e18b2015-10-21 20:17:09 -070043 va_list ap;
44 va_start(ap, format);
45 DoLog(level, format, ap);
46 va_end(ap);
47 }
Austin Schuh044e18b2015-10-21 20:17:09 -070048};
49
Brian Silverman17291d82015-10-24 22:30:57 -040050namespace internal {
51
52// Prints format (with ap) into output and correctly deals with the result
53// being too long etc.
54size_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 Silverman17291d82015-10-24 22:30:57 -040058} // namespace internal
Austin Schuh044e18b2015-10-21 20:17:09 -070059} // namespace logging
60} // namespace aos
61
John Park33858a32018-09-28 23:05:48 -070062#endif // AOS_LOGGING_INTERFACE_H_