blob: f6fa24959c7cbfab2175cef5741e6bcc71e9b55d [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 Schuh044e18b2015-10-21 20:17:09 -07007
John Park33858a32018-09-28 23:05:48 -07008#include "aos/logging/logging.h"
9#include "aos/macros.h"
Austin Schuh044e18b2015-10-21 20:17:09 -070010
Brian Silvermancb5da1f2015-12-05 22:19:58 -050011// This file has the non-C-compatible parts of the logging client interface.
12
Austin Schuh044e18b2015-10-21 20:17:09 -070013namespace aos {
14
15struct MessageType;
16
Austin Schuh044e18b2015-10-21 20:17:09 -070017namespace logging {
18
Brian Silverman17291d82015-10-24 22:30:57 -040019// Takes a message and logs it. It will set everything up and then call DoLog
20// for the current LogImplementation.
21void VLog(log_level level, const char *format, va_list ap)
22 __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 2, 0)));
Brian Silverman17291d82015-10-24 22:30:57 -040023
Austin Schuh044e18b2015-10-21 20:17:09 -070024// Represents a system that can actually take log messages and do something
25// useful with them.
Austin Schuh044e18b2015-10-21 20:17:09 -070026class LogImplementation {
27 public:
Tyler Chatow4b471e12020-01-05 20:19:36 -080028 LogImplementation() {}
Austin Schuh044e18b2015-10-21 20:17:09 -070029
Tyler Chatow4b471e12020-01-05 20:19:36 -080030 virtual ~LogImplementation() {}
Austin Schuh044e18b2015-10-21 20:17:09 -070031
Austin Schuh044e18b2015-10-21 20:17:09 -070032 // Actually logs the given message. Implementations should somehow create a
33 // LogMessage and then call internal::FillInMessage.
Tyler Chatow4b471e12020-01-05 20:19:36 -080034 __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 0))) virtual void DoLog(
35 log_level level, const char *format, va_list ap) = 0;
36 __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 4))) void DoLogVariadic(
37 log_level level, const char *format, ...) {
Austin Schuh044e18b2015-10-21 20:17:09 -070038 va_list ap;
39 va_start(ap, format);
40 DoLog(level, format, ap);
41 va_end(ap);
42 }
Austin Schuh044e18b2015-10-21 20:17:09 -070043};
44
Brian Silverman17291d82015-10-24 22:30:57 -040045namespace internal {
46
47// Prints format (with ap) into output and correctly deals with the result
48// being too long etc.
49size_t ExecuteFormat(char *output, size_t output_size, const char *format,
50 va_list ap)
51 __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 0)));
52
Brian Silverman17291d82015-10-24 22:30:57 -040053} // namespace internal
Austin Schuh044e18b2015-10-21 20:17:09 -070054} // namespace logging
55} // namespace aos
56
John Park33858a32018-09-28 23:05:48 -070057#endif // AOS_LOGGING_INTERFACE_H_