partey_workadventure/desktop/electron/src/log.ts

55 lines
1.4 KiB
TypeScript
Raw Normal View History

2022-02-17 18:09:57 +01:00
import { dialog, shell } from "electron";
2022-02-19 01:48:56 +01:00
import ElectronLog from "electron-log";
2022-02-17 18:09:57 +01:00
import log from "electron-log";
function onError(e: Error) {
2022-02-18 23:03:05 +01:00
try {
log.error(e);
dialog.showErrorBox("WorkAdventure - A JavaScript error occurred", e.stack || "");
} catch (logError) {
console.error(e);
}
2022-02-17 18:09:57 +01:00
}
function onRejection(reason: Error) {
2022-02-18 23:03:05 +01:00
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;
2022-02-17 18:09:57 +01:00
}
2022-02-18 23:03:05 +01:00
const error = new Error(JSON.stringify(reason));
error.name = "UnhandledRejection";
onError(error);
2022-02-17 18:09:57 +01:00
}
function init() {
2022-02-18 23:03:05 +01:00
console.log = log.log.bind(log);
2022-02-17 18:09:57 +01:00
2022-02-18 23:03:05 +01:00
process.on("uncaughtException", onError);
process.on("unhandledRejection", onRejection);
2022-02-17 18:09:57 +01:00
}
export async function openLog() {
2022-02-18 23:03:05 +01:00
const logFilePath = log.transports.file.getFile().path;
await shell.openPath(logFilePath);
2022-02-17 18:09:57 +01:00
}
2022-02-19 01:48:56 +01:00
export function setLogLevel(logLevel: ElectronLog.LogLevel) {
log.transports.console.level = logLevel;
log.transports.file.level = logLevel;
}
2022-02-17 18:09:57 +01:00
export default {
2022-02-18 23:03:05 +01:00
init,
2022-02-17 18:09:57 +01:00
};