From 19254e128cb86b1e8aa54d38e961496f2e4f19b3 Mon Sep 17 00:00:00 2001 From: N0VA Date: Mon, 9 Feb 2026 18:00:20 +0100 Subject: [PATCH] Enhance Discord error reporting: code blocks and crash robustness --- iridium_installer/backend/network_logging.py | 33 +++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/iridium_installer/backend/network_logging.py b/iridium_installer/backend/network_logging.py index 23cb62b..0306fdc 100644 --- a/iridium_installer/backend/network_logging.py +++ b/iridium_installer/backend/network_logging.py @@ -3,6 +3,7 @@ import requests import threading import time import os +import atexit from datetime import datetime logger = logging.getLogger(__name__ + ".network_logging") @@ -13,11 +14,14 @@ LOG_QUEUE = [] FULL_LOG = [] QUEUE_LOCK = threading.Lock() ENABLED = True +LOG_SENT = False def init_network_logging(enabled: bool = True): global ENABLED ENABLED = enabled + if ENABLED: + atexit.register(send_full_log) def log_to_discord(level: str, message: str, module: str = "general"): @@ -37,22 +41,27 @@ def log_to_discord(level: str, message: str, module: str = "general"): # If it's an error, try to send it immediately as a message too if level.upper() in ["ERROR", "CRITICAL"]: - threading.Thread( + formatted_msg = f"**[{level.upper()}] [{module}]**\n```\n{message[:1800]}\n```" + # Use a non-daemon thread to ensure it finishes even if main thread exits + t = threading.Thread( target=send_discord_message, - args=(f"**[{level.upper()}] [{module}]** {message[:1900]}",), - daemon=True - ).start() + args=(formatted_msg,), + daemon=False + ) + t.start() def flush_logs(): - # Deprecated: No partial flushing anymore pass def send_full_log(): """Sends the entire session log as a text file attachment at the end.""" - if not ENABLED: + global LOG_SENT + if not ENABLED or LOG_SENT: return + + LOG_SENT = True # Give a tiny bit of time for final async logs to land time.sleep(0.5) @@ -64,7 +73,7 @@ def send_full_log(): return def send_sync(): - temp_file = "/tmp/iridium_install_log.txt" + temp_file = f"/tmp/iridium_install_log_{int(time.time())}.txt" try: with open(temp_file, "w") as f: for log in logs_to_send: @@ -80,12 +89,12 @@ def send_full_log(): print(f"Failed to send full log file to Discord: {e}") finally: if os.path.exists(temp_file): - os.remove(temp_file) + try: + os.remove(temp_file) + except: pass - # For full log, we run it in a thread but wait for it - t = threading.Thread(target=send_sync) - t.start() - t.join(timeout=30) + # Run sync if we are in atexit, otherwise thread is fine + send_sync() def send_discord_message(content: str):