]> git.ipfire.org Git - pakfire.git/commitdiff
python: Use libpakfire's logging system
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 16 Feb 2018 18:15:33 +0000 (19:15 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 16 Feb 2018 18:21:10 +0000 (19:21 +0100)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/_pakfire/pakfire.c
src/libpakfire/include/pakfire/logging.h
src/libpakfire/include/pakfire/pakfire.h
src/libpakfire/libpakfire.sym
src/libpakfire/logging.c
src/pakfire/__init__.py
src/pakfire/base.py
src/pakfire/logger.py

index a63e6e390651018f2f6181b9a36fdde667f6838b..fffbc7851ba713903eaed097e465878162876074 100644 (file)
@@ -20,6 +20,7 @@
 
 #include <Python.h>
 
+#include <pakfire/logging.h>
 #include <pakfire/packagelist.h>
 #include <pakfire/pakfire.h>
 #include <pakfire/key.h>
@@ -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 },
 };
 
index 662e6eff9043ff3eded789ece20466c6904fcf2e..58a5bb05b7bd92e8cd45c7198f72026a3cf27ebe 100644 (file)
@@ -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, ...) {}
index 9e10ca375a3d855f1d621c45a928b4845b131009..0743fae008afd11325e83641f999014263507817 100644 (file)
@@ -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);
index 2b5a0a76b8b72ad659afd935bfd6f1ffb52b5276..56a796ef87fe56439572db9ec876ff34ff32feaa 100644 (file)
@@ -113,6 +113,7 @@ global:
        pakfire_key_unref;
 
        # log
+       pakfire_log;
        pakfire_log_get_function;
        pakfire_log_get_priority;
        pakfire_log_set_function;
index 13cc716937ad8ea696d27930d3da53fe8a9e07fb..8743faaaeb564f6ec059b745eaff62ae398331d8 100644 (file)
 
 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);
 }
 
index b3abd80e2cc63d483f00bb2bbfe169621676718a..16905eea40f9caad5fe4f40f7ca1585ce180bdfe 100644 (file)
 #                                                                             #
 ###############################################################################
 
-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
index b87520e59a9f2742e62d358bcb59a7f34e979acd..39eb3fb7efc029ac099d1baa7e9fc3ce8ae672fa 100644 (file)
@@ -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
index 3561e97e5386c3beb229c1ad56da12f92ae8f22d..0de478859c6008aadfbefb423080a0a30f860810 100644 (file)
 #                                                                             #
 ###############################################################################
 
-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):