Skip to content

Commit

Permalink
🐛 Fix: some case will cause picgo-gui-local.log too large
Browse files Browse the repository at this point in the history
  • Loading branch information
Molunerfinn committed Oct 17, 2022
1 parent ae87a51 commit 3c01861
Showing 1 changed file with 32 additions and 15 deletions.
47 changes: 32 additions & 15 deletions src/main/apis/core/utils/localLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -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 {
Expand All @@ -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
}
}
}
Expand Down

0 comments on commit 3c01861

Please sign in to comment.