diff --git a/gui/components/log_pipeline.py b/gui/components/log_pipeline.py new file mode 100644 index 0000000..68e5589 --- /dev/null +++ b/gui/components/log_pipeline.py @@ -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 diff --git a/gui/gui_app.py b/gui/gui_app.py index db18e51..45c96a6 100644 --- a/gui/gui_app.py +++ b/gui/gui_app.py @@ -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)