mirror of
https://github.com/zhom/donutbrowser.git
synced 2026-10-20 14:37:30 +02:00
refactor: better logs
This commit is contained in:
1 parent
2604994507
commit
bf625fac2c
91 files changed
+4353
-3239
No files matched your search
+21
-1
@@ -1,12 +1,32 @@
|
||||
import { attachConsole } from "@tauri-apps/plugin-log";
|
||||
import { attachConsole, error as logError } from "@tauri-apps/plugin-log";
|
||||
|
||||
let consoleAttached = false;
|
||||
let errorsForwarded = false;
|
||||
|
||||
function describe(reason: unknown): string {
|
||||
if (reason instanceof Error) return reason.stack ?? reason.message;
|
||||
return typeof reason === "string" ? reason : JSON.stringify(reason);
|
||||
}
|
||||
|
||||
export async function setupLogging() {
|
||||
if (consoleAttached) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Only uncaught failures go to the log file. Forwarding console.error too
|
||||
// would loop: attachConsole writes every backend line back to the console.
|
||||
if (!errorsForwarded) {
|
||||
errorsForwarded = true;
|
||||
window.addEventListener("error", (event) => {
|
||||
void logError(
|
||||
`Uncaught error err="${describe(event.error ?? event.message)}" source=${event.filename}:${event.lineno}:${event.colno}`,
|
||||
);
|
||||
});
|
||||
window.addEventListener("unhandledrejection", (event) => {
|
||||
void logError(`Unhandled rejection err="${describe(event.reason)}"`);
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
await attachConsole();
|
||||
consoleAttached = true;
|
||||
|
||||
Reference in new issue
Block a user