Skip to content

Commit

Permalink
enh: introduce logging and make logging Path available to the user
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmueller committed Aug 23, 2024
1 parent c8b1cfe commit ffdced0
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,4 @@ dmypy.json
_version_save.py
_version.py

mpldc-dump.txt
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
0.7.3
- fix: segmentation fault due to mixing threading.Thread with PyQt6
- enh: introduce logging and make logging Path available to the user
- enh: improved logging
- enh: increased verbosity when encountering exceptions during hash check
0.7.2
Expand Down
38 changes: 38 additions & 0 deletions mpl_data_cast/gui/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from importlib import resources
import logging
import os
import time
import signal
import pathlib
Expand Down Expand Up @@ -38,6 +39,30 @@ def __init__(self, *args, **kwargs):
QtCore.QEventLoop.ProcessEventsFlag.AllEvents, 300)
sys.exit(0)

# Logging output path
path_logging = pathlib.Path(
QtCore.QStandardPaths.writableLocation(
QtCore.QStandardPaths.StandardLocation.TempLocation)
) / "MPLDCUILogs"
path_logging.mkdir(parents=True, exist_ok=True)
# Remove logs if there are more than 10
if len(logs := sorted(path_logging.glob("*.log"))) > 10:
for _ in range(len(logs) - 10):
try:
logs.pop(-1).unlink()
except BaseException:
print(traceback.format_exc())
self.log_path = path_logging / time.strftime(
"MPLDCUILog_%Y-%m-%d_%H.%M.%S.log", time.localtime())
mpldc_logger = logging.getLogger("mpl_data_cast")
log_formatter = logging.Formatter(
"%(asctime)s %(levelname)s %(processName)s/%(threadName)s "
+ "in %(name)s: %(message)s")
file_handler = logging.FileHandler(self.log_path)
file_handler.setFormatter(log_formatter)
mpldc_logger.addHandler(file_handler)
logging.info(f"Log file: {self.log_path}")

ref_ui = resources.files("mpl_data_cast.gui") / "main.ui"
with resources.as_file(ref_ui) as path_ui:
uic.loadUi(path_ui, self)
Expand Down Expand Up @@ -65,6 +90,7 @@ def __init__(self, *args, **kwargs):
self.actionPreferences.triggered.connect(self.on_action_preferences)
self.actionQuit.triggered.connect(self.on_action_quit)
# Help menu
self.actionShowLogDirectory.triggered.connect(self.on_action_logshow)
self.actionSoftware.triggered.connect(self.on_action_software)
self.actionAbout.triggered.connect(self.on_action_about)

Expand All @@ -85,6 +111,18 @@ def current_recipe(self):
name = self.comboBox_recipe.currentData()
return mpldc_recipe.map_recipe_name_to_class(name)

@QtCore.pyqtSlot()
def on_action_logshow(self):
"""Show the logging directory to the user"""
if os.name == "nt":
os.startfile(os.path.normpath(str(self.log_path.parent)))
else:
QtWidgets.QMessageBox.information(
self,
"Logging directory",
f"The logging directory is located at: {self.log_path.parent}"
)

@QtCore.pyqtSlot()
def on_action_preferences(self):
"""Show the preferences dialog"""
Expand Down
22 changes: 14 additions & 8 deletions mpl_data_cast/gui/main.ui
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<string>**Recipe:**</string>
</property>
<property name="textFormat">
<enum>Qt::MarkdownText</enum>
<enum>Qt::TextFormat::MarkdownText</enum>
</property>
</widget>
</item>
Expand All @@ -66,14 +66,14 @@
<string>Recipe description goes here.</string>
</property>
<property name="textFormat">
<enum>Qt::MarkdownText</enum>
<enum>Qt::TextFormat::MarkdownText</enum>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
<enum>Qt::Orientation::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
Expand Down Expand Up @@ -160,7 +160,7 @@
<x>0</x>
<y>0</y>
<width>1142</width>
<height>22</height>
<height>23</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
Expand All @@ -174,6 +174,7 @@
<property name="title">
<string>Help</string>
</property>
<addaction name="actionShowLogDirectory"/>
<addaction name="actionSoftware"/>
<addaction name="actionAbout"/>
</widget>
Expand All @@ -183,25 +184,30 @@
<widget class="QStatusBar" name="statusbar"/>
<action name="actionQuit">
<property name="text">
<string>Quit</string>
<string>&amp;Quit</string>
</property>
<property name="iconVisibleInMenu">
<bool>true</bool>
</property>
</action>
<action name="actionAbout">
<property name="text">
<string>About</string>
<string>&amp;About</string>
</property>
</action>
<action name="actionSoftware">
<property name="text">
<string>Software</string>
<string>&amp;Software</string>
</property>
</action>
<action name="actionPreferences">
<property name="text">
<string>Preferences</string>
<string>&amp;Preferences</string>
</property>
</action>
<action name="actionShowLogDirectory">
<property name="text">
<string>Show &amp;Log directory</string>
</property>
</action>
</widget>
Expand Down

0 comments on commit ffdced0

Please sign in to comment.