Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve logging #295

Merged
merged 1 commit into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
21 changes: 19 additions & 2 deletions tdmgr/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
Loading