Use logger

This commit is contained in:
CPD 2025-03-06 11:54:11 +01:00
parent 1e35f9618a
commit 40632d5de5
3 changed files with 43 additions and 16 deletions

View File

@ -16,15 +16,15 @@ def run() -> int:
Returns:
int: The exit status code.
"""
log.debug("Starting app")
app: QApplication = QApplication(sys.argv)
icon_path = resources.get_resource_path("icons/icon.png")
app.setWindowIcon(QIcon(icon_path))
AppConfig.initialize()
window: MainWindow = MainWindow()
window.show()
exitcode = app.exec()
print("Saving configuration")
log.info("Saving configuration")
AppConfig.finalize()
return sys.exit(exitcode)
@ -33,11 +33,24 @@ import sys
import traceback
import logging
from PyQt6 import QtCore, QtWidgets
from os import path, makedirs
AppConfig.initialize()
log_path = path.expanduser(AppConfig.MAIN_CFG.get_or("path_log", "~/.cache/cpdctrl-gui.log"))
makedirs(path.dirname(log_path), exist_ok=True)
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(message)s",
handlers=[
logging.FileHandler(log_path),
logging.StreamHandler()
]
)
log = logging.getLogger(__name__)
log.critical("Test")
# This is taken from:
# https://timlehr.com/2018/01/python-exception-hooks-with-qt-message-box/index.html
# basic logger functionality
log = logging.getLogger(__name__)
handler = logging.StreamHandler(stream=sys.stdout)
log.addHandler(handler)
@ -75,10 +88,10 @@ class UncaughtHook(QtCore.QObject):
exc_info = (exc_type, exc_value, exc_traceback)
log_msg = '\n'.join([''.join(traceback.format_tb(exc_traceback)),
'{0}: {1}'.format(exc_type.__name__, exc_value)])
log.critical("Uncaught exception:\n {0}".format(log_msg), exc_info=exc_info)
log.critical(f"Uncaught exception:\n {log_msg}", exc_info=exc_info)
# trigger message box show
self._exception_caught.emit(log_msg)
# create a global instance of our class to register the hook
# qt_exception_hook = UncaughtHook()
qt_exception_hook = UncaughtHook()

View File

@ -17,6 +17,9 @@ from .widgets.about import MarkdownView
from .widgets.led_script import LedScriptViewer
# from .widgets.treeview import TreeView
import logging
log = logging.getLogger(__name__)
import multiprocessing as mp
import threading as mt
@ -28,8 +31,6 @@ from cpdctrl.led_script import LedScript
from cpdctrl.utility.data import DataCollector
from cpdctrl.measurement import measure
class MainWindow(QMainWindow):
"""
MainWindow
@ -62,7 +63,6 @@ class MainWindow(QMainWindow):
layout.addWidget(self.w_leftbox)
metadata_init_dict = AppConfig.MEAS_CFG.get_or("metadata", {})
print(metadata_init_dict)
self.w_metadata = MetadataInput(metadata_init_dict)
self.w_leftbox.addItem(self.w_metadata, "Measurement metadata")
# Measurement settings
@ -138,6 +138,7 @@ class MainWindow(QMainWindow):
# LED DEVICE MANAGEMENT
def leddev_connect(self, leddev_type, leddev_name):
log.info(f"Connecting to LED device {leddev_name} ({leddev_type})")
self.leddev = ledd.connect_device(leddev_type, leddev_name)
AppConfig.MAIN_CFG.set("led_device_last.type", leddev_type)
AppConfig.MAIN_CFG.set("led_device_last.name", leddev_name)
@ -149,6 +150,9 @@ class MainWindow(QMainWindow):
self.leddev_connect(leddev_type, leddev_name)
except KeyError:
pass
except Exception as e:
log.error(f"Failed to auto-connect to LED device: {e}")
def leddev_connect_from_dialog(self):
"""
Open a dialog that lets the user choose a LED control device,
@ -158,11 +162,11 @@ class MainWindow(QMainWindow):
device_dialog = ListChoice(devices, window_title="Select LED device")
if device_dialog.exec() == QDialog.DialogCode.Accepted:
leddev_type, leddev_name = device_dialog.get_selected()
if self.verbose: print(f"Connecting to {leddev_type}:{leddev_name}")
self.leddev_connect(leddev_type, leddev_name)
# VOLTAGE DEVICE MANAGEMENT
def vmdev_connect(self, vmdev_type, vmdev_name):
log.info(f"Connecting to voltage measurement device {vmdev_name} ({vmdev_type})")
self.vmdev = vmd.connect_device(vmdev_type, vmdev_name)
AppConfig.MAIN_CFG.set("voltage_measurement_device_last.type", vmdev_type)
AppConfig.MAIN_CFG.set("voltage_measurement_device_last.name", vmdev_name)
@ -176,7 +180,7 @@ class MainWindow(QMainWindow):
except KeyError:
pass
except Exception as e:
print(f"Failed to auto-connect to voltage measurement device: {e}")
log.error(f"Failed to auto-connect to voltage measurement device: {e}")
def vmdev_connect_from_dialog(self):
"""
Open a dialog that lets the user choose a voltage measurement device,
@ -186,7 +190,6 @@ class MainWindow(QMainWindow):
device_dialog = ListChoice(devices, window_title="Select CPD Measurement Device")
if device_dialog.exec() == QDialog.DialogCode.Accepted:
vmdev_type, vmdev_name = device_dialog.get_selected()
if self.verbose: print(f"Connecting to {vmdev_type}:{vmdev_name}")
self.vmdev_connect(vmdev_type, vmdev_name)
# MEASUREMENT
@ -227,8 +230,7 @@ class MainWindow(QMainWindow):
"led_script": str(script)
})
if self.verbose:
print(f"Starting measurement with:\n\tinterval = {interval}\n\tflush_after = {flush_after}\n\tuse_buffer = {use_buffer}\n\tmax_measurements = {max_measurements}\n\tstop_on_script_end = {stop_on_script_end}")
log.info(f"Starting measurement with:\n\tinterval = {interval}\n\tflush_after = {flush_after}\n\tuse_buffer = {use_buffer}\n\tmax_measurements = {max_measurements}\n\tstop_on_script_end = {stop_on_script_end}")
self.led_script = LedScript(script=script, auto_update=True, verbose=True)
self.data_collector = DataCollector(metadata=metadata, data_path=AppConfig.MAIN_CFG.get("datadir"), data_name=measurement_name)
@ -256,6 +258,7 @@ class MainWindow(QMainWindow):
self.measurement_timer.start(300) # TODO: set interval
def measure_stop(self):
log.info("Stopping measurement")
if not self.measurement_is_running():
raise RuntimeError("measure_stop: Measurement is not running")
self.command_queue.put("stop")
@ -328,15 +331,16 @@ class MainWindow(QMainWindow):
buttons.accepted.connect(dialog.accept)
dialog.setLayout(QVBoxLayout())
# show the logo via a pixmap in a label
img_path = get_resource_path("icons/logo.svg")
img_path = get_resource_path("icons/logo2.svg")
pixmap = QPixmap(img_path)
pixmap = pixmap.scaled(128, 128, Qt.AspectRatioMode.KeepAspectRatio, Qt.TransformationMode.SmoothTransformation)
# qt cant find the file
label = QLabel()
label.setPixmap(pixmap)
label.setAlignment(Qt.AlignmentFlag.AlignCenter) # center the image
dialog.layout().addWidget(label)
# show about.md
dialog.layout().addWidget(MarkdownView("about.md"))
dialog.layout().addWidget(MarkdownView("about2.md"))
dialog.layout().addWidget(buttons)
dialog.exec()

View File

@ -1,3 +1,4 @@
from importlib.metadata import PackageNotFoundError
from os import path, environ
from cpdctrl.utility.config_file import ConfigFile
@ -8,6 +9,7 @@ class AppConfig:
Configuration File
"""
APP_NAME: str = "cpdctrl-gui"
APP_VERSION: str = "unknown"
CONFIG_DIR: str = path.expanduser("~/.config/cpdctrl")
MAIN_CFG: ConfigFile = None
MEAS_CFG: ConfigFile = None
@ -31,6 +33,14 @@ class AppConfig:
AppConfig.MEAS_CFG_PATH = path.join(AppConfig.CONFIG_DIR, "cpdctrl-gui-measurement.yaml")
AppConfig.MEAS_CFG = ConfigFile(AppConfig.MEAS_CFG_PATH, {})
# Get the Version from importlib
# This will always show the version of the installed package, not the local development version
from importlib.metadata import version, PackageNotFoundError
try:
AppConfig.APP_VERSION = version("cpdctrl-gui")
except PackageNotFoundError:
pass
@classmethod
def finalize(cls) -> None:
"""