feat: add logging and initial prototype

This commit is contained in:
Asger Gitz-Johansen 2024-10-10 07:26:08 +02:00
parent 9070db4a14
commit 5bcbfa3b6d
2 changed files with 30 additions and 3 deletions

View File

@ -1,12 +1,39 @@
import sys
from loguru import logger from loguru import logger
from scii.mq import MessageQueue from scii.mq import MessageQueue
logger.catch logger.catch
def main(): def main():
setup_logging("TRACE", True)
logger.info("welcome to the SCI Interface") logger.info("welcome to the SCI Interface")
with MessageQueue(rx="/sci_tx", tx="/sci_rx") as qu: with MessageQueue(rx="/sci_tx", tx="/sci_rx") as queue:
while True: while True:
r = qu.receive() r = queue.receive()
if r is None: if r is None:
break break
logger.info("msg: {}", r)
queue.send("list")
r = queue.receive()
if r is None:
break
logger.info("list: {}", r)
def setup_logging(level: str, use_colors: bool) -> None:
"""Setup logging.
Args:
level: The logging level.
use_colors: Whether to use colors in the log output.
"""
logger.remove() # A fresh start
_ = logger.add(
sys.stderr,
colorize=use_colors,
format="{time:HH:mm:ss} <level>{level}</level> <fg #888>{file}:{line}:</fg #888> <bold>{message}</bold>",
level=level,
)

View File

@ -43,7 +43,7 @@ class MessageQueue:
def receive(self) -> str | None: def receive(self) -> str | None:
if not self._is_rx_valid(): if not self._is_rx_valid():
raise RuntimeError(f"rx queue is invalid {self._rx_queue}") raise RuntimeError(f"rx queue is invalid {self._rx_queue}")
result = create_string_buffer(b'\0' * 32) result = create_string_buffer(b'\0' * 64)
res = libc.mq_receive(self._rx_queue, result, 8192, None) res = libc.mq_receive(self._rx_queue, result, 8192, None)
if res == -1: if res == -1:
libc.perror(b"mq_receive") libc.perror(b"mq_receive")