move the logging writer for general use

This commit is contained in:
Jakob Ketterl 2023-08-22 19:42:04 +02:00
parent 063023564b
commit ce1ad5ce02
2 changed files with 24 additions and 21 deletions

View File

@ -1,5 +1,5 @@
from pycsdr.modules import Module as BaseModule 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 pycsdr.types import Format
from abc import ABCMeta, abstractmethod from abc import ABCMeta, abstractmethod
from threading import Thread from threading import Thread
@ -7,6 +7,7 @@ from io import BytesIO
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
from functools import partial from functools import partial
import pickle import pickle
import logging
class Module(BaseModule, metaclass=ABCMeta): class Module(BaseModule, metaclass=ABCMeta):
@ -134,3 +135,23 @@ class PopenModule(AutoStartModule, metaclass=ABCMeta):
self.process.wait() self.process.wait()
self.process = None self.process = None
self.reader.stop() 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()))

View File

@ -1,5 +1,6 @@
from pycsdr.types import Format from pycsdr.types import Format
from pycsdr.modules import Writer, TcpSource, ExecModule, CallbackWriter from pycsdr.modules import Writer, TcpSource, ExecModule, CallbackWriter
from csdr.module import LogWriter
from owrx.config.core import CoreConfig from owrx.config.core import CoreConfig
from owrx.config import Config from owrx.config import Config
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
@ -143,25 +144,6 @@ IGLOGIN {callsign} {password}
return config 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): class DirewolfModule(ExecModule, DirewolfConfigSubscriber):
def __init__(self, service: bool = False): def __init__(self, service: bool = False):
self.tcpSource = None 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"]) 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() # 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 # the output on its STDOUT is informative, but we still want to log it
super().setWriter(LogWriter()) super().setWriter(LogWriter(__name__))
self.start() self.start()
def __writeConfig(self): def __writeConfig(self):