1#ifndef BASE_LOG_H
2#define BASE_LOG_H
3
4#include <cstdarg>
5#include <cstdint>
6
7#ifdef __GNUC__
8#define GNUC_ATTRIBUTE(x) __attribute__(x)
9#else
10#define GNUC_ATTRIBUTE(x)
11#endif
12
13enum LEVEL : char
14{
15 LEVEL_ERROR,
16 LEVEL_WARN,
17 LEVEL_INFO,
18 LEVEL_DEBUG,
19 LEVEL_TRACE,
20};
21
22struct LOG_COLOR
23{
24 uint8_t r;
25 uint8_t g;
26 uint8_t b;
27};
28
29#define log_error(sys, ...) log_log(LEVEL_ERROR, sys, __VA_ARGS__)
30#define log_warn(sys, ...) log_log(LEVEL_WARN, sys, __VA_ARGS__)
31#define log_info(sys, ...) log_log(LEVEL_INFO, sys, __VA_ARGS__)
32#define log_debug(sys, ...) log_log(LEVEL_DEBUG, sys, __VA_ARGS__)
33#define log_trace(sys, ...) log_log(LEVEL_TRACE, sys, __VA_ARGS__)
34
35/**
36 * @defgroup Log
37 *
38 * Methods for outputting log messages and way of handling them.
39 */
40
41/**
42 * @ingroup Log
43 *
44 * Prints a log message.
45 *
46 * @param level Severity of the log message.
47 * @param sys A string that describes what system the message belongs to.
48 * @param fmt A printf styled format string.
49 */
50void log_log(LEVEL level, const char *sys, const char *fmt, ...)
51 GNUC_ATTRIBUTE((format(printf, 3, 4)));
52
53/**
54 * @ingroup Log
55 *
56 * Prints a log message with a given color.
57 *
58 * @param level Severity of the log message.
59 * @param color Requested color for the log message output.
60 * @param sys A string that describes what system the message belongs to.
61 * @param fmt A printf styled format string.
62 */
63void log_log_color(LEVEL level, LOG_COLOR color, const char *sys, const char *fmt, ...)
64 GNUC_ATTRIBUTE((format(printf, 4, 5)));
65
66/**
67 * @ingroup Log
68 *
69 * Same as `log_log`, but takes a `va_list` instead.
70 *
71 * @param level Severity of the log message.
72 * @param sys A string that describes what system the message belongs to.
73 * @param fmt A printf styled format string.
74 * @param args The variable argument list.
75 */
76void log_log_v(LEVEL level, const char *sys, const char *fmt, va_list args)
77 GNUC_ATTRIBUTE((format(printf, 3, 0)));
78
79/**
80 * @ingroup Log
81 *
82 * Same as `log_log_color`, but takes a `va_list` instead.
83 *
84 * @param level Severity of the log message.
85 * @param color Requested color for the log message output.
86 * @param sys A string that describes what system the message belongs to.
87 * @param fmt A printf styled format string.
88 * @param args The variable argument list.
89 */
90void log_log_color_v(LEVEL level, LOG_COLOR color, const char *sys, const char *fmt, va_list args)
91 GNUC_ATTRIBUTE((format(printf, 4, 0)));
92#endif // BASE_LOG_H
93