(path: string | null)
| 164 | } |
| 165 | |
| 166 | export function setLogFile(path: string | null): void { |
| 167 | if (!path) { |
| 168 | if (logFileStream) { |
| 169 | try { |
| 170 | logFileStream.end(); |
| 171 | } catch { |
| 172 | // ignore |
| 173 | } |
| 174 | } |
| 175 | logFileStream = null; |
| 176 | logFilePath = null; |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | if (logFilePath === path && logFileStream) { |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | if (logFileStream) { |
| 185 | try { |
| 186 | logFileStream.end(); |
| 187 | } catch { |
| 188 | // ignore |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | try { |
| 193 | const stream = createWriteStream(path, { flags: 'a' }); |
| 194 | stream.on('error', (error) => { |
| 195 | if (stream !== logFileStream) return; |
| 196 | logFileStream = null; |
| 197 | logFilePath = null; |
| 198 | const message = error instanceof Error ? error.message : String(error); |
| 199 | const timestamp = new Date().toISOString(); |
| 200 | if (!areProcessStdioWritesSuppressed()) { |
| 201 | console.error(`[${timestamp}] [ERROR] Log file disabled after error: ${message}`); |
| 202 | } |
| 203 | }); |
| 204 | logFileStream = stream; |
| 205 | logFilePath = path; |
| 206 | const timestamp = new Date().toISOString(); |
| 207 | logFileStream.write(`[${timestamp}] [INFO] Log file initialized\n`); |
| 208 | } catch { |
| 209 | logFileStream = null; |
| 210 | logFilePath = null; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Get the current client-requested log level |
no test coverage detected