From 1935727a1113b3a59b5f0a61344e2eeb1b8ec51f Mon Sep 17 00:00:00 2001 From: Dirk Alders Date: Sat, 26 Jul 2025 16:35:08 +0200 Subject: [PATCH] filelogging added and update of mytui --- .gitignore | 3 +++ mqtt_sniffer.py | 24 ++++++++++++++++++++++-- mytui | 2 +- report | 2 +- style.tcss | 8 ++++++-- 5 files changed, 33 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index d08faf7..e99ab5b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# loggs +mqtt_sniffer.log* + # ---> Linux *~ diff --git a/mqtt_sniffer.py b/mqtt_sniffer.py index 54bf9a0..2de76e1 100644 --- a/mqtt_sniffer.py +++ b/mqtt_sniffer.py @@ -3,13 +3,13 @@ import config import getpass import logging import mqtt -import re +import os import report import time from textual.app import App, ComposeResult from textual.containers import Vertical -from textual.widgets import Footer, Header, Input, RichLog, Button, Select +from textual.widgets import Footer, Header, Input, RichLog, Button, Select, Checkbox from mytui import MultiSelect @@ -19,6 +19,15 @@ except ImportError: ROOT_LOGGER_NAME = 'root' logger = logging.getLogger(ROOT_LOGGER_NAME) +filename = os.path.splitext(__file__)[0] + '.log' +filelogger = logging.getLogger('filelogger') +filehandler = report.add_handler_file(filelogger, filename=filename, maxMbytes=5, backupCount=3, fmt="[%(asctime)s] %(message)s") + + +def filerotate(): + if os.path.getsize(filename) > 0: + filehandler.doRollover() + class MqttHandler(object): def __init__(self, app): @@ -58,6 +67,8 @@ class MqttSniffer(App): self.args = args self.password = password # + self.__log_to_file__ = False + # self.mqtt = None self.all_logs = [] self.__topic_selection__ = MultiSelect((), prompt="Full", id="topic_filter") @@ -73,6 +84,7 @@ class MqttSniffer(App): with Vertical(id="filter-bar"): yield self.__topic_selection__ yield Input(placeholder="Filter", id="select_filter") + yield Checkbox("ToFile", self.__log_to_file__, id="log_to_file") with Vertical(id="send-bar"): yield Input(placeholder="topic...", id="send_topic") yield Input(placeholder="payload...", id="send_payload") @@ -87,6 +99,8 @@ class MqttSniffer(App): """Add new mqt messages and update the tui.""" asctime = time.asctime() self.__topic_selection__.AddEntry(record.topic) + if self.__log_to_file__ and self.__topic_selection__.IsSelected(record.topic): + filelogger.info("%s - %s", record.topic, record.payload) self.all_logs.append((asctime, record)) if len(self.all_logs) > self.MAX_LOGS: self.all_logs = self.all_logs[-self.MAX_LOGS:] @@ -136,6 +150,12 @@ class MqttSniffer(App): else: logger.warning("Can't send mqtt message with empty topic. topic=%s; payload=%s", repr(self.send_topic), repr(self.send_payload)) + def on_checkbox_changed(self, message: Checkbox.Changed) -> None: + if message.checkbox.id == "log_to_file": + if message.value: + filerotate() + self.__log_to_file__ = message.value + if __name__ == "__main__": # diff --git a/mytui b/mytui index bd390d2..8fc7e7b 160000 --- a/mytui +++ b/mytui @@ -1 +1 @@ -Subproject commit bd390d27f00147c1305e244fbd6d0650a6bdcec9 +Subproject commit 8fc7e7b5622ec8b53be05f2bbe82b4bc46e24709 diff --git a/report b/report index 92bf9fb..3b8fbbc 160000 --- a/report +++ b/report @@ -1 +1 @@ -Subproject commit 92bf9fb66a809a8bc71b02b6b815a4b00d56e445 +Subproject commit 3b8fbbc0844d17377b3d5da84bca8cad6d24d143 diff --git a/style.tcss b/style.tcss index 51c5f48..2d8f35f 100644 --- a/style.tcss +++ b/style.tcss @@ -7,8 +7,8 @@ #filter-bar { layout: grid; - grid-size: 2; - grid-columns: 4fr 1fr; + grid-size: 3; + grid-columns: 5fr 1fr 1fr; height: 4; border-top: solid $primary; } @@ -21,6 +21,10 @@ border-top: solid $primary; } +#log_to_file { + width: 100%; +} + #topic_filter { width: 100%; }