]> git.ipfire.org Git - pakfire.git/commitdiff
ctx: Add log level
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 15 Oct 2023 14:09:29 +0000 (14:09 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 15 Oct 2023 14:09:29 +0000 (14:09 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/_pakfire/_pakfiremodule.c
src/libpakfire/ctx.c
src/libpakfire/include/pakfire/ctx.h
src/libpakfire/libpakfire.sym
tests/parser/test.c
tests/testsuite.c

index a9313351684074281b88ef945ff609013a88a20e..abdb210211f8c1e4f408257d268ae245160fcf74 100644 (file)
@@ -20,6 +20,7 @@
 
 #include <Python.h>
 #include <libintl.h>
+#include <syslog.h>
 
 #include <pakfire/arch.h>
 #include <pakfire/dependencies.h>
@@ -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);
index c557313a53df51857a9d71a216e112f64a2210a1..b2555cface003c445d670528609959b14ce67ca8 100644 (file)
 #                                                                             #
 #############################################################################*/
 
+#include <ctype.h>
 #include <errno.h>
 #include <stdlib.h>
+#include <string.h>
+#include <syslog.h>
 
 #include <pakfire/ctx.h>
 #include <pakfire/private.h>
 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;
+}
index acdd1b205467d81c26f1d2f167b74d0e64a14700..169716d33bb8c9d10cdfd5a31b202089fa9c2847 100644 (file)
@@ -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 */
index e877a0cd97766f314efc1b4e5ae51e8006334965..a40107e7a4184e167380a000b59a8efdd632ef4c 100644 (file)
@@ -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;
index 982da638103db18914799b1b1959fdef3410d297..55a98cdb9508e6d5d299e8cee2ca0e1ab4b5334b 100644 (file)
@@ -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);
index f9bd4055710665ce51a776dafa1e9c8374254bfb..9428f08d79dae44c054f93a1f5c00755456535f3 100644 (file)
@@ -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) {