partey_workadventure/desktop/src/log.ts

60 lines
1.4 KiB
TypeScript
Raw Normal View History

2022-02-17 18:09:57 +01:00
import { dialog, shell } from "electron";
import log from "electron-log";
import settings from "./settings";
function onError(e: Error) {
try {
log.error(e);
dialog.showErrorBox(
"WorkAdventure - A JavaScript error occurred",
e.stack || ""
);
} catch (logError) {
// eslint-disable-next-line no-console
console.error(e);
}
}
function onRejection(reason: Error) {
if (reason instanceof Error) {
let _reason = reason;
const errPrototype = Object.getPrototypeOf(reason);
const nameProperty = Object.getOwnPropertyDescriptor(errPrototype, "name");
if (!nameProperty || !nameProperty.writable) {
_reason = new Error(reason.message);
}
_reason.name = `UnhandledRejection ${_reason.name}`;
onError(_reason);
return;
}
const error = new Error(JSON.stringify(reason));
error.name = "UnhandledRejection";
onError(error);
}
function init() {
const logLevel = settings.get("log_level", "info");
log.transports.console.level = logLevel;
2022-02-18 01:51:35 +01:00
log.transports.file.level = logLevel;
2022-02-17 18:09:57 +01:00
// eslint-disable-next-line no-console
console.log = log.log.bind(log);
process.on("uncaughtException", onError);
process.on("unhandledRejection", onRejection);
}
export async function openLog() {
const logFilePath = log.transports.file.getFile().path;
await shell.openPath(logFilePath);
}
export default {
init,
};