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 Reader, Writer, CallbackWriter
from pycsdr.modules import Reader, Writer, Buffer
from pycsdr.types import Format
from abc import ABCMeta, abstractmethod
from threading import Thread
@ -198,21 +198,31 @@ class PopenModule(AutoStartModule, metaclass=ABCMeta):
self.reader.stop()
class LogWriter(CallbackWriter):
def __init__(self, prefix: str):
class LogReader(Thread):
def __init__(self, prefix: str, buffer: Buffer):
self.reader = buffer.getReader()
self.logger = logging.getLogger(prefix)
self.retained = bytes()
super().__init__(Format.CHAR)
super().__init__()
self.start()
def write(self, data: memoryview) -> None:
self.retained += data
lines = self.retained.split(b"\n")
def run(self) -> None:
while True:
data = self.reader.read()
if data is None:
return
# 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]
self.retained += data
lines = self.retained.split(b"\n")
# log all completed lines
for line in lines[0:-1]:
self.logger.info("{}: {}".format("STDOUT", line.strip(b'\n').decode()))
# 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()))
def stop(self):
self.reader.stop()

View file

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