Skip to content

Commit

Permalink
Improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jziolkowski committed Sep 28, 2024
1 parent 8b47273 commit d725241
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
14 changes: 10 additions & 4 deletions tdmgr/GUI/dialogs/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
QPushButton,
QStatusBar,
)
from paho.mqtt import MQTTException

from tdmgr.GUI.console import ConsoleWidget
from tdmgr.GUI.devices import DevicesListWidget
Expand Down Expand Up @@ -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 ###")
Expand Down Expand Up @@ -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()
Expand Down
22 changes: 20 additions & 2 deletions tdmgr/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@
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.exit(1)


sys.excepthook = exception_hook


def configure_logging(args) -> None:
log_path = os.path.join(QDir.tempPath(), "tdm.log")
Expand All @@ -33,13 +44,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)
Expand Down Expand Up @@ -71,6 +84,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
Expand Down

0 comments on commit d725241

Please sign in to comment.