import "./styles.css"; import type { DebugInfo } from "../shared/ipc"; type TabId = "stream" | "file" | "debug"; declare global { interface Window { api: { info(): Promise; log( level: "debug" | "info" | "warn" | "error", msg: string, ): Promise<{ ok: boolean }>; clear(): Promise<{ ok: boolean }>; devtools(): Promise<{ ok: boolean }>; }; __setTab?: (tab: TabId) => void; __snapshot?: () => unknown; } } const state: { tab: TabId; info: DebugInfo | null; busy: boolean } = { tab: "stream", info: null, busy: false, }; const tabs: Array<{ id: TabId; label: string }> = [ { id: "stream", label: "日志流" }, { id: "file", label: "日志文件" }, { id: "debug", label: "调试开关" }, ]; function escapeHtml(value: string): string { return value .replaceAll("&", "&") .replaceAll("<", "<") .replaceAll(">", ">") .replaceAll('"', """); } function time(at: number): string { return new Date(at).toLocaleTimeString("zh-CN", { hour12: false }); } function kb(bytes: number): string { return `${(bytes / 1024).toFixed(1)} KB`; } function stream(info: DebugInfo): string { return `

JSON 行日志

最近 ${info.logs.length} 条;每行一个 JSON,含 at/level/evt。

${info.logs .map( (record) => ``, ) .join("")}
时间级别事件消息
${time(record.at)}${record.level}${escapeHtml(record.evt)}${escapeHtml(record.msg ?? "")}

页面日志也走主进程:window.api.log() → IPC → JSON 行。

`; } function file(info: DebugInfo): string { return `

日志文件

路径${escapeHtml(info.logFile)}
大小${kb(info.logSize)}
轮转上限${kb(info.logLimit)}

超过上限会轮转到 main.log.1;日志目录来自 app.getPath("logs")。

`; } function debug(info: DebugInfo): string { return `

调试开关

process.execArgv${escapeHtml(info.execArgv.join(" ") || "(空)")}
ELECTRON_ENABLE_LOGGING${info.enableLogging}
DevTools 已打开${info.devToolsOpen}
app.isPackaged${info.isPackaged}

主进程断点用 electron-forge start -- --inspect=9229;渲染进程 DevTools 由 webContents.openDevTools() 打开。

`; } function render(): void { const root = document.querySelector("#app"); if (!root) return; const info = state.info; const body = !info ? `

正在读取调试信息…

` : state.tab === "stream" ? stream(info) : state.tab === "file" ? file(info) : debug(info); root.innerHTML = `

开发、调试与日志

JSON 行日志、日志文件轮转、主进程/渲染进程调试入口

${body}
`; for (const button of root.querySelectorAll("[data-tab]")) { button.addEventListener("click", () => { const tab = button.dataset.tab; if (tab === "stream" || tab === "file" || tab === "debug") { state.tab = tab; render(); } }); } const refresh = async (action: () => Promise) => { if (state.busy) return; state.busy = true; try { await action(); state.info = await window.api.info(); } finally { state.busy = false; render(); } }; root.querySelector("#log-button")?.addEventListener("click", () => { void refresh(() => window.api.log("info", "hello from renderer")); }); root.querySelector("#clear-button")?.addEventListener("click", () => { void refresh(() => window.api.clear()); }); root.querySelector("#devtools-button")?.addEventListener("click", () => { void refresh(() => window.api.devtools()); }); } async function boot(): Promise { state.info = await window.api.info(); render(); window.__setTab = (tab) => { state.tab = tab; render(); }; window.__snapshot = () => ({ tab: state.tab, logs: state.info?.logs.length, logFile: state.info?.logFile, }); } void boot();