From: Michael Tremer Date: Sun, 15 Oct 2023 14:09:29 +0000 (+0000) Subject: ctx: Add log level X-Git-Tag: 0.9.30~1502 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=273ce4d5392d86cd66342087c140c4953328482f;p=pakfire.git ctx: Add log level Signed-off-by: Michael Tremer --- diff --git a/src/_pakfire/_pakfiremodule.c b/src/_pakfire/_pakfiremodule.c index a93133516..abdb21021 100644 --- a/src/_pakfire/_pakfiremodule.c +++ b/src/_pakfire/_pakfiremodule.c @@ -20,6 +20,7 @@ #include #include +#include #include #include @@ -58,6 +59,9 @@ static int initialize_context(void) { goto ERROR; } + // Set the log level to DEBUG + pakfire_ctx_set_log_level(pakfire_ctx, LOG_DEBUG); + ERROR: if (pakfire_ctx) pakfire_ctx_unref(pakfire_ctx); diff --git a/src/libpakfire/ctx.c b/src/libpakfire/ctx.c index c557313a5..b2555cfac 100644 --- a/src/libpakfire/ctx.c +++ b/src/libpakfire/ctx.c @@ -18,8 +18,11 @@ # # #############################################################################*/ +#include #include #include +#include +#include #include #include @@ -27,14 +30,57 @@ struct pakfire_ctx { // Reference counter int nrefs; + + // Logging + struct pakfire_ctx_log { + int level; + } log; }; +static int parse_log_level(const char* level) { + char* end = NULL; + + int l = strtol(level, &end, 10); + + if (*end == '\0' || isspace(*end)) + return l; + + if (strcmp(level, "error") == 0) + return LOG_ERR; + + if (strcmp(level, "info") == 0) + return LOG_INFO; + + if (strcmp(level, "debug") == 0) + return LOG_DEBUG; + + return 0; +} + +static int pakfire_ctx_setup_logging(struct pakfire_ctx* ctx) { + const char* env = NULL; + + // Set the default level to INFO + int level = LOG_INFO; + + // Try setting the log level from the environment + env = secure_getenv("PAKFIRE_LOG"); + if (env) + level = parse_log_level(env); + + // Store the log level + pakfire_ctx_set_log_level(ctx, level); + + return 0; +} + static void pakfire_ctx_free(struct pakfire_ctx* ctx) { free(ctx); } PAKFIRE_EXPORT int pakfire_ctx_create(struct pakfire_ctx** ctx) { struct pakfire_ctx* c = NULL; + int r; // Allocate the context c = calloc(1, sizeof(*c)); @@ -44,10 +90,21 @@ PAKFIRE_EXPORT int pakfire_ctx_create(struct pakfire_ctx** ctx) { // Initialize the reference counter c->nrefs = 1; + // Setup logging + r = pakfire_ctx_setup_logging(c); + if (r) + goto ERROR; + // Return the pointer *ctx = c; return 0; + +ERROR: + if (c) + pakfire_ctx_free(c); + + return r; } PAKFIRE_EXPORT struct pakfire_ctx* pakfire_ctx_ref(struct pakfire_ctx* ctx) { @@ -63,3 +120,13 @@ PAKFIRE_EXPORT struct pakfire_ctx* pakfire_ctx_unref(struct pakfire_ctx* ctx) { pakfire_ctx_free(ctx); return NULL; } + +// Logging + +PAKFIRE_EXPORT int pakfire_ctx_get_log_level(struct pakfire_ctx* ctx) { + return ctx->log.level; +} + +PAKFIRE_EXPORT void pakfire_ctx_set_log_level(struct pakfire_ctx* ctx, int level) { + ctx->log.level = level; +} diff --git a/src/libpakfire/include/pakfire/ctx.h b/src/libpakfire/include/pakfire/ctx.h index acdd1b205..169716d33 100644 --- a/src/libpakfire/include/pakfire/ctx.h +++ b/src/libpakfire/include/pakfire/ctx.h @@ -28,4 +28,9 @@ int pakfire_ctx_create(struct pakfire_ctx** ctx); struct pakfire_ctx* pakfire_ctx_ref(struct pakfire_ctx* ctx); struct pakfire_ctx* pakfire_ctx_unref(struct pakfire_ctx* ctx); +// Logging + +int pakfire_ctx_get_log_level(struct pakfire_ctx* ctx); +void pakfire_ctx_set_log_level(struct pakfire_ctx* ctx, int level); + #endif /* PAKFIRE_CTX_H */ diff --git a/src/libpakfire/libpakfire.sym b/src/libpakfire/libpakfire.sym index e877a0cd9..a40107e7a 100644 --- a/src/libpakfire/libpakfire.sym +++ b/src/libpakfire/libpakfire.sym @@ -24,6 +24,8 @@ global: pakfire_ctx_create; pakfire_ctx_ref; pakfire_ctx_unref; + pakfire_ctx_get_log_level; + pakfire_ctx_set_log_level; # pakfire pakfire_check; diff --git a/tests/parser/test.c b/tests/parser/test.c index 982da6381..55a98cdb9 100644 --- a/tests/parser/test.c +++ b/tests/parser/test.c @@ -48,6 +48,9 @@ int main(int argc, const char* argv[]) { if (r) goto ERROR; + // Set the log level to DEBUG + pakfire_ctx_set_log_level(ctx, LOG_DEBUG); + // 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 f9bd40557..9428f08d7 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -58,6 +58,9 @@ static int test_run(int i, struct test* t) { goto ERROR; } + // Set the log level to DEBUG + pakfire_ctx_set_log_level(ctx, LOG_DEBUG); + // Open the configuration file c = fopen(TEST_SRC_PATH "/pakfire.conf", "r"); if (!c) {