From fbe1981ff41d22db8ab2cbc303f63bc5f306d24b Mon Sep 17 00:00:00 2001 From: dylansdaniels Date: Tue, 26 Nov 2024 20:31:39 -0500 Subject: [PATCH] add class to capture print messages to the logger (#956) --- hnn_core/gui/gui.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/hnn_core/gui/gui.py b/hnn_core/gui/gui.py index a99da48fa..1588a76d8 100644 --- a/hnn_core/gui/gui.py +++ b/hnn_core/gui/gui.py @@ -151,6 +151,12 @@ def __init__(self, output_widget, *args, **kwargs): def emit(self, record): formatted_record = self.format(record) + # Further format the message for GUI presentation + try: + formatted_record = formatted_record.replace(" - ", "\n") + formatted_record = "[TIME] " + formatted_record + "\n" + except: + pass new_output = { 'name': 'stdout', 'output_type': 'stream', @@ -159,6 +165,24 @@ def emit(self, record): self.out.outputs = self.out.outputs + (new_output, ) +class _GUI_PrintToLogger: + """Class to redirect print messages to the logger in the GUI""" + # when print is used, call the write method instead + def write(self, message): + # avoid logging empty/new lines + if message.strip(): + # send the message to the logger + logger.info(message.strip()) + + # The flush method is required for compatibility with print + def flush(self): + pass + + +# assign class to stdout to redirect print statements to the logger +sys.stdout = _GUI_PrintToLogger() + + class HNNGUI: """HNN GUI class