#include <Python.h>
+#include <pakfire/logging.h>
#include <pakfire/packagelist.h>
#include <pakfire/pakfire.h>
#include <pakfire/key.h>
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);
METH_VARARGS|METH_KEYWORDS,
NULL
},
+ {
+ "_log",
+ (PyCFunction)Pakfire_log,
+ METH_VARARGS|METH_KEYWORDS,
+ NULL
+ },
{ NULL },
};
#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, ...) {}
// 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);
pakfire_key_unref;
# log
+ pakfire_log;
pakfire_log_get_function;
pakfire_log_get_priority;
pakfire_log_set_function;
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);
}
# #
###############################################################################
-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
# #
###############################################################################
+import logging
import os
import random
import string
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
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
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
# #
###############################################################################
-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):