dte test coverage


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 50.0% high: ≥ 85.0%
Coverage Exec / Excl / Total
Lines: 100.0% 5 / 0 / 5
Functions: 100.0% 3 / 0 / 3
Branches: -% 0 / 0 / 0

src/util/log.h
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 typedef enum {
25 LOGOPEN_USE_COLOR = 1 << 0, // Use SGR color sequences in log output
26 LOGOPEN_ALLOW_CTTY = 1 << 1, // Allow logging to the controlling tty(4) (/dev/tty)
27 } LogOpenFlags;
28
29 #define LOG(level, ...) log_msg(level, __FILE__, __LINE__, __VA_ARGS__)
30 #define LOG_CRITICAL(...) LOG(LOG_LEVEL_CRITICAL, __VA_ARGS__)
31 #define LOG_ERROR(...) LOG(LOG_LEVEL_ERROR, __VA_ARGS__)
32 #define LOG_ERRNO(prefix) log_errno(__FILE__, __LINE__, prefix)
33 #define LOG_WARNING(...) LOG(LOG_LEVEL_WARNING, __VA_ARGS__)
34 #define LOG_NOTICE(...) LOG(LOG_LEVEL_NOTICE, __VA_ARGS__)
35 #define LOG_INFO(...) LOG(LOG_LEVEL_INFO, __VA_ARGS__)
36 #define WARN_ON(cond) if (unlikely(cond)) {LOG_WARNING("%s", #cond);}
37 #define LOG_ERRNO_ON(cond, prefix) if (unlikely(cond)) {LOG_ERRNO(prefix);}
38 #define TRACE_LOGGING_ENABLED (DEBUG >= 3) // See also: src/trace.h
39
40 bool log_level_enabled(LogLevel level);
41
42 #if DEBUG >= 2
43 #define DEBUG_LOGGING_ENABLED 1
44 #define LOG_DEBUG(...) LOG(LOG_LEVEL_DEBUG, __VA_ARGS__)
45 2 static inline bool log_level_debug_enabled(void) {return log_level_enabled(LOG_LEVEL_DEBUG);}
46 #else
47 #define DEBUG_LOGGING_ENABLED 0
48 static inline PRINTF(1) void LOG_DEBUG(const char* UNUSED_ARG(fmt), ...) {}
49 static inline bool log_level_debug_enabled(void) {return false;}
50 #endif
51
52 3 static inline LogLevel log_level_default(void)
53 {
54 3 return DEBUG_LOGGING_ENABLED ? LOG_LEVEL_DEBUG : LOG_LEVEL_INFO;
55 }
56
57 561 static inline LogLevel log_level_max(void)
58 {
59 561 return TRACE_LOGGING_ENABLED ? LOG_LEVEL_TRACE : log_level_default();
60 }
61
62 LogLevel log_open(const char *filename, LogLevel level, LogOpenFlags logflags);
63 bool log_close(void);
64 void log_msg(LogLevel level, const char *file, int line, const char *fmt, ...) PRINTF(4);
65 void log_msgv(LogLevel level, const char *file, int line, const char *fmt, va_list ap) VPRINTF(4);
66 void log_write(LogLevel level, const char *str, size_t len);
67 SystemErrno log_errno(const char *file, int line, const char *prefix) COLD NONNULL_ARGS;
68 LogLevel log_level_from_str(const char *str);
69 const char *log_level_to_str(LogLevel level);
70
71 #endif
72