src/_pakfire/util.h
_pakfire_la_CPPFLAGS = \
+ -include $(top_builddir)/config.h \
$(PAKFIRE_CPPFLAGS)
_pakfire_la_CFLAGS = \
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 \
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 \
tests/libtestsuite.la
TESTS_ENVIRONMENT = \
+ PAKFIRE_LOG=debug \
PYTHONPATH="$(top_srcdir)/.libs:$(top_srcdir)/src" \
topdir="$(shell pwd)"
--- /dev/null
+/*#############################################################################
+# #
+# 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 */
#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);
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);
char* path;
char* arch;
PakfirePool pool;
+
+ // Logging
+ pakfire_log_function_t log_function;
+ int log_priority;
+
int nrefs;
};
#ifndef PAKFIRE_TYPES_H
#define PAKFIRE_TYPES_H
+#include <stdarg.h>
+
typedef struct _Pakfire* Pakfire;
typedef struct _PakfireArchive* PakfireArchive;
typedef struct _PakfireArchiveSignature* PakfireArchiveSignature;
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,
# 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
--- /dev/null
+/*#############################################################################
+# #
+# 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
# #
#############################################################################*/
+#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) {
}
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);
}
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;
}