From bfa112c3ad6bb8deb6626fb8f4ca9dc6341876f2 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 16 Feb 2018 19:15:33 +0100 Subject: [PATCH] python: Use libpakfire's logging system Signed-off-by: Michael Tremer --- src/_pakfire/pakfire.c | 28 ++++++++++++ src/libpakfire/include/pakfire/logging.h | 4 -- src/libpakfire/include/pakfire/pakfire.h | 4 ++ src/libpakfire/libpakfire.sym | 1 + src/libpakfire/logging.c | 7 ++- src/pakfire/__init__.py | 6 --- src/pakfire/base.py | 21 +++++++-- src/pakfire/logger.py | 55 ++++++++++++------------ 8 files changed, 84 insertions(+), 42 deletions(-) diff --git a/src/_pakfire/pakfire.c b/src/_pakfire/pakfire.c index a63e6e390..fffbc7851 100644 --- a/src/_pakfire/pakfire.c +++ b/src/_pakfire/pakfire.c @@ -20,6 +20,7 @@ #include +#include #include #include #include @@ -68,6 +69,27 @@ static PyObject* Pakfire_repr(PakfireObject* self) { return PyUnicode_FromFormat("<_pakfire.Pakfire %s (%s)>", path, arch); } +static PyObject* Pakfire_log(PakfireObject* self, PyObject* args, PyObject* kwds) { + char* kwlist[] = { "priority", "message", "filename", "lineno", "function", NULL }; + + int priority; + const char* message; + + const char* filename = NULL; + int lineno = 0; + const char* function = NULL; + + if (!PyArg_ParseTupleAndKeywords(args, kwds, "is|sis", kwlist, &priority, &message, + &filename, &lineno, &function)) + return NULL; + + // Send message to the pakfire logger + if (pakfire_log_get_priority(self->pakfire) >= priority) + pakfire_log(self->pakfire, priority, filename, lineno, function, "%s\n", message); + + Py_RETURN_NONE; +} + static PyObject* Pakfire_get_path(PakfireObject* self) { const char* path = pakfire_get_path(self->pakfire); @@ -348,6 +370,12 @@ static struct PyMethodDef Pakfire_methods[] = { METH_VARARGS|METH_KEYWORDS, NULL }, + { + "_log", + (PyCFunction)Pakfire_log, + METH_VARARGS|METH_KEYWORDS, + NULL + }, { NULL }, }; diff --git a/src/libpakfire/include/pakfire/logging.h b/src/libpakfire/include/pakfire/logging.h index 662e6eff9..58a5bb05b 100644 --- a/src/libpakfire/include/pakfire/logging.h +++ b/src/libpakfire/include/pakfire/logging.h @@ -33,10 +33,6 @@ void pakfire_log_syslog(int priority, const char* file, #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, ...) {} diff --git a/src/libpakfire/include/pakfire/pakfire.h b/src/libpakfire/include/pakfire/pakfire.h index 9e10ca375..0743fae00 100644 --- a/src/libpakfire/include/pakfire/pakfire.h +++ b/src/libpakfire/include/pakfire/pakfire.h @@ -65,6 +65,10 @@ FILE* pakfire_cache_open(Pakfire pakfire, const char* path, const char* flags); // Logging +void pakfire_log(Pakfire pakfire, int priority, const char *file, + int line, const char *fn, const char *format, ...) + __attribute__((format(printf, 6, 7))); + pakfire_log_function_t pakfire_log_get_function(Pakfire pakfire); void pakfire_log_set_function(Pakfire pakfire, pakfire_log_function_t log_function); int pakfire_log_get_priority(Pakfire pakfire); diff --git a/src/libpakfire/libpakfire.sym b/src/libpakfire/libpakfire.sym index 2b5a0a76b..56a796ef8 100644 --- a/src/libpakfire/libpakfire.sym +++ b/src/libpakfire/libpakfire.sym @@ -113,6 +113,7 @@ global: pakfire_key_unref; # log + pakfire_log; pakfire_log_get_function; pakfire_log_get_priority; pakfire_log_set_function; diff --git a/src/libpakfire/logging.c b/src/libpakfire/logging.c index 13cc71693..8743faaae 100644 --- a/src/libpakfire/logging.c +++ b/src/libpakfire/logging.c @@ -26,7 +26,12 @@ PAKFIRE_EXPORT 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); + fprintf(stderr, "pakfire: "); + + // Optionally log the function name + if (fn) + fprintf(stderr, "%s: ", fn); + vfprintf(stderr, format, args); } diff --git a/src/pakfire/__init__.py b/src/pakfire/__init__.py index b3abd80e2..16905eea4 100644 --- a/src/pakfire/__init__.py +++ b/src/pakfire/__init__.py @@ -19,10 +19,4 @@ # # ############################################################################### -from .constants import PAKFIRE_VERSION as __version__ -from .logger import setup_logging - -log = setup_logging() -log.debug("Pakfire %s initialised" % __version__) - from .base import Pakfire, PakfireBuilder, PakfireServer diff --git a/src/pakfire/base.py b/src/pakfire/base.py index b87520e59..39eb3fb7e 100644 --- a/src/pakfire/base.py +++ b/src/pakfire/base.py @@ -19,6 +19,7 @@ # # ############################################################################### +import logging import os import random import string @@ -27,13 +28,11 @@ from . import _pakfire from . import distro from . import downloaders from . import filelist +from . import logger from . import packages from . import repository from . import util -import logging -log = logging.getLogger("pakfire") - from .config import Config from .system import system @@ -47,6 +46,9 @@ class Pakfire(_pakfire.Pakfire): def __init__(self, path="/", config=None, arch=None, distro=None, cache_path=None): _pakfire.Pakfire.__init__(self, path, arch or system.native_arch) + # Initialise logging system + self.log = self._setup_logger() + # Default to system distribution self.distro = distro or system.distro @@ -70,6 +72,19 @@ class Pakfire(_pakfire.Pakfire): if repos_dir: self.repos.load_configuration(repos_dir) + def _setup_logger(self): + log = logging.getLogger("pakfire") + log.propagate = 0 + + # Always process all messages (include debug) + log.setLevel(logging.DEBUG) + + # Pass everything down to libpakfire + handler = logger.PakfireLogHandler(self) + log.addHandler(handler) + + return log + def make_path(self, path): """ Returns path relative to the (base)path diff --git a/src/pakfire/logger.py b/src/pakfire/logger.py index 3561e97e5..0de478859 100644 --- a/src/pakfire/logger.py +++ b/src/pakfire/logger.py @@ -19,43 +19,42 @@ # # ############################################################################### -import time - import logging -import logging.handlers - -from .config import config +import time -def setup_logging(debug=None): - """ - This function initialized the logger that is enabled immediately - """ - # If debug is None, we read it from the configuration - if debug is None: - debug = config.get_bool("log", "debug", False) +class PakfireLogHandler(logging.Handler): + LOG_EMERG = 0 # system is unusable + LOG_ALERT = 1 # action must be taken immediately + LOG_CRIT = 2 # critical conditions + LOG_ERR = 3 # error conditions + LOG_WARNING = 4 # warning conditions + LOG_NOTICE = 5 # normal but significant condition + LOG_INFO = 6 # informational + LOG_DEBUG = 7 # debug-level messages - l = logging.getLogger("pakfire") - l.propagate = 0 + priority_map = { + "DEBUG" : LOG_DEBUG, + "INFO" : LOG_INFO, + "WARNING" : LOG_WARNING, + "ERROR" : LOG_ERR, + "CRITICAL" : LOG_CRIT, + } - # Set level of logger always to DEBUG. - l.setLevel(logging.DEBUG) + def __init__(self, pakfire): + logging.Handler.__init__(self) - # Remove all previous defined handlers. - l.handlers = [] + self.pakfire = pakfire - # Log to syslog - handler = logging.handlers.SysLogHandler("/dev/log") - l.addHandler(handler) + def emit(self, record): + line = self.format(record) + prio = self._get_priority(record.levelname) - # Formatter - f = logging.Formatter("%(name)s: %(message)s") - handler.setFormatter(f) + self.pakfire._log(prio, line, filename=record.pathname, + lineno=record.lineno, function=record.funcName) - # Configure debugging - if not debug: - handler.setLevel(logging.INFO) + def _get_priority(self, level): + return self.priority_map.get(level, self.LOG_WARNING) - return l class BuildFormatter(logging.Formatter): def __init__(self): -- 2.39.5