Line |
Branch |
Exec |
Source |
1 |
|
|
#ifndef UTIL_LOG_H |
2 |
|
|
#define UTIL_LOG_H |
3 |
|
|
|
4 |
|
|
#include <stdarg.h> |
5 |
|
|
#include <stdbool.h> |
6 |
|
|
#include <string.h> |
7 |
|
|
#include "debug.h" |
8 |
|
|
#include "errorcode.h" |
9 |
|
|
#include "macros.h" |
10 |
|
|
|
11 |
|
|
// NOLINTNEXTLINE(readability-enum-initial-value,cert-int09-c) |
12 |
|
|
typedef enum { |
13 |
|
|
LOG_LEVEL_INVALID = -1, |
14 |
|
|
LOG_LEVEL_NONE = 0, |
15 |
|
|
LOG_LEVEL_CRITICAL, |
16 |
|
|
LOG_LEVEL_ERROR, |
17 |
|
|
LOG_LEVEL_WARNING, |
18 |
|
|
LOG_LEVEL_NOTICE, |
19 |
|
|
LOG_LEVEL_INFO, |
20 |
|
|
LOG_LEVEL_DEBUG, |
21 |
|
|
LOG_LEVEL_TRACE, |
22 |
|
|
} LogLevel; |
23 |
|
|
|
24 |
|
|
#define LOG(level, ...) log_msg(level, __FILE__, __LINE__, __VA_ARGS__) |
25 |
|
|
#define LOG_CRITICAL(...) LOG(LOG_LEVEL_CRITICAL, __VA_ARGS__) |
26 |
|
|
#define LOG_ERROR(...) LOG(LOG_LEVEL_ERROR, __VA_ARGS__) |
27 |
|
|
#define LOG_ERRNO(prefix) log_errno(__FILE__, __LINE__, prefix) |
28 |
|
|
#define LOG_WARNING(...) LOG(LOG_LEVEL_WARNING, __VA_ARGS__) |
29 |
|
|
#define LOG_NOTICE(...) LOG(LOG_LEVEL_NOTICE, __VA_ARGS__) |
30 |
|
|
#define LOG_INFO(...) LOG(LOG_LEVEL_INFO, __VA_ARGS__) |
31 |
|
|
#define WARN_ON(cond) if (unlikely(cond)) {LOG_WARNING("%s", #cond);} |
32 |
|
|
#define LOG_ERRNO_ON(cond, prefix) if (unlikely(cond)) {LOG_ERRNO(prefix);} |
33 |
|
|
|
34 |
|
|
bool log_level_enabled(LogLevel level); |
35 |
|
|
|
36 |
|
|
#if DEBUG >= 2 |
37 |
|
|
#define DEBUG_LOGGING_ENABLED 1 |
38 |
|
|
#define LOG_DEBUG(...) LOG(LOG_LEVEL_DEBUG, __VA_ARGS__) |
39 |
|
2 |
static inline bool log_level_debug_enabled(void) {return log_level_enabled(LOG_LEVEL_DEBUG);} |
40 |
|
|
#else |
41 |
|
|
#define DEBUG_LOGGING_ENABLED 0 |
42 |
|
|
static inline PRINTF(1) void LOG_DEBUG(const char* UNUSED_ARG(fmt), ...) {} |
43 |
|
|
static inline bool log_level_debug_enabled(void) {return false;} |
44 |
|
|
#endif |
45 |
|
|
|
46 |
|
|
#if DEBUG >= 3 |
47 |
|
|
// See also: src/trace.h |
48 |
|
|
#define TRACE_LOGGING_ENABLED 1 |
49 |
|
|
#else |
50 |
|
|
#define TRACE_LOGGING_ENABLED 0 |
51 |
|
|
#endif |
52 |
|
|
|
53 |
|
|
LogLevel log_open(const char *filename, LogLevel level, bool use_color); |
54 |
|
|
bool log_close(void); |
55 |
|
|
void log_msg(LogLevel level, const char *file, int line, const char *fmt, ...) PRINTF(4); |
56 |
|
|
void log_msgv(LogLevel level, const char *file, int line, const char *fmt, va_list ap) VPRINTF(4); |
57 |
|
|
void log_write(LogLevel level, const char *str, size_t len); |
58 |
|
|
SystemErrno log_errno(const char *file, int line, const char *prefix) COLD NONNULL_ARGS; |
59 |
|
|
LogLevel log_level_default(void); |
60 |
|
|
LogLevel log_level_from_str(const char *str); |
61 |
|
|
const char *log_level_to_str(LogLevel level); |
62 |
|
|
|
63 |
|
|
#endif |
64 |
|
|
|