Skip to content

Commit ffdced0

Browse files
committed
enh: introduce logging and make logging Path available to the user
1 parent c8b1cfe commit ffdced0

File tree

4 files changed

+54
-8
lines changed

4 files changed

+54
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,4 @@ dmypy.json
132132
_version_save.py
133133
_version.py
134134

135+
mpldc-dump.txt

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
0.7.3
22
- fix: segmentation fault due to mixing threading.Thread with PyQt6
3+
- enh: introduce logging and make logging Path available to the user
34
- enh: improved logging
45
- enh: increased verbosity when encountering exceptions during hash check
56
0.7.2

mpl_data_cast/gui/main.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from importlib import resources
22
import logging
3+
import os
34
import time
45
import signal
56
import pathlib
@@ -38,6 +39,30 @@ def __init__(self, *args, **kwargs):
3839
QtCore.QEventLoop.ProcessEventsFlag.AllEvents, 300)
3940
sys.exit(0)
4041

42+
# Logging output path
43+
path_logging = pathlib.Path(
44+
QtCore.QStandardPaths.writableLocation(
45+
QtCore.QStandardPaths.StandardLocation.TempLocation)
46+
) / "MPLDCUILogs"
47+
path_logging.mkdir(parents=True, exist_ok=True)
48+
# Remove logs if there are more than 10
49+
if len(logs := sorted(path_logging.glob("*.log"))) > 10:
50+
for _ in range(len(logs) - 10):
51+
try:
52+
logs.pop(-1).unlink()
53+
except BaseException:
54+
print(traceback.format_exc())
55+
self.log_path = path_logging / time.strftime(
56+
"MPLDCUILog_%Y-%m-%d_%H.%M.%S.log", time.localtime())
57+
mpldc_logger = logging.getLogger("mpl_data_cast")
58+
log_formatter = logging.Formatter(
59+
"%(asctime)s %(levelname)s %(processName)s/%(threadName)s "
60+
+ "in %(name)s: %(message)s")
61+
file_handler = logging.FileHandler(self.log_path)
62+
file_handler.setFormatter(log_formatter)
63+
mpldc_logger.addHandler(file_handler)
64+
logging.info(f"Log file: {self.log_path}")
65+
4166
ref_ui = resources.files("mpl_data_cast.gui") / "main.ui"
4267
with resources.as_file(ref_ui) as path_ui:
4368
uic.loadUi(path_ui, self)
@@ -65,6 +90,7 @@ def __init__(self, *args, **kwargs):
6590
self.actionPreferences.triggered.connect(self.on_action_preferences)
6691
self.actionQuit.triggered.connect(self.on_action_quit)
6792
# Help menu
93+
self.actionShowLogDirectory.triggered.connect(self.on_action_logshow)
6894
self.actionSoftware.triggered.connect(self.on_action_software)
6995
self.actionAbout.triggered.connect(self.on_action_about)
7096

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

114+
@QtCore.pyqtSlot()
115+
def on_action_logshow(self):
116+
"""Show the logging directory to the user"""
117+
if os.name == "nt":
118+
os.startfile(os.path.normpath(str(self.log_path.parent)))
119+
else:
120+
QtWidgets.QMessageBox.information(
121+
self,
122+
"Logging directory",
123+
f"The logging directory is located at: {self.log_path.parent}"
124+
)
125+
88126
@QtCore.pyqtSlot()
89127
def on_action_preferences(self):
90128
"""Show the preferences dialog"""

mpl_data_cast/gui/main.ui

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
<string>**Recipe:**</string>
5454
</property>
5555
<property name="textFormat">
56-
<enum>Qt::MarkdownText</enum>
56+
<enum>Qt::TextFormat::MarkdownText</enum>
5757
</property>
5858
</widget>
5959
</item>
@@ -66,14 +66,14 @@
6666
<string>Recipe description goes here.</string>
6767
</property>
6868
<property name="textFormat">
69-
<enum>Qt::MarkdownText</enum>
69+
<enum>Qt::TextFormat::MarkdownText</enum>
7070
</property>
7171
</widget>
7272
</item>
7373
<item>
7474
<spacer name="horizontalSpacer">
7575
<property name="orientation">
76-
<enum>Qt::Horizontal</enum>
76+
<enum>Qt::Orientation::Horizontal</enum>
7777
</property>
7878
<property name="sizeHint" stdset="0">
7979
<size>
@@ -160,7 +160,7 @@
160160
<x>0</x>
161161
<y>0</y>
162162
<width>1142</width>
163-
<height>22</height>
163+
<height>23</height>
164164
</rect>
165165
</property>
166166
<widget class="QMenu" name="menuFile">
@@ -174,6 +174,7 @@
174174
<property name="title">
175175
<string>Help</string>
176176
</property>
177+
<addaction name="actionShowLogDirectory"/>
177178
<addaction name="actionSoftware"/>
178179
<addaction name="actionAbout"/>
179180
</widget>
@@ -183,25 +184,30 @@
183184
<widget class="QStatusBar" name="statusbar"/>
184185
<action name="actionQuit">
185186
<property name="text">
186-
<string>Quit</string>
187+
<string>&amp;Quit</string>
187188
</property>
188189
<property name="iconVisibleInMenu">
189190
<bool>true</bool>
190191
</property>
191192
</action>
192193
<action name="actionAbout">
193194
<property name="text">
194-
<string>About</string>
195+
<string>&amp;About</string>
195196
</property>
196197
</action>
197198
<action name="actionSoftware">
198199
<property name="text">
199-
<string>Software</string>
200+
<string>&amp;Software</string>
200201
</property>
201202
</action>
202203
<action name="actionPreferences">
203204
<property name="text">
204-
<string>Preferences</string>
205+
<string>&amp;Preferences</string>
206+
</property>
207+
</action>
208+
<action name="actionShowLogDirectory">
209+
<property name="text">
210+
<string>Show &amp;Log directory</string>
205211
</property>
206212
</action>
207213
</widget>

0 commit comments

Comments
 (0)