refactor(gui): 抽 pump_logs 到 components/log_pump (4117 → 4044)
This commit is contained in:
1 parent
c5df7a05d2
commit
a7ef92fc7c
2 files changed
+154
-84
No files matched your search
@@ -0,0 +1,143 @@
|
||||
"""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)
|
||||
|
||||
+11
-84
@@ -57,6 +57,7 @@ from gui.components.actions_log_view import (
|
||||
from gui.components.wifi_adb_pick_dialog import open_scan_pick_dialog as _open_scan_pick_dialog_impl
|
||||
from gui.components.wifi_adb_reconnect import try_reconnect_last_wifi as _try_reconnect_last_wifi_impl
|
||||
from gui.components.ocr_results_view import update_ocr_results as _update_ocr_results_impl
|
||||
from gui.components.log_pump import pump_logs as _pump_logs_impl
|
||||
|
||||
# 导入日志颜色配置模块
|
||||
try:
|
||||
@@ -3488,90 +3489,16 @@ def main():
|
||||
ttk.Label(actions, textvariable=status_var).pack(side=RIGHT)
|
||||
|
||||
def pump_logs():
|
||||
changed = False
|
||||
try:
|
||||
# 获取当前主题(用于颜色配置)
|
||||
try:
|
||||
current_theme = str(gui_theme_var.get() or "litera")
|
||||
except Exception:
|
||||
current_theme = "litera"
|
||||
|
||||
while True:
|
||||
line = q.get_nowait()
|
||||
# 运行日志关键字筛选(与行为日志一致)
|
||||
try:
|
||||
kw = log_filter_var.get().strip()
|
||||
if kw and kw not in line:
|
||||
continue
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
# 插入日志行并应用颜色标签
|
||||
start_pos = log_text.index("end-1c")
|
||||
|
||||
# 插入文本
|
||||
log_text.insert("end", line + "\n")
|
||||
end_pos = log_text.index("end-1c")
|
||||
|
||||
# 优先级1: 先检查操作类型颜色(更具体)
|
||||
op_type_applied = False
|
||||
try:
|
||||
op_type = get_operation_type(line)
|
||||
if op_type:
|
||||
apply_operation_color(
|
||||
log_text, 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:
|
||||
log_text.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 runner.is_running() and status_var.get() == "运行中":
|
||||
status_var.set("已结束")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 确保递归调用始终执行,即使前面出现异常
|
||||
try:
|
||||
root.after(100, pump_logs) # type: ignore[arg-type]
|
||||
except Exception as e:
|
||||
# 如果 root 已被销毁,停止日志循环
|
||||
_LOGGER.warning("[GUI日志错误] 无法调度下次日志更新: %s", e)
|
||||
|
||||
_pump_logs_impl(
|
||||
q,
|
||||
text_widget=log_text,
|
||||
filter_getter=lambda: log_filter_var.get().strip(),
|
||||
theme_getter=lambda: str(gui_theme_var.get() or "litera"),
|
||||
root=root,
|
||||
is_running_getter=runner.is_running,
|
||||
status_getter=status_var.get,
|
||||
status_setter=status_var.set,
|
||||
)
|
||||
# 统计信息显示
|
||||
stats_top = ttk.Frame(stats_tab)
|
||||
stats_top.pack(fill=X, padx=10, pady=(10, 0))
|
||||
|
||||
Reference in new issue
Block a user