From 3c01861122350b51473c477ba4b3e826ef840bfe Mon Sep 17 00:00:00 2001 From: PiEgg Date: Mon, 17 Oct 2022 17:53:15 +0800 Subject: [PATCH] :bug: Fix: some case will cause picgo-gui-local.log too large --- src/main/apis/core/utils/localLogger.ts | 47 +++++++++++++++++-------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/src/main/apis/core/utils/localLogger.ts b/src/main/apis/core/utils/localLogger.ts index 2e325f47..67faddd8 100644 --- a/src/main/apis/core/utils/localLogger.ts +++ b/src/main/apis/core/utils/localLogger.ts @@ -7,17 +7,25 @@ const checkLogFileIsLarge = (logPath: string): { logFileSize?: number logFileSizeLimit?: number } => { - if (fs.existsSync(logPath)) { - const logFileSize = fs.statSync(logPath).size - const logFileSizeLimit = 10 * 1024 * 1024 // 10 MB default + try { + if (fs.existsSync(logPath)) { + const logFileSize = fs.statSync(logPath).size + const logFileSizeLimit = 10 * 1024 * 1024 // 10 MB default + return { + isLarge: logFileSize > logFileSizeLimit, + logFileSize, + logFileSizeLimit + } + } return { - isLarge: logFileSize > logFileSizeLimit, - logFileSize, - logFileSizeLimit + isLarge: false + } + } catch (e) { + // why throw error??? + console.error(e) + return { + isLarge: true } - } - return { - isLarge: false } } @@ -32,11 +40,17 @@ const recreateLogFile = (logPath: string): void => { * for local log before picgo inited */ const getLogger = (logPath: string) => { - if (!fs.existsSync(logPath)) { - fs.ensureFileSync(logPath) - } - if (checkLogFileIsLarge(logPath).isLarge) { - recreateLogFile(logPath) + let hasUncathcedError = false + try { + if (!fs.existsSync(logPath)) { + fs.ensureFileSync(logPath) + } + if (checkLogFileIsLarge(logPath).isLarge) { + recreateLogFile(logPath) + } + } catch (e) { + console.error(e) + hasUncathcedError = true } return (type: string, ...msg: any[]) => { try { @@ -54,9 +68,12 @@ const getLogger = (logPath: string) => { log += '\n' console.log(log) // A synchronized approach to avoid log msg sequence errors - fs.appendFileSync(logPath, log) + if (!hasUncathcedError) { + fs.appendFileSync(logPath, log) + } } catch (e) { console.error(e) + hasUncathcedError = true } } }