30 lines
1.3 KiB
TypeScript
30 lines
1.3 KiB
TypeScript
import { app } from 'electron'
|
|
import { dirname, join } from 'path'
|
|
import { inspect } from 'util'
|
|
import { createDailyLog } from './dailyLog'
|
|
|
|
const fallback = app.getPath('logs')
|
|
export const appLog = createDailyLog([
|
|
process.env.APPIMAGE
|
|
? join(dirname(process.env.APPIMAGE), 'logs')
|
|
: app.isPackaged ? join(dirname(process.execPath), 'logs') : fallback,
|
|
fallback
|
|
])
|
|
|
|
for (const level of ['warn', 'error'] as const) {
|
|
const original = console[level].bind(console)
|
|
console[level] = (...args: unknown[]) => {
|
|
appLog.write(level, args.map((value) => typeof value === 'string' ? value : inspect(value, { depth: 4 })).join(' '))
|
|
original(...args)
|
|
}
|
|
}
|
|
|
|
// Monitor fatal exceptions without changing Node's normal termination behavior.
|
|
process.on('uncaughtExceptionMonitor', (error, origin) => {
|
|
appLog.write(origin, error.stack ?? String(error))
|
|
})
|
|
process.on('warning', (warning) => appLog.write('warning', warning.stack ?? warning.message))
|
|
process.on('exit', (code) => appLog.write('exit', `code=${code}`))
|
|
app.on('child-process-gone', (_event, details) => appLog.write('child-process-gone', JSON.stringify(details)))
|
|
appLog.write('startup', `version=${app.getVersion()} platform=${process.platform} arch=${process.arch} electron=${process.versions.electron} executable=${process.env.APPIMAGE || process.execPath}`)
|