From f06095f14b2e206c157f3a466219c90414a6fe88 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Tue, 28 Nov 2017 22:18:06 +0100 Subject: [PATCH] libpakfire: Add logging functionality Signed-off-by: Michael Tremer --- Makefile.am | 4 ++ src/libpakfire/include/pakfire/logging.h | 57 ++++++++++++++++++++++++ src/libpakfire/include/pakfire/pakfire.h | 11 +++++ src/libpakfire/include/pakfire/types.h | 6 +++ src/libpakfire/libpakfire.sym | 4 ++ src/libpakfire/logging.c | 56 +++++++++++++++++++++++ src/libpakfire/pakfire.c | 48 ++++++++++++++++++++ 7 files changed, 186 insertions(+) create mode 100644 src/libpakfire/include/pakfire/logging.h create mode 100644 src/libpakfire/logging.c diff --git a/Makefile.am b/Makefile.am index 7549959d3..74e3250d6 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 index 000000000..87c58ed10 --- /dev/null +++ b/src/libpakfire/include/pakfire/logging.h @@ -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 . # +# # +#############################################################################*/ + +#ifndef PAKFIRE_LOGGING_H +#define PAKFIRE_LOGGING_H + +#include + +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 */ diff --git a/src/libpakfire/include/pakfire/pakfire.h b/src/libpakfire/include/pakfire/pakfire.h index 0ba4b276d..3d07352d2 100644 --- a/src/libpakfire/include/pakfire/pakfire.h +++ b/src/libpakfire/include/pakfire/pakfire.h @@ -21,6 +21,7 @@ #ifndef PAKFIRE_PAKFIRE_H #define PAKFIRE_PAKFIRE_H +#include #include 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; }; diff --git a/src/libpakfire/include/pakfire/types.h b/src/libpakfire/include/pakfire/types.h index 2899ff2c6..b90ad1a89 100644 --- a/src/libpakfire/include/pakfire/types.h +++ b/src/libpakfire/include/pakfire/types.h @@ -21,6 +21,8 @@ #ifndef PAKFIRE_TYPES_H #define PAKFIRE_TYPES_H +#include + 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, diff --git a/src/libpakfire/libpakfire.sym b/src/libpakfire/libpakfire.sym index c34cf108f..d268d3ec4 100644 --- a/src/libpakfire/libpakfire.sym +++ b/src/libpakfire/libpakfire.sym @@ -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 index 000000000..50986cc1a --- /dev/null +++ b/src/libpakfire/logging.c @@ -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 . # +# # +#############################################################################*/ + +#include +#include +#include +#include + +#include +#include + +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 diff --git a/src/libpakfire/pakfire.c b/src/libpakfire/pakfire.c index 7552055ae..13f36139e 100644 --- a/src/libpakfire/pakfire.c +++ b/src/libpakfire/pakfire.c @@ -18,12 +18,36 @@ # # #############################################################################*/ +#include +#include +#include +#include + #include #include #include #include #include +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; } -- 2.39.5