Skip to content

Commit a3320f6

Browse files
Add virtual fit log messages to status bar (#139)
1 parent 5150e24 commit a3320f6

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

OpenLIFUData/OpenLIFUData.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
display_errors,
4141
create_noneditable_QStandardItem,
4242
ensure_list,
43-
add_slicer_log_handler,
43+
add_slicer_log_handler_for_openlifu_object,
4444
BusyCursor,
4545
)
4646

@@ -1474,7 +1474,7 @@ def load_database(self, path: Path) -> Sequence[Tuple[str,str]]:
14741474
self._subjects = {}
14751475

14761476
self.db = openlifu_lz().Database(path)
1477-
add_slicer_log_handler(self.db)
1477+
add_slicer_log_handler_for_openlifu_object(self.db)
14781478

14791479
subject_ids : List[str] = ensure_list(self.db.get_subject_ids())
14801480
self._subjects = {

OpenLIFULib/OpenLIFULib/util.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ def emit(self, record):
5454
method_to_use = self.handle_warning
5555
else: # info or any other unaccounted for log message level
5656
method_to_use = self.handle_info
57+
58+
slicer.app.processEvents()
5759
method_to_use(self.format(record))
60+
slicer.app.processEvents()
5861

5962
def handle_error(self, msg):
6063
slicer.util.errorDisplay(f"{self.name_to_print}: {msg}")
@@ -65,9 +68,26 @@ def handle_warning(self, msg):
6568
def handle_info(self, msg):
6669
slicer.util.showStatusMessage(f"{self.name_to_print}: {msg}")
6770

68-
def add_slicer_log_handler(openlifu_object: Any):
71+
def add_slicer_log_handler(logger_name : str, name_to_print : str):
72+
"""Adds a SlicerLogHandler to the logger of a given name,
73+
and only doing so if that logger doesn't already have a SlicerLogHandler.
74+
75+
Args:
76+
logger_name: The name of the logger that should receive Slicer log handling
77+
name_to_print: The display name of the logger to put on Slicer messages and
78+
dialogs to indicate which logger the messages are coming from.
79+
"""
80+
logger : logging.Logger = logging.getLogger(logger_name)
81+
if not any(isinstance(h, SlicerLogHandler) for h in logger.handlers):
82+
handler = SlicerLogHandler(name_to_print)
83+
logger.addHandler(handler)
84+
85+
def add_slicer_log_handler_for_openlifu_object(openlifu_object: Any):
6986
"""Adds an appropriately named SlicerLogHandler to the logger of an openlifu object,
70-
and only doing so if that logger doesn't already have a SlicerLogHandler"""
87+
and only doing so if that logger doesn't already have a SlicerLogHandler.
88+
This is designed to work with those openlifu classes that have a `logger` attribute,
89+
a common pattern in the openlifu python codebase.
90+
"""
7191
if not hasattr(openlifu_object, "logger"):
7292
raise ValueError("This object does not have a logger attribute.")
7393
if not hasattr(openlifu_object, "__class__"):

OpenLIFUPrePlanning/OpenLIFUPrePlanning.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
SlicerOpenLIFUProtocol,
2323
SlicerOpenLIFUTransducer,
2424
)
25-
from OpenLIFULib.util import replace_widget, BusyCursor
25+
from OpenLIFULib.util import replace_widget, BusyCursor, add_slicer_log_handler
2626
from OpenLIFULib.virtual_fit_results import (
2727
add_virtual_fit_result,
2828
clear_virtual_fit_results,
@@ -556,6 +556,8 @@ def virtual_fit(
556556
target: vtkMRMLMarkupsFiducialNode,
557557
) -> Optional[vtkMRMLTransformNode]:
558558

559+
add_slicer_log_handler("VirtualFit", "Virtual fitting")
560+
559561
# TODO: Many quantities are hard-coded here will not have to be when these two issues are done:
560562
# https://github.com/OpenwaterHealth/OpenLIFU-python/issues/166
561563
# https://github.com/OpenwaterHealth/OpenLIFU-python/issues/165
@@ -578,7 +580,6 @@ def virtual_fit(
578580
(-50, 50), # ax
579581
),
580582
)
581-
# TODO: add log handler for this
582583

583584
session = get_openlifu_data_parameter_node().loaded_session
584585
session_id : Optional[str] = session.get_session_id() if session is not None else None

0 commit comments

Comments
 (0)