| 59 | } |
| 60 | |
| 61 | async function checkAndTruncateLogFile() { |
| 62 | const logFilePath = path.join(os.homedir(), '.devdb', 'mcp-log.txt'); |
| 63 | |
| 64 | try { |
| 65 | const stats = await fs.promises.stat(logFilePath); |
| 66 | if (stats.size > 5 * 1024) { |
| 67 | const data = await fs.promises.readFile(logFilePath, 'utf8'); |
| 68 | const lines = data.split('\n'); |
| 69 | let truncatedData = ''; |
| 70 | |
| 71 | for (let i = lines.length - 1; i >= 0; i--) { |
| 72 | const testData = lines[i] + '\n' + truncatedData; |
| 73 | if (Buffer.byteLength(testData, 'utf8') > 1024) { |
| 74 | break; |
| 75 | } |
| 76 | truncatedData = testData; |
| 77 | } |
| 78 | |
| 79 | await fs.promises.writeFile(logFilePath, truncatedData); |
| 80 | logger.info('Log file truncated', { originalSize: stats.size, newSize: Buffer.byteLength(truncatedData, 'utf8') }); |
| 81 | } |
| 82 | } catch (error) { |
| 83 | if ((error as any).code !== 'ENOENT') { |
| 84 | logger.error('Failed to check/truncate log file', { error: (error as Error).message }); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | export async function startHttpServer() { |
| 90 | await checkAndTruncateLogFile(); |