2019-07-02 09:38:19 +02:00
|
|
|
/* eslint-disable no-undef */
|
|
|
|
/* eslint-disable no-console */
|
|
|
|
/* eslint-disable no-unused-vars */
|
2019-05-24 11:02:44 +02:00
|
|
|
let ipc = null
|
2019-06-18 09:00:05 +02:00
|
|
|
let logMessages = new Set()
|
2019-07-02 09:38:19 +02:00
|
|
|
let logHandlers = {
|
|
|
|
log: console.log,
|
|
|
|
warn: console.warn,
|
|
|
|
error: console.error
|
|
|
|
}
|
2019-05-24 10:29:10 +02:00
|
|
|
|
|
|
|
try {
|
2020-09-15 11:36:59 +02:00
|
|
|
ipc = window.ipcRenderer || require('electron').ipcRenderer
|
2019-07-02 09:38:19 +02:00
|
|
|
logHandlers.log = message => ipc.send('log', message)
|
|
|
|
logHandlers.warn = message => ipc.send('warn', message)
|
|
|
|
logHandlers.error = message => ipc.send('error', message)
|
2019-06-18 09:00:05 +02:00
|
|
|
} catch (e) {
|
2019-07-02 09:38:19 +02:00
|
|
|
console.log('Cannot use electron logging.')
|
2019-06-18 09:00:05 +02:00
|
|
|
}
|
2019-05-24 10:29:10 +02:00
|
|
|
|
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.
|
2019-07-18 12:26:39 +02:00
|
|
|
* Uses a logMessage cache to prevent error overflows. This is
|
2019-06-18 09:00:05 +02:00
|
|
|
* needed since errors may occur very frequently
|
|
|
|
* (e.g. display update loops at 60fps, programmatic loops, ...).
|
2019-07-18 12:26:39 +02:00
|
|
|
*
|
2019-07-02 09:38:19 +02:00
|
|
|
* The logging handlers can be overwritten by calling the static
|
|
|
|
* setup method.
|
2019-05-24 09:53:27 +02:00
|
|
|
*/
|
|
|
|
export default class Logging {
|
|
|
|
/** Static log function.
|
|
|
|
* @param {*} message
|
|
|
|
*/
|
2019-05-24 10:29:10 +02:00
|
|
|
static log(message) {
|
2019-07-02 09:38:19 +02:00
|
|
|
logHandlers.log(message)
|
2019-05-24 09:53:27 +02:00
|
|
|
}
|
2019-06-17 17:21:47 +02:00
|
|
|
|
2019-06-18 09:00:05 +02:00
|
|
|
/**
|
2019-07-18 12:26:39 +02:00
|
|
|
* Static warn function.
|
2019-06-18 09:00:05 +02:00
|
|
|
* Emits each warning only once per session.
|
2019-07-18 12:26:39 +02:00
|
|
|
* @param {*} message
|
2019-06-18 09:00:05 +02:00
|
|
|
*/
|
2019-06-17 17:21:47 +02:00
|
|
|
static warn(message) {
|
2019-06-18 09:00:05 +02:00
|
|
|
if (!logMessages.has(message)) {
|
2019-06-18 16:01:20 +02:00
|
|
|
logMessages.add(message)
|
2019-07-02 09:38:19 +02:00
|
|
|
logHandlers.warn(message)
|
2019-06-17 17:21:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 09:00:05 +02:00
|
|
|
/**
|
2019-07-18 12:26:39 +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) {
|
2019-06-18 09:00:05 +02:00
|
|
|
if (!logMessages.has(message)) {
|
2019-06-18 16:01:20 +02:00
|
|
|
logMessages.add(message)
|
2019-07-02 09:38:19 +02:00
|
|
|
logHandlers.error(message)
|
2019-06-17 17:21:47 +02:00
|
|
|
}
|
|
|
|
}
|
2019-07-02 09:38:19 +02:00
|
|
|
|
2019-07-30 16:56:29 +02:00
|
|
|
static setup({ log = console.log, warn = console.warn, error = console.error } = {}) {
|
2019-07-02 09:38:19 +02:00
|
|
|
logHandlers.log = log
|
|
|
|
logHandlers.warn = warn
|
|
|
|
logHandlers.error = error
|
|
|
|
}
|
2019-05-24 09:53:27 +02:00
|
|
|
}
|