From 3aaacce02e86d849764e33fa2fd245365c7caeb5 Mon Sep 17 00:00:00 2001 From: Kubat <mael.martin31@gmail.com> Date: Thu, 14 May 2020 12:15:24 +0200 Subject: [PATCH] Can now change loglevel --- inc/common/macro.h | 27 +++++++++++++++++---------- src/common.c | 2 ++ src/database/queue.c | 10 +++++----- src/module/repo.c | 10 ++++++---- src/net/listen.c | 18 +++++------------- 5 files changed, 35 insertions(+), 32 deletions(-) diff --git a/inc/common/macro.h b/inc/common/macro.h index 5e0aebdf..fe3ad890 100644 --- a/inc/common/macro.h +++ b/inc/common/macro.h @@ -45,16 +45,23 @@ err_flag = errno != 0 || endptr == str; \ } -#define ERROR 1 -#define WARN 2 -#define INFO 3 -#define DEBUG 4 -#define LOG(format, level, section, ...) \ - fprintf(stderr, " %c %s%s: " format "\n", \ - level == ERROR ? '!' : level == WARN ? '*' : level == INFO ? '.' : ' ', \ - sizeof(section) > sizeof("") ? ("[" section "] ") : "", \ - __func__, \ - __VA_ARGS__) +extern int log_level; +enum log_level { + ERROR = 1, + WARN = 2, + INFO = 3, + DEBUG = 4, +}; + +#define LOG(format, level, section, ...) \ + { \ + if (level >= log_level) \ + fprintf(stderr, " %c %s%s: " format "\n", \ + level == ERROR ? '!' : level == WARN ? '*' : level == INFO ? '.' : ' ', \ + sizeof(section) > sizeof("") ? ("[" section "] ") : "", \ + __func__, \ + __VA_ARGS__); \ + } #define LOG_INFO(section, format, ...) LOG(format, INFO, section, __VA_ARGS__) #define LOG_WARN(section, format, ...) LOG(format, WARN, section, __VA_ARGS__) #define LOG_ERROR(section, format, ...) LOG(format, ERROR, section, __VA_ARGS__) diff --git a/src/common.c b/src/common.c index 0894ea89..06d37add 100644 --- a/src/common.c +++ b/src/common.c @@ -8,6 +8,8 @@ #include <stdarg.h> #include <sys/stat.h> +int log_level = INFO; + void __not_implemented(const char *func, char *file, int line) { diff --git a/src/database/queue.c b/src/database/queue.c index 80a3d9e2..a6f9e36b 100644 --- a/src/database/queue.c +++ b/src/database/queue.c @@ -115,12 +115,12 @@ error: return ret; } -#define reorder(db, prio, error) \ - if (prio > 1) { \ - if (__queue_reorder(db)) \ +#define reorder(db, prio, error) \ + if (prio > 1) { \ + if (__queue_reorder(db)) { \ LOG_INFO("DB", "%s", "Queue has been reordered"); \ - else \ - goto error; \ + } else \ + goto error; \ } static bool diff --git a/src/module/repo.c b/src/module/repo.c index c57b2fba..f9b13911 100644 --- a/src/module/repo.c +++ b/src/module/repo.c @@ -250,8 +250,10 @@ __download_kara(const char *url, const char *path, int override) retest: if (fd < 0) { - if (errno == EEXIST && ! override) + if (errno == EEXIST && ! override) { LOG_ERROR("REPO", "File '%s' already exists", path); + return 1; + } else if (errno == EEXIST && override) { if (unlink(path)) { @@ -264,10 +266,10 @@ retest: goto retest; } - else + else { LOG_ERROR("REPO", "Could not open file '%s'", path); - - return 1; + return 1; + } } struct file file = { diff --git a/src/net/listen.c b/src/net/listen.c index 93be3cbf..daad13d8 100644 --- a/src/net/listen.c +++ b/src/net/listen.c @@ -129,10 +129,8 @@ send_status(struct lkt_state *srv, size_t c, int status, const char *cmd_name) LOG_INFO("COMMAND", "Command '%s'", cmd_name); send_ok(srv, c); } else { - if (status == 2) - LOG_INFO("COMMAND", "Unknown command '%s'", cmd_name); - else - LOG_INFO("COMMAND", "Command failed '%s'", cmd_name); + LOG_INFO("COMMAND", "Command '%s' %s", cmd_name, + status == 2 ? "is unknown" : "has failed"); send_ack(srv, c, cmd_name); } } @@ -480,10 +478,8 @@ failure: freeaddrinfo(available); if (fd < 0) { - if (host) - LOG_ERROR("NETWORK", "Failed to bind to %s:%s", host, port); - else - LOG_ERROR("NETWORK", "Failed to bind to port %s", port); + LOG_ERROR("NETWORK", "Failed to bind to %s:%s", + host ? host : "0.0.0.0", port); return -1; } @@ -492,11 +488,7 @@ failure: return -1; } - if (host) - LOG_INFO("NETWORK", "Listening on %s:%s", host, port); - else - LOG_INFO("NETWORK", "Listening on port %s", port); - + LOG_INFO("NETWORK", "Listening on %s:%s", host ? host : "0.0.0.0", port); return fd; } -- GitLab