-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcrash_handler.py
More file actions
51 lines (42 loc) · 1.75 KB
/
Copy pathcrash_handler.py
File metadata and controls
51 lines (42 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import sys
import traceback
import datetime
import os
from pathlib import Path
def setup_crash_handler():
"""设置全局崩溃处理器"""
def handle_exception(exc_type, exc_value, exc_traceback):
if issubclass(exc_type, KeyboardInterrupt):
sys.__excepthook__(exc_type, exc_value, exc_traceback)
return
# 动态获取版本号,避免硬编码
try:
from utils.config import VERSION
version = VERSION
except Exception:
version = "unknown"
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
error_dir = Path.home() / ".phone_controller" / "crash_reports"
error_dir.mkdir(parents=True, exist_ok=True)
error_file = error_dir / f"crash_{timestamp}.txt"
with open(error_file, 'w', encoding='utf-8') as f:
f.write(f"=== ClickZen Crash Report ===\n")
f.write(f"Time: {datetime.datetime.now()}\n")
f.write(f"Version: {version}\n")
f.write(f"Python: {sys.version}\n")
f.write(f"OS: {os.name} {sys.platform}\n\n=== Error Details ===\n")
traceback.print_exception(exc_type, exc_value, exc_traceback, file=f)
try:
from PyQt6.QtWidgets import QApplication, QMessageBox
if QApplication.instance():
QMessageBox.critical(
None,
"程序崩溃",
f"程序已崩溃!\n\n"
f"类型: {exc_type.__name__}\n"
f"信息: {exc_value}\n\n"
f"报告保存位置:\n{error_file}"
)
except Exception:
print(f"错误报告已保存到: {error_file}")
sys.excepthook = handle_exception