Files
douyi_guaji_main/gui/components/actions_log_viewer.py
T
Your Name bdf9312bc9 chore: Remove obsolete scripts and enhance path configuration
- Deleted `check_md_files.py` and `fix_cursor_md_issue.py` as they are no longer needed.
- Added new functions in `path_config.py` for better path management, including actions log and run_cli paths.
- Updated various modules to utilize the new path configuration, ensuring consistency and maintainability across the project.
2025-12-28 20:55:34 +08:00

118 lines
4.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""行为日志查看组件
包含行为日志显示、搜索等功能的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
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:
actions_log_text.insert("1.0", "\n".join(tail))
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