tracing_subscriber_multi/
rotating_file_writer.rs

1use std::{io::Write, path::Path};
2
3pub use file_rotate::{
4    compression::Compression,
5    suffix::{AppendCount, AppendTimestamp},
6    ContentLimit,
7};
8use file_rotate::{suffix::SuffixScheme, FileRotate};
9
10/// A rotating log file. This will create log files that are limited in size,
11/// before being moved to `name.X` and optionally compressed.
12pub struct RotatingFile<S: SuffixScheme> {
13    inner: FileRotate<S>,
14}
15
16impl<S: SuffixScheme> RotatingFile<S> {
17    /// Create a new rotating file writer
18    pub fn new<P>(
19        path: P,
20        suffix_scheme: S,
21        content_limit: ContentLimit,
22        compression: Compression,
23    ) -> Self
24    where
25        P: AsRef<Path>,
26    {
27        Self {
28            inner: FileRotate::new(
29                path,
30                suffix_scheme,
31                content_limit,
32                compression,
33                #[cfg(unix)]
34                None,
35            ),
36        }
37    }
38}
39
40impl<S: SuffixScheme> Write for RotatingFile<S> {
41    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
42        self.inner.write(buf)
43    }
44
45    fn flush(&mut self) -> std::io::Result<()> {
46        self.inner.flush()
47    }
48}