Enhance Discord error reporting: code blocks and crash robustness

This commit is contained in:
2026-02-09 18:00:20 +01:00
parent 5abba6ce88
commit 19254e128c

View File

@@ -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):