blob: 387e55df77ce5c13dc21a06fa522ca8a588c0834 [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
4#include <stdarg.h>
5
Austin Schuh044e18b2015-10-21 20:17:09 -07006#include <functional>
Tyler Chatow4b471e12020-01-05 20:19:36 -08007#include <string>
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 Schuh044e18b2015-10-21 20:17:09 -070033 // Actually logs the given message. Implementations should somehow create a
34 // LogMessage and then call internal::FillInMessage.
Tyler Chatow4b471e12020-01-05 20:19:36 -080035 __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 0))) virtual void DoLog(
36 log_level level, const char *format, va_list ap) = 0;
37 __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 4))) void DoLogVariadic(
38 log_level level, const char *format, ...) {
Austin Schuh044e18b2015-10-21 20:17:09 -070039 va_list ap;
40 va_start(ap, format);
41 DoLog(level, format, ap);
42 va_end(ap);
43 }
Austin Schuh044e18b2015-10-21 20:17:09 -070044};
45
Brian Silverman17291d82015-10-24 22:30:57 -040046namespace internal {
47
48// Prints format (with ap) into output and correctly deals with the result
49// being too long etc.
50size_t ExecuteFormat(char *output, size_t output_size, const char *format,
51 va_list ap)
52 __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 0)));
53
Brian Silverman17291d82015-10-24 22:30:57 -040054} // namespace internal
Austin Schuh044e18b2015-10-21 20:17:09 -070055} // namespace logging
56} // namespace aos
57
John Park33858a32018-09-28 23:05:48 -070058#endif // AOS_LOGGING_INTERFACE_H_