#!/usr/bin/python3
import asyncio
+import collections
import configparser
import io
import logging
+import logging.handlers
import pakfire
from . import base
class PakfireConfig(base.Object):
def init(self, distro=None, repos=None, vendor=None, contact=None,
- arch=None, include_source=False, mirrored=True):
+ arch=None, include_source=False, mirrored=True, logger=None):
self.distro = distro
self.repos = set()
# Should the repositories use any mirrors?
self.mirrored = mirrored
+ # Log messages to here
+ self.logger = logger
+
# Add all repositories belonging to the distribution
if self.distro:
for repo in self.distro.repos:
if message:
message = message.removesuffix("\n")
+ # Log to the logger if configured
+ if self.logger:
+ return self.logger(level, message)
+
return log.log(level, message)
# Repositories
include_source=self.include_source, mirrored=self.mirrored)
return config
+
+
+class PakfireLogger(object):
+ """
+ This is a simple logger instance which collects all log
+ messages which can in the end be exported to for example being stored in the database.
+ """
+ id = 0
+
+ class QueueHandler(logging.handlers.QueueHandler):
+ """
+ This calls overwrites the enqueue() method so that
+ we can use an alternative queue which allows us to
+ iterate over its data without removing anything.
+ """
+ def enqueue(self, record):
+ self.queue.append(record)
+
+ def __init__(self, level=logging.DEBUG):
+ # Create a new child logger
+ self.log = log.getChild("logger-%s" % self.id)
+ self.log.setLevel(level)
+
+ # Increment the ID
+ self.id += 1
+
+ # Propagate messages to the parent logger
+ self.log.propagate = True
+
+ # Create a new queue to buffer any messages
+ self.queue = collections.deque()
+
+ # Create a log formatter
+ formatter = logging.Formatter(
+ "%(asctime)s %(levelname)-8s %(message)s"
+ )
+
+ # Create a queue handler
+ handler = self.QueueHandler(self.queue)
+ handler.setFormatter(formatter)
+
+ # Register the queue with the logger
+ self.log.addHandler(handler)
+
+ def __call__(self, *args, **kwargs):
+ """
+ Logs a message
+ """
+ return self.log.log(*args, **kwargs)
+
+ def __str__(self):
+ """
+ Returns the entire log as a string
+ """
+ return "\n".join((record.getMessage() for record in self.queue))