| 1 | #ifndef BASE_LOG_H |
| 2 | #define BASE_LOG_H |
| 3 | |
| 4 | #include <cstdarg> |
| 5 | #include <cstdint> |
| 6 | |
| 7 | enum LEVEL : char |
| 8 | { |
| 9 | LEVEL_ERROR, |
| 10 | LEVEL_WARN, |
| 11 | LEVEL_INFO, |
| 12 | LEVEL_DEBUG, |
| 13 | LEVEL_TRACE, |
| 14 | }; |
| 15 | |
| 16 | struct LOG_COLOR |
| 17 | { |
| 18 | uint8_t r; |
| 19 | uint8_t g; |
| 20 | uint8_t b; |
| 21 | }; |
| 22 | |
| 23 | #define log_error(sys, ...) log_log(LEVEL_ERROR, sys, __VA_ARGS__) |
| 24 | #define log_warn(sys, ...) log_log(LEVEL_WARN, sys, __VA_ARGS__) |
| 25 | #define log_info(sys, ...) log_log(LEVEL_INFO, sys, __VA_ARGS__) |
| 26 | #define log_debug(sys, ...) log_log(LEVEL_DEBUG, sys, __VA_ARGS__) |
| 27 | #define log_trace(sys, ...) log_log(LEVEL_TRACE, sys, __VA_ARGS__) |
| 28 | |
| 29 | #define log_error_color(color, sys, ...) log_log_color(LEVEL_ERROR, color, sys, __VA_ARGS__) |
| 30 | #define log_warn_color(color, sys, ...) log_log_color(LEVEL_WARN, color, sys, __VA_ARGS__) |
| 31 | #define log_info_color(color, sys, ...) log_log_color(LEVEL_INFO, color, sys, __VA_ARGS__) |
| 32 | #define log_debug_color(color, sys, ...) log_log_color(LEVEL_DEBUG, color, sys, __VA_ARGS__) |
| 33 | #define log_trace_color(color, sys, ...) log_log_color(LEVEL_TRACE, color, sys, __VA_ARGS__) |
| 34 | |
| 35 | /** |
| 36 | * @defgroup Log Logging |
| 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 | */ |
| 50 | [[gnu::format(printf, 3, 4)]] void log_log(LEVEL level, const char *sys, const char *fmt, ...); |
| 51 | |
| 52 | /** |
| 53 | * @ingroup Log |
| 54 | * |
| 55 | * Prints a log message with a given color. |
| 56 | * |
| 57 | * @param level Severity of the log message. |
| 58 | * @param color Requested color for the log message output. |
| 59 | * @param sys A string that describes what system the message belongs to. |
| 60 | * @param fmt A printf styled format string. |
| 61 | */ |
| 62 | [[gnu::format(printf, 4, 5)]] void log_log_color(LEVEL level, LOG_COLOR color, const char *sys, const char *fmt, ...); |
| 63 | |
| 64 | /** |
| 65 | * @ingroup Log |
| 66 | * |
| 67 | * Same as `log_log`, but takes a `va_list` instead. |
| 68 | * |
| 69 | * @param level Severity of the log message. |
| 70 | * @param sys A string that describes what system the message belongs to. |
| 71 | * @param fmt A printf styled format string. |
| 72 | * @param args The variable argument list. |
| 73 | */ |
| 74 | [[gnu::format(printf, 3, 0)]] void log_log_v(LEVEL level, const char *sys, const char *fmt, va_list args); |
| 75 | |
| 76 | /** |
| 77 | * @ingroup Log |
| 78 | * |
| 79 | * Same as `log_log_color`, but takes a `va_list` instead. |
| 80 | * |
| 81 | * @param level Severity of the log message. |
| 82 | * @param color Requested color for the log message output. |
| 83 | * @param sys A string that describes what system the message belongs to. |
| 84 | * @param fmt A printf styled format string. |
| 85 | * @param args The variable argument list. |
| 86 | */ |
| 87 | [[gnu::format(printf, 4, 0)]] void log_log_color_v(LEVEL level, LOG_COLOR color, const char *sys, const char *fmt, va_list args); |
| 88 | |
| 89 | #endif // BASE_LOG_H |
| 90 | |