filelogging added and update of mytui

This commit is contained in:
Dirk Alders 2025-07-26 16:35:08 +02:00
parent b3d07b2acb
commit 1935727a11
5 changed files with 33 additions and 6 deletions

3
.gitignore vendored
View File

@ -1,3 +1,6 @@
# loggs
mqtt_sniffer.log*
# ---> Linux # ---> Linux
*~ *~

View File

@ -3,13 +3,13 @@ import config
import getpass import getpass
import logging import logging
import mqtt import mqtt
import re import os
import report import report
import time import time
from textual.app import App, ComposeResult from textual.app import App, ComposeResult
from textual.containers import Vertical 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 from mytui import MultiSelect
@ -19,6 +19,15 @@ except ImportError:
ROOT_LOGGER_NAME = 'root' ROOT_LOGGER_NAME = 'root'
logger = logging.getLogger(ROOT_LOGGER_NAME) 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): class MqttHandler(object):
def __init__(self, app): def __init__(self, app):
@ -58,6 +67,8 @@ class MqttSniffer(App):
self.args = args self.args = args
self.password = password self.password = password
# #
self.__log_to_file__ = False
#
self.mqtt = None self.mqtt = None
self.all_logs = [] self.all_logs = []
self.__topic_selection__ = MultiSelect((), prompt="Full", id="topic_filter") self.__topic_selection__ = MultiSelect((), prompt="Full", id="topic_filter")
@ -73,6 +84,7 @@ class MqttSniffer(App):
with Vertical(id="filter-bar"): with Vertical(id="filter-bar"):
yield self.__topic_selection__ yield self.__topic_selection__
yield Input(placeholder="Filter", id="select_filter") yield Input(placeholder="Filter", id="select_filter")
yield Checkbox("ToFile", self.__log_to_file__, id="log_to_file")
with Vertical(id="send-bar"): with Vertical(id="send-bar"):
yield Input(placeholder="topic...", id="send_topic") yield Input(placeholder="topic...", id="send_topic")
yield Input(placeholder="payload...", id="send_payload") yield Input(placeholder="payload...", id="send_payload")
@ -87,6 +99,8 @@ class MqttSniffer(App):
"""Add new mqt messages and update the tui.""" """Add new mqt messages and update the tui."""
asctime = time.asctime() asctime = time.asctime()
self.__topic_selection__.AddEntry(record.topic) 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)) self.all_logs.append((asctime, record))
if len(self.all_logs) > self.MAX_LOGS: if len(self.all_logs) > self.MAX_LOGS:
self.all_logs = self.all_logs[-self.MAX_LOGS:] self.all_logs = self.all_logs[-self.MAX_LOGS:]
@ -136,6 +150,12 @@ class MqttSniffer(App):
else: else:
logger.warning("Can't send mqtt message with empty topic. topic=%s; payload=%s", repr(self.send_topic), repr(self.send_payload)) 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__": if __name__ == "__main__":
# #

2
mytui

@ -1 +1 @@
Subproject commit bd390d27f00147c1305e244fbd6d0650a6bdcec9 Subproject commit 8fc7e7b5622ec8b53be05f2bbe82b4bc46e24709

2
report

@ -1 +1 @@
Subproject commit 92bf9fb66a809a8bc71b02b6b815a4b00d56e445 Subproject commit 3b8fbbc0844d17377b3d5da84bca8cad6d24d143

View File

@ -7,8 +7,8 @@
#filter-bar { #filter-bar {
layout: grid; layout: grid;
grid-size: 2; grid-size: 3;
grid-columns: 4fr 1fr; grid-columns: 5fr 1fr 1fr;
height: 4; height: 4;
border-top: solid $primary; border-top: solid $primary;
} }
@ -21,6 +21,10 @@
border-top: solid $primary; border-top: solid $primary;
} }
#log_to_file {
width: 100%;
}
#topic_filter { #topic_filter {
width: 100%; width: 100%;
} }