poc/project/llmclipboard/debug.py
2025-03-02 18:56:33 +08:00

117 lines
3.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
LLMClipboard 调试脚本
用于调试LLMClipboard应用程序提供详细的错误信息和环境检查
"""
import traceback
import sys
import os
import importlib
import time
import platform
def check_imports():
"""检查必要的导入是否可用"""
required_modules = [
'PyQt6', 'configparser', 'darkdetect', 'pynput', 'win32clipboard',
'win32con', 'keyboard', 'html2text', 'PIL', 'requests'
]
results = {}
for module in required_modules:
try:
importlib.import_module(module)
results[module] = "成功"
except ImportError as e:
results[module] = f"失败: {str(e)}"
return results
def check_resources():
"""检查资源文件是否存在"""
resources_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "resources")
if not os.path.exists(resources_dir):
return f"资源目录不存在: {resources_dir}"
icon_path = os.path.join(resources_dir, "icon.png")
if not os.path.exists(icon_path):
return f"图标文件不存在: {icon_path}"
return "资源文件检查通过"
def get_system_info():
"""获取系统信息"""
info = {
"系统": platform.system(),
"版本": platform.version(),
"架构": platform.architecture(),
"Python版本": sys.version,
"可执行文件": sys.executable,
"当前目录": os.getcwd(),
"脚本路径": os.path.abspath(__file__),
"冻结状态": getattr(sys, 'frozen', False)
}
return info
def main():
"""主函数"""
# 创建调试日志文件
debug_log_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'debug_log.txt')
with open(debug_log_path, 'w', encoding='utf-8') as f:
# 写入时间戳
f.write(f"=== LLMClipboard 调试日志 ===\n")
f.write(f"时间: {time.strftime('%Y-%m-%d %H:%M:%S')}\n\n")
# 写入系统信息
f.write("=== 系统信息 ===\n")
for key, value in get_system_info().items():
f.write(f"{key}: {value}\n")
f.write("\n")
# 检查导入
f.write("=== 模块导入检查 ===\n")
import_results = check_imports()
for module, result in import_results.items():
f.write(f"{module}: {result}\n")
f.write("\n")
# 检查资源
f.write("=== 资源文件检查 ===\n")
f.write(f"{check_resources()}\n\n")
# 尝试启动应用程序
f.write("=== 应用程序启动 ===\n")
try:
f.write("尝试导入 llmclipboard.app...\n")
from llmclipboard.app import main as app_main
f.write("尝试启动应用程序...\n")
app_main()
f.write("应用程序正常退出\n")
except Exception as e:
f.write(f"错误: {str(e)}\n")
f.write(traceback.format_exc())
# 打印到控制台
print(f"错误: {str(e)}")
print(traceback.format_exc())
# 在打包环境中,确保错误信息被记录
if getattr(sys, 'frozen', False):
error_log_path = os.path.join(os.path.dirname(sys.executable), 'error_log.txt')
with open(error_log_path, 'a', encoding='utf-8') as error_file:
error_file.write(f"{time.strftime('%Y-%m-%d %H:%M:%S')} - 错误: {str(e)}\n")
traceback.print_exc(file=error_file)
return 1
print(f"调试日志已写入: {debug_log_path}")
return 0
if __name__ == "__main__":
sys.exit(main())