From: Emil Velikov Date: Tue, 15 Oct 2024 19:36:50 +0000 (+0100) Subject: Always define and use ENABLE_DEBUG X-Git-Tag: v34~223 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=8c1c901e9e429a2a7a3f564656b5267d80bfc700;p=thirdparty%2Fkmod.git Always define and use ENABLE_DEBUG Convert the "if defined FOO" pre-processor checks for compiler ones "if (FOO == 1)". This makes things easier to reason with and ensures both code-paths are build-tested. In case, the option is disabled DCE will kick in (assuming you're not force disabling all optimisations) and remove the respective code. Signed-off-by: Emil Velikov Link: https://github.com/kmod-project/kmod/pull/173 Signed-off-by: Lucas De Marchi --- diff --git a/configure.ac b/configure.ac index 500c2179..96749f4b 100644 --- a/configure.ac +++ b/configure.ac @@ -216,6 +216,8 @@ AC_ARG_ENABLE([debug], [], [enable_debug=no]) AS_IF([test "x$enable_debug" = "xyes"], [ AC_DEFINE(ENABLE_DEBUG, [1], [Debug messages.]) +], [ + AC_DEFINE(ENABLE_DEBUG, [0], [Debug messages.]) ]) AC_ARG_ENABLE([coverage], diff --git a/libkmod/libkmod-internal.h b/libkmod/libkmod-internal.h index b4828887..cdbd33cf 100644 --- a/libkmod/libkmod-internal.h +++ b/libkmod/libkmod-internal.h @@ -10,22 +10,15 @@ #include "libkmod.h" -static _always_inline_ _printf_format_(2, 3) void kmod_log_null(const struct kmod_ctx *ctx, - const char *format, ...) -{ -} - -#define kmod_log_cond(ctx, prio, arg...) \ - do { \ - if (ENABLE_LOGGING == 1 && kmod_get_log_priority(ctx) >= prio) \ - kmod_log(ctx, prio, __FILE__, __LINE__, __func__, ##arg); \ +#define kmod_log_cond(ctx, prio, arg...) \ + do { \ + if (ENABLE_LOGGING == 1 && \ + (ENABLE_DEBUG == 1 || (!ENABLE_DEBUG && prio != LOG_DEBUG)) && \ + kmod_get_log_priority(ctx) >= prio) \ + kmod_log(ctx, prio, __FILE__, __LINE__, __func__, ##arg); \ } while (0) -#ifdef ENABLE_DEBUG #define DBG(ctx, arg...) kmod_log_cond(ctx, LOG_DEBUG, ##arg) -#else -#define DBG(ctx, arg...) kmod_log_null(ctx, ##arg) -#endif #define NOTICE(ctx, arg...) kmod_log_cond(ctx, LOG_NOTICE, ##arg) #define INFO(ctx, arg...) kmod_log_cond(ctx, LOG_INFO, ##arg) #define ERR(ctx, arg...) kmod_log_cond(ctx, LOG_ERR, ##arg) diff --git a/libkmod/libkmod.c b/libkmod/libkmod.c index 16b09de7..51a10b48 100644 --- a/libkmod/libkmod.c +++ b/libkmod/libkmod.c @@ -116,11 +116,10 @@ _printf_format_(6, 0) static void log_filep(void *data, int priority, const char snprintf(buf, sizeof(buf), "L:%d", priority); priname = buf; } -#ifdef ENABLE_DEBUG - fprintf(fp, "libkmod: %s %s:%d %s: ", priname, file, line, fn); -#else - fprintf(fp, "libkmod: %s: %s: ", priname, fn); -#endif + if (ENABLE_DEBUG == 1) + fprintf(fp, "libkmod: %s %s:%d %s: ", priname, file, line, fn); + else + fprintf(fp, "libkmod: %s: %s: ", priname, fn); vfprintf(fp, format, args); } diff --git a/meson.build b/meson.build index ed161257..aac442f1 100644 --- a/meson.build +++ b/meson.build @@ -19,9 +19,7 @@ cdata.set_quoted('PACKAGE', meson.project_name()) cdata.set_quoted('VERSION', meson.project_version()) cdata.set10('ENABLE_LOGGING', get_option('logging')) -if get_option('debug-messages') - cdata.set('ENABLE_DEBUG', true) -endif +cdata.set10('ENABLE_DEBUG', get_option('debug-messages')) pkg = import('pkgconfig') cc = meson.get_compiler('c') diff --git a/tools/log.c b/tools/log.c index 8bd6a595..e2e34cae 100644 --- a/tools/log.c +++ b/tools/log.c @@ -62,19 +62,19 @@ _printf_format_(6, 0) static void log_kmod(void *data, int priority, const char return; if (log_use_syslog) { -#ifdef ENABLE_DEBUG - syslog(priority, "%s: %s:%d %s() %s", prioname, file, line, fn, str); -#else - syslog(priority, "%s: %s", prioname, str); -#endif + if (ENABLE_DEBUG == 1) + syslog(priority, "%s: %s:%d %s() %s", prioname, file, line, fn, + str); + else + syslog(priority, "%s: %s", prioname, str); } else { -#ifdef ENABLE_DEBUG - fprintf(stderr, "%s: %s: %s:%d %s() %s", program_invocation_short_name, - prioname, file, line, fn, str); -#else - fprintf(stderr, "%s: %s: %s", program_invocation_short_name, prioname, - str); -#endif + if (ENABLE_DEBUG == 1) + fprintf(stderr, "%s: %s: %s:%d %s() %s", + program_invocation_short_name, prioname, file, line, fn, + str); + else + fprintf(stderr, "%s: %s: %s", program_invocation_short_name, + prioname, str); } free(str);