From: Michael Tremer Date: Sun, 15 Oct 2023 14:27:33 +0000 (+0000) Subject: ctx: Add a log callback X-Git-Tag: 0.9.30~1501 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6641e7801f828ccab763dfa413304945717bf0bc;p=people%2Fms%2Fpakfire.git ctx: Add a log callback Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/ctx.c b/src/libpakfire/ctx.c index b2555cfac..6088f78fa 100644 --- a/src/libpakfire/ctx.c +++ b/src/libpakfire/ctx.c @@ -25,6 +25,7 @@ #include #include +#include #include 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; +} diff --git a/src/libpakfire/include/pakfire/ctx.h b/src/libpakfire/include/pakfire/ctx.h index 169716d33..feef7a333 100644 --- a/src/libpakfire/include/pakfire/ctx.h +++ b/src/libpakfire/include/pakfire/ctx.h @@ -23,6 +23,8 @@ struct pakfire_ctx; +#include + 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 */ diff --git a/src/libpakfire/include/pakfire/logging.h b/src/libpakfire/include/pakfire/logging.h index d9d3271d6..f9f35bbd7 100644 --- a/src/libpakfire/include/pakfire/logging.h +++ b/src/libpakfire/include/pakfire/logging.h @@ -21,8 +21,12 @@ #ifndef PAKFIRE_LOGGING_H #define PAKFIRE_LOGGING_H +#include #include +typedef void (*pakfire_log_callback)(void* data, int priority, const char* file, + int line, const char* fn, const char* format, va_list args); + #include void pakfire_log_stderr(void* data, int priority, const char* file, diff --git a/src/libpakfire/include/pakfire/pakfire.h b/src/libpakfire/include/pakfire/pakfire.h index dbf12b9f9..14a186fcf 100644 --- a/src/libpakfire/include/pakfire/pakfire.h +++ b/src/libpakfire/include/pakfire/pakfire.h @@ -32,6 +32,7 @@ struct pakfire; #include #include #include +#include #include #include #include @@ -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, diff --git a/src/libpakfire/libpakfire.sym b/src/libpakfire/libpakfire.sym index a40107e7a..c221aabb0 100644 --- a/src/libpakfire/libpakfire.sym +++ b/src/libpakfire/libpakfire.sym @@ -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; diff --git a/tests/parser/test.c b/tests/parser/test.c index 55a98cdb9..acac5f7d7 100644 --- a/tests/parser/test.c +++ b/tests/parser/test.c @@ -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); diff --git a/tests/testsuite.c b/tests/testsuite.c index 9428f08d7..8bbe1c0fc 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -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) {