47 lines
1.4 KiB
Python
47 lines
1.4 KiB
Python
"""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
|