blob: 9889078feb29a09463f40341e8fb93c00cb505bc [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
Stephan Pleines0960c262024-05-31 20:29:24 -07004#include <stddef.h>
5
Tyler Chatowbf0609c2021-07-31 16:13:27 -07006#include <cstdarg>
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
Austin Schuh044e18b2015-10-21 20:17:09 -070016namespace logging {
17
Brian Silverman17291d82015-10-24 22:30:57 -040018// Takes a message and logs it. It will set everything up and then call DoLog
19// for the current LogImplementation.
20void VLog(log_level level, const char *format, va_list ap)
21 __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 2, 0)));
Brian Silverman17291d82015-10-24 22:30:57 -040022
Austin Schuh044e18b2015-10-21 20:17:09 -070023// Represents a system that can actually take log messages and do something
24// useful with them.
Austin Schuh044e18b2015-10-21 20:17:09 -070025class LogImplementation {
26 public:
Tyler Chatow4b471e12020-01-05 20:19:36 -080027 LogImplementation() {}
Austin Schuh044e18b2015-10-21 20:17:09 -070028
Tyler Chatow4b471e12020-01-05 20:19:36 -080029 virtual ~LogImplementation() {}
Austin Schuh044e18b2015-10-21 20:17:09 -070030
Austin Schuhad9e5eb2021-11-19 20:33:55 -080031 // 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 Schuh044e18b2015-10-21 20:17:09 -070035 // Actually logs the given message. Implementations should somehow create a
36 // LogMessage and then call internal::FillInMessage.
Tyler Chatow4b471e12020-01-05 20:19:36 -080037 __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 Schuh044e18b2015-10-21 20:17:09 -070041 va_list ap;
42 va_start(ap, format);
43 DoLog(level, format, ap);
44 va_end(ap);
45 }
Austin Schuh044e18b2015-10-21 20:17:09 -070046};
47
Brian Silverman17291d82015-10-24 22:30:57 -040048namespace internal {
49
50// Prints format (with ap) into output and correctly deals with the result
51// being too long etc.
52size_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 Silverman17291d82015-10-24 22:30:57 -040056} // namespace internal
Austin Schuh044e18b2015-10-21 20:17:09 -070057} // namespace logging
58} // namespace aos
59
John Park33858a32018-09-28 23:05:48 -070060#endif // AOS_LOGGING_INTERFACE_H_