From 3b1df332a32ff60e1cbe40439389f5965aa6ed11 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Wed, 31 May 2023 13:09:16 +0200 Subject: [PATCH] feat: allow setting log level --- include/log.h | 6 +++++- src/lander/lander_post.c | 1 + src/log.c | 6 ++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/include/log.h b/include/log.h index 2a33278..dbb4fbc 100644 --- a/include/log.h +++ b/include/log.h @@ -6,6 +6,8 @@ #include #include +extern const char *log_level_names[]; + typedef enum log_level { log_level_debug = 0, log_level_info = 1, @@ -14,10 +16,12 @@ typedef enum log_level { log_level_critical = 4, } log_level; -extern const char *log_level_names[]; +extern log_level _log_level; void _lander_log(log_level level, FILE *f, const char *fmt, ...); +#define log_level(level) _log_level = level + #define flog(level, f, ...) _lander_log(level, f, __VA_ARGS__) #define log(level, ...) _lander_log(level, NULL, __VA_ARGS__) #define debug(...) _lander_log(log_level_debug, NULL, __VA_ARGS__) diff --git a/src/lander/lander_post.c b/src/lander/lander_post.c index bb00e14..5d0c688 100644 --- a/src/lander/lander_post.c +++ b/src/lander/lander_post.c @@ -45,6 +45,7 @@ static bool add_entry(char **key_ptr, int *key_len_ptr, http_loop_ctx *ctx, buf[key_len + 1] = '\0'; http_res_add_header(&ctx->res, http_header_location, buf, true); + ctx->res.status = http_created; if (key_ptr != NULL) { *key_ptr = key; diff --git a/src/log.c b/src/log.c index b6b0ab6..1ca0bc7 100644 --- a/src/log.c +++ b/src/log.c @@ -5,7 +5,13 @@ const char *log_level_names[] = {"DEBUG", "INFO ", "WARN ", "ERROR", "CRITICAL"}; +log_level _log_level = log_level_debug; + void _lander_log(log_level level, FILE *f, const char *fmt, ...) { + if (level < _log_level) { + return; + } + // Log to stdout by default f = (f == NULL) ? stdout : f;