iwmlib/lib/logging.js

62 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-05-24 11:02:44 +02:00
let ipc = null
let logMessages = new Set()
try {
ipc = require('electron').ipcRenderer
} catch (e) {
console.log("Cannot use electron logging.")
}
2019-05-24 09:53:27 +02:00
/** Basic class for app specific logging requirements.
* Can be used to implement persistent logging in electron apps.
* Uses a logMessage cache to prevent error overflows. This is
* needed since errors may occur very frequently
* (e.g. display update loops at 60fps, programmatic loops, ...).
2019-05-24 09:53:27 +02:00
*/
export default class Logging {
/** Static log function.
* @param {*} message
*/
static log(message) {
if (ipc) {
ipc.send('log', message)
} else {
console.log(message)
}
2019-05-24 09:53:27 +02:00
}
2019-06-17 17:21:47 +02:00
/**
* Static warn function.
* Emits each warning only once per session.
* @param {*} message
*/
2019-06-17 17:21:47 +02:00
static warn(message) {
if (!logMessages.has(message)) {
logMessages.add(message)
if (ipc) {
ipc.send('warn', message)
} else {
console.warn(message)
}
2019-06-17 17:21:47 +02:00
}
}
/**
* Static error function.
* Emits each error message only once per session.
* @param {*} message
*/
2019-06-17 17:21:47 +02:00
static error(message) {
if (!logMessages.has(message)) {
logMessages.add(message)
if (ipc) {
ipc.send('error', message)
} else {
console.error(message)
}
2019-06-17 17:21:47 +02:00
}
}
2019-05-24 09:53:27 +02:00
}