diff --git a/inc/common/macro.h b/inc/common/macro.h index 5e0aebdf35305f9b84bd78f35879b41fe8a10b9c..fe3ad89047837b868929bff89a6791e462b7415c 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 0894ea89f2c80d6fba2345dc195c7f183d4ed73a..06d37addec3f2235d0aa26fd929305d01cec64fe 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 80a3d9e2419a8d48f35cf212e5e710009a62d0e1..a6f9e36b0ea9d7674c666192b180d15de648f752 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 c57b2fba30cb159e5f864a59bec8db3ced0c5b54..f9b139114cbe82a2eee073fc2112a7aa2f5d4bcc 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 93be3cbf3c47f4f724f7c4c69d212dfbd054bf30..daad13d8ace43a705fab6f62d904f8fca86c571f 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; }