]> git.ipfire.org Git - pakfire.git/commitdiff
ctx: Copy the log framework
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 15 Oct 2023 14:52:47 +0000 (14:52 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 15 Oct 2023 14:52:47 +0000 (14:52 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/ctx.c
src/libpakfire/include/pakfire/ctx.h
src/libpakfire/include/pakfire/logging.h

index 6088f78faa0e0320054d4641873fe32829e526f8..d22af95f715aff4a40b08c8d5e092c6a083f0861 100644 (file)
@@ -20,6 +20,7 @@
 
 #include <ctype.h>
 #include <errno.h>
+#include <stdarg.h>
 #include <stdlib.h>
 #include <string.h>
 #include <syslog.h>
@@ -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);
+}
index feef7a33325e7683566e2bac6733d09144e9e4f5..e5c4980f36efc2b4c900b3ebd91ee87a110544da 100644 (file)
@@ -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 */
index f9f35bbd775f4d8370a5ed3d0bd8b794dc2a52e4..5193a259d21b0dae02fc639df5ef736c046b55e0 100644 (file)
@@ -36,6 +36,29 @@ void pakfire_log_syslog(void* data, int priority, const char* file,
 
 #ifdef PAKFIRE_PRIVATE
 
+#include <pakfire/ctx.h>
+
+#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, ...) {}