|
1 | | -import { createWriteStream, mkdirSync } from "node:fs"; |
| 1 | +import { createWriteStream, mkdirSync, readdirSync, statSync, unlinkSync } from "node:fs"; |
2 | 2 | import { join } from "node:path"; |
3 | 3 | import { homedir } from "node:os"; |
4 | 4 | import { env } from "node:process"; |
@@ -67,8 +67,42 @@ function getLogsDir(customLogDir?: string): string { |
67 | 67 | * Builds a timestamped log file path. |
68 | 68 | */ |
69 | 69 | function createLogFilePath(customLogDir?: string): string { |
| 70 | + const logsDir = getLogsDir(customLogDir); |
| 71 | + cleanupOldLogs(logsDir, 25); |
70 | 72 | const timestamp = new Date().toISOString().replace(/[:.]/g, "-"); |
71 | | - return join(getLogsDir(customLogDir), `antigravity-debug-${timestamp}.log`); |
| 73 | + return join(logsDir, `antigravity-debug-${timestamp}.log`); |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Cleans up old log files, keeping only the most recent maxFiles. |
| 78 | + */ |
| 79 | +function cleanupOldLogs(logsDir: string, maxFiles: number): void { |
| 80 | + try { |
| 81 | + const files = readdirSync(logsDir) |
| 82 | + .filter((file) => file.startsWith("antigravity-debug-") && file.endsWith(".log")) |
| 83 | + .map((file) => join(logsDir, file)); |
| 84 | + |
| 85 | + if (files.length <= maxFiles) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + const sortedFiles = files |
| 90 | + .map((file) => ({ |
| 91 | + file, |
| 92 | + mtime: statSync(file).mtimeMs, |
| 93 | + })) |
| 94 | + .sort((a, b) => b.mtime - a.mtime); |
| 95 | + |
| 96 | + for (let i = maxFiles; i < sortedFiles.length; i++) { |
| 97 | + try { |
| 98 | + unlinkSync(sortedFiles[i]!.file); |
| 99 | + } catch { |
| 100 | + // Ignore deletion errors |
| 101 | + } |
| 102 | + } |
| 103 | + } catch { |
| 104 | + // Ignore directory read errors |
| 105 | + } |
72 | 106 | } |
73 | 107 |
|
74 | 108 | /** |
|
0 commit comments