Files
douyi_guaji_main/gui/components/log_pump.py
T

144 lines
5.1 KiB
Python

"""GUI 日志泵: 把后台线程发来的日志行批量插入到日志 Text widget。
从 ``gui/gui_app.py`` 抽出。原本是 ``main()`` 内的闭包 ``pump_logs``,
通过 ``root.after(100, pump_logs)`` 自递归调度。
依赖通过显式参数注入:
- ``log_queue`` - 后台线程往里塞日志行的 ``queue.Queue``
- ``text_widget`` - 展示用的 Text/ScrolledText
- ``filter_getter`` - ``() -> str`` 返回筛选关键字(已 strip)
- ``theme_getter`` - ``() -> str`` 返回当前主题名
- ``root`` - 主窗口(用于自递归 ``after``)
- ``is_running_getter`` - ``() -> bool`` 返回 runner 是否还在跑
- ``status_getter`` - ``() -> str`` 读取状态文本
- ``status_setter`` - ``(str) -> None`` 写入状态文本
"""
from __future__ import annotations
import logging
import queue
import re
import tkinter as tk
from typing import Callable
try:
from gui.components.log_color_config import (
apply_operation_color,
get_operation_type,
)
except ImportError:
def get_operation_type(line: str): # type: ignore[no-redef]
return None
def apply_operation_color(*args, **kwargs): # type: ignore[no-redef]
pass
_LOGGER = logging.getLogger("gui.gui_app")
def pump_logs(
log_queue: "queue.Queue[str]",
*,
text_widget: tk.Text,
filter_getter: Callable[[], str],
theme_getter: Callable[[], str],
root: tk.Misc,
is_running_getter: Callable[[], bool],
status_getter: Callable[[], str],
status_setter: Callable[[str], None],
) -> None:
changed = False
try:
# 获取当前主题(用于颜色配置)
try:
current_theme = theme_getter()
except Exception:
current_theme = "litera"
while True:
line = log_queue.get_nowait()
# 运行日志关键字筛选(与行为日志一致)
try:
kw = filter_getter()
if kw and kw not in line:
continue
except Exception:
pass
try:
# 插入日志行并应用颜色标签
start_pos = text_widget.index("end-1c")
# 插入文本
text_widget.insert("end", line + "\n")
end_pos = text_widget.index("end-1c")
# 优先级1: 先检查操作类型颜色(更具体)
op_type_applied = False
try:
op_type = get_operation_type(line)
if op_type:
apply_operation_color(
text_widget, start_pos, end_pos, line, current_theme
)
op_type_applied = True
except Exception:
pass # 忽略操作类型颜色应用失败
# 优先级2: 再检查日志级别标签(作为补充,如果操作类型未匹配)
# 只有在没有操作类型颜色时才应用级别标签
if not op_type_applied:
try:
tag = None
if "[INFO]" in line:
tag = "log_info"
elif "[WARNING]" in line:
tag = "log_warning"
elif "[ERROR]" in line:
tag = "log_error"
elif "[CRITICAL]" in line:
tag = "log_critical"
elif "elapsed_seconds=" in line or "耗时" in line:
tag = "log_performance"
elif re.search(r"\[[\w\.]+\]", line):
tag = "log_module"
if tag:
text_widget.tag_add(tag, start_pos, end_pos)
except Exception:
pass # 忽略级别标签应用失败
# 不再自动滚动到底部,让用户自由控制滚动位置
changed = True
except Exception as e:
# 如果插入日志失败,至少记录到 logger(调试用)
_LOGGER.error("[GUI日志错误] 无法插入日志: %s", e)
except queue.Empty:
pass
except Exception as e:
# 捕获其他异常,确保日志循环不会中断
_LOGGER.error("[GUI日志错误] pump_logs异常: %s", e)
try:
if changed and not is_running_getter() and status_getter() == "运行中":
status_setter("已结束")
except Exception:
pass
# 确保递归调用始终执行,即使前面出现异常
try:
root.after(100, lambda: pump_logs(
log_queue,
text_widget=text_widget,
filter_getter=filter_getter,
theme_getter=theme_getter,
root=root,
is_running_getter=is_running_getter,
status_getter=status_getter,
status_setter=status_setter,
)) # type: ignore[arg-type]
except Exception as e:
# 如果 root 已被销毁,停止日志循环
_LOGGER.warning("[GUI日志错误] 无法调度下次日志更新: %s", e)