From: Michael Tremer Date: Tue, 15 Jun 2021 15:13:22 +0000 (+0000) Subject: python: Pass logger to Pakfire instance X-Git-Tag: 0.9.28~1238 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0e45f213c38164cb96644b800c7cfa51da7a503e;p=pakfire.git python: Pass logger to Pakfire instance Signed-off-by: Michael Tremer --- diff --git a/src/_pakfire/pakfire.c b/src/_pakfire/pakfire.c index a01c65b9f..515220804 100644 --- a/src/_pakfire/pakfire.c +++ b/src/_pakfire/pakfire.c @@ -44,13 +44,57 @@ static PyObject* Pakfire_new(PyTypeObject* type, PyObject* args, PyObject* kwds) PakfireObject* self = (PakfireObject *)type->tp_alloc(type, 0); if (self) { self->pakfire = NULL; + self->logger = NULL; } return (PyObject *)self; } +static void Pakfire_logging_callback(void* data, int priority, const char* file, int line, + const char* fn, const char* format, va_list args) { + PyObject* callback = (PyObject*)data; + + // Do nothing if callback isn't set + if (!callback) + return; + + // Translate priority to Python logging priorities + switch (priority) { + case LOG_INFO: + priority = 20; + break; + + case LOG_ERR: + priority = 40; + break; + } + + PyObject* tuple = NULL; + PyObject* result = NULL; + char* buffer = NULL; + + // Make line + int r = vasprintf(&buffer, format, args); + if (r < 0) + goto ERROR; + + // Build a tuple with the priority and the log message + tuple = Py_BuildValue("(is)", priority, buffer); + if (!tuple) + goto ERROR; + + // Call the callback + result = PyObject_CallObject(callback, tuple); + +ERROR: + if (buffer) + free(buffer); + Py_XDECREF(tuple); + Py_XDECREF(result); +} + static int Pakfire_init(PakfireObject* self, PyObject* args, PyObject* kwds) { - char* kwlist[] = { "path", "arch", "offline", "conf", "build", + char* kwlist[] = { "path", "arch", "logger", "offline", "conf", "build", "enable_ccache", "enable_snapshot", NULL }; const char* path = NULL; const char* arch = NULL; @@ -60,10 +104,17 @@ static int Pakfire_init(PakfireObject* self, PyObject* args, PyObject* kwds) { int enable_ccache = 1; int enable_snapshot = 1; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|zzpzppp", kwlist, - &path, &arch, &offline, &conf, &build, &enable_ccache, &enable_snapshot)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|zzOpzppp", kwlist, + &path, &arch, &self->logger, &offline, &conf, &build, + &enable_ccache, &enable_snapshot)) return -1; + // Check if logger is callable + if (self->logger && !PyCallable_Check(self->logger)) { + PyErr_SetString(PyExc_TypeError, "logger must be callable\n"); + return -1; + } + int flags = 0; // Enable offline mode @@ -82,7 +133,8 @@ static int Pakfire_init(PakfireObject* self, PyObject* args, PyObject* kwds) { } // Create a new Pakfire instance - int r = pakfire_create(&self->pakfire, path, arch, conf, flags, NULL, NULL); + int r = pakfire_create(&self->pakfire, path, arch, conf, flags, + (self->logger) ? Pakfire_logging_callback : NULL, self->logger); if (r) { switch (errno) { // Invalid architecture or path @@ -98,6 +150,8 @@ static int Pakfire_init(PakfireObject* self, PyObject* args, PyObject* kwds) { return -1; } + Py_XINCREF(self->logger); + return 0; } @@ -105,6 +159,9 @@ static void Pakfire_dealloc(PakfireObject* self) { if (self->pakfire) pakfire_unref(self->pakfire); + if (self->logger) + Py_DECREF(self->logger); + Py_TYPE(self)->tp_free((PyObject *)self); } diff --git a/src/_pakfire/pakfire.h b/src/_pakfire/pakfire.h index c8f001875..208ad4d94 100644 --- a/src/_pakfire/pakfire.h +++ b/src/_pakfire/pakfire.h @@ -28,6 +28,7 @@ typedef struct { PyObject_HEAD Pakfire pakfire; + PyObject* logger; } PakfireObject; extern PyTypeObject PakfireType; diff --git a/src/pakfire/logger.py b/src/pakfire/logger.py index 6c3b698de..ecc798874 100644 --- a/src/pakfire/logger.py +++ b/src/pakfire/logger.py @@ -95,7 +95,7 @@ class ConsoleHandler(logging.Handler): fd = sys.stdout # Write the output - fd.write("%s\n" % msg) + fd.write(msg) # Immediately flush self.flush() diff --git a/src/scripts/pakfire-builder.in b/src/scripts/pakfire-builder.in index 6814edbdc..b3a27a871 100644 --- a/src/scripts/pakfire-builder.in +++ b/src/scripts/pakfire-builder.in @@ -20,14 +20,30 @@ ############################################################################### import argparse +import logging import os.path import sys import pakfire +import pakfire.logger from pakfire.constants import CONFIG_DISTRO_DIR from pakfire.i18n import _ class Cli(object): + @property + def logger(self): + log = logging.getLogger("pakfire.builder.cli") + log.setLevel(logging.DEBUG) + + # Do not propagate anything + log.propagate = False + + # Enable console output + handler = pakfire.logger.ConsoleHandler() + log.addHandler(handler) + + return log + def parse_cli(self): parser = argparse.ArgumentParser( description = _("Pakfire builder command line interface"), @@ -126,9 +142,13 @@ class Cli(object): conf=conf, arch=ns.arch, + # Set up logging + logger=self.logger.log, + # Enable build mode build=build, - enable_snapshot=not ns.disable_snapshot + enable_snapshot=not ns.disable_snapshot, + ) def __call__(self):