dte test coverage


Directory: ./
File: src/util/log.h
Date: 2024-12-21 16:03:22
Exec Total Coverage
Lines: 0 2 0.0%
Functions: 0 2 0.0%
Branches: 0 0 -%

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