]> git.ipfire.org Git - pakfire.git/commitdiff
Move logging stuff away from Pakfire context
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 28 Nov 2017 22:36:54 +0000 (23:36 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 28 Nov 2017 22:36:54 +0000 (23:36 +0100)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/_pakfire/_pakfiremodule.c
src/libpakfire/include/pakfire/logging.h
src/libpakfire/include/pakfire/pakfire.h
src/libpakfire/include/pakfire/types.h
src/libpakfire/libpakfire.sym
src/libpakfire/logging.c
src/libpakfire/pakfire.c

index a10bb233cd1748ceede30dc2511a95385db48ad9..d2c436791a02fc136ae795125d853fbc75c51dcc 100644 (file)
@@ -115,6 +115,11 @@ PyMODINIT_FUNC PyInit__pakfire(void) {
        bindtextdomain(PACKAGE_TARNAME, "/usr/share/locale");
        textdomain(PACKAGE_TARNAME);
 
+       // Initialize the pakfire library
+       int r = pakfire_init();
+       if (r)
+               return NULL;
+
        // Create the module
        PyObject* module = PyModule_Create(&moduledef);
        if (!module)
index 87c58ed10c3647c06a7ad157702aa5052b43b6a0..ae0d45e5f6dfbd0294d09c586ccaa589752df3b0 100644 (file)
 #ifndef PAKFIRE_LOGGING_H
 #define PAKFIRE_LOGGING_H
 
+#include <syslog.h>
+
 #include <pakfire/types.h>
 
-void pakfire_log_stderr(Pakfire pakfire, int priority, const char* file,
+void pakfire_log_stderr(int priority, const char* file,
                int line, const char* fn, const char* format, va_list args);
-void pakfire_log_syslog(Pakfire pakfire, int priority, const char* file,
+void pakfire_log_syslog(int priority, const char* file,
                int line, const char* fn, const char* format, va_list args);
 
+pakfire_log_function_t pakfire_log_get_function();
+void pakfire_log_set_function(pakfire_log_function_t func);
+int pakfire_log_get_priority();
+void pakfire_log_set_priority(int priority);
+
 #ifdef PAKFIRE_PRIVATE
 
-void pakfire_log(Pakfire pakfire, int priority, const char *file,
+typedef struct pakfire_logging_config {
+       pakfire_log_function_t function;
+       int priority;
+} pakfire_logging_config_t;
+
+void pakfire_setup_logging();
+void pakfire_log(int priority, const char *file,
        int line, const char *fn, const char *format, ...)
-       __attribute__((format(printf, 6, 7)));
+       __attribute__((format(printf, 5, 6)));
 
 // This function does absolutely nothing
-static inline void __attribute__((always_inline, format(printf, 2, 3)))
-       pakfire_log_null(Pakfire pakfire, const char *format, ...) {}
+static inline void __attribute__((always_inline, format(printf, 1, 2)))
+       pakfire_log_null(const char *format, ...) {}
 
-#define pakfire_log_condition(pakfire, prio, arg...) \
+#define pakfire_log_condition(prio, arg...) \
        do { \
-               if (pakfire_get_log_priority(pakfire) >= prio) \
-                       pakfire_log(pakfire, prio, __FILE__, __LINE__, __FUNCTION__, ## arg); \
+               if (pakfire_log_get_priority() >= prio) \
+                       pakfire_log(prio, __FILE__, __LINE__, __FUNCTION__, ## arg); \
        } while (0)
 
-#define INFO(pakfire, arg...) pakfire_log_condition(pakfire, LOG_INFO, ## arg)
-#define ERROR(pakfire, arg...) pakfire_log_condition(pakfire, LOG_ERR, ## arg)
+#define INFO(arg...) pakfire_log_condition(LOG_INFO, ## arg)
+#define ERROR(arg...) pakfire_log_condition(LOG_ERR, ## arg)
 
 #ifdef ENABLE_DEBUG
-#      define DEBUG(pakfire, arg...) pakfire_log_condition(pakfire, LOG_DEBUG, ## arg)
+#      define DEBUG(arg...) pakfire_log_condition(LOG_DEBUG, ## arg)
 #else
-#      define DEBUG(pakfire, arg...) pakfire_log_null(pakfire, ## arg)
+#      define DEBUG pakfire_log_null
 #endif
 
 #endif /* PAKFIRE_PRIVATE */
index 3d07352d2a90bd094f41834e4035ba18812e4f93..69296c8e226996052e45fe5a523c0f5035db6764 100644 (file)
 #include <pakfire/logging.h>
 #include <pakfire/types.h>
 
+int pakfire_init();
+
 Pakfire pakfire_create(const char* path, const char* arch);
 
 Pakfire pakfire_ref(Pakfire pakfire);
 void pakfire_unref(Pakfire pakfire);
 
-pakfire_log_function_t pakfire_get_log_function(Pakfire pakfire);
-void pakfire_set_log_function(Pakfire pakfire, pakfire_log_function_t func);
-int pakfire_get_log_priority(Pakfire pakfire);
-void pakfire_set_log_priority(Pakfire pakfire, int priority);
-
 const char* pakfire_get_path(Pakfire pakfire);
 const char* pakfire_get_arch(Pakfire pakfire);
 
index b90ad1a8974b78bdfe22d68406a8bb49e809bf8c..ca6f47a59f0a11295f77a7358c03f3e88f766390 100644 (file)
@@ -45,7 +45,7 @@ typedef struct _PakfireSolution* PakfireSolution;
 typedef struct _PakfireStep* PakfireStep;
 typedef struct _PakfireTransaction* PakfireTransaction;
 
-typedef void (*pakfire_log_function_t)(Pakfire pakfire, int priority,
+typedef void (*pakfire_log_function_t)(int priority,
                const char* file, int line, const char* fn,
                const char* format, va_list args);
 
index d268d3ec456361211277836f75065787af6f3db6..470818cf08180d25c85420f8c4512cd762f115ff 100644 (file)
 LIBPAKFIRE_0 {
 global:
        # pakfire
+       pakfire_init;
        pakfire_create;
        pakfire_get_arch;
-       pakfire_get_log_function;
-       pakfire_get_log_priority;
        pakfire_get_path;
        pakfire_get_pool;
        pakfire_ref;
-       pakfire_set_log_function;
-       pakfire_set_log_priority;
        pakfire_unref;
 
        # archive
@@ -102,6 +99,12 @@ global:
        pakfire_key_ref;
        pakfire_key_unref;
 
+       # log
+       pakfire_log_get_function;
+       pakfire_log_get_priority;
+       pakfire_log_set_function;
+       pakfire_log_set_priority;
+
        # package
        pakfire_package_add_conflicts;
        pakfire_package_add_obsoletes;
index 8113cea912edd03a6ffd488d0305b9d43d9282dc..9f1c33805854c5f0d794749b41aa441b2e4877be 100644 (file)
 #                                                                             #
 #############################################################################*/
 
+#include <ctype.h>
 #include <errno.h>
 #include <stdarg.h>
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 #include <syslog.h>
 
 #include <pakfire/logging.h>
 #include <pakfire/pakfire.h>
 
-void pakfire_log(Pakfire pakfire, int priority, const char* file, int line,
+static pakfire_logging_config_t conf = {
+       .function = pakfire_log_stderr,
+       .priority = LOG_ERR,
+};
+
+static int log_priority(const char* priority) {
+       char* end;
+
+       int prio = strtol(priority, &end, 10);
+       if (*end == '\0' || isspace(*end))
+               return prio;
+
+       if (strncmp(priority, "error", strlen("error")) == 0)
+               return LOG_ERR;
+
+       if (strncmp(priority, "info", strlen("info")) == 0)
+               return LOG_INFO;
+
+       if (strncmp(priority, "debug", strlen("debug")) == 0)
+               return LOG_DEBUG;
+
+       return 0;
+}
+
+void pakfire_setup_logging() {
+       const char* priority = secure_getenv("PAKFIRE_LOG");
+       if (priority)
+               pakfire_log_set_priority(log_priority(priority));
+}
+
+pakfire_log_function_t pakfire_log_get_function() {
+       return conf.function;
+}
+
+void pakfire_log_set_function(pakfire_log_function_t func) {
+       conf.function = func;
+}
+
+int pakfire_log_get_priority() {
+       return conf.priority;
+}
+
+void pakfire_log_set_priority(int priority) {
+       conf.priority = priority;
+}
+
+void pakfire_log(int priority, const char* file, int line,
                const char* fn, const char* format, ...) {
        va_list args;
 
-       pakfire_log_function_t log_function = pakfire_get_log_function(pakfire);
-
        // Save errno
        int saved_errno = errno;
 
        va_start(args, format);
-       log_function(pakfire, priority, file, line, fn, format, args);
+       conf.function(priority, file, line, fn, format, args);
        va_end(args);
 
        // Restore errno
        errno = saved_errno;
 }
 
-void pakfire_log_stderr(Pakfire pakfire, int priority, const char* file,
+void pakfire_log_stderr(int priority, const char* file,
                int line, const char* fn, const char* format, va_list args) {
        fprintf(stderr, "pakfire: %s: ", fn);
        vfprintf(stderr, format, args);
 }
 
-void pakfire_log_syslog(Pakfire pakfire, int priority, const char* file,
+void pakfire_log_syslog(int priority, const char* file,
                int line, const char* fn, const char* format, va_list args) {
        openlog("pakfire", LOG_PID, LOG_DAEMON);
        vsyslog(priority | LOG_DAEMON, format, args);
index b163913573824704a19e27be35e66141659bb28f..fe063b2d57ec94d347466e81d3fd787dffc61905 100644 (file)
 #                                                                             #
 #############################################################################*/
 
-#include <ctype.h>
-#include <stdlib.h>
-#include <string.h>
-#include <syslog.h>
-
 #include <pakfire/pakfire.h>
 #include <pakfire/pool.h>
 #include <pakfire/system.h>
 #include <pakfire/types.h>
 #include <pakfire/util.h>
 
-static int log_priority(const char* priority) {
-       char* end;
-
-       int prio = strtol(priority, &end, 10);
-       if (*end == '\0' || isspace(*end))
-               return prio;
-
-       if (strncmp(priority, "error", strlen("error")) == 0)
-               return LOG_ERR;
-
-       if (strncmp(priority, "info", strlen("info")) == 0)
-               return LOG_INFO;
-
-       if (strncmp(priority, "debug", strlen("debug")) == 0)
-               return LOG_DEBUG;
+int pakfire_init() {
+       // Setup logging
+       pakfire_setup_logging();
 
        return 0;
 }
@@ -59,17 +42,9 @@ Pakfire pakfire_create(const char* path, const char* arch) {
                }
                pakfire->arch = pakfire_strdup(arch);
 
-               // Setup logging
-               pakfire->log_function = pakfire_log_syslog;
-               pakfire->log_priority = LOG_ERR;
-
-               const char* priority = secure_getenv("PAKFIRE_LOG");
-               if (priority)
-                       pakfire_set_log_priority(pakfire, log_priority(priority));
-
-               DEBUG(pakfire, "Pakfire initialized at %p\n", pakfire);
-               DEBUG(pakfire, "  arch = %s\n", pakfire->arch);
-               DEBUG(pakfire, "  path = %s\n", pakfire->path);
+               DEBUG("Pakfire initialized at %p\n", pakfire);
+               DEBUG("  arch = %s\n", pakfire->arch);
+               DEBUG("  path = %s\n", pakfire->path);
 
                // Initialize the pool
                pakfire->pool = pakfire_pool_create(pakfire);
@@ -93,26 +68,10 @@ void pakfire_unref(Pakfire pakfire) {
        pakfire_free(pakfire->path);
        pakfire_free(pakfire->arch);
 
-       DEBUG(pakfire, "Pakfire released at %p\n", pakfire);
+       DEBUG("Pakfire released at %p\n", pakfire);
        pakfire_free(pakfire);
 }
 
-pakfire_log_function_t pakfire_get_log_function(Pakfire pakfire) {
-       return pakfire->log_function;
-}
-
-void pakfire_set_log_function(Pakfire pakfire, pakfire_log_function_t func) {
-       pakfire->log_function = func;
-}
-
-int pakfire_get_log_priority(Pakfire pakfire) {
-       return pakfire->log_priority;
-}
-
-void pakfire_set_log_priority(Pakfire pakfire, int priority) {
-       pakfire->log_priority = priority;
-}
-
 const char* pakfire_get_path(Pakfire pakfire) {
        return pakfire->path;
 }