Skip to content

Commit

Permalink
fix: Log does not backup after closed
Browse files Browse the repository at this point in the history
  • Loading branch information
ci010 committed Dec 9, 2023
1 parent 31a9772 commit 700b2f0
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions xmcl-runtime/lib/plugins/pluginLogConsumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import { PassThrough, Transform, pipeline } from 'stream'
import { LauncherAppPlugin } from '../app/LauncherApp'
import { filterSensitiveData } from '../util/complaince'
import { format } from 'util'
import { join, resolve } from 'path'
import { basename, join, resolve } from 'path'
import { WriteStream, createWriteStream, ensureDir } from 'fs-extra'
import { IS_DEV } from '../constant'
import { kLogRoot } from '../entities/log'
import { isSystemError } from '../util/error'
import { ZipTask } from '../util/zip'
import filenamify from 'filenamify'
import { readFile, readdir, stat, unlink } from 'fs/promises'

function formatMsg(message: any, options: any[]) { return options.length !== 0 ? format(message, ...options.map(filterSensitiveData)) : format(message) }
function baseTransform(tag: string) { return new Transform({ transform(c, e, cb) { cb(undefined, `[${tag}] [${new Date().toLocaleString()}] ${c}`) } }) }
Expand All @@ -27,6 +30,7 @@ class LogSink {

private stream: WriteStream | undefined
private passthrough: PassThrough
path: string | undefined

constructor(readonly name: string) {
this.passthrough = new PassThrough({ transform(chunk, encode, cb) { cb(undefined, chunk + '\n') } })
Expand All @@ -36,7 +40,8 @@ class LogSink {
}

init(root: string) {
this.stream = createWriteStream(join(root, this.name + '.log'), { encoding: 'utf-8', flags: 'w+' })
this.path = join(root, this.name + '.log')
this.stream = createWriteStream(this.path, { encoding: 'utf-8', flags: 'w+' })
this.passthrough.pipe(this.stream)
}

Expand All @@ -54,6 +59,7 @@ export const pluginLogConsumer: LauncherAppPlugin = (app) => {
const logRoot = resolve(app.appDataPath, 'logs')
const logger = app.getLogger('LogConsumer')

let hasError = false
app.logEmitter.on('info', (destination, tag, message, ...args) => {
if (!sinks[destination]) {
sinks[destination] = new LogSink(destination)
Expand All @@ -67,6 +73,7 @@ export const pluginLogConsumer: LauncherAppPlugin = (app) => {
sinks[destination].entries.warn.write(`[${tag}] ${formatMsg(message, args)}`)
})
app.logEmitter.on('failure', (destination, tag, e) => {
hasError = true
if (!sinks[destination]) {
sinks[destination] = new LogSink(destination)
}
Expand Down Expand Up @@ -109,6 +116,23 @@ export const pluginLogConsumer: LauncherAppPlugin = (app) => {
}))
}

setTimeout(async () => {
// remove the zips older than a week
const root = logRoot!
const files = await readdir(root)
const zips = files.filter(f => f.endsWith('.zip'))
const check = async (path: string) => {
const fstat = await stat(path)
if (Date.now() - fstat.mtime.getTime() > 7 * 24 * 60 * 60 * 1000) {
await unlink(path)
}
}
for (const file of zips) {
const path = join(root, file)
check(path)
}
}, 60 * 1000)

const init = async () => {
await ensureDir(logRoot)
for (const destination of Object.values(sinks)) {
Expand All @@ -121,8 +145,18 @@ export const pluginLogConsumer: LauncherAppPlugin = (app) => {
init()

app.registryDisposer(async () => {
for (const destination of Object.values(sinks)) {
destination.dispose()
try {
if (hasError) {
const zip = new ZipTask(join(logRoot, filenamify(new Date().toJSON()) + '.zip'))
for (const sink of Object.values(sinks)) {
zip.addBuffer(await readFile(sink.path!), `logs/${basename(sink.name)}.log`)
}
await zip.startAndWait()
}
} finally {
for (const destination of Object.values(sinks)) {
destination.dispose()
}
}
})
}

0 comments on commit 700b2f0

Please sign in to comment.