]> git.ipfire.org Git - people/ms/pakfire.git/blobdiff - src/_pakfire/ctx.c
python: ctx: Setup the default logger
[people/ms/pakfire.git] / src / _pakfire / ctx.c
index e8fd927bf3a65c93ffe6cb727da8f7d3d5fe1fdb..e6729be4dd6ccc9546bdcf915ca28e208b52842c 100644 (file)
@@ -119,10 +119,11 @@ static void Ctx___set_logger(CtxObject* self, PyObject* logger) {
 
        // Store the new logger
        self->logger = logger;
-       Py_INCREF(self->logger);
+       Py_XINCREF(self->logger);
 
        // Set the logger
-       pakfire_ctx_set_log_callback(self->ctx, Ctx_log_callback, self->logger);
+       if (self->ctx)
+               pakfire_ctx_set_log_callback(self->ctx, Ctx_log_callback, self->logger);
 }
 
 static int Ctx_setup_logging(CtxObject* self) {
@@ -143,6 +144,9 @@ static int Ctx_setup_logging(CtxObject* self) {
        // Set default logger
        Ctx___set_logger(self, logger);
 
+       // Success
+       r = 0;
+
 ERROR:
        Py_XDECREF(logging);
        Py_XDECREF(logger);
@@ -171,15 +175,21 @@ static int Ctx_init(CtxObject* self, PyObject* args, PyObject* kwargs) {
        // Set the log level to DEBUG
        pakfire_ctx_set_log_level(self->ctx, LOG_DEBUG);
 
+       // Setup the default logger
+       r = Ctx_setup_logging(self);
+       if (r)
+               return r;
+
        return 0;
 }
 
 static void Ctx_dealloc(CtxObject* self) {
+       // Reset the logger
+       Ctx___set_logger(self, NULL);
+
        if (self->ctx)
                pakfire_ctx_unref(self->ctx);
 
-       Py_XDECREF(self->logger);
-
        Py_TYPE(self)->tp_free((PyObject *)self);
 }
 
@@ -191,8 +201,6 @@ static PyObject* Ctx_set_logger(CtxObject* self, PyObject* args) {
        if (!PyArg_ParseTuple(args, "O", &logger))
                return NULL;
 
-       // XXX Check if we have a log method
-
        // Set the logger
        Ctx___set_logger(self, logger);