| 28 | } |
| 29 | |
| 30 | function pruneOldLogs(logsDir: string): { pruned: string[]; error: any } { |
| 31 | if (!fs.existsSync(logsDir)) { |
| 32 | return { pruned: [], error: null }; |
| 33 | } |
| 34 | |
| 35 | const files = fs.readdirSync(logsDir); |
| 36 | const logFiles: { name: string; num: number }[] = []; |
| 37 | |
| 38 | for (const file of files) { |
| 39 | const match = file.match(/^waveapp\.(\d+)\.log$/); |
| 40 | if (match) { |
| 41 | logFiles.push({ name: file, num: parseInt(match[1], 10) }); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | if (logFiles.length <= 5) { |
| 46 | return { pruned: [], error: null }; |
| 47 | } |
| 48 | |
| 49 | logFiles.sort((a, b) => b.num - a.num); |
| 50 | const toDelete = logFiles.slice(5); |
| 51 | const pruned: string[] = []; |
| 52 | let firstError: any = null; |
| 53 | |
| 54 | for (const logFile of toDelete) { |
| 55 | try { |
| 56 | fs.unlinkSync(path.join(logsDir, logFile.name)); |
| 57 | pruned.push(logFile.name); |
| 58 | } catch (e) { |
| 59 | if (firstError == null) { |
| 60 | firstError = e; |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return { pruned, error: firstError }; |
| 66 | } |
| 67 | |
| 68 | function rotateLogIfNeeded(): string | null { |
| 69 | const waveDataDir = getWaveDataDir(); |