From 0e85c4b8fffbb53e9a5feef7825730e88a854e93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacek=20Zi=C3=B3=C5=82kowski?= Date: Sat, 28 Sep 2024 16:31:21 +0200 Subject: [PATCH] Improve logging --- tdmgr/GUI/dialogs/main.py | 14 ++++++++++---- tdmgr/run.py | 21 +++++++++++++++++++-- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/tdmgr/GUI/dialogs/main.py b/tdmgr/GUI/dialogs/main.py index ed158a2..560201f 100644 --- a/tdmgr/GUI/dialogs/main.py +++ b/tdmgr/GUI/dialogs/main.py @@ -15,6 +15,7 @@ QPushButton, QStatusBar, ) +from paho.mqtt import MQTTException from tdmgr.GUI.console import ConsoleWidget from tdmgr.GUI.devices import DevicesListWidget @@ -121,9 +122,6 @@ def __init__( self.load_window_state() - if self.settings.value("connect_on_startup", False, bool): - self.actToggleConnect.trigger() - self.tele_docks = [] self.consoles = [] log.info(f"### TDM {self._version} START ###") @@ -252,7 +250,15 @@ def mqtt_connect(self): self.mqtt.setAuth(self.broker_username, self.broker_password) if self.mqtt.state == self.mqtt.Disconnected: - self.mqtt.connectToHost() + try: + self.mqtt.connectToHost() + except ConnectionError as e: + self.statusBar().showMessage(e.strerror) + log.error("MQTT: %s", e.strerror) + + except MQTTException as e: + self.statusBar().showMessage(e) + log.error("MQTT: %s", e) def mqtt_disconnect(self): self.mqtt.disconnectFromHost() diff --git a/tdmgr/run.py b/tdmgr/run.py index 07566f0..bf5e94e 100644 --- a/tdmgr/run.py +++ b/tdmgr/run.py @@ -17,6 +17,16 @@ except ImportError: version = "" +sys._excepthook = sys.excepthook + + +def exception_hook(exctype, value, tb): + logging.exception("%s %s", exctype, value) + sys.__excepthook__(exctype, value, tb) + + +sys.excepthook = exception_hook + def configure_logging(args) -> None: log_path = os.path.join(QDir.tempPath(), "tdm.log") @@ -33,13 +43,15 @@ def configure_logging(args) -> None: ) logging.getLogger().addHandler( - TimedRotatingFileHandler(filename=log_path, when="d", interval=1) + TimedRotatingFileHandler(filename=log_path, when="d", interval=1, encoding="utf-8") ) + logging.info("Writing log to %s", log_path) + def get_settings(args: argparse.Namespace, filename: str) -> QSettings: if args.local: - return QSettings(filename, QSettings.IniFormat) + return QSettings(f"{filename}.ini", QSettings.IniFormat) if args.config_location: return QSettings(os.path.join(args.config_location, filename), QSettings.IniFormat) return QSettings(QSettings.IniFormat, QSettings.UserScope, "tdm", filename) @@ -71,6 +83,11 @@ def start() -> None: settings, devices = get_settings(args, "tdm"), get_settings(args, "devices") MW = MainWindow(version, settings, devices, args.debug) MW.show() + app.processEvents() + + if settings.value("connect_on_startup", False, bool): + MW.mqtt_connect() + sys.exit(app.exec_()) except Exception as e: # noqa: 722