Enhance Discord error reporting: code blocks and crash robustness
This commit is contained in:
@@ -3,6 +3,7 @@ import requests
|
|||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
import os
|
import os
|
||||||
|
import atexit
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
logger = logging.getLogger(__name__ + ".network_logging")
|
logger = logging.getLogger(__name__ + ".network_logging")
|
||||||
@@ -13,11 +14,14 @@ LOG_QUEUE = []
|
|||||||
FULL_LOG = []
|
FULL_LOG = []
|
||||||
QUEUE_LOCK = threading.Lock()
|
QUEUE_LOCK = threading.Lock()
|
||||||
ENABLED = True
|
ENABLED = True
|
||||||
|
LOG_SENT = False
|
||||||
|
|
||||||
|
|
||||||
def init_network_logging(enabled: bool = True):
|
def init_network_logging(enabled: bool = True):
|
||||||
global ENABLED
|
global ENABLED
|
||||||
ENABLED = enabled
|
ENABLED = enabled
|
||||||
|
if ENABLED:
|
||||||
|
atexit.register(send_full_log)
|
||||||
|
|
||||||
|
|
||||||
def log_to_discord(level: str, message: str, module: str = "general"):
|
def log_to_discord(level: str, message: str, module: str = "general"):
|
||||||
@@ -37,23 +41,28 @@ 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 it's an error, try to send it immediately as a message too
|
||||||
if level.upper() in ["ERROR", "CRITICAL"]:
|
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,
|
target=send_discord_message,
|
||||||
args=(f"**[{level.upper()}] [{module}]** {message[:1900]}",),
|
args=(formatted_msg,),
|
||||||
daemon=True
|
daemon=False
|
||||||
).start()
|
)
|
||||||
|
t.start()
|
||||||
|
|
||||||
|
|
||||||
def flush_logs():
|
def flush_logs():
|
||||||
# Deprecated: No partial flushing anymore
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def send_full_log():
|
def send_full_log():
|
||||||
"""Sends the entire session log as a text file attachment at the end."""
|
"""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
|
return
|
||||||
|
|
||||||
|
LOG_SENT = True
|
||||||
|
|
||||||
# Give a tiny bit of time for final async logs to land
|
# Give a tiny bit of time for final async logs to land
|
||||||
time.sleep(0.5)
|
time.sleep(0.5)
|
||||||
|
|
||||||
@@ -64,7 +73,7 @@ def send_full_log():
|
|||||||
return
|
return
|
||||||
|
|
||||||
def send_sync():
|
def send_sync():
|
||||||
temp_file = "/tmp/iridium_install_log.txt"
|
temp_file = f"/tmp/iridium_install_log_{int(time.time())}.txt"
|
||||||
try:
|
try:
|
||||||
with open(temp_file, "w") as f:
|
with open(temp_file, "w") as f:
|
||||||
for log in logs_to_send:
|
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}")
|
print(f"Failed to send full log file to Discord: {e}")
|
||||||
finally:
|
finally:
|
||||||
if os.path.exists(temp_file):
|
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
|
# Run sync if we are in atexit, otherwise thread is fine
|
||||||
t = threading.Thread(target=send_sync)
|
send_sync()
|
||||||
t.start()
|
|
||||||
t.join(timeout=30)
|
|
||||||
|
|
||||||
|
|
||||||
def send_discord_message(content: str):
|
def send_discord_message(content: str):
|
||||||
|
|||||||
Reference in New Issue
Block a user