Files
douyin_guaji-main/test_realtime_logging.py
T
Your Name e68f1dd3de feat: Refactor coverage management and enhance logging functionality
- Removed the `.coverage` file from the repository to streamline coverage management.
- Updated `.gitignore` to exclude coverage-related files and directories, ensuring a cleaner project structure.
- Introduced new scripts for checking GPU availability and detecting GUI parameter conflicts, enhancing the overall functionality of the application.
- Improved logging mechanisms across various modules to support better debugging and monitoring.
- Added comprehensive documentation for new features and updated existing markdown files for clarity.
2025-12-23 02:38:51 +08:00

161 lines
5.1 KiB
Python

"""真机测试:验证GUI日志写入功能"""
import os
import sys
import time
import subprocess
from datetime import datetime
os.chdir(os.path.dirname(os.path.abspath(__file__)))
from path_config import get_logs_dir, PROJECT_ROOT
def get_log_file():
"""获取今天的日志文件路径"""
log_dir = get_logs_dir()
today = datetime.now().strftime("%Y-%m-%d")
try:
import json
config_path = os.path.join(PROJECT_ROOT, "config.json")
if os.path.exists(config_path):
with open(config_path, "r", encoding="utf-8") as f:
config = json.load(f)
log_path = config.get("log_unified_path") or "logs/unified.log"
if not os.path.isabs(log_path):
log_path = os.path.join(PROJECT_ROOT, log_path)
use_dated_logs = config.get("logging", {}).get("use_dated_logs", True)
if use_dated_logs:
base_name = os.path.splitext(log_path)[0]
return f"{base_name}_{today}.log"
return log_path
except Exception:
pass
return os.path.join(log_dir, f"unified_{today}.log")
def main():
print("=" * 60)
print("GUI日志真机测试")
print("=" * 60)
log_file = get_log_file()
print(f"日志文件: {log_file}")
# 检查初始状态
if os.path.exists(log_file):
initial_size = os.path.getsize(log_file)
with open(log_file, "r", encoding="utf-8-sig") as f:
initial_lines = len(f.readlines())
print(f"初始大小: {initial_size} bytes")
print(f"初始行数: {initial_lines}")
else:
initial_size = 0
initial_lines = 0
print("日志文件不存在,等待创建...")
# 启动run_cli.py进行测试
print("\n[测试] 启动run_cli.py进行30秒测试...")
print("[测试] 设备ID: MB75DEFQZHZTGMLJ")
try:
proc = subprocess.Popen(
[sys.executable, "run_cli.py", "--wait-minutes", "0", "--device-id", "MB75DEFQZHZTGMLJ"],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
encoding="utf-8",
errors="replace",
cwd=os.path.dirname(__file__)
)
print("[测试] 进程已启动,等待30秒...")
# 每5秒检查一次日志文件
for i in range(6):
time.sleep(5)
elapsed = (i + 1) * 5
if os.path.exists(log_file):
current_size = os.path.getsize(log_file)
with open(log_file, "r", encoding="utf-8-sig") as f:
current_lines = len(f.readlines())
print(f"[{elapsed:2d}s] 大小: {current_size:6d} bytes | 行数: {current_lines:4d}")
else:
print(f"[{elapsed:2d}s] 日志文件尚未创建")
print("\n[测试] 30秒已过,终止进程...")
proc.terminate()
try:
proc.wait(timeout=5)
except subprocess.TimeoutExpired:
proc.kill()
print("[测试] 进程已终止")
except Exception as e:
print(f"[错误] 启动测试失败: {e}")
return False
# 最终检查
print("\n" + "=" * 60)
print("测试结果")
print("=" * 60)
if not os.path.exists(log_file):
print("[失败] 日志文件未创建")
return False
final_size = os.path.getsize(log_file)
with open(log_file, "r", encoding="utf-8-sig") as f:
final_lines = len(f.readlines())
size_diff = final_size - initial_size
line_diff = final_lines - initial_lines
print(f"初始大小: {initial_size} bytes")
print(f"最终大小: {final_size} bytes")
print(f"增加大小: {size_diff} bytes")
print(f"初始行数: {initial_lines}")
print(f"最终行数: {final_lines}")
print(f"增加行数: {line_diff}")
# 检查日志格式
print("\n[验证] 检查日志格式...")
with open(log_file, "r", encoding="utf-8-sig") as f:
lines = f.readlines()
# 检查最后10行
check_lines = lines[-10:] if len(lines) >= 10 else lines
format_ok = True
print("\n最后10行日志:")
for i, line in enumerate(check_lines, 1):
line = line.strip()
if line:
print(f" {i:2d}. {line[:100]}")
# 检查格式
if not (line[0].isdigit() and " - [" in line):
if "[GUI] -" not in line:
format_ok = False
# 判断结果
print("\n" + "=" * 60)
if size_diff > 0 and line_diff > 0 and format_ok:
print("[结果] 测试通过!日志写入功能正常。")
return True
else:
print("[结果] 测试失败。")
if size_diff <= 0:
print(" - 日志文件大小未增加")
if line_diff <= 0:
print(" - 日志文件行数未增加")
if not format_ok:
print(" - 日志格式异常")
return False
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)