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;
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
}
// 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
return -1;
}
+ Py_XINCREF(self->logger);
+
return 0;
}
if (self->pakfire)
pakfire_unref(self->pakfire);
+ if (self->logger)
+ Py_DECREF(self->logger);
+
Py_TYPE(self)->tp_free((PyObject *)self);
}
###############################################################################
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"),
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):