mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 08:45:41 +08:00
feat: migrate logs API from REST to IPC streaming (#4277)
* feat: migrate logs API from REST to IPC streaming - Replace REST API `/logs` calls with IPC streaming implementation - Add new `src-tauri/src/ipc/logs.rs` with `LogsMonitor` for real-time log streaming - Implement duplicate stream prevention with level tracking - Add frontend-backend communication via Tauri commands for log management - Remove WebSocket compatibility, maintain IPC-only mode - Fix duplicate monitoring task startup when toggling log service - Add proper task lifecycle management with JoinHandle cleanup * refactor: remove dead code from logs.rs to fix clippy warnings - Remove unused `timestamp` field from LogItem struct - Remove unused `client` field from LogsMonitor struct - Remove unused methods: `is_fresh`, `get_current_monitoring_level`, `get_current_logs` - Simplify LogsMonitor initialization by removing client dependency - All clippy warnings with -D warnings now resolved * refactor: extract duplicate fmt_bytes function to utils module - Create new utils/format.rs module with fmt_bytes function - Remove duplicate fmt_bytes implementations from traffic.rs and memory.rs - Update imports to use shared utils::format::fmt_bytes - Add comprehensive unit tests for fmt_bytes function - Ensure DRY principle compliance and code maintainability
This commit is contained in:
63
src/services/ipc-log-service.ts
Normal file
63
src/services/ipc-log-service.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
// IPC-based log service using Tauri commands with streaming support
|
||||
import {
|
||||
getClashLogs,
|
||||
startLogsMonitoring,
|
||||
clearLogs as clearLogsCmd,
|
||||
} from "@/services/cmds";
|
||||
import dayjs from "dayjs";
|
||||
|
||||
export type LogLevel = "warning" | "info" | "debug" | "error" | "all";
|
||||
|
||||
export interface ILogItem {
|
||||
time?: string;
|
||||
type: string;
|
||||
payload: string;
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
// Start logs monitoring with specified level
|
||||
export const startLogsStreaming = async (logLevel: LogLevel = "info") => {
|
||||
try {
|
||||
const level = logLevel === "all" ? undefined : logLevel;
|
||||
await startLogsMonitoring(level);
|
||||
console.log(
|
||||
`[IPC-LogService] Started logs monitoring with level: ${logLevel}`,
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("[IPC-LogService] Failed to start logs monitoring:", error);
|
||||
}
|
||||
};
|
||||
|
||||
// Fetch logs using IPC command (now from streaming cache)
|
||||
export const fetchLogsViaIPC = async (
|
||||
logLevel: LogLevel = "info",
|
||||
): Promise<ILogItem[]> => {
|
||||
try {
|
||||
const level = logLevel === "all" ? undefined : logLevel;
|
||||
const response = await getClashLogs(level);
|
||||
|
||||
// The response should be in the format expected by the frontend
|
||||
// Transform the logs to match the expected format
|
||||
if (Array.isArray(response)) {
|
||||
return response.map((log: any) => ({
|
||||
...log,
|
||||
time: log.time || dayjs().format("HH:mm:ss"),
|
||||
}));
|
||||
}
|
||||
|
||||
return [];
|
||||
} catch (error) {
|
||||
console.error("[IPC-LogService] Failed to fetch logs:", error);
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
// Clear logs
|
||||
export const clearLogs = async () => {
|
||||
try {
|
||||
await clearLogsCmd();
|
||||
console.log("[IPC-LogService] Logs cleared");
|
||||
} catch (error) {
|
||||
console.error("[IPC-LogService] Failed to clear logs:", error);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user