iwmlib/lib/logging.js
Ukmasmu 114c217ffa Improved logging and adjusted card's click handler
+ Logging now also works when ipcRenderer is appended to the window element.
+ Card's callback replacement method was changed from click to pointerdown.
2020-09-15 11:36:59 +02:00

68 lines
1.8 KiB
JavaScript

/* eslint-disable no-undef */
/* eslint-disable no-console */
/* eslint-disable no-unused-vars */
let ipc = null
let logMessages = new Set()
let logHandlers = {
log: console.log,
warn: console.warn,
error: console.error
}
try {
ipc = window.ipcRenderer || require('electron').ipcRenderer
logHandlers.log = message => ipc.send('log', message)
logHandlers.warn = message => ipc.send('warn', message)
logHandlers.error = message => ipc.send('error', message)
} catch (e) {
console.log('Cannot use electron logging.')
}
/** 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, ...).
*
* The logging handlers can be overwritten by calling the static
* setup method.
*/
export default class Logging {
/** Static log function.
* @param {*} message
*/
static log(message) {
logHandlers.log(message)
}
/**
* Static warn function.
* Emits each warning only once per session.
* @param {*} message
*/
static warn(message) {
if (!logMessages.has(message)) {
logMessages.add(message)
logHandlers.warn(message)
}
}
/**
* Static error function.
* Emits each error message only once per session.
* @param {*} message
*/
static error(message) {
if (!logMessages.has(message)) {
logMessages.add(message)
logHandlers.error(message)
}
}
static setup({ log = console.log, warn = console.warn, error = console.error } = {}) {
logHandlers.log = log
logHandlers.warn = warn
logHandlers.error = error
}
}