Skip to content

Commit 52cccd1

Browse files
committed
fix: debug mode changes logging
1 parent 3a22d1a commit 52cccd1

File tree

2 files changed

+58
-25
lines changed

2 files changed

+58
-25
lines changed

loopstructural/gui/visualisation/feature_list_widget.py

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import logging
12
from typing import Optional, Union
23

34
from PyQt5.QtWidgets import QMenu, QPushButton, QTreeWidget, QTreeWidgetItem, QVBoxLayout, QWidget
45

56
from LoopStructural.datatypes import VectorPoints
67

8+
logger = logging.getLogger(__name__)
9+
710

811
class FeatureListWidget(QWidget):
912
def __init__(self, parent=None, *, model_manager=None, viewer=None):
@@ -183,22 +186,22 @@ def add_data(self, feature_name):
183186
source_feature=feature_name,
184187
source_type='feature_data',
185188
)
186-
print(f"Adding data to feature: {feature_name}")
189+
logger.debug(f"Adding data to feature: {feature_name}")
187190

188191
def add_model_bounding_box(self):
189192
if not self.model_manager:
190-
print("Model manager is not set.")
193+
logger.debug("Model manager is not set.")
191194
return
192195
bb = self.model_manager.model.bounding_box.vtk().outline()
193196
self.viewer.add_mesh_object(
194197
bb, name='model_bounding_box', source_feature='__model__', source_type='bounding_box'
195198
)
196199
# Logic for adding model bounding box
197-
print("Adding model bounding box...")
200+
logger.debug("Adding model bounding box...")
198201

199202
def add_fault_surfaces(self):
200203
if not self.model_manager:
201-
print("Model manager is not set.")
204+
logger.debug("Model manager is not set.")
202205
return
203206
self.model_manager.update_all_features(subset='faults')
204207
fault_surfaces = self.model_manager.model.get_fault_surfaces()
@@ -210,11 +213,11 @@ def add_fault_surfaces(self):
210213
source_type='fault_surface',
211214
isovalue=0.0,
212215
)
213-
print("Adding fault surfaces...")
216+
logger.debug("Adding fault surfaces...")
214217

215218
def add_stratigraphic_surfaces(self):
216219
if not self.model_manager:
217-
print("Model manager is not set.")
220+
logger.debug("Model manager is not set.")
218221
return
219222
stratigraphic_surfaces = self.model_manager.model.get_stratigraphic_surfaces()
220223
for surface in stratigraphic_surfaces:
@@ -238,8 +241,8 @@ def _on_model_update(self, event: str, *args):
238241
'model_updated' notifications the previous behaviour (re-add all
239242
affected feature representations) is preserved.
240243
"""
241-
print(f"Model update event received: {event} with args: {args}")
242-
print([f"Mesh: {name}, Meta: {meta}" for name, meta in self.viewer.meshes.items()])
244+
logger.debug(f"Model update event received: {event} with args: {args}")
245+
logger.debug([f"Mesh: {name}, Meta: {meta}" for name, meta in self.viewer.meshes.items()])
243246
if not self.model_manager or not self.viewer:
244247
return
245248
if event not in ('model_updated', 'feature_updated'):
@@ -253,14 +256,14 @@ def _on_model_update(self, event: str, *args):
253256
if feature_name is not None:
254257
if meta.get('source_feature', None) == feature_name:
255258
affected_features.add(feature_name)
256-
print(f"Updating visualisation for feature: {feature_name}")
259+
logger.debug(f"Updating visualisation for feature: {feature_name}")
257260
continue
258261

259262
sf = meta.get('source_feature', None)
260263

261264
if sf is not None:
262265
affected_features.add(sf)
263-
print(f"Affected features to update: {affected_features}")
266+
logger.debug(f"Affected features to update: {affected_features}")
264267
# For each affected feature, only update existing meshes tied to that feature
265268
for feature_name in affected_features:
266269
# collect mesh names that belong to this feature (snapshot to avoid mutation while iterating)
@@ -269,7 +272,7 @@ def _on_model_update(self, event: str, *args):
269272
for name, meta in list(self.viewer.meshes.items())
270273
if meta.get('source_feature') == feature_name
271274
]
272-
print(f"Re-adding meshes for feature: {feature_name}: {meshes_for_feature}")
275+
logger.debug(f"Re-adding meshes for feature: {feature_name}: {meshes_for_feature}")
273276

274277
for mesh_name in meshes_for_feature:
275278
meta = self.viewer.meshes.get(mesh_name, {})
@@ -280,9 +283,9 @@ def _on_model_update(self, event: str, *args):
280283
# remove existing actor/entry so add_mesh_object can recreate with same name
281284
try:
282285
self.viewer.remove_object(mesh_name)
283-
print(f"Removed existing mesh: {mesh_name}")
286+
logger.debug(f"Removed existing mesh: {mesh_name}")
284287
except Exception:
285-
print(f"Failed to remove existing mesh: {mesh_name}")
288+
logger.debug(f"Failed to remove existing mesh: {mesh_name}")
286289
pass
287290

288291
try:
@@ -297,7 +300,7 @@ def _on_model_update(self, event: str, *args):
297300

298301
if surfaces:
299302
add_name = mesh_name
300-
print(
303+
logger.debug(
301304
f"Re-adding surface for feature: {feature_name} with isovalue: {isovalue}"
302305
)
303306
self.viewer.add_mesh_object(
@@ -310,7 +313,7 @@ def _on_model_update(self, event: str, *args):
310313
)
311314
continue
312315
except Exception as e:
313-
print(
316+
logger.debug(
314317
f"Failed to find matching surface for feature: {feature_name} with isovalue: {isovalue}, trying all surfaces. Error: {e}"
315318
)
316319

@@ -323,7 +326,7 @@ def _on_model_update(self, event: str, *args):
323326
None,
324327
)
325328
if match is not None:
326-
print(f"Re-adding fault surface for: {feature_name}")
329+
logger.debug(f"Re-adding fault surface for: {feature_name}")
327330
self.viewer.add_mesh_object(
328331
match.vtk(),
329332
name=mesh_name,
@@ -334,7 +337,7 @@ def _on_model_update(self, event: str, *args):
334337
)
335338
continue
336339
except Exception as e:
337-
print(f"Failed to re-add fault surface for {feature_name}: {e}")
340+
logger.debug(f"Failed to re-add fault surface for {feature_name}: {e}")
338341

339342
# Stratigraphic surfaces (added via add_stratigraphic_surfaces)
340343
if source_type == 'stratigraphic_surface':
@@ -345,7 +348,7 @@ def _on_model_update(self, event: str, *args):
345348
None,
346349
)
347350
if match is not None:
348-
print(f"Re-adding stratigraphic surface for: {feature_name}")
351+
logger.debug(f"Re-adding stratigraphic surface for: {feature_name}")
349352
self.viewer.add_mesh_object(
350353
match.vtk(),
351354
name=mesh_name,
@@ -356,49 +359,53 @@ def _on_model_update(self, event: str, *args):
356359
)
357360
continue
358361
except Exception as e:
359-
print(f"Failed to re-add stratigraphic surface for {feature_name}: {e}")
362+
logger.debug(
363+
f"Failed to re-add stratigraphic surface for {feature_name}: {e}"
364+
)
360365

361366
# Vectors, points, scalar fields and other feature related objects
362367
if source_type == 'feature_vector' or source_type == 'feature_vectors':
363368
try:
364369
self.add_vector_field(feature_name)
365370
continue
366371
except Exception as e:
367-
print(f"Failed to re-add vector field for {feature_name}: {e}")
372+
logger.debug(f"Failed to re-add vector field for {feature_name}: {e}")
368373

369374
if source_type in ('feature_points', 'feature_data'):
370375
try:
371376
self.add_data(feature_name)
372377
continue
373378
except Exception as e:
374-
print(f"Failed to re-add data for {feature_name}: {e}")
379+
logger.debug(f"Failed to re-add data for {feature_name}: {e}")
375380

376381
if source_type == 'feature_scalar':
377382
try:
378383
self.add_scalar_field(feature_name)
379384
continue
380385
except Exception as e:
381-
print(f"Failed to re-add scalar field for {feature_name}: {e}")
386+
logger.debug(f"Failed to re-add scalar field for {feature_name}: {e}")
382387

383388
if source_type == 'bounding_box' or mesh_name == 'model_bounding_box':
384389
try:
385390
self.add_model_bounding_box()
386391
continue
387392
except Exception as e:
388-
print(f"Failed to re-add bounding box: {e}")
393+
logger.debug(f"Failed to re-add bounding box: {e}")
389394

390395
# Fallback: if nothing matched, attempt to re-add by using viewer metadata
391396
# Many viewer entries store the vtk source under meta['vtk'] or similar; try best-effort
392397
try:
393398
vtk_src = meta.get('vtk')
394399
if vtk_src is not None:
395-
print(f"Fallback re-add for mesh {mesh_name}")
400+
logger.debug(f"Fallback re-add for mesh {mesh_name}")
396401
self.viewer.add_mesh_object(vtk_src, name=mesh_name, **kwargs)
397402
except Exception:
398403
pass
399404

400405
except Exception as e:
401-
print(f"Failed to update visualisation for feature: {feature_name}. Error: {e}")
406+
logger.debug(
407+
f"Failed to update visualisation for feature: {feature_name}. Error: {e}"
408+
)
402409

403410
# Refresh the viewer
404411
try:

loopstructural/toolbelt/preferences.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"""
88

99
# standard
10+
import logging
1011
from dataclasses import asdict, dataclass, fields
1112

1213
# PyQGIS
@@ -44,6 +45,21 @@ class PlgOptionsManager:
4445
plugin to persist user preferences such as debug mode and UI options.
4546
"""
4647

48+
@staticmethod
49+
def _configure_logging(debug_mode: bool):
50+
"""Configure Python logging level according to plugin debug setting.
51+
52+
When debug_mode is True the root logger level is set to DEBUG so that
53+
any logger.debug(...) calls in the plugin will be emitted. When False
54+
the level is set to INFO to reduce verbosity.
55+
"""
56+
try:
57+
root = logging.getLogger()
58+
root.setLevel(logging.DEBUG if bool(debug_mode) else logging.INFO)
59+
except Exception:
60+
# Best-effort: do not raise from logging configuration issues
61+
pass
62+
4763
@staticmethod
4864
def get_plg_settings() -> PlgSettingsStructure:
4965
"""Load and return plugin settings as a PlgSettingsStructure instance.
@@ -74,6 +90,9 @@ def get_plg_settings() -> PlgSettingsStructure:
7490

7591
settings.endGroup()
7692

93+
# Ensure logging level matches the loaded debug_mode preference
94+
PlgOptionsManager._configure_logging(options.debug_mode)
95+
7796
return options
7897

7998
@staticmethod
@@ -171,6 +190,13 @@ def set_value_from_key(cls, key: str, value) -> bool:
171190
try:
172191
settings.setValue(key, value)
173192
out_value = True
193+
194+
# If debug mode was toggled, immediately apply logging configuration
195+
if key == "debug_mode":
196+
try:
197+
PlgOptionsManager._configure_logging(value)
198+
except Exception:
199+
pass
174200
except Exception as err:
175201
log_hdlr.PlgLogger.log(
176202
message="Error occurred trying to set settings: {}.Trace: {}".format(key, err)

0 commit comments

Comments
 (0)