From 50c07827f18ea5d682ab1b0d348c1ff61af64806 Mon Sep 17 00:00:00 2001 From: pdxlocations <117498748+pdxlocations@users.noreply.github.com> Date: Wed, 26 Feb 2025 15:36:01 -0800 Subject: [PATCH] Suppress errors from the console (#135) * suppress console messages * send errors to log * stderr and stdout --- main.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/main.py b/main.py index c39c3af..b5dfde6 100644 --- a/main.py +++ b/main.py @@ -9,9 +9,11 @@ V 1.2.1 import curses from pubsub import pub import os +import sys import logging import traceback import threading +import contextlib from utilities.arg_parser import setup_parser from utilities.interfaces import initialize_interface @@ -24,8 +26,6 @@ from db_handler import init_nodedb, load_messages_from_db import default_config as config import globals -import os - # Set ncurses compatibility settings os.environ["NCURSES_NO_UTF8_ACS"] = "1" os.environ["LANG"] = "C.UTF-8" @@ -74,8 +74,15 @@ def main(stdscr): raise if __name__ == "__main__": - try: - curses.wrapper(main) - except Exception as e: - logging.error("Fatal error in curses wrapper: %s", e) - logging.error("Traceback: %s", traceback.format_exc()) \ No newline at end of file + log_file = config.log_file_path + log_f = open(log_file, "a", buffering=1) # Enable line-buffering for immediate log writes + + sys.stdout = log_f + sys.stderr = log_f + + with contextlib.redirect_stderr(log_f), contextlib.redirect_stdout(log_f): + try: + curses.wrapper(main) + except Exception as e: + logging.error("Fatal error in curses wrapper: %s", e) + logging.error("Traceback: %s", traceback.format_exc()) \ No newline at end of file