Introduce logging

This commit is contained in:
clerie 2022-07-02 03:19:32 +02:00
parent 51ce2e6ff0
commit a8f007d421
4 changed files with 35 additions and 2 deletions

View File

@ -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')

View File

@ -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:

View File

@ -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()))

View File

@ -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__":