From: Michael Tremer Date: Sat, 10 Aug 2024 11:40:22 +0000 (+0000) Subject: logging: Add convenience functions to call the logger without having a context X-Git-Tag: 0.9.30~1224 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0b366d67fd6dfd791b0420ce4c4b13adce29ea9c;p=pakfire.git logging: Add convenience functions to call the logger without having a context Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/include/pakfire/logging.h b/src/libpakfire/include/pakfire/logging.h index 643b58d9c..b85b00213 100644 --- a/src/libpakfire/include/pakfire/logging.h +++ b/src/libpakfire/include/pakfire/logging.h @@ -57,29 +57,36 @@ static inline void __attribute__((always_inline, format(printf, 2, 3))) # define CTX_DEBUG pakfire_ctx_log_null #endif -// Below is the legacy logging mechanism +#define PAKFIRE_INFO(pakfire, arg...) pakfire_log_condition(pakfire, LOG_INFO, ## arg) +#define PAKFIRE_ERROR(pakfire, arg...) pakfire_log_condition(pakfire, LOG_ERR, ## arg) -#ifdef PAKFIRE_LEGACY_LOGGING +#define pakfire_log_condition(pakfire, level, arg...) \ + do { \ + if (pakfire_get_log_level(pakfire) >= level) \ + pakfire_log(pakfire, level, __FILE__, __LINE__, __FUNCTION__, ## arg); \ + } while (0) // This function does absolutely nothing static inline void __attribute__((always_inline, format(printf, 2, 3))) - pakfire_log_null(struct pakfire* pakfire, const char *format, ...) {} + pakfire_log_null(struct pakfire_ctx* ctx, const char *format, ...) {} + +#ifdef ENABLE_DEBUG +# define PAKFIRE_DEBUG(pakfire, arg...) pakfire_log_condition(pakfire, LOG_DEBUG, ## arg) +#else +# define PAKFIRE_DEBUG pakfire_log_null +#endif -#define pakfire_log_condition(pakfire, prio, r, arg...) \ - pakfire_log(pakfire, prio, r, __FILE__, __LINE__, __FUNCTION__, ## arg) +// Below is the legacy logging mechanism -#define INFO(pakfire, arg...) pakfire_log_condition(pakfire, LOG_INFO, 0, ## arg) -#define ERROR(pakfire, arg...) pakfire_log_condition(pakfire, LOG_ERR, 0, ## arg) +#ifdef PAKFIRE_LEGACY_LOGGING -#define INFO_ERRNO(pakfire, r, arg...) pakfire_log_condition(pakfire, LOG_INFO, r, ## arg) -#define ERROR_ERRNO(pakfire, r, arg...) pakfire_log_condition(pakfire, LOG_ERR, r, ## arg) +#define INFO(pakfire, arg...) pakfire_log_condition(pakfire, LOG_INFO, ## arg) +#define ERROR(pakfire, arg...) pakfire_log_condition(pakfire, LOG_ERR, ## arg) #ifdef ENABLE_DEBUG -# define DEBUG(pakfire, arg...) pakfire_log_condition(pakfire, LOG_DEBUG, 0, ## arg) -# define DEBUG_ERRNO(pakfire, r, arg...) pakfire_log_condition(pakfire, LOG_DEBUG, r, ## arg) +# define DEBUG(pakfire, arg...) pakfire_log_condition(pakfire, LOG_DEBUG, ## arg) #else # define DEBUG pakfire_log_null -# define DEBUG_ERRNO pakfire_log_null #endif #endif /* PAKFIRE_LEGACY_LOGGING */ diff --git a/src/libpakfire/include/pakfire/pakfire.h b/src/libpakfire/include/pakfire/pakfire.h index fde9ac212..bae86bd89 100644 --- a/src/libpakfire/include/pakfire/pakfire.h +++ b/src/libpakfire/include/pakfire/pakfire.h @@ -109,9 +109,12 @@ gid_t pakfire_gid(struct pakfire* pakfire); const struct pakfire_subid* pakfire_subuid(struct pakfire* pakfire); const struct pakfire_subid* pakfire_subgid(struct pakfire* pakfire); -void pakfire_log(struct pakfire* pakfire, int priority, int r, const char *file, - int line, const char *fn, const char *format, ...) - __attribute__((format(printf, 7, 8))); +// Logging + +int pakfire_get_log_level(struct pakfire* pakfire); + +void pakfire_log(struct pakfire* pakfire, int level, const char* file, int line, + const char* fn, const char* format, ...) __attribute__((format(printf, 6, 7))); // Locking int pakfire_acquire_lock(struct pakfire* pakfire); diff --git a/src/libpakfire/pakfire.c b/src/libpakfire/pakfire.c index 99cc684a1..9478e4cb4 100644 --- a/src/libpakfire/pakfire.c +++ b/src/libpakfire/pakfire.c @@ -1487,24 +1487,20 @@ PAKFIRE_EXPORT int pakfire_search(struct pakfire* pakfire, const char* what, int // Logging -// XXX This function is deprecated and needs to be removed -void pakfire_log(struct pakfire* pakfire, int priority, int r, +int pakfire_get_log_level(struct pakfire* pakfire) { + return pakfire_ctx_get_log_level(pakfire->ctx); +} + +void pakfire_log(struct pakfire* pakfire, int priority, const char* file, int line, const char* fn, const char* format, ...) { char* buffer = NULL; va_list args; - - // Save errno - int saved_errno = errno; - if (r) - errno = r; + int r; va_start(args, format); r = vasprintf(&buffer, format, args); va_end(args); - // Restore errno - errno = saved_errno; - if (r < 0) return;