]> git.ipfire.org Git - pakfire.git/commitdiff
libpakfire: Add logging functionality
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 28 Nov 2017 21:18:06 +0000 (22:18 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 28 Nov 2017 21:18:06 +0000 (22:18 +0100)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/libpakfire/include/pakfire/logging.h [new file with mode: 0644]
src/libpakfire/include/pakfire/pakfire.h
src/libpakfire/include/pakfire/types.h
src/libpakfire/libpakfire.sym
src/libpakfire/logging.c [new file with mode: 0644]
src/libpakfire/pakfire.c

index 7549959d342f50e50cc4456f5df9746b836cbd78..74e3250d6d7cba9f8e7601eed022b68539db32cc 100644 (file)
@@ -212,6 +212,7 @@ _pakfire_la_SOURCES = \
        src/_pakfire/util.h
 
 _pakfire_la_CPPFLAGS = \
+       -include $(top_builddir)/config.h \
        $(PAKFIRE_CPPFLAGS)
 
 _pakfire_la_CFLAGS = \
@@ -244,6 +245,7 @@ libpakfire_la_SOURCES = \
        src/libpakfire/file.c \
        src/libpakfire/filter.c \
        src/libpakfire/key.c \
+       src/libpakfire/logging.c \
        src/libpakfire/package.c \
        src/libpakfire/packagelist.c \
        src/libpakfire/pakfire.c \
@@ -271,6 +273,7 @@ pkginclude_HEADERS += \
        src/libpakfire/include/pakfire/filter.h \
        src/libpakfire/include/pakfire/i18n.h \
        src/libpakfire/include/pakfire/key.h \
+       src/libpakfire/include/pakfire/logging.h \
        src/libpakfire/include/pakfire/package.h \
        src/libpakfire/include/pakfire/packagecache.h \
        src/libpakfire/include/pakfire/packagelist.h \
@@ -483,6 +486,7 @@ TESTSUITE_LDADD = \
        tests/libtestsuite.la
 
 TESTS_ENVIRONMENT = \
+       PAKFIRE_LOG=debug \
        PYTHONPATH="$(top_srcdir)/.libs:$(top_srcdir)/src" \
        topdir="$(shell pwd)"
 
diff --git a/src/libpakfire/include/pakfire/logging.h b/src/libpakfire/include/pakfire/logging.h
new file mode 100644 (file)
index 0000000..87c58ed
--- /dev/null
@@ -0,0 +1,57 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2017 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifndef PAKFIRE_LOGGING_H
+#define PAKFIRE_LOGGING_H
+
+#include <pakfire/types.h>
+
+void pakfire_log_stderr(Pakfire pakfire, 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,
+               int line, const char* fn, const char* format, va_list args);
+
+#ifdef PAKFIRE_PRIVATE
+
+void pakfire_log(Pakfire pakfire, int priority, const char *file,
+       int line, const char *fn, const char *format, ...)
+       __attribute__((format(printf, 6, 7)));
+
+// This function does absolutely nothing
+static inline void __attribute__((always_inline, format(printf, 2, 3)))
+       pakfire_log_null(Pakfire pakfire, const char *format, ...) {}
+
+#define pakfire_log_condition(pakfire, prio, arg...) \
+       do { \
+               if (pakfire_get_log_priority(pakfire) >= prio) \
+                       pakfire_log(pakfire, 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)
+
+#ifdef ENABLE_DEBUG
+#      define DEBUG(pakfire, arg...) pakfire_log_condition(pakfire, LOG_DEBUG, ## arg)
+#else
+#      define DEBUG(pakfire, arg...) pakfire_log_null(pakfire, ## arg)
+#endif
+
+#endif /* PAKFIRE_PRIVATE */
+#endif /* PAKFIRE_LOGGING_H */
index 0ba4b276dd6cba799d810b010e1c58806ea1426b..3d07352d2a90bd094f41834e4035ba18812e4f93 100644 (file)
@@ -21,6 +21,7 @@
 #ifndef PAKFIRE_PAKFIRE_H
 #define PAKFIRE_PAKFIRE_H
 
+#include <pakfire/logging.h>
 #include <pakfire/types.h>
 
 Pakfire pakfire_create(const char* path, const char* arch);
@@ -28,6 +29,11 @@ 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);
 
@@ -39,6 +45,11 @@ struct _Pakfire {
        char* path;
        char* arch;
        PakfirePool pool;
+
+       // Logging
+       pakfire_log_function_t log_function;
+       int log_priority;
+
        int nrefs;
 };
 
index 2899ff2c64f7734533a24528ca124985619c4c1d..b90ad1a8974b78bdfe22d68406a8bb49e809bf8c 100644 (file)
@@ -21,6 +21,8 @@
 #ifndef PAKFIRE_TYPES_H
 #define PAKFIRE_TYPES_H
 
+#include <stdarg.h>
+
 typedef struct _Pakfire* Pakfire;
 typedef struct _PakfireArchive* PakfireArchive;
 typedef struct _PakfireArchiveSignature* PakfireArchiveSignature;
@@ -43,6 +45,10 @@ typedef struct _PakfireSolution* PakfireSolution;
 typedef struct _PakfireStep* PakfireStep;
 typedef struct _PakfireTransaction* PakfireTransaction;
 
+typedef void (*pakfire_log_function_t)(Pakfire pakfire, int priority,
+               const char* file, int line, const char* fn,
+               const char* format, va_list args);
+
 enum _pakfire_comparison_types {
        PAKFIRE_ICASE     = 1 << 0,
        PAKFIRE_NOT       = 1 << 1,
index c34cf108f6ec84cee4e3e9000f6fe65e9e8f1824..d268d3ec456361211277836f75065787af6f3db6 100644 (file)
@@ -23,9 +23,13 @@ global:
        # pakfire
        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
diff --git a/src/libpakfire/logging.c b/src/libpakfire/logging.c
new file mode 100644 (file)
index 0000000..50986cc
--- /dev/null
@@ -0,0 +1,56 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2017 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <errno.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <syslog.h>
+
+#include <pakfire/logging.h>
+#include <pakfire/pakfire.h>
+
+void pakfire_log(Pakfire pakfire, 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);
+       va_end(args);
+
+       // Restore errno
+       errno = saved_errno;
+}
+
+void pakfire_log_stderr(Pakfire pakfire, 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,
+               int line, const char* fn, const char* format, va_list args) {
+       openlog("UNKNOWN", LOG_PID, LOG_DAEMON);
+       vsyslog(priority | LOG_DAEMON, format, args);
+}
\ No newline at end of file
index 7552055ae75e263702001fd660256b73580cbc49..13f36139e6514d4e3458d6c5c61c35f0f227814c 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;
+
+       return 0;
+}
+
 Pakfire pakfire_create(const char* path, const char* arch) {
        Pakfire pakfire = pakfire_calloc(1, sizeof(*pakfire));
        if (pakfire) {
@@ -35,6 +59,14 @@ 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));
+
                // Initialize the pool
                pakfire->pool = pakfire_pool_create(pakfire);
        }
@@ -59,6 +91,22 @@ void pakfire_unref(Pakfire 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;
 }