diff --git a/backend/app_frontend_operation_log.go b/backend/app_frontend_operation_log.go new file mode 100644 index 00000000..31b0e567 --- /dev/null +++ b/backend/app_frontend_operation_log.go @@ -0,0 +1,21 @@ +package backend + +import "ant-chrome/backend/internal/logger" + +// FrontendOperationLog records frontend-triggered Wails operation results into the app log. +func (a *App) FrontendOperationLog(level string, method string, success bool, durationMs int64, message string) { + log := logger.New("Frontend") + fields := []logger.Field{ + logger.F("method", method), + logger.F("success", success), + logger.F("duration_ms", durationMs), + } + if message != "" { + fields = append(fields, logger.F("message", message)) + } + if !success || level == "error" || level == "ERROR" { + log.Error("前端操作失败", fields...) + return + } + log.Info("前端操作完成", fields...) +} diff --git a/backend/internal/logger/memory_writer.go b/backend/internal/logger/memory_writer.go index 4081c47a..4a9d2872 100644 --- a/backend/internal/logger/memory_writer.go +++ b/backend/internal/logger/memory_writer.go @@ -1,10 +1,11 @@ package logger import ( + "strings" "sync" ) -const defaultMemoryBufferSize = 500 +const defaultMemoryBufferSize = 2000 // MemoryLogEntry 内存日志条目(供前端消费) type MemoryLogEntry struct { @@ -40,6 +41,9 @@ func (w *MemoryWriter) Write(entry *LogEntry) error { if entry == nil { return nil } + if shouldSkipMemoryLog(entry) { + return nil + } w.mu.Lock() defer w.mu.Unlock() @@ -57,6 +61,19 @@ func (w *MemoryWriter) Write(entry *LogEntry) error { return nil } +func shouldSkipMemoryLog(entry *LogEntry) bool { + if entry.Level != INFO || entry.Component != "Xray" { + return false + } + message := strings.TrimSpace(entry.Message) + switch message { + case "xray 内核进程已启动", "回收空闲桥接进程", "复用 xray 桥接进程", "复用已就绪 xray 桥接进程": + return true + default: + return false + } +} + func (w *MemoryWriter) Close() error { return nil } // GetEntries 返回所有缓冲日志(最新在后) diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 5dc41216..445d8357 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -8,6 +8,7 @@ import { AppRoutes } from "./routes/AppRoutes"; import { lazyNamed } from "./routes/lazyNamed"; import { useNotificationStore } from "./store/notificationStore"; import { useBackupStore } from "./store/backupStore"; +import { installWailsOperationLogger } from "./utils/wailsOperationLogger"; import { ForceQuit as ForceQuitApp, QuitAppOnly as QuitAppOnlyApp, @@ -73,6 +74,45 @@ function useWailsNotifications() { }, [addNotification]); } +function useGlobalErrorNotifications() { + const addNotification = useNotificationStore((s) => s.addNotification); + + useEffect(() => { + const toMessage = (value: unknown) => { + if (value instanceof Error) return value.message || String(value); + if (typeof value === "string") return value; + try { + return JSON.stringify(value); + } catch { + return String(value); + } + }; + + const handleError = (event: ErrorEvent) => { + addNotification({ + type: "error", + title: "前端异常", + message: event.message || toMessage(event.error) || "未知脚本错误", + }); + }; + + const handleUnhandledRejection = (event: PromiseRejectionEvent) => { + addNotification({ + type: "error", + title: "未处理异步异常", + message: toMessage(event.reason) || "未知 Promise 异常", + }); + }; + + window.addEventListener("error", handleError); + window.addEventListener("unhandledrejection", handleUnhandledRejection); + return () => { + window.removeEventListener("error", handleError); + window.removeEventListener("unhandledrejection", handleUnhandledRejection); + }; + }, [addNotification]); +} + function CloseConfirmModal() { const [open, setOpen] = useState(false); const [platform, setPlatform] = useState("windows"); @@ -246,7 +286,11 @@ function CloseConfirmModal() { } function App() { + useEffect(() => { + installWailsOperationLogger(); + }, []); useWailsNotifications(); + useGlobalErrorNotifications(); const [quickLaunchOpen, setQuickLaunchOpen] = useState(false); const routeFallback = (
diff --git a/frontend/src/modules/browser/components/AutomationScriptHistoryModal.tsx b/frontend/src/modules/browser/components/AutomationScriptHistoryModal.tsx index d563e6a3..9ba46e61 100644 --- a/frontend/src/modules/browser/components/AutomationScriptHistoryModal.tsx +++ b/frontend/src/modules/browser/components/AutomationScriptHistoryModal.tsx @@ -223,15 +223,7 @@ export function AutomationScriptHistoryModal({ } >
-
-
-
- 查看所有脚本最近的调用情况 -
-
- 表格里只保留关键信息,点击某一行可展开查看错误、返回内容和完整摘要。 -
-
+