"""行为日志查看组件 包含行为日志显示、搜索等功能的GUI组件。 """ from __future__ import annotations from typing import Any, Callable, Tuple, Optional import tkinter as tk from tkinter import ttk from tkinter.scrolledtext import ScrolledText import os from typing import List try: from gui.components.log_color_config_fixed import ( get_operation_type, get_color_config, apply_operation_color, ) except Exception: # 回退到旧配置模块名(兼容旧代码) try: from gui.components.log_color_config import ( get_operation_type, get_color_config, apply_operation_color, ) except Exception: get_operation_type = None get_color_config = None apply_operation_color = None try: from ttkbootstrap.scrolled import ScrolledText as TBSrolledText ScrolledText = TBSrolledText except ImportError: from tkinter.scrolledtext import ScrolledText def create_actions_log_viewer( parent: Any, filter_var: Optional[tk.StringVar] = None ): """创建行为日志查看器组件。 参数: parent: 父容器 filter_var: 可选的筛选关键字变量(StringVar),如果提供则用于筛选日志 返回: (actions_log_text, refresh_actions_log_func) """ actions_log_text = ScrolledText(parent) actions_log_text.pack(fill="both", expand=True, padx=10, pady=10) try: actions_log_text.configure(font=("Consolas", 10)) except Exception: pass def refresh_actions_log() -> None: """刷新行为日志:读取 logs/actions.log 的尾部若干行,在 GUI 中展示关键行为日志(支持简单关键字筛选)。""" # 使用统一的路径配置 try: import path_config path = path_config.get_actions_log_path() except Exception: # 回退到旧路径(向后兼容) import path_config path = path_config.get_actions_log_path() try: if os.path.exists(path): # 仿照 web_gui 的实现:只读最后一小段,避免文件巨大 size = os.path.getsize(path) if size == 0: actions_log_text.delete("1.0", "end") actions_log_text.insert("1.0", "行为日志文件为空") return read_bytes = min(size, 65536) with open(path, "rb") as f: if read_bytes < size: f.seek(-read_bytes, os.SEEK_END) data = f.read() # 尝试多种编码方式 try: # 先尝试 utf-8-sig(带BOM) text = data.decode("utf-8-sig", errors="replace") except Exception: try: # 再尝试 utf-8 text = data.decode("utf-8", errors="replace") except Exception: # 最后尝试 gbk(Windows常见编码) text = data.decode("gbk", errors="replace") lines = text.splitlines() # 只取最近 300 行 tail = lines[-300:] # 关键字筛选:若提供了筛选变量且填写了筛选词,只保留包含该关键字的行 if filter_var is not None: kw = filter_var.get().strip() if kw: tail = [ln for ln in tail if kw in ln] # 保存当前滚动位置,避免刷新时自动置顶 try: scroll_position = actions_log_text.yview()[0] # 获取当前滚动位置(顶部可见位置的比例) except Exception: scroll_position = 0.0 actions_log_text.delete("1.0", "end") if tail: # 按行插入,并根据操作类型应用颜色标签(如果可用) for i, ln in enumerate(tail): try: # 记录开始索引 start_index = actions_log_text.index("end-1c") actions_log_text.insert("end", ln + ("\n" if i < len(tail) - 1 else "")) end_index = actions_log_text.index("end-1c") # 尝试识别操作类型并着色 if get_operation_type and apply_operation_color: try: op_type = get_operation_type(ln) # type: ignore[call-arg] # 使用默认主题(GUI 会在外部统一配置标签颜色),但 apply_operation_color 会确保标签存在 apply_operation_color(actions_log_text, start_index, end_index, op_type, theme="litera") # type: ignore[call-arg] except Exception: pass except Exception: # 回退:如果逐行插入失败,直接一次性插入整个文本 actions_log_text.delete("1.0", "end") actions_log_text.insert("1.0", "\n".join(tail)) break else: actions_log_text.insert("1.0", "暂无匹配的行为日志") # 恢复之前的滚动位置,避免自动置顶 try: actions_log_text.yview_moveto(scroll_position) except Exception: pass else: actions_log_text.delete("1.0", "end") actions_log_text.insert( "1.0", "暂无行为日志(logs/actions.log 不存在)" ) except Exception as e: actions_log_text.delete("1.0", "end") actions_log_text.insert( "1.0", f"读取行为日志失败: {str(e)}" ) return actions_log_text, refresh_actions_log