diff --git a/DEBUG_GUIDE.md b/DEBUG_GUIDE.md deleted file mode 100644 index cbb3045c..00000000 --- a/DEBUG_GUIDE.md +++ /dev/null @@ -1,243 +0,0 @@ -# 调试指南 - -本文档介绍如何在开发过程中进行调试,包括断点调试、日志调试等方法。 - -## 调试方式概览 - -### 1. 批处理文件调试 (推荐新手) -```bash -# 启动调试模式 -./start_debug.bat -``` - -### 2. Python脚本调试 (推荐) -```bash -# 直接运行调试服务器 -python debug_server.py -``` - -### 3. VS Code调试 (推荐开发者) -- 打开VS Code -- 按F5或点击调试按钮 -- 选择"Debug Backend Server"配置 - -### 4. 命令行调试 -```bash -# 激活虚拟环境 -backend\venv\Scripts\activate - -# 设置环境变量 -set FLASK_ENV=development -set FLASK_DEBUG=1 -set PYTHONPATH=%cd% - -# 启动调试服务器 -python -u backend\app.py -``` - -## 详细调试方法 - -### VS Code 断点调试 - -1. **安装Python扩展** - - 确保VS Code已安装Python扩展 - -2. **打开项目** - ```bash - code . - ``` - -3. **设置断点** - - 在代码行号左侧点击设置断点 - - 红色圆点表示断点已设置 - -4. **启动调试** - - 按F5或点击调试面板的播放按钮 - - 选择"Debug Backend Server"配置 - -5. **调试操作** - - F10: 单步跳过 - - F11: 单步进入 - - Shift+F11: 单步跳出 - - F5: 继续执行 - - Shift+F5: 停止调试 - -### PyCharm 调试 - -1. **打开项目** - - File -> Open -> 选择项目目录 - -2. **配置Python解释器** - - File -> Settings -> Project -> Python Interpreter - - 选择backend/venv/Scripts/python.exe - -3. **创建运行配置** - - Run -> Edit Configurations - - 添加新的Python配置 - - Script path: debug_server.py - - Working directory: 项目根目录 - -4. **设置断点并调试** - - 点击行号左侧设置断点 - - 点击调试按钮启动 - -### 命令行调试 - -1. **使用pdb调试器** - ```python - import pdb - pdb.set_trace() # 在需要调试的地方插入 - ``` - -2. **使用ipdb (增强版pdb)** - ```bash - pip install ipdb - ``` - ```python - import ipdb - ipdb.set_trace() - ``` - -## 调试配置说明 - -### 环境变量 -- `FLASK_ENV=development`: 启用开发模式 -- `FLASK_DEBUG=1`: 启用调试模式 -- `PYTHONPATH`: 设置Python模块搜索路径 - -### 调试端口 -- 后端服务: http://127.0.0.1:5000 -- 健康检查: http://127.0.0.1:5000/health -- WebSocket: ws://127.0.0.1:5000/socket.io/ - -## 常见调试场景 - -### 1. WebSocket连接问题 -```python -# 在handle_connect函数中设置断点 -@socketio.on('connect') -def handle_connect(): - print(f'客户端连接: {request.sid}') # 添加调试输出 - # 设置断点在这里 - emit('connect_status', {'status': 'connected'}) -``` - -### 2. RTSP流问题 -```python -# 在generate_video_frames函数中设置断点 -def generate_video_frames(): - print(f'RTSP URL: {rtsp_url}') # 调试输出 - # 设置断点检查rtsp_url值 - cap = cv2.VideoCapture(rtsp_url) -``` - -### 3. API请求问题 -```python -# 在API路由中设置断点 -@app.route('/api/patients', methods=['GET']) -def get_patients(): - print(f'请求参数: {request.args}') # 调试输出 - # 设置断点检查请求参数 -``` - -### 4. 数据库操作问题 -```python -# 在数据库操作中设置断点 -def get_patients(self, page, size, keyword): - print(f'查询参数: page={page}, size={size}, keyword={keyword}') - # 设置断点检查SQL查询 -``` - -## 日志调试 - -### 查看日志文件 -```bash -# 实时查看日志 -tail -f logs/debug.log -tail -f logs/backend.log -``` - -### 调整日志级别 -```python -# 在代码中临时调整日志级别 -import logging -logging.getLogger().setLevel(logging.DEBUG) -``` - -## 前端调试 - -### 浏览器开发者工具 -1. 按F12打开开发者工具 -2. Console标签页查看JavaScript错误 -3. Network标签页查看网络请求 -4. WebSocket连接在Network -> WS中查看 - -### 前端调试技巧 -```javascript -// 在浏览器控制台中测试WebSocket连接 -const socket = io('http://127.0.0.1:5000'); -socket.on('connect', () => console.log('连接成功')); -socket.emit('start_video', {}); -``` - -## 性能调试 - -### 使用cProfile -```bash -python -m cProfile -o profile_output.prof debug_server.py -``` - -### 内存使用监控 -```bash -pip install memory-profiler -python -m memory_profiler debug_server.py -``` - -## 故障排除 - -### 常见问题 - -1. **端口被占用** - ```bash - netstat -ano | findstr :5000 - taskkill /PID /F - ``` - -2. **模块导入错误** - - 检查PYTHONPATH设置 - - 确认虚拟环境已激活 - -3. **权限问题** - - 以管理员身份运行 - - 检查文件夹权限 - -4. **依赖包问题** - ```bash - pip install -r backend/requirements.txt --force-reinstall - ``` - -### 调试检查清单 - -- [ ] Python虚拟环境已激活 -- [ ] 所有依赖包已安装 -- [ ] 环境变量设置正确 -- [ ] 端口5000未被占用 -- [ ] config.ini文件存在且配置正确 -- [ ] 日志文件可以正常写入 -- [ ] 断点设置在正确位置 - -## 调试最佳实践 - -1. **逐步调试**: 从简单的断点开始,逐步深入 -2. **日志记录**: 在关键位置添加详细的日志输出 -3. **单元测试**: 编写测试用例验证功能 -4. **代码审查**: 定期检查代码逻辑 -5. **版本控制**: 使用Git跟踪代码变更 - -## 获取帮助 - -如果遇到调试问题,可以: -1. 查看logs目录下的日志文件 -2. 检查控制台输出信息 -3. 使用浏览器开发者工具 -4. 参考项目文档和代码注释 \ No newline at end of file diff --git a/debug_server.py b/debug_server.py deleted file mode 100644 index 7473dc0a..00000000 --- a/debug_server.py +++ /dev/null @@ -1,164 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -""" -身体平衡评估系统 - 调试服务器启动脚本 - -这个脚本专门用于调试模式,提供更好的调试体验: -1. 支持IDE断点调试 -2. 详细的错误信息输出 -3. 热重载功能 -4. 调试日志输出 -""" - -import os -import sys -import logging -import socket -from pathlib import Path - -# 添加项目路径 -project_root = Path(__file__).parent -sys.path.insert(0, str(project_root)) -sys.path.insert(0, str(project_root / 'backend')) - -def setup_debug_logging(): - """设置调试日志""" - # 创建logs目录 - logs_dir = project_root / 'logs' - logs_dir.mkdir(exist_ok=True) - - # 配置日志格式 - logging.basicConfig( - level=logging.DEBUG, - format='%(asctime)s - %(name)s - %(levelname)s - [%(filename)s:%(lineno)d] - %(message)s', - handlers=[ - logging.FileHandler(logs_dir / 'debug.log', encoding='utf-8'), - logging.StreamHandler(sys.stdout) - ] - ) - - # 设置Flask和SocketIO的日志级别 - logging.getLogger('werkzeug').setLevel(logging.DEBUG) - logging.getLogger('socketio').setLevel(logging.DEBUG) - logging.getLogger('engineio').setLevel(logging.DEBUG) - - # 禁用第三方库的详细日志 - logging.getLogger('PIL').setLevel(logging.WARNING) - logging.getLogger('PIL.PngImagePlugin').setLevel(logging.WARNING) - logging.getLogger('matplotlib').setLevel(logging.WARNING) - logging.getLogger('matplotlib.font_manager').setLevel(logging.WARNING) - - logger = logging.getLogger(__name__) - logger.info('调试日志已启用') - return logger - -def get_local_ip(): - """获取本机IP地址""" - try: - # 创建一个UDP socket连接到外部地址来获取本机IP - with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: - s.connect(("8.8.8.8", 80)) - return s.getsockname()[0] - except Exception: - return "127.0.0.1" - -def check_debug_environment(): - """检查调试环境""" - logger = logging.getLogger(__name__) - - # 检查Python版本 - if sys.version_info < (3, 8): - logger.error('需要Python 3.8或更高版本') - return False - - # 检查必要文件 - required_files = [ - 'backend/app.py', - 'backend/config.ini', - 'backend/requirements.txt' - ] - - for file_path in required_files: - if not (project_root / file_path).exists(): - logger.error(f'缺少必要文件: {file_path}') - return False - - logger.info('调试环境检查通过') - return True - -def start_debug_server(): - """启动调试服务器""" - logger = logging.getLogger(__name__) - - try: - # 设置环境变量 - os.environ['FLASK_ENV'] = 'development' - os.environ['FLASK_DEBUG'] = '1' - os.environ['PYTHONPATH'] = str(project_root) - - # 导入Flask应用 - from backend.app import app, socketio, init_app - - # 初始化应用 - logger.info('初始化应用...') - try: - init_app() - logger.info('应用初始化成功') - except Exception as init_e: - logger.error(f'应用初始化失败: {init_e}', exc_info=True) - raise - - # 获取本机IP地址 - local_ip = get_local_ip() - - # 启动调试服务器 - logger.info('启动调试服务器...') - logger.info('调试模式已启用 - 可以在IDE中设置断点') - logger.info('本地访问: http://127.0.0.1:5000') - logger.info(f'远程访问: http://{local_ip}:5000') - logger.info('健康检查: http://127.0.0.1:5000/health') - logger.info('按 Ctrl+C 停止服务器') - - # 启动SocketIO服务器(支持调试和远程访问) - # socketio.run( - # app, - # host='0.0.0.0', # 允许所有IP访问 - # port=5000, - # debug=True, - # use_reloader=False, # 禁用热重载以避免FemtoBolt设备资源冲突 - # log_output=True, # 输出详细日志 - # allow_unsafe_werkzeug=True - # ) - - except KeyboardInterrupt: - logger.info('服务器被用户中断') - except Exception as e: - logger.error(f'服务器启动失败: {e}', exc_info=True) - return False - - return True - -def main(): - """主函数""" - print('='*50) - print('身体平衡评估系统 - 调试模式') - print('='*50) - print() - - # 设置调试日志 - logger = setup_debug_logging() - - # 检查环境 - if not check_debug_environment(): - input('按任意键退出...') - sys.exit(1) - - # 启动调试服务器 - success = start_debug_server() - - if not success: - input('按任意键退出...') - sys.exit(1) - -if __name__ == '__main__': - main() \ No newline at end of file