Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MRG] Add class to capture print messages to the logger #956

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions hnn_core/gui/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -159,6 +165,24 @@ def emit(self, record):
self.out.outputs = (new_output, ) + self.out.outputs


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

Expand Down
Loading