0.1.0
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
// file: kb_app_demo/frontend/ts/frontend_log.ts
|
||||
// version: 1
|
||||
|
||||
//! Frontend logging helpers that preserve explicit tracing targets.
|
||||
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
import type { FrontendLogPayload } from "./bindings/kb_app_demo/frontend_log/FrontendLogPayload";
|
||||
|
||||
type FrontendLogLevel = "trace" | "debug" | "info" | "warn" | "error";
|
||||
|
||||
type ConsoleMethod = (...items: unknown[]) => void;
|
||||
|
||||
const originalConsole = {
|
||||
trace: console.trace.bind(console),
|
||||
debug: console.debug.bind(console),
|
||||
log: console.log.bind(console),
|
||||
info: console.info.bind(console),
|
||||
warn: console.warn.bind(console),
|
||||
error: console.error.bind(console),
|
||||
};
|
||||
|
||||
function stringifyItem(item: unknown): string {
|
||||
if (item instanceof Error) {
|
||||
return item.stack ?? item.message;
|
||||
}
|
||||
if (typeof item === "string") {
|
||||
return item;
|
||||
}
|
||||
try {
|
||||
return JSON.stringify(item);
|
||||
} catch (caughtError) {
|
||||
return String(item);
|
||||
}
|
||||
}
|
||||
|
||||
function formatMessage(items: unknown[]): string {
|
||||
return items.map(item => stringifyItem(item)).join(" ");
|
||||
}
|
||||
|
||||
async function sendFrontendLog(level: FrontendLogLevel, target: string, message: string): Promise<void> {
|
||||
const payload: FrontendLogPayload = {
|
||||
level,
|
||||
target,
|
||||
message,
|
||||
};
|
||||
try {
|
||||
await invoke("emit_frontend_log", { payload });
|
||||
} catch (caughtError) {
|
||||
originalConsole.error("frontend tracing bridge failed", caughtError);
|
||||
}
|
||||
}
|
||||
|
||||
export function frontendTrace(target: string, ...items: unknown[]): void {
|
||||
const message = formatMessage(items);
|
||||
originalConsole.trace(message);
|
||||
void sendFrontendLog("trace", target, message);
|
||||
}
|
||||
|
||||
export function frontendDebug(target: string, ...items: unknown[]): void {
|
||||
const message = formatMessage(items);
|
||||
originalConsole.debug(message);
|
||||
void sendFrontendLog("debug", target, message);
|
||||
}
|
||||
|
||||
export function frontendInfo(target: string, ...items: unknown[]): void {
|
||||
const message = formatMessage(items);
|
||||
originalConsole.info(message);
|
||||
void sendFrontendLog("info", target, message);
|
||||
}
|
||||
|
||||
export function frontendWarn(target: string, ...items: unknown[]): void {
|
||||
const message = formatMessage(items);
|
||||
originalConsole.warn(message);
|
||||
void sendFrontendLog("warn", target, message);
|
||||
}
|
||||
|
||||
export function frontendError(target: string, ...items: unknown[]): void {
|
||||
const message = formatMessage(items);
|
||||
originalConsole.error(message);
|
||||
void sendFrontendLog("error", target, message);
|
||||
}
|
||||
|
||||
function buildConsoleBridge(level: FrontendLogLevel, target: string, original: ConsoleMethod): ConsoleMethod {
|
||||
return (...items: unknown[]) => {
|
||||
original(...items);
|
||||
void sendFrontendLog(level, target, formatMessage(items));
|
||||
};
|
||||
}
|
||||
|
||||
export function installFrontendConsoleBridge(target: string): void {
|
||||
console.trace = buildConsoleBridge("trace", target, originalConsole.trace);
|
||||
console.debug = buildConsoleBridge("debug", target, originalConsole.debug);
|
||||
console.log = buildConsoleBridge("info", target, originalConsole.log);
|
||||
console.info = buildConsoleBridge("info", target, originalConsole.info);
|
||||
console.warn = buildConsoleBridge("warn", target, originalConsole.warn);
|
||||
console.error = buildConsoleBridge("error", target, originalConsole.error);
|
||||
}
|
||||
Reference in New Issue
Block a user