File size: 1,279 Bytes
3c124f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Mirror the overlay's console to the daemon (/api/debug/log → /tmp/puck-frontend.log)
// because the WKWebView inspector is unusable behind a fullscreen always-on-top
// transparent window. Tauri-only so the browser sim isn't noisy.

import { inTauri } from "./tauri";

const stringify = (a: unknown): string => {
  if (typeof a === "string") return a;
  try {
    return JSON.stringify(a);
  } catch {
    return String(a);
  }
};

export function installDebugLog(): void {
  if (!inTauri()) return;
  const send = (level: string, args: unknown[]) => {
    void fetch("/api/debug/log", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ level, msg: args.map(stringify).join(" ") }),
    }).catch(() => {});
  };
  for (const level of ["log", "warn", "error"] as const) {
    const orig = console[level].bind(console);
    console[level] = (...args: unknown[]) => {
      orig(...args);
      send(level, args);
    };
  }
  window.addEventListener("error", (e) =>
    send("error", [`onerror: ${e.message} @ ${e.filename}:${e.lineno}`]),
  );
  window.addEventListener("unhandledrejection", (e) =>
    send("error", [`unhandledrejection: ${String(e.reason)}`]),
  );
  console.log("puck: debug-log forwarding on");
}