switch out logreader with a buffer-based implementation (again)

This commit is contained in:
Jakob Ketterl 2023-09-16 00:39:00 +02:00
parent e201ca07e3
commit 5058a91db0
2 changed files with 31 additions and 17 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, CallbackWriter from pycsdr.modules import Reader, Writer, Buffer
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
@ -198,21 +198,31 @@ class PopenModule(AutoStartModule, metaclass=ABCMeta):
self.reader.stop() self.reader.stop()
class LogWriter(CallbackWriter): class LogReader(Thread):
def __init__(self, prefix: str): def __init__(self, prefix: str, buffer: Buffer):
self.reader = buffer.getReader()
self.logger = logging.getLogger(prefix) self.logger = logging.getLogger(prefix)
self.retained = bytes() self.retained = bytes()
super().__init__(Format.CHAR) super().__init__()
self.start()
def write(self, data: memoryview) -> None: def run(self) -> None:
self.retained += data while True:
lines = self.retained.split(b"\n") data = self.reader.read()
if data is None:
return
# keep the last line self.retained += data
# this should either be empty if the last char was \n lines = self.retained.split(b"\n")
# or an incomplete line if the read returned early
self.retained = lines[-1]
# log all completed lines # keep the last line
for line in lines[0:-1]: # this should either be empty if the last char was \n
self.logger.info("{}: {}".format("STDOUT", line.strip(b'\n').decode())) # 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()))
def stop(self):
self.reader.stop()

View file

@ -1,6 +1,6 @@
from pycsdr.types import Format from pycsdr.types import Format
from pycsdr.modules import Writer, TcpSource, ExecModule from pycsdr.modules import Writer, TcpSource, ExecModule, Buffer
from csdr.module import LogWriter from csdr.module import LogReader
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
@ -160,7 +160,9 @@ 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(__name__)) buffer = Buffer(Format.CHAR)
self.logReader = LogReader(__name__, buffer)
super().setWriter(buffer)
self.start() self.start()
def __writeConfig(self): def __writeConfig(self):
@ -199,6 +201,8 @@ class DirewolfModule(ExecModule, DirewolfConfigSubscriber):
def stop(self) -> None: def stop(self) -> None:
super().stop() super().stop()
self.logReader.stop()
self.logReader = None
os.unlink(self.direwolfConfigPath) os.unlink(self.direwolfConfigPath)
self.direwolfConfig.unwire(self) self.direwolfConfig.unwire(self)
self.direwolfConfig = None self.direwolfConfig = None