From: Michael Tremer Date: Sun, 15 Oct 2023 14:52:47 +0000 (+0000) Subject: ctx: Copy the log framework X-Git-Tag: 0.9.30~1500 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=67550e47e3058827aedfb841e6b5491579b2fbb5;p=pakfire.git ctx: Copy the log framework Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/ctx.c b/src/libpakfire/ctx.c index 6088f78fa..d22af95f7 100644 --- a/src/libpakfire/ctx.c +++ b/src/libpakfire/ctx.c @@ -20,6 +20,7 @@ #include #include +#include #include #include #include @@ -144,3 +145,16 @@ PAKFIRE_EXPORT void pakfire_ctx_set_log_callback(struct pakfire_ctx* ctx, ctx->log.callback = callback; ctx->log.data = data; } + +void pakfire_ctx_log(struct pakfire_ctx* ctx, int level, const char* file, int line, + const char* fn, const char* format, ...) { + va_list args; + + // Return if the callback hasn't been set + if (!ctx->log.callback) + return; + + va_start(args, format); + ctx->log.callback(ctx->log.data, level, file, line, fn, format, args); + va_end(args); +} diff --git a/src/libpakfire/include/pakfire/ctx.h b/src/libpakfire/include/pakfire/ctx.h index feef7a333..e5c4980f3 100644 --- a/src/libpakfire/include/pakfire/ctx.h +++ b/src/libpakfire/include/pakfire/ctx.h @@ -38,4 +38,11 @@ void pakfire_ctx_set_log_level(struct pakfire_ctx* ctx, int level); void pakfire_ctx_set_log_callback(struct pakfire_ctx* ctx, pakfire_log_callback callback, void* data); +#ifdef PAKFIRE_PRIVATE + +void pakfire_ctx_log(struct pakfire_ctx* ctx, int level, const char* file, int line, + const char* fn, const char* format, ...) __attribute__((format(printf, 6, 7))); + +#endif /* PAKFIRE_PRIVATE */ + #endif /* PAKFIRE_CTX_H */ diff --git a/src/libpakfire/include/pakfire/logging.h b/src/libpakfire/include/pakfire/logging.h index f9f35bbd7..5193a259d 100644 --- a/src/libpakfire/include/pakfire/logging.h +++ b/src/libpakfire/include/pakfire/logging.h @@ -36,6 +36,29 @@ void pakfire_log_syslog(void* data, int priority, const char* file, #ifdef PAKFIRE_PRIVATE +#include + +#define pakfire_ctx_log_condition(ctx, level, r, arg...) \ + do { \ + if (pakfire_ctx_get_log_level(pakfire) >= level) \ + pakfire_ctx_log(ctx, level, r, __FILE__, __LINE__, __FUNCTION__, ## arg); \ + } while (0) + +// This function does absolutely nothing +static inline void __attribute__((always_inline, format(printf, 2, 3))) + pakfire_ctx_log_null(struct pakfire_ctx* ctx, const char *format, ...) {} + +#define CTX_INFO(ctx, arg...) pakfire_ctx_log_condition(ctx, LOG_INFO, 0, ## arg) +#define CTX_ERROR(ctx, arg...) pakfire_ctx_log_condition(ctx, LOG_ERR, 0, ## arg) + +#ifdef ENABLE_DEBUG +# define CTX_DEBUG(ctx, arg...) pakfire_ctx_log_condition(ctx, LOG_DEBUG, 0, ## arg) +#else +# define CTX_DEBUG pakfire_ctx_log_null +#endif + +// Below is the legacy logging mechanism + // This function does absolutely nothing static inline void __attribute__((always_inline, format(printf, 2, 3))) pakfire_log_null(struct pakfire* pakfire, const char *format, ...) {}