Add log_remove_target

Don't need to deinit+init struct log any more just to switch log target
:)

Signed-off-by: Yuxuan Shui <yshuiv7@gmail.com>
This commit is contained in:
Yuxuan Shui
2019-02-07 21:48:39 +00:00
parent 049cc94e7a
commit 68c77cd7d8
3 changed files with 37 additions and 17 deletions

View File

@@ -17,12 +17,11 @@ enum log_level {
LOG_LEVEL_FATAL,
};
#define LOG(level, x, ...) \
do { \
if (LOG_LEVEL_##level >= log_get_level_tls()) { \
log_printf(tls_logger, LOG_LEVEL_##level, __func__, x, \
##__VA_ARGS__); \
} \
#define LOG(level, x, ...) \
do { \
if (LOG_LEVEL_##level >= log_get_level_tls()) { \
log_printf(tls_logger, LOG_LEVEL_##level, __func__, x, ##__VA_ARGS__); \
} \
} while (0)
#define log_trace(x, ...) LOG(TRACE, x, ##__VA_ARGS__)
#define log_debug(x, ...) LOG(DEBUG, x, ##__VA_ARGS__)
@@ -38,11 +37,15 @@ attr_printf(4, 5) void log_printf(struct log *, int level, const char *func,
const char *fmt, ...);
attr_malloc struct log *log_new(void);
/// Destroy a log struct and every log target added to it
attr_nonnull_all void log_destroy(struct log *);
attr_nonnull(1) void log_set_level(struct log *l, int level);
attr_pure enum log_level log_get_level(const struct log *l);
attr_nonnull_all void log_add_target(struct log *, struct log_target *);
attr_const enum log_level string_to_log_level(const char *);
/// Remove a previously added log target for a log struct, and destroy it. If the log
/// target was never added, nothing happens.
void log_remove_target(struct log *l, struct log_target *tgt);
extern thread_local struct log *tls_logger;
@@ -60,6 +63,11 @@ static inline attr_nonnull_all void log_add_target_tls(struct log_target *tgt) {
log_add_target(tls_logger, tgt);
}
static inline attr_nonnull_all void log_remove_target_tls(struct log_target *tgt) {
assert(tls_logger);
log_remove_target(tls_logger, tgt);
}
static inline attr_pure enum log_level log_get_level_tls(void) {
assert(tls_logger);
return log_get_level(tls_logger);