feat: allow setting log level

c-web-server
Jef Roosens 2023-05-31 13:09:16 +02:00
parent eff1b13065
commit 3b1df332a3
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
3 changed files with 12 additions and 1 deletions

View File

@ -6,6 +6,8 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
extern const char *log_level_names[];
typedef enum log_level { typedef enum log_level {
log_level_debug = 0, log_level_debug = 0,
log_level_info = 1, log_level_info = 1,
@ -14,10 +16,12 @@ typedef enum log_level {
log_level_critical = 4, log_level_critical = 4,
} log_level; } log_level;
extern const char *log_level_names[]; extern log_level _log_level;
void _lander_log(log_level level, FILE *f, const char *fmt, ...); 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 flog(level, f, ...) _lander_log(level, f, __VA_ARGS__)
#define log(level, ...) _lander_log(level, NULL, __VA_ARGS__) #define log(level, ...) _lander_log(level, NULL, __VA_ARGS__)
#define debug(...) _lander_log(log_level_debug, NULL, __VA_ARGS__) #define debug(...) _lander_log(log_level_debug, NULL, __VA_ARGS__)

View File

@ -45,6 +45,7 @@ static bool add_entry(char **key_ptr, int *key_len_ptr, http_loop_ctx *ctx,
buf[key_len + 1] = '\0'; buf[key_len + 1] = '\0';
http_res_add_header(&ctx->res, http_header_location, buf, true); http_res_add_header(&ctx->res, http_header_location, buf, true);
ctx->res.status = http_created;
if (key_ptr != NULL) { if (key_ptr != NULL) {
*key_ptr = key; *key_ptr = key;

View File

@ -5,7 +5,13 @@
const char *log_level_names[] = {"DEBUG", "INFO ", "WARN ", "ERROR", const char *log_level_names[] = {"DEBUG", "INFO ", "WARN ", "ERROR",
"CRITICAL"}; "CRITICAL"};
log_level _log_level = log_level_debug;
void _lander_log(log_level level, FILE *f, const char *fmt, ...) { void _lander_log(log_level level, FILE *f, const char *fmt, ...) {
if (level < _log_level) {
return;
}
// Log to stdout by default // Log to stdout by default
f = (f == NULL) ? stdout : f; f = (f == NULL) ? stdout : f;