]> git.ipfire.org Git - pakfire.git/commitdiff
ctx: Add a log callback
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 15 Oct 2023 14:27:33 +0000 (14:27 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 15 Oct 2023 14:27:33 +0000 (14:27 +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
src/libpakfire/include/pakfire/pakfire.h
src/libpakfire/libpakfire.sym
tests/parser/test.c
tests/testsuite.c

index b2555cface003c445d670528609959b14ce67ca8..6088f78faa0e0320054d4641873fe32829e526f8 100644 (file)
@@ -25,6 +25,7 @@
 #include <syslog.h>
 
 #include <pakfire/ctx.h>
+#include <pakfire/logging.h>
 #include <pakfire/private.h>
 
 struct pakfire_ctx {
@@ -34,6 +35,10 @@ struct pakfire_ctx {
        // Logging
        struct pakfire_ctx_log {
                int level;
+
+               // Callback
+               pakfire_log_callback callback;
+               void* data;
        } log;
 };
 
@@ -71,6 +76,9 @@ static int pakfire_ctx_setup_logging(struct pakfire_ctx* ctx) {
        // Store the log level
        pakfire_ctx_set_log_level(ctx, level);
 
+       // Log to syslog by default
+       pakfire_ctx_set_log_callback(ctx, pakfire_log_syslog, NULL);
+
        return 0;
 }
 
@@ -130,3 +138,9 @@ PAKFIRE_EXPORT int pakfire_ctx_get_log_level(struct pakfire_ctx* ctx) {
 PAKFIRE_EXPORT void pakfire_ctx_set_log_level(struct pakfire_ctx* ctx, int level) {
        ctx->log.level = level;
 }
+
+PAKFIRE_EXPORT void pakfire_ctx_set_log_callback(struct pakfire_ctx* ctx,
+               pakfire_log_callback callback, void* data) {
+       ctx->log.callback = callback;
+       ctx->log.data     = data;
+}
index 169716d33bb8c9d10cdfd5a31b202089fa9c2847..feef7a33325e7683566e2bac6733d09144e9e4f5 100644 (file)
@@ -23,6 +23,8 @@
 
 struct pakfire_ctx;
 
+#include <pakfire/logging.h>
+
 int pakfire_ctx_create(struct pakfire_ctx** ctx);
 
 struct pakfire_ctx* pakfire_ctx_ref(struct pakfire_ctx* ctx);
@@ -33,4 +35,7 @@ struct pakfire_ctx* pakfire_ctx_unref(struct pakfire_ctx* ctx);
 int pakfire_ctx_get_log_level(struct pakfire_ctx* ctx);
 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);
+
 #endif /* PAKFIRE_CTX_H */
index d9d3271d618bb0cdd2662237cae0f21cf4396639..f9f35bbd775f4d8370a5ed3d0bd8b794dc2a52e4 100644 (file)
 #ifndef PAKFIRE_LOGGING_H
 #define PAKFIRE_LOGGING_H
 
+#include <stdarg.h>
 #include <syslog.h>
 
+typedef void (*pakfire_log_callback)(void* data, int priority, const char* file,
+       int line, const char* fn, const char* format, va_list args);
+
 #include <pakfire/pakfire.h>
 
 void pakfire_log_stderr(void* data, int priority, const char* file,
index dbf12b9f925a96ac442a4d257930f1f3ae27154e..14a186fcf2707cacaa010809583a1cea90162f89 100644 (file)
@@ -32,6 +32,7 @@ struct pakfire;
 #include <pakfire/ctx.h>
 #include <pakfire/filelist.h>
 #include <pakfire/key.h>
+#include <pakfire/logging.h>
 #include <pakfire/packagelist.h>
 #include <pakfire/parser.h>
 #include <pakfire/progress.h>
@@ -46,8 +47,6 @@ enum pakfire_flags {
 };
 
 // Callbacks
-typedef void (*pakfire_log_callback)(void* data, int priority, const char* file,
-        int line, const char* fn, const char* format, va_list args);
 void pakfire_set_log_callback(struct pakfire* pakfire,
        pakfire_log_callback callback, void* data);
 typedef int (*pakfire_confirm_callback)(struct pakfire* pakfire, void* data,
index a40107e7a4184e167380a000b59a8efdd632ef4c..c221aabb0263d49ffc68eb41489c425b24304a8c 100644 (file)
@@ -26,6 +26,7 @@ global:
        pakfire_ctx_unref;
        pakfire_ctx_get_log_level;
        pakfire_ctx_set_log_level;
+       pakfire_ctx_set_log_callback;
 
        # pakfire
        pakfire_check;
index 55a98cdb9508e6d5d299e8cee2ca0e1ab4b5334b..acac5f7d70c728f6c5f81a27699b5f588fcf1bc0 100644 (file)
@@ -51,6 +51,9 @@ int main(int argc, const char* argv[]) {
        // Set the log level to DEBUG
        pakfire_ctx_set_log_level(ctx, LOG_DEBUG);
 
+       // Log everything to the console
+       pakfire_ctx_set_log_callback(ctx, pakfire_log_stderr, NULL);
+
        // Create a pakfire instance
        r = pakfire_create(&pakfire, ctx, root, NULL, NULL,
                PAKFIRE_FLAGS_DEBUG, pakfire_log_stderr, NULL);
index 9428f08d79dae44c054f93a1f5c00755456535f3..8bbe1c0fca44295a4ef26386963f170dcf38465f 100644 (file)
@@ -61,6 +61,9 @@ static int test_run(int i, struct test* t) {
        // Set the log level to DEBUG
        pakfire_ctx_set_log_level(ctx, LOG_DEBUG);
 
+       // Log everything to the console
+       pakfire_ctx_set_log_callback(ctx, pakfire_log_stderr, NULL);
+
        // Open the configuration file
        c = fopen(TEST_SRC_PATH "/pakfire.conf", "r");
        if (!c) {