Skip to content

Commit

Permalink
fix: backup log if has error
Browse files Browse the repository at this point in the history
  • Loading branch information
ci010 committed Jul 15, 2020
1 parent d89c9b3 commit 355a2f0
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 17 deletions.
12 changes: 10 additions & 2 deletions src/main/app/LauncherApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,15 @@ export abstract class LauncherApp extends EventEmitter {
/**
* Quit the app gentally.
*/
abstract quit(): void;
quit() {
Promise.all(this.managers.map(m => m.beforeQuit()))
.then(() => this.quitApp());
}

/**
* Quit the app gentally.
*/
protected abstract quitApp(): void;

/**
* Force exit the app with exit code
Expand Down Expand Up @@ -337,7 +345,7 @@ export abstract class LauncherApp extends EventEmitter {
this
.on('window-all-closed', () => {
if (this.parking) return;
if (process.platform !== 'darwin') { this.quit(); }
if (process.platform !== 'darwin') { this.quitApp(); }
})
.on('minecraft-start', () => { this.parking = true; })
.on('minecraft-exit', () => { this.parking = false; });
Expand Down
2 changes: 1 addition & 1 deletion src/main/app/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export async function quitAndInstallAsar(this: ElectronLauncherApp) {
await promisify(unlink)(appAsarPath);
await promisify(rename)(updateAsarPath, appAsarPath);
}
this.quit();
this.quitApp();
}


Expand Down
2 changes: 1 addition & 1 deletion src/main/electron/ElectronLauncherApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export default class ElectronLauncherApp extends LauncherApp {

showItemInFolder = shell.showItemInFolder;

quit = app.quit;
quitApp = app.quit;

exit = app.exit;

Expand Down
38 changes: 26 additions & 12 deletions src/main/manager/LogManager.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import LauncherApp from '@main/app/LauncherApp';
import { IS_DEV } from '@main/constant';
import { createWriteStream, WriteStream } from 'fs';
import { ensureDir } from 'fs-extra';
import { resolve } from 'path';
import { ensureDir, copy, readFile, writeFile } from 'fs-extra';
import { resolve, join } from 'path';
import { PassThrough, pipeline, Transform } from 'stream';
import { format } from 'util';
import { Manager } from '.';
import { createGzip } from 'zlib';
import filenamify from 'filenamify';
import { gzip } from '@main/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}\n`); } }); }
Expand All @@ -25,13 +28,19 @@ export default class LogManager extends Manager {

private openedStream: { [name: string]: WriteStream } = {};

private hasError = false;

constructor(app: LauncherApp) {
super(app);

pipeline(this.loggerEntries.log, this.output, () => { });
pipeline(this.loggerEntries.warn, this.output, () => { });
pipeline(this.loggerEntries.error, this.output, () => { });

this.loggerEntries.error.once('data', () => {
this.hasError = true;
});

process.on('uncaughtException', (err) => {
this.error('Uncaught Exception');
this.error(err);
Expand Down Expand Up @@ -76,21 +85,26 @@ export default class LogManager extends Manager {
}

async redirectLogPipeline(root: string) {
try {
await ensureDir(resolve(root, 'logs'));
} catch (e) {
if (e.code !== 'EEXIST') {
throw e;
}
}
this.logRoot = resolve(root, 'logs');
let mainLog = resolve(root, 'logs', 'main.log');
this.output.pipe(createWriteStream(mainLog, { encoding: 'utf-8', flags: 'w+' }));
await ensureDir(this.logRoot);
const mainLog = join(this.logRoot, 'main.log');
const stream = createWriteStream(mainLog, { encoding: 'utf-8', flags: 'w+' });
this.output.pipe(stream);
this.openedStream.MAIN_LOG = stream;
}

// SETUP CODE

setup() {
this.redirectLogPipeline(this.app.root);
return this.redirectLogPipeline(this.app.root);
}

async beforeQuit() {
const mainLog = this.openedStream.MAIN_LOG;
mainLog.close();
const mainLogPath = join(this.logRoot, 'main.log');
if (this.hasError) {
await writeFile(join(this.logRoot, filenamify(new Date().toJSON())), await gzip(await readFile(mainLogPath)));
}
}
}
2 changes: 2 additions & 0 deletions src/main/manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export abstract class Manager {
storeReady(store: StaticStore<any>): Promise<void> | void { }
/* eslint-enable */

beforeQuit(): Promise<void> | void { }

log(m: any, ...args: any[]) { this.app.logManager.log(`[${this.name}] ${m}`, ...args); }

warn(m: any, ...args: any[]) { this.app.logManager.warn(`[${this.name}] ${m}`, ...args); }
Expand Down
2 changes: 1 addition & 1 deletion src/main/service/BaseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export default class BaseService extends Service {
this.commit('updateStatus', 'ready');
}

quit = this.app.quit;
quit = this.app.quit.bind(this.app);

exit = this.app.exit;
}

0 comments on commit 355a2f0

Please sign in to comment.