fix: can not filiter log level as expected

This commit is contained in:
Tunglies
2025-08-03 09:52:27 +08:00
parent 3eb2a5b3ef
commit d16c691c0f
10 changed files with 83 additions and 44 deletions

View File

@@ -30,6 +30,8 @@ import { initGlobalLogService } from "@/services/global-log-service";
import { invoke } from "@tauri-apps/api/core";
import { showNotice } from "@/services/noticeService";
import { NoticeManager } from "@/components/base/NoticeManager";
import { useLocalStorage } from "foxact/use-local-storage";
import { LogLevel } from "@/hooks/use-log-data";
const appWindow = getCurrentWebviewWindow();
export let portableFlag = false;
@@ -154,6 +156,7 @@ const Layout = () => {
const { verge } = useVerge();
const { clashInfo } = useClashInfo();
const [enableLog] = useEnableLog();
const [logLevel] = useLocalStorage<LogLevel>("log:log-level", "info");
const { language, start_page } = verge ?? {};
const navigate = useNavigate();
const location = useLocation();
@@ -184,9 +187,9 @@ const Layout = () => {
useEffect(() => {
if (clashInfo) {
const { server = "", secret = "" } = clashInfo;
initGlobalLogService(enableLog, "info");
initGlobalLogService(enableLog, logLevel);
}
}, [clashInfo, enableLog]);
}, [clashInfo, enableLog, logLevel]);
// 设置监听器
useEffect(() => {

View File

@@ -74,8 +74,8 @@ const LogPage = () => {
changeLogLevel(newLevel);
};
const handleToggleLog = () => {
toggleLogEnabled();
const handleToggleLog = async () => {
await toggleLogEnabled();
setEnableLog(!enableLog);
};

View File

@@ -420,6 +420,10 @@ export async function startLogsMonitoring(level?: string) {
return invoke<void>("start_logs_monitoring", { level });
}
export async function stopLogsMonitoring() {
return invoke<void>("stop_logs_monitoring");
}
export async function clearLogs() {
return invoke<void>("clear_logs");
}

View File

@@ -3,6 +3,7 @@ import { create } from "zustand";
import {
fetchLogsViaIPC,
startLogsStreaming,
stopLogsStreaming,
clearLogs as clearLogsIPC,
} from "@/services/ipc-log-service";
import dayjs from "dayjs";
@@ -123,12 +124,21 @@ const clearIpcPolling = () => {
}
};
// 关闭全局日志连接 (仅IPC模式)
export const closeGlobalLogConnection = () => {
// 停止日志监控 (仅IPC模式)
export const stopGlobalLogMonitoring = async () => {
clearIpcPolling();
isInitializing = false; // 重置初始化标志
// 调用后端停止监控
await stopLogsStreaming();
useGlobalLogStore.setState({ isConnected: false });
console.log("[GlobalLog-IPC] 日志服务已关闭");
console.log("[GlobalLog-IPC] 日志监控已停止");
};
// 关闭全局日志连接 (仅IPC模式) - 保持向后兼容
export const closeGlobalLogConnection = async () => {
await stopGlobalLogMonitoring();
};
// 切换日志级别 (仅IPC模式)
@@ -150,7 +160,7 @@ export const changeLogLevel = (level: LogLevel) => {
};
// 切换启用状态 (仅IPC模式)
export const toggleLogEnabled = () => {
export const toggleLogEnabled = async () => {
const { enabled, currentLevel } = useGlobalLogStore.getState();
const newEnabled = !enabled;
@@ -160,14 +170,14 @@ export const toggleLogEnabled = () => {
// IPC模式下直接启动
initGlobalLogService(newEnabled, currentLevel);
} else {
closeGlobalLogConnection();
await stopGlobalLogMonitoring();
}
};
// 获取日志清理函数
// 获取日志清理函数 - 只清理前端日志,不停止监控
export const clearGlobalLogs = () => {
useGlobalLogStore.getState().clearLogs();
// 同时清理后端流式缓存
// 同时清理后端缓存的日志,但不停止监控
clearLogsIPC();
};

View File

@@ -2,6 +2,7 @@
import {
getClashLogs,
startLogsMonitoring,
stopLogsMonitoring,
clearLogs as clearLogsCmd,
} from "@/services/cmds";
import dayjs from "dayjs";
@@ -28,6 +29,16 @@ export const startLogsStreaming = async (logLevel: LogLevel = "info") => {
}
};
// Stop logs monitoring
export const stopLogsStreaming = async () => {
try {
await stopLogsMonitoring();
console.log("[IPC-LogService] Stopped logs monitoring");
} catch (error) {
console.error("[IPC-LogService] Failed to stop logs monitoring:", error);
}
};
// Fetch logs using IPC command (now from streaming cache)
export const fetchLogsViaIPC = async (
logLevel: LogLevel = "info",