Skip to content

Commit a9be90d

Browse files
committed
feat(debug): add log file cleanup functionality to manage old logs
1 parent 148ab72 commit a9be90d

1 file changed

Lines changed: 36 additions & 2 deletions

File tree

src/plugin/debug.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createWriteStream, mkdirSync } from "node:fs";
1+
import { createWriteStream, mkdirSync, readdirSync, statSync, unlinkSync } from "node:fs";
22
import { join } from "node:path";
33
import { homedir } from "node:os";
44
import { env } from "node:process";
@@ -67,8 +67,42 @@ function getLogsDir(customLogDir?: string): string {
6767
* Builds a timestamped log file path.
6868
*/
6969
function createLogFilePath(customLogDir?: string): string {
70+
const logsDir = getLogsDir(customLogDir);
71+
cleanupOldLogs(logsDir, 25);
7072
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+
}
72106
}
73107

74108
/**

0 commit comments

Comments
 (0)