()
| 306 | function noop(): void {} |
| 307 | |
| 308 | function getDebugWriter(): BufferedWriter { |
| 309 | if (!debugWriter) { |
| 310 | let ensuredDir: string | null = null |
| 311 | debugWriter = createBufferedWriter({ |
| 312 | writeFn: content => { |
| 313 | if (debugLoggingUnavailable) { |
| 314 | return |
| 315 | } |
| 316 | const path = getDebugLogPath() |
| 317 | const dir = dirname(path) |
| 318 | const needMkdir = ensuredDir !== dir |
| 319 | ensuredDir = dir |
| 320 | if (isDebugMode()) { |
| 321 | // immediateMode: must stay sync. Async writes are lost on direct |
| 322 | // process.exit() and keep the event loop alive in beforeExit |
| 323 | // handlers (infinite loop with Perfetto tracing). See #22257. |
| 324 | if (needMkdir) { |
| 325 | try { |
| 326 | getFsImplementation().mkdirSync(dir) |
| 327 | } catch (error) { |
| 328 | if (isFsInaccessible(error)) { |
| 329 | markDebugLoggingUnavailable(error) |
| 330 | return |
| 331 | } |
| 332 | // Directory already exists |
| 333 | } |
| 334 | } |
| 335 | try { |
| 336 | const budgetedContent = applyDebugLogBudgetSync(path, content) |
| 337 | if (budgetedContent === null) { |
| 338 | return |
| 339 | } |
| 340 | getFsImplementation().appendFileSync(path, budgetedContent) |
| 341 | void updateLatestDebugLogSymlink() |
| 342 | } catch (error) { |
| 343 | if (isFsInaccessible(error)) { |
| 344 | markDebugLoggingUnavailable(error) |
| 345 | return |
| 346 | } |
| 347 | throw error |
| 348 | } |
| 349 | return |
| 350 | } |
| 351 | // Buffered path (ants without --debug): flushes ~1/sec so chain |
| 352 | // depth stays ~1. .bind over a closure so only the bound args are |
| 353 | // retained, not this scope. |
| 354 | pendingWrite = pendingWrite |
| 355 | .then(appendAsync.bind(null, needMkdir, dir, path, content)) |
| 356 | .catch(noop) |
| 357 | }, |
| 358 | flushIntervalMs: 1000, |
| 359 | maxBufferSize: 100, |
| 360 | immediateMode: isDebugMode(), |
| 361 | }) |
| 362 | registerCleanup(async () => { |
| 363 | debugWriter?.dispose() |
| 364 | await pendingWrite |
| 365 | }) |
no test coverage detected