BodyBalanceEvaluation/debug_server.py

153 lines
4.3 KiB
Python
Raw 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 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)
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',
'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('初始化应用...')
init_app()
# 获取本机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()