From a8f007d4212049aad72ba3e42b65f3ed017dc4f5 Mon Sep 17 00:00:00 2001 From: clerie Date: Sat, 2 Jul 2022 03:19:32 +0200 Subject: [PATCH] Introduce logging --- fieldpoc/controller.py | 5 +++++ fieldpoc/dect.py | 4 ++++ fieldpoc/fieldpoc.py | 10 ++++++++++ fieldpoc/run.py | 18 ++++++++++++++++-- 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/fieldpoc/controller.py b/fieldpoc/controller.py index cb90ccf..716d4f0 100644 --- a/fieldpoc/controller.py +++ b/fieldpoc/controller.py @@ -1,11 +1,14 @@ #!/usr/bin/env python3 +import logging import selectors import socket import socketserver import time import threading +logger = logging.getLogger("fieldpoc.controller") + class Controller: def __init__(self, fp): self.fp = fp @@ -63,6 +66,7 @@ exit Disconnect def run(self): + logger.info("starting server") class Server(socketserver.ThreadingTCPServer): allow_reuse_address = True @@ -75,6 +79,7 @@ exit Disconnect self.fp.queues["controller"].task_done() if msg.get("type") == "stop": + logger.info("stopping server") server.shutdown() for h in self.handlers: h.send(b'\0') diff --git a/fieldpoc/dect.py b/fieldpoc/dect.py index 26a6178..4fc5352 100644 --- a/fieldpoc/dect.py +++ b/fieldpoc/dect.py @@ -1,8 +1,11 @@ #!/usr/bin/env python3 +import logging import mitel_ommclient2 import time +logger = logging.getLogger("fieldpoc.dect") + class Dect: def __init__(self, fp): self.fp = fp @@ -30,6 +33,7 @@ class Dect: def run(self): + logger.info("initialising connection to OMM") self._init_client() while True: diff --git a/fieldpoc/fieldpoc.py b/fieldpoc/fieldpoc.py index dc10ca3..eb8131c 100644 --- a/fieldpoc/fieldpoc.py +++ b/fieldpoc/fieldpoc.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 import json +import logging import pathlib import queue import threading @@ -9,21 +10,26 @@ from . import config from . import controller from . import dect +logger = logging.getLogger("fieldpoc.fieldpoc") + class FieldPOC: config = None extensions = None def __init__(self, config_file_path, extensions_file_path): + logger.info("loading configuration") self.config_file_path = pathlib.Path(config_file_path) self._load_config() self.extensions_file_path = pathlib.Path(extensions_file_path) self._load_extensions() + logger.info("initialising queues") self.queues = { "controller": queue.Queue(), "dect": queue.Queue(), } + logger.info("initialising components") self._controller = controller.Controller(self) self._dect = dect.Dect(self) @@ -36,12 +42,16 @@ class FieldPOC: queue.put(msg) def run(self): + logger.info("stating components") + self._controller_thread = threading.Thread(target=self._controller.run) self._controller_thread.start() self._dect_thread = threading.Thread(target=self._dect.run) self._dect_thread.start() + logger.info("started components") + def _load_config(self): self.config = config.Config(json.loads(self.config_file_path.read_text())) diff --git a/fieldpoc/run.py b/fieldpoc/run.py index b8d1811..c0e4f60 100644 --- a/fieldpoc/run.py +++ b/fieldpoc/run.py @@ -1,17 +1,25 @@ #!/usr/bin/env python3 import argparse +import logging import selectors import signal import socket from . import fieldpoc +logger = logging.getLogger("fieldpoc.run") + ap = argparse.ArgumentParser(prog="fieldpoc") ap.add_argument("-c", "--config", dest="config_file_path", default="fieldpoc_config.json", help="Path to the fieldpoc config file") ap.add_argument("-e", "--extensions", dest="extensions_file_path", default="fieldpoc_extensions.json", help="Path to the fieldpoc extensions file") +ap.add_argument('--debug', dest="debug", action='store_true') def run(): + args = ap.parse_args() + if args.debug: + logging.basicConfig(level=logging.DEBUG) + sel = selectors.DefaultSelector() interrupt_stop_read, interrupt_stop_write = socket.socketpair() @@ -22,17 +30,23 @@ def run(): signal.signal(signal.SIGHUP, lambda signum, frame: interrupt_reload_write.send(b'\0')) sel.register(interrupt_reload_read, selectors.EVENT_READ) - args = ap.parse_args() - fp = fieldpoc.FieldPOC(**args.__dict__) + logger.info("prepared signal handling") + + fp = fieldpoc.FieldPOC(config_file_path=args.config_file_path, extensions_file_path=args.extensions_file_path) fp.run() + logger.info("handle signals") + while True: for key, mask in sel.select(): + logger.info("incoming signal") key.fileobj.recv(1) if key.fileobj == interrupt_stop_read: + logger.info("signal requested service stop") fp.queue_all({"type": "stop"}) exit() elif key.fileobj == interrupt_reload_read: + logger.info("signal requested reload") fp.queue_all({"type": "reload"}) if __name__ == "__main__":