Swap atomically swaps the underlying file the sink writes to. The caller opens newFile; the previous file is returned so the caller can close it after Swap returns. Buffered writes are flushed to the previous file before the swap, and the cap-tripped state is reset. Concurrent log Writes are safe —
(newFile *os.File)
| 510 | // cappedWriteSyncer's mutex, so no record can land half-written |
| 511 | // across the swap boundary. |
| 512 | func (s *DebugFileSink) Swap(newFile *os.File) error { |
| 513 | if s == nil || s.capped == nil || s.buffered == nil || newFile == nil { |
| 514 | return fmt.Errorf("debug file sink: swap called on uninitialized sink or nil file") |
| 515 | } |
| 516 | s.mu.Lock() |
| 517 | defer s.mu.Unlock() |
| 518 | if err := s.buffered.Sync(); err != nil { |
| 519 | return fmt.Errorf("debug file sink: flush before swap: %w", err) |
| 520 | } |
| 521 | s.capped.swap(zapcore.AddSync(newFile)) |
| 522 | s.originPath = newFile.Name() |
| 523 | return nil |
| 524 | } |
| 525 | |
| 526 | // RotateForScope swaps the sink to a per-scope file derived from the |
| 527 | // sink's origin path. Scope semantics are caller-defined; for the |
no test coverage detected