diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.yml
index a4245d0a0..ce2623260 100644
--- a/.github/ISSUE_TEMPLATE/bug_report.yml
+++ b/.github/ISSUE_TEMPLATE/bug_report.yml
@@ -12,6 +12,23 @@ body:
- label: "我确认在 Issues 列表中并无其他人已经提出过与此问题相同或相似的问题"
required: true
- label: "我使用了 Docker"
+- type: dropdown
+ attributes:
+ label: "使用的分支"
+ description: "请选择您正在使用的版本分支"
+ options:
+ - main
+ - main-fix
+ - refactor
+ validations:
+ required: true
+- type: input
+ attributes:
+ label: "具体版本号"
+ description: "请输入您使用的具体版本号"
+ placeholder: "例如:0.5.11、0.5.8"
+ validations:
+ required: true
- type: textarea
attributes:
label: 遇到的问题
diff --git a/.gitignore b/.gitignore
index 2dd6de62e..b7920bdc0 100644
--- a/.gitignore
+++ b/.gitignore
@@ -16,6 +16,7 @@ memory_graph.gml
.env.*
config/bot_config_dev.toml
config/bot_config.toml
+config/bot_config.toml.bak
src/plugins/remote/client_uuid.json
# Byte-compiled / optimized / DLL files
__pycache__/
@@ -25,7 +26,7 @@ llm_statistics.txt
mongodb
napcat
run_dev.bat
-
+elua.confirmed
# C extensions
*.so
@@ -205,3 +206,8 @@ jieba.cache
.idea
*.iml
*.ipr
+
+# PyEnv
+# If using PyEnv and configured to use a specific Python version locally
+# a .local-version file will be created in the root of the project to specify the version.
+.python-version
diff --git a/EULA.md b/EULA.md
new file mode 100644
index 000000000..c878ff81d
--- /dev/null
+++ b/EULA.md
@@ -0,0 +1,69 @@
+
+---
+# **MaimBot用户协议**
+**生效日期:** 2025.3.14
+
+---
+
+### **特别声明**
+1. **MaimBot为遵循GPLv3协议的开源项目**
+ - 代码托管于GitHub,**开发者不持有任何法律实体**,项目由社区共同维护;
+ - 用户可自由使用、修改、分发代码,但**必须遵守GPLv3许可证要求**(详见项目仓库)。
+
+2. **无责任声明**
+ - 本项目**不提供任何形式的担保**,开发者及贡献者均不对使用后果负责;
+ - 所有功能依赖第三方API,**生成内容不受我方控制**。
+
+---
+
+### **一、基础说明**
+1. **MaimBot是什么**
+ - MaimBot是基于第三方AI技术(如ChatGPT等)的自动回复机器人,**所有输出内容均由AI自动生成,不代表我方观点**。
+ - 用户可提交自定义指令(Prompt),经我方内容过滤后调用第三方API生成结果,**输出可能存在错误、偏见或不适宜内容**。
+
+---
+
+### **二、用户责任**
+1. **禁止内容**
+ 您承诺**不提交或生成以下内容**,否则我方有权永久封禁账号:
+ - 违法、暴力、色情、歧视性内容;
+ - 诈骗、谣言、恶意代码等危害他人或社会的内容;
+ - 侵犯他人隐私、肖像权、知识产权的内容。
+
+2. **后果自负**
+ - 您需对**输入的指令(Prompt)和生成内容的使用负全责**;
+ - **禁止将结果用于医疗、法律、投资等专业领域**,否则风险自行承担。
+
+---
+
+### **三、我们不负责什么**
+1. **技术问题**
+ - 因第三方API故障、网络延迟、内容过滤误判导致的服务异常;
+ - AI生成内容的不准确、冒犯性、时效性错误。
+
+2. **用户行为**
+ - 因您违反本协议或滥用MaimBot导致的任何纠纷、损失;
+ - 他人通过您的账号生成的违规内容。
+
+---
+
+### **四、其他重要条款**
+1. **隐私与数据**
+ - 您提交的指令和生成内容可能被匿名化后用于优化服务,**敏感信息请勿输入**;
+ - **我方会收集部分统计信息(如使用频率、基础指令类型)以改进服务,您可在[bot_config.toml]随时关闭此功能**。
+
+2. **精神健康风险**
+ ⚠️ **MaimBot仅为工具型机器人,不具备情感交互能力。建议用户:**
+ - 避免过度依赖AI回复处理现实问题或情绪困扰;
+ - 如感到心理不适,请及时寻求专业心理咨询服务。
+ - 如遇心理困扰,请寻求专业帮助(全国心理援助热线:12355)。
+
+3. **封禁权利**
+ - 我方有权不经通知**删除违规内容、暂停或终止您的访问权限**。
+
+4. **争议解决**
+ - 本协议适用中国法律,争议提交相关地区法院管辖;
+ - 若因GPLv3许可产生纠纷,以许可证官方解释为准。
+
+
+---
diff --git a/README.md b/README.md
index 183d59fee..5de6f5dfd 100644
--- a/README.md
+++ b/README.md
@@ -21,8 +21,6 @@
> [!WARNING]
> 注意,3月12日的v0.5.13, 该版本更新较大,建议单独开文件夹部署,然后转移/data文件 和数据库,数据库可能需要删除messages下的内容(不需要删除记忆)
-
-
@@ -45,17 +43,14 @@
- [三群](https://qm.qq.com/q/wlH5eT8OmQ) 1035228475(开发和建议相关讨论)不一定有空回复,会优先写文档和代码
- [四群](https://qm.qq.com/q/wlH5eT8OmQ) 729957033(开发和建议相关讨论)不一定有空回复,会优先写文档和代码
-
-
**📚 有热心网友创作的wiki:** https://maimbot.pages.dev/
+**📚 由SLAPQ制作的B站教程:** https://www.bilibili.com/opus/1041609335464001545
**😊 其他平台版本**
- (由 [CabLate](https://github.com/cablate) 贡献) [Telegram 与其他平台(未来可能会有)的版本](https://github.com/cablate/MaiMBot/tree/telegram) - [集中讨论串](https://github.com/SengokuCola/MaiMBot/discussions/149)
-
-
## 📝 注意注意注意注意注意注意注意注意注意注意注意注意注意注意注意注意注意
**如果你有想法想要提交pr**
- 由于本项目在快速迭代和功能调整,并且有重构计划,目前不接受任何未经过核心开发组讨论的pr合并,谢谢!如您仍旧希望提交pr,可以详情请看置顶issue
@@ -78,8 +73,6 @@
- [🐳 Docker部署指南](docs/docker_deploy.md)
-
-
### 配置说明
- [🎀 新手配置指南](docs/installation_cute.md) - 通俗易懂的配置教程,适合初次使用的猫娘
diff --git a/bot.py b/bot.py
index 7a97f485e..bf853bc0c 100644
--- a/bot.py
+++ b/bot.py
@@ -2,6 +2,7 @@ import asyncio
import os
import shutil
import sys
+from pathlib import Path
import nonebot
import time
@@ -10,10 +11,11 @@ import uvicorn
from dotenv import load_dotenv
from nonebot.adapters.onebot.v11 import Adapter
import platform
-from src.plugins.utils.logger_config import LogModule, LogClassification
+from src.common.logger import get_module_logger
-# 配置日志格式
+# 配置主程序日志格式
+logger = get_module_logger("main_bot")
# 获取没有加载env时的环境变量
env_mask = {key: os.getenv(key) for key in os.environ}
@@ -76,11 +78,11 @@ def init_env():
def load_env():
# 使用闭包实现对加载器的横向扩展,避免大量重复判断
def prod():
- logger.success("加载生产环境变量配置")
+ logger.success("成功加载生产环境变量配置")
load_dotenv(".env.prod", override=True) # override=True 允许覆盖已存在的环境变量
def dev():
- logger.success("加载开发环境变量配置")
+ logger.success("成功加载开发环境变量配置")
load_dotenv(".env.dev", override=True) # override=True 允许覆盖已存在的环境变量
fn_map = {"prod": prod, "dev": dev}
@@ -100,11 +102,6 @@ def load_env():
RuntimeError(f"ENVIRONMENT 配置错误,请检查 .env 文件中的 ENVIRONMENT 变量及对应 .env.{env} 是否存在")
-def load_logger():
- global logger # 使得bot.py中其他函数也能调用
- log_module = LogModule()
- logger = log_module.setup_logger(LogClassification.BASE)
-
def scan_provider(env_config: dict):
provider = {}
@@ -168,13 +165,35 @@ async def uvicorn_main():
uvicorn_server = server
await server.serve()
+def check_eula():
+ eula_file = Path("elua.confirmed")
+
+ # 如果已经确认过EULA,直接返回
+ if eula_file.exists():
+ return
+
+ print("使用MaiMBot前请先阅读ELUA协议,继续运行视为同意协议")
+ print("协议内容:https://github.com/SengokuCola/MaiMBot/blob/main/EULA.md")
+ print('输入"同意"或"confirmed"继续运行')
+
+ while True:
+ user_input = input().strip().lower() # 转换为小写以忽略大小写
+ if user_input in ['同意', 'confirmed']:
+ # 创建确认文件
+ eula_file.touch()
+ break
+ else:
+ print('请输入"同意"或"confirmed"以继续运行')
+
def raw_main():
# 利用 TZ 环境变量设定程序工作的时区
# 仅保证行为一致,不依赖 localtime(),实际对生产环境几乎没有作用
if platform.system().lower() != "windows":
time.tzset()
-
+
+ check_eula()
+
easter_egg()
init_config()
init_env()
@@ -206,8 +225,6 @@ def raw_main():
if __name__ == "__main__":
try:
- # 配置日志,使得主程序直接退出时候也能访问logger
- load_logger()
raw_main()
app = nonebot.get_asgi()
diff --git a/config/auto_update.py b/config/auto_update.py
index 28ab108da..d87b7c129 100644
--- a/config/auto_update.py
+++ b/config/auto_update.py
@@ -42,8 +42,16 @@ def update_config():
update_dict(target[key], value)
else:
try:
- # 直接使用tomlkit的item方法创建新值
- target[key] = tomlkit.item(value)
+ # 对数组类型进行特殊处理
+ if isinstance(value, list):
+ # 如果是空数组,确保它保持为空数组
+ if not value:
+ target[key] = tomlkit.array()
+ else:
+ target[key] = tomlkit.array(value)
+ else:
+ # 其他类型使用item方法创建新值
+ target[key] = tomlkit.item(value)
except (TypeError, ValueError):
# 如果转换失败,直接赋值
target[key] = value
diff --git a/src/common/logger.py b/src/common/logger.py
new file mode 100644
index 000000000..c546b700b
--- /dev/null
+++ b/src/common/logger.py
@@ -0,0 +1,198 @@
+from loguru import logger
+from typing import Dict, Optional, Union, List
+import sys
+import os
+from types import ModuleType
+from pathlib import Path
+from dotenv import load_dotenv
+
+load_dotenv()
+
+# 保存原生处理器ID
+default_handler_id = None
+for handler_id in logger._core.handlers:
+ default_handler_id = handler_id
+ break
+
+# 移除默认处理器
+if default_handler_id is not None:
+ logger.remove(default_handler_id)
+
+# 类型别名
+LoguruLogger = logger.__class__
+
+# 全局注册表:记录模块与处理器ID的映射
+_handler_registry: Dict[str, List[int]] = {}
+
+# 获取日志存储根地址
+current_file_path = Path(__file__).resolve()
+LOG_ROOT = "logs"
+
+# 默认全局配置
+DEFAULT_CONFIG = {
+ # 日志级别配置
+ "console_level": "INFO",
+ "file_level": "DEBUG",
+
+ # 格式配置
+ "console_format": (
+ "{time:YYYY-MM-DD HH:mm:ss} | "
+ "{level: <8} | "
+ "{extra[module]: <12} | "
+ "{message}"
+ ),
+ "file_format": (
+ "{time:YYYY-MM-DD HH:mm:ss} | "
+ "{level: <8} | "
+ "{extra[module]: <15} | "
+ "{message}"
+ ),
+ "log_dir": LOG_ROOT,
+ "rotation": "00:00",
+ "retention": "3 days",
+ "compression": "zip",
+}
+
+
+def is_registered_module(record: dict) -> bool:
+ """检查是否为已注册的模块"""
+ return record["extra"].get("module") in _handler_registry
+
+
+def is_unregistered_module(record: dict) -> bool:
+ """检查是否为未注册的模块"""
+ return not is_registered_module(record)
+
+
+def log_patcher(record: dict) -> None:
+ """自动填充未设置模块名的日志记录,保留原生模块名称"""
+ if "module" not in record["extra"]:
+ # 尝试从name中提取模块名
+ module_name = record.get("name", "")
+ if module_name == "":
+ module_name = "root"
+ record["extra"]["module"] = module_name
+
+
+# 应用全局修补器
+logger.configure(patcher=log_patcher)
+
+
+class LogConfig:
+ """日志配置类"""
+
+ def __init__(self, **kwargs):
+ self.config = DEFAULT_CONFIG.copy()
+ self.config.update(kwargs)
+
+ def to_dict(self) -> dict:
+ return self.config.copy()
+
+ def update(self, **kwargs):
+ self.config.update(kwargs)
+
+
+def get_module_logger(
+ module: Union[str, ModuleType],
+ *,
+ console_level: Optional[str] = None,
+ file_level: Optional[str] = None,
+ extra_handlers: Optional[List[dict]] = None,
+ config: Optional[LogConfig] = None
+) -> LoguruLogger:
+ module_name = module if isinstance(module, str) else module.__name__
+ current_config = config.config if config else DEFAULT_CONFIG
+
+ # 清理旧处理器
+ if module_name in _handler_registry:
+ for handler_id in _handler_registry[module_name]:
+ logger.remove(handler_id)
+ del _handler_registry[module_name]
+
+ handler_ids = []
+
+ # 控制台处理器
+ console_id = logger.add(
+ sink=sys.stderr,
+ level=os.getenv("CONSOLE_LOG_LEVEL", console_level or current_config["console_level"]),
+ format=current_config["console_format"],
+ filter=lambda record: record["extra"].get("module") == module_name,
+ enqueue=True,
+ )
+ handler_ids.append(console_id)
+
+ # 文件处理器
+ log_dir = Path(current_config["log_dir"])
+ log_dir.mkdir(parents=True, exist_ok=True)
+ log_file = log_dir / module_name / f"{{time:YYYY-MM-DD}}.log"
+ log_file.parent.mkdir(parents=True, exist_ok=True)
+
+ file_id = logger.add(
+ sink=str(log_file),
+ level=os.getenv("FILE_LOG_LEVEL", file_level or current_config["file_level"]),
+ format=current_config["file_format"],
+ rotation=current_config["rotation"],
+ retention=current_config["retention"],
+ compression=current_config["compression"],
+ encoding="utf-8",
+ filter=lambda record: record["extra"].get("module") == module_name,
+ enqueue=True,
+ )
+ handler_ids.append(file_id)
+
+ # 额外处理器
+ if extra_handlers:
+ for handler in extra_handlers:
+ handler_id = logger.add(**handler)
+ handler_ids.append(handler_id)
+
+ # 更新注册表
+ _handler_registry[module_name] = handler_ids
+
+ return logger.bind(module=module_name)
+
+
+def remove_module_logger(module_name: str) -> None:
+ """清理指定模块的日志处理器"""
+ if module_name in _handler_registry:
+ for handler_id in _handler_registry[module_name]:
+ logger.remove(handler_id)
+ del _handler_registry[module_name]
+
+
+# 添加全局默认处理器(只处理未注册模块的日志--->控制台)
+DEFAULT_GLOBAL_HANDLER = logger.add(
+ sink=sys.stderr,
+ level=os.getenv("DEFAULT_CONSOLE_LOG_LEVEL", "SUCCESS"),
+ format=(
+ "{time:YYYY-MM-DD HH:mm:ss} | "
+ "{level: <8} | "
+ "{name: <12} | "
+ "{message}"
+ ),
+ filter=is_unregistered_module, # 只处理未注册模块的日志
+ enqueue=True,
+)
+
+# 添加全局默认文件处理器(只处理未注册模块的日志--->logs文件夹)
+log_dir = Path(DEFAULT_CONFIG["log_dir"])
+log_dir.mkdir(parents=True, exist_ok=True)
+other_log_dir = log_dir / "other"
+other_log_dir.mkdir(parents=True, exist_ok=True)
+
+DEFAULT_FILE_HANDLER = logger.add(
+ sink=str(other_log_dir / f"{{time:YYYY-MM-DD}}.log"),
+ level=os.getenv("DEFAULT_FILE_LOG_LEVEL", "DEBUG"),
+ format=(
+ "{time:YYYY-MM-DD HH:mm:ss} | "
+ "{level: <8} | "
+ "{name: <15} | "
+ "{message}"
+ ),
+ rotation=DEFAULT_CONFIG["rotation"],
+ retention=DEFAULT_CONFIG["retention"],
+ compression=DEFAULT_CONFIG["compression"],
+ encoding="utf-8",
+ filter=is_unregistered_module, # 只处理未注册模块的日志
+ enqueue=True,
+)
diff --git a/src/gui/logger_gui.py b/src/gui/logger_gui.py
new file mode 100644
index 000000000..f2dd698cd
--- /dev/null
+++ b/src/gui/logger_gui.py
@@ -0,0 +1,347 @@
+import customtkinter as ctk
+import subprocess
+import threading
+import queue
+import re
+import os
+import signal
+from collections import deque
+
+# 设置应用的外观模式和默认颜色主题
+ctk.set_appearance_mode("dark")
+ctk.set_default_color_theme("blue")
+
+
+class LogViewerApp(ctk.CTk):
+ """日志查看器应用的主类,继承自customtkinter的CTk类"""
+
+ def __init__(self):
+ """初始化日志查看器应用的界面和状态"""
+ super().__init__()
+ self.title("日志查看器")
+ self.geometry("1200x800")
+
+ # 初始化进程、日志队列、日志数据等变量
+ self.process = None
+ self.log_queue = queue.Queue()
+ self.log_data = deque(maxlen=10000) # 使用固定长度队列
+ self.available_levels = set()
+ self.available_modules = set()
+ self.sorted_modules = []
+ self.module_checkboxes = {} # 存储模块复选框的字典
+
+ # 日志颜色配置
+ self.color_config = {
+ "time": "#888888",
+ "DEBUG": "#2196F3",
+ "INFO": "#4CAF50",
+ "WARNING": "#FF9800",
+ "ERROR": "#F44336",
+ "module": "#D4D0AB",
+ "default": "#FFFFFF",
+ }
+
+ # 列可见性配置
+ self.column_visibility = {"show_time": True, "show_level": True, "show_module": True}
+
+ # 选中的日志等级和模块
+ self.selected_levels = set()
+ self.selected_modules = set()
+
+ # 创建界面组件并启动日志队列处理
+ self.create_widgets()
+ self.after(100, self.process_log_queue)
+
+ def create_widgets(self):
+ """创建应用界面的各个组件"""
+ self.grid_columnconfigure(0, weight=1)
+ self.grid_rowconfigure(1, weight=1)
+
+ # 控制面板
+ control_frame = ctk.CTkFrame(self)
+ control_frame.grid(row=0, column=0, sticky="ew", padx=10, pady=5)
+
+ self.start_btn = ctk.CTkButton(control_frame, text="启动", command=self.start_process)
+ self.start_btn.pack(side="left", padx=5)
+
+ self.stop_btn = ctk.CTkButton(control_frame, text="停止", command=self.stop_process, state="disabled")
+ self.stop_btn.pack(side="left", padx=5)
+
+ self.clear_btn = ctk.CTkButton(control_frame, text="清屏", command=self.clear_logs)
+ self.clear_btn.pack(side="left", padx=5)
+
+ column_filter_frame = ctk.CTkFrame(control_frame)
+ column_filter_frame.pack(side="left", padx=20)
+
+ self.time_check = ctk.CTkCheckBox(column_filter_frame, text="显示时间", command=self.refresh_logs)
+ self.time_check.pack(side="left", padx=5)
+ self.time_check.select()
+
+ self.level_check = ctk.CTkCheckBox(column_filter_frame, text="显示等级", command=self.refresh_logs)
+ self.level_check.pack(side="left", padx=5)
+ self.level_check.select()
+
+ self.module_check = ctk.CTkCheckBox(column_filter_frame, text="显示模块", command=self.refresh_logs)
+ self.module_check.pack(side="left", padx=5)
+ self.module_check.select()
+
+ # 筛选面板
+ filter_frame = ctk.CTkFrame(self)
+ filter_frame.grid(row=0, column=1, rowspan=2, sticky="ns", padx=5)
+
+ ctk.CTkLabel(filter_frame, text="日志等级筛选").pack(pady=5)
+ self.level_scroll = ctk.CTkScrollableFrame(filter_frame, width=150, height=200)
+ self.level_scroll.pack(fill="both", expand=True, padx=5)
+
+ ctk.CTkLabel(filter_frame, text="模块筛选").pack(pady=5)
+ self.module_filter_entry = ctk.CTkEntry(filter_frame, placeholder_text="输入模块过滤词")
+ self.module_filter_entry.pack(pady=5)
+ self.module_filter_entry.bind("", self.update_module_filter)
+
+ self.module_scroll = ctk.CTkScrollableFrame(filter_frame, width=300, height=200)
+ self.module_scroll.pack(fill="both", expand=True, padx=5)
+
+ self.log_text = ctk.CTkTextbox(self, wrap="word")
+ self.log_text.grid(row=1, column=0, sticky="nsew", padx=10, pady=5)
+
+ self.init_text_tags()
+
+ def update_module_filter(self, event):
+ """根据模块过滤词更新模块复选框的显示"""
+ filter_text = self.module_filter_entry.get().strip().lower()
+ for module, checkbox in self.module_checkboxes.items():
+ if filter_text in module.lower():
+ checkbox.pack(anchor="w", padx=5, pady=2)
+ else:
+ checkbox.pack_forget()
+
+ def update_filters(self, level, module):
+ """更新日志等级和模块的筛选器"""
+ if level not in self.available_levels:
+ self.available_levels.add(level)
+ self.add_checkbox(self.level_scroll, level, "level")
+
+ module_key = self.get_module_key(module)
+ if module_key not in self.available_modules:
+ self.available_modules.add(module_key)
+ self.sorted_modules = sorted(self.available_modules, key=lambda x: x.lower())
+ self.rebuild_module_checkboxes()
+
+ def rebuild_module_checkboxes(self):
+ """重新构建模块复选框"""
+ # 清空现有复选框
+ for widget in self.module_scroll.winfo_children():
+ widget.destroy()
+ self.module_checkboxes.clear()
+
+ # 重建排序后的复选框
+ for module in self.sorted_modules:
+ self.add_checkbox(self.module_scroll, module, "module")
+
+ def add_checkbox(self, parent, text, type_):
+ """在指定父组件中添加复选框"""
+
+ def update_filter():
+ current = cb.get()
+ if type_ == "level":
+ (self.selected_levels.add if current else self.selected_levels.discard)(text)
+ else:
+ (self.selected_modules.add if current else self.selected_modules.discard)(text)
+ self.refresh_logs()
+
+ cb = ctk.CTkCheckBox(parent, text=text, command=update_filter)
+ cb.select() # 初始选中
+
+ # 手动同步初始状态到集合(关键修复)
+ if type_ == "level":
+ self.selected_levels.add(text)
+ else:
+ self.selected_modules.add(text)
+
+ if type_ == "module":
+ self.module_checkboxes[text] = cb
+ cb.pack(anchor="w", padx=5, pady=2)
+ return cb
+
+ def check_filter(self, entry):
+ """检查日志条目是否符合当前筛选条件"""
+ level_ok = not self.selected_levels or entry["level"] in self.selected_levels
+ module_key = self.get_module_key(entry["module"])
+ module_ok = not self.selected_modules or module_key in self.selected_modules
+ return level_ok and module_ok
+
+ def init_text_tags(self):
+ """初始化日志文本的颜色标签"""
+ for tag, color in self.color_config.items():
+ self.log_text.tag_config(tag, foreground=color)
+ self.log_text.tag_config("default", foreground=self.color_config["default"])
+
+ def start_process(self):
+ """启动日志进程并开始读取输出"""
+ self.process = subprocess.Popen(
+ ["nb", "run"],
+ stdout=subprocess.PIPE,
+ stderr=subprocess.STDOUT,
+ text=True,
+ bufsize=1,
+ encoding="utf-8",
+ errors="ignore",
+ )
+ self.start_btn.configure(state="disabled")
+ self.stop_btn.configure(state="normal")
+ threading.Thread(target=self.read_output, daemon=True).start()
+
+ def stop_process(self):
+ """停止日志进程并清理相关资源"""
+ if self.process:
+ try:
+ if hasattr(self.process, "pid"):
+ if os.name == "nt":
+ subprocess.run(
+ ["taskkill", "/F", "/T", "/PID", str(self.process.pid)], check=True, capture_output=True
+ )
+ else:
+ os.killpg(os.getpgid(self.process.pid), signal.SIGTERM)
+ except (subprocess.CalledProcessError, ProcessLookupError, OSError) as e:
+ print(f"终止进程失败: {e}")
+ finally:
+ self.process = None
+ self.log_queue.queue.clear()
+ self.start_btn.configure(state="normal")
+ self.stop_btn.configure(state="disabled")
+ self.refresh_logs()
+
+ def read_output(self):
+ """读取日志进程的输出并放入队列"""
+ try:
+ while self.process and self.process.poll() is None:
+ line = self.process.stdout.readline()
+ if line:
+ self.log_queue.put(line)
+ else:
+ break # 避免空循环
+ self.process.stdout.close() # 确保关闭文件描述符
+ except ValueError: # 处理可能的I/O操作异常
+ pass
+
+ def process_log_queue(self):
+ """处理日志队列中的日志条目"""
+ while not self.log_queue.empty():
+ line = self.log_queue.get()
+ self.process_log_line(line)
+ self.after(100, self.process_log_queue)
+
+ def process_log_line(self, line):
+ """解析单行日志并更新日志数据和筛选器"""
+ match = re.match(
+ r"""^
+ (?:(?P