refactor(gui): 抽 on_line + Runner + log_writer 到 components/log_pipeline (440 → 418)

This commit is contained in:
Your Name committed 2026-05-30 18:47:36 +08:00
1 parent 8e0ae9396d
commit 84c5731cc3
2 files changed
+50 -26

No files matched your search

+46
View File
@@ -0,0 +1,46 @@
"""GUI 日志管道工厂:queue + 线程安全文件写入 + Runner 集成。
从 ``gui/gui_app.py`` 抽出 on_line 闭包及其依赖。
"""
from __future__ import annotations
import logging
import queue
from typing import Any, Callable, Tuple
from gui.gui_core import Runner
from gui.components.log_writer import ThreadSafeLogWriter
_LOGGER = logging.getLogger("gui.gui_app")
def setup_log_pipeline(
*,
format_gui_log_line: Callable[[str], str],
) -> Tuple["queue.Queue[str]", Any, Callable[[str], None], Any]:
"""构建 log queue + ThreadSafeLogWriter + on_line + Runner。
返回 ``(q, log_writer, on_line, runner)``,主程序后续把 ``runner`` 传入按钮回调,
把 ``q`` 传入 ``pump_logs`` 用于 GUI 端消费。
"""
q: "queue.Queue[str]" = queue.Queue()
log_writer = ThreadSafeLogWriter()
def on_line(line: str) -> None:
"""处理日志行:GUI 显示用精简版本,文件写入用完整版本"""
# GUI 显示:使用精简后的版本
try:
display_line = format_gui_log_line(line)
except Exception:
display_line = line
q.put(display_line)
# 文件:写入完整日志行(不做精简处理)
try:
log_writer.write(line)
except Exception:
pass # 文件写入失败不应影响GUI显示
runner = Runner(on_line=on_line)
return q, log_writer, on_line, runner
+4 -26
View File
@@ -1,6 +1,5 @@
import logging
import os
import queue
import re
import sys
import warnings
@@ -16,7 +15,6 @@ from PIL import ImageTk # type: ignore[import]
# 抽离:ADB/配置/参数结构体到 gui_core.py(便于后续模块化)
from gui.gui_core import (
RunParams,
Runner,
read_config,
write_config,
)
@@ -80,6 +78,7 @@ from gui.components.config_vars import build_config_vars
from gui.components.top_device_bar import setup_top_device_bar
from gui.components.top_buttons import setup_top_buttons
from gui.components.log_tab import setup_log_tab
from gui.components.log_pipeline import setup_log_pipeline
from gui.components.periodic_refreshers import (
setup_periodic_refreshers,
setup_close_handler,
@@ -156,30 +155,9 @@ def main():
root.title("抖音福袋挂机 - 可视化控制台")
root.geometry("1400x900") # 增大窗口尺寸,提供更多空间
q: queue.Queue[str] = queue.Queue()
# 线程安全的日志文件写入器(已抽到 gui/components/log_writer)
from gui.components.log_writer import ThreadSafeLogWriter
log_writer = ThreadSafeLogWriter()
# 日志格式化已移至模块级别函数 format_gui_log_line()
def on_line(line: str):
"""处理日志行:GUI 显示用精简版本,文件写入用完整版本"""
# GUI 显示:使用精简后的版本
try:
display_line = format_gui_log_line(line)
except Exception:
display_line = line
q.put(display_line)
# 文件:写入完整日志行(不做精简处理)
try:
log_writer.write(line)
except Exception:
pass # 文件写入失败不应影响GUI显示
runner = Runner(on_line=on_line)
q, _log_writer, on_line, runner = setup_log_pipeline(
format_gui_log_line=format_gui_log_line
)
# 顶部:设备选择 + Y偏移(其余运行参数放到左侧分组,避免重复)
top = ttk.Frame(root)