Debian Codegraph @

src/log.h

raw symbols SCIP
1 // SPDX-License-Identifier: MPL-2.0
2 // Copyright (c) 2018 Yuxuan Shui <[email protected]>
3
4 #pragma once
5 #include <assert.h>
6 #include <errno.h>
7 #include <stdio.h>
8
9 #include "compiler.h"
10
11 enum log_level {
12 LOG_LEVEL_INVALID = -1,
13 /// Very noisy debug messages, many lines per frame.
14 LOG_LEVEL_TRACE = 0,
15 /// Frequent debug messages, a few lines per frame.
16 LOG_LEVEL_VERBOSE,
17 /// Less frequent debug messages.
18 LOG_LEVEL_DEBUG,
19 /// Informational messages.
20 LOG_LEVEL_INFO,
21 /// Warnings.
22 LOG_LEVEL_WARN,
23 /// Errors.
24 LOG_LEVEL_ERROR,
25 /// Fatal errors.
26 LOG_LEVEL_FATAL,
27 };
28
29 #define LOG_UNLIKELY(level, x, ...) \
30 do { \
31 if (unlikely(LOG_LEVEL_##level >= log_get_level_tls())) { \
32 log_printf(tls_logger, LOG_LEVEL_##level, __func__, x, ##__VA_ARGS__); \
33 } \
34 } while (0)
35
36 #define LOG_(level, x, ...) \
37 do { \
38 if (level >= log_get_level_tls()) { \
39 log_printf(tls_logger, level, __func__, x, ##__VA_ARGS__); \
40 } \
41 } while (0)
42 #define LOG(level, x, ...) LOG_(LOG_LEVEL_##level, x, ##__VA_ARGS__)
43 #define log_trace(x, ...) LOG_UNLIKELY(TRACE, x, ##__VA_ARGS__)
44 #define log_verbose(x, ...) LOG_UNLIKELY(VERBOSE, x, ##__VA_ARGS__)
45 #define log_debug(x, ...) LOG_UNLIKELY(DEBUG, x, ##__VA_ARGS__)
46 #define log_info(x, ...) LOG(INFO, x, ##__VA_ARGS__)
47 #define log_warn(x, ...) LOG(WARN, x, ##__VA_ARGS__)
48 #define log_error(x, ...) LOG(ERROR, x, ##__VA_ARGS__)
49 #define log_fatal(x, ...) LOG(FATAL, x, ##__VA_ARGS__)
50
51 #define log_error_errno(x, ...) LOG(ERROR, x ": %s", ##__VA_ARGS__, strerror(errno))
52
53 struct log;
54 struct log_target;
55
56 attr_printf(4, 5) void log_printf(struct log *, int level, const char *func,
57 const char *fmt, ...);
58
59 attr_malloc struct log *log_new(void);
60 /// Destroy a log struct and every log target added to it
61 attr_nonnull_all void log_destroy(struct log *);
62 attr_nonnull(1) void log_set_level(struct log *l, int level);
63 attr_pure enum log_level log_get_level(const struct log *l);
64 attr_nonnull_all void log_add_target(struct log *, struct log_target *);
65 attr_pure int string_to_log_level(const char *);
66 /// Remove a previously added log target for a log struct, and destroy it. If the log
67 /// target was never added, nothing happens.
68 void log_remove_target(struct log *l, struct log_target *tgt);
69
70 extern thread_local struct log *tls_logger;
71
72 /// Create a thread local logger
73 static inline void log_init_tls(void) {
74 tls_logger = log_new();
75 }
76 /// Set thread local logger log level
77 static inline void log_set_level_tls(int level) {
78 assert(tls_logger);
79 log_set_level(tls_logger, level);
80 }
81 static inline attr_nonnull_all void log_add_target_tls(struct log_target *tgt) {
82 assert(tls_logger);
83 log_add_target(tls_logger, tgt);
84 }
85
86 static inline attr_nonnull_all void log_remove_target_tls(struct log_target *tgt) {
87 assert(tls_logger);
88 log_remove_target(tls_logger, tgt);
89 }
90
91 static inline attr_pure enum log_level log_get_level_tls(void) {
92 assert(tls_logger);
93 return log_get_level(tls_logger);
94 }
95
96 static inline void log_deinit_tls(void) {
97 assert(tls_logger);
98 log_destroy(tls_logger);
99 tls_logger = NULL;
100 }
101
102 attr_malloc struct log_target *stderr_logger_new(void);
103 attr_malloc struct log_target *file_logger_new(const char *file);
104 attr_malloc struct log_target *null_logger_new(void);
105 attr_malloc struct log_target *gl_string_marker_logger_new(void);
106
107 // vim: set noet sw=8 ts=8: