Skip to content

Commit

Permalink
fix: Correctly store the logs
Browse files Browse the repository at this point in the history
  • Loading branch information
ci010 committed Oct 23, 2022
1 parent ec98d99 commit 689fde8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion xmcl-runtime/lib/app/LauncherApp.ts
Expand Up @@ -273,7 +273,7 @@ export abstract class LauncherApp extends EventEmitter {

try {
await Promise.race([
setTimeout(1000).then(() => false),
setTimeout(10000).then(() => false),
Promise.all(this.managers.map(m => m.dispose())).then(() => true),
])
} finally {
Expand Down
21 changes: 12 additions & 9 deletions xmcl-runtime/lib/managers/LogManager.ts
@@ -1,14 +1,15 @@
import filenamify from 'filenamify'
import { createWriteStream, WriteStream } from 'fs'
import { ensureDir, readFile, writeFile } from 'fs-extra'
import { join, resolve } from 'path'
import { ensureDir } from 'fs-extra'
import { readFile } from 'fs/promises'
import { basename, join, resolve } from 'path'
import { PassThrough, pipeline, Transform } from 'stream'
import { format } from 'util'
import { Manager } from '.'
import LauncherApp from '../app/LauncherApp'
import { IS_DEV } from '../constant'
import { Logger } from '../util/log'
import { gzip, ZipTask } from '../util/zip'
import { ZipTask } from '../util/zip'

function formatMsg(message: any, options: any[]) { return options.length !== 0 ? format(message, ...options) : format(message) }
function baseTransform(tag: string) { return new Transform({ transform(c, e, cb) { cb(undefined, `[${tag}] [${new Date().toLocaleString()}] ${c}`) } }) }
Expand All @@ -32,7 +33,7 @@ export default class LogManager extends Manager {
pipeline(this.loggerEntries.warn, output, () => { })
pipeline(this.loggerEntries.error, output, () => { })
this.outputs.push(output)
Reflect.set(output, 'name', 'MAIN')
Reflect.set(output, 'name', 'main')

this.loggerEntries.error.once('data', () => {
this.hasError = true
Expand Down Expand Up @@ -90,7 +91,7 @@ export default class LogManager extends Manager {
const loggerPath = resolve(this.logRoot, `renderer.${name}.log`)
this.log(`Setup renderer logger for window ${name} to ${loggerPath}`)
const stream = createWriteStream(loggerPath, { encoding: 'utf-8', flags: 'w+' })
this.openedStream[name] = stream
this.openedStream[loggerPath] = stream
return stream
}

Expand Down Expand Up @@ -124,7 +125,8 @@ export default class LogManager extends Manager {
}

closeWindowLog(name: string) {
this.openedStream[name].close()
const loggerPath = resolve(this.logRoot, `renderer.${name}.log`)
this.openedStream[loggerPath].close()
}

async setOutputRoot(root: string) {
Expand All @@ -135,16 +137,17 @@ export default class LogManager extends Manager {
const logPath = join(this.logRoot, `${name}.log`)
const stream = createWriteStream(logPath, { encoding: 'utf-8', flags: 'w+' })
output.pipe(stream)
this.openedStream[logPath] = stream
}
this.log(`Set log root to ${root}`)
}

async dispose() {
const mainLogPath = join(this.logRoot, 'main.log')
if (this.hasError) {
const zip = new ZipTask(join(this.logRoot, filenamify(new Date().toJSON()) + '.zip'))
zip.addFile(mainLogPath, 'main.log')
zip.addFile(mainLogPath, 'main.log')
for (const p of Object.keys(this.openedStream)) {
zip.addBuffer(await readFile(p), `logs/${basename(p)}`)
}
await zip.startAndWait()
}
}
Expand Down
6 changes: 5 additions & 1 deletion xmcl-runtime/lib/util/zip.ts
Expand Up @@ -66,6 +66,10 @@ export class ZipTask extends AbortableTask<void> {
this.update(buffer.length)
})
const promise = pipeline(this.zipFile.outputStream, this.writeStream)
const fileClose = new Promise<void>((resolve, reject) => {
this.writeStream?.on('close', resolve)
this.writeStream?.on('error', reject)
})
if (!(this.zipFile as any).ended) {
await new Promise<void>((resolve) => {
this.zipFile.end({ forceZip64Format: false }, (...args: any[]) => {
Expand All @@ -74,7 +78,7 @@ export class ZipTask extends AbortableTask<void> {
})
})
}
await promise
await Promise.all([fileClose, promise])
}

protected isAbortedError(e: any): boolean {
Expand Down

0 comments on commit 689fde8

Please sign in to comment.