From ce1ad5ce02af2024d75ee016f9a58109eec23bc9 Mon Sep 17 00:00:00 2001 From: Jakob Ketterl Date: Tue, 22 Aug 2023 19:42:04 +0200 Subject: [PATCH] move the logging writer for general use --- csdr/module/__init__.py | 23 ++++++++++++++++++++++- owrx/aprs/direwolf.py | 22 ++-------------------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/csdr/module/__init__.py b/csdr/module/__init__.py index 9168d4fb..f2c1f9fa 100644 --- a/csdr/module/__init__.py +++ b/csdr/module/__init__.py @@ -1,5 +1,5 @@ from pycsdr.modules import Module as BaseModule -from pycsdr.modules import Reader, Writer +from pycsdr.modules import Reader, Writer, CallbackWriter from pycsdr.types import Format from abc import ABCMeta, abstractmethod from threading import Thread @@ -7,6 +7,7 @@ from io import BytesIO from subprocess import Popen, PIPE from functools import partial import pickle +import logging class Module(BaseModule, metaclass=ABCMeta): @@ -134,3 +135,23 @@ class PopenModule(AutoStartModule, metaclass=ABCMeta): self.process.wait() self.process = None self.reader.stop() + + +class LogWriter(CallbackWriter): + def __init__(self, prefix: str): + self.logger = logging.getLogger(prefix) + self.retained = bytes() + super().__init__(Format.CHAR) + + def write(self, data: bytes) -> None: + self.retained += data + lines = self.retained.split(b"\n") + + # keep the last line + # this should either be empty if the last char was \n + # or an incomplete line if the read returned early + self.retained = lines[-1] + + # log all completed lines + for line in lines[0:-1]: + self.logger.info("{}: {}".format("STDOUT", line.strip(b'\n').decode())) diff --git a/owrx/aprs/direwolf.py b/owrx/aprs/direwolf.py index 0b7929c8..7e0eae90 100644 --- a/owrx/aprs/direwolf.py +++ b/owrx/aprs/direwolf.py @@ -1,5 +1,6 @@ from pycsdr.types import Format from pycsdr.modules import Writer, TcpSource, ExecModule, CallbackWriter +from csdr.module import LogWriter from owrx.config.core import CoreConfig from owrx.config import Config from abc import ABC, abstractmethod @@ -143,25 +144,6 @@ IGLOGIN {callsign} {password} return config -class LogWriter(CallbackWriter): - def __init__(self): - self.retained = bytes() - super().__init__(Format.CHAR) - - def write(self, data: bytes) -> None: - self.retained += data - lines = self.retained.split(b"\n") - - # keep the last line - # this should either be empty if the last char was \n - # or an incomplete line if the read returned early - self.retained = lines[-1] - - # log all completed lines - for line in lines[0:-1]: - logger.info("{}: {}".format("STDOUT", line.strip(b'\n').decode())) - - class DirewolfModule(ExecModule, DirewolfConfigSubscriber): def __init__(self, service: bool = False): self.tcpSource = None @@ -178,7 +160,7 @@ class DirewolfModule(ExecModule, DirewolfConfigSubscriber): super().__init__(Format.SHORT, Format.CHAR, ["direwolf", "-c", self.direwolfConfigPath, "-r", "48000", "-t", "0", "-q", "d", "-q", "h"]) # direwolf supplies the data via a socket which we tap into in start() # the output on its STDOUT is informative, but we still want to log it - super().setWriter(LogWriter()) + super().setWriter(LogWriter(__name__)) self.start() def __writeConfig(self):