()
| 81 | } |
| 82 | |
| 83 | private flush(): void { |
| 84 | if (this.logQueue.length === 0) { |
| 85 | return |
| 86 | } |
| 87 | |
| 88 | // Only flush to disk if mcpServer is enabled |
| 89 | if (!this.mcpServerEnabled) { |
| 90 | this.logQueue.length = 0 // Clear the queue without GC overhead |
| 91 | this.flushTimer = null |
| 92 | return |
| 93 | } |
| 94 | |
| 95 | try { |
| 96 | // Ensure the directory exists before writing |
| 97 | const logDir = path.dirname(this.logFilePath) |
| 98 | if (!fs.existsSync(logDir)) { |
| 99 | fs.mkdirSync(logDir, { recursive: true }) |
| 100 | } |
| 101 | |
| 102 | const logsToWrite = this.logQueue.join('') |
| 103 | // Writing logs to files synchronously to ensure they're written before returning |
| 104 | fs.appendFileSync(this.logFilePath, logsToWrite) |
| 105 | this.logQueue.length = 0 // Clear the queue without GC overhead |
| 106 | } catch (error) { |
| 107 | console.error('Failed to flush logs to file:', error) |
| 108 | } finally { |
| 109 | this.flushTimer = null |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | private enqueueLog(formattedEntry: string): void { |
| 114 | this.logQueue.push(formattedEntry) |
no test coverage detected