tracing_setup/
file_logging_configuration.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
crate::ix!();

pub struct FileLoggingConfiguration {
    log_path:  Option<PathBuf>,
    log_level: Level,
    rotation:  Option<Rotation>,
    temporary: bool,
}

impl FileLoggingConfiguration {

    pub fn default_temporary() -> Self {
        Self {
            log_path:  Some(PathBuf::from("default.log")),
            log_level: Level::INFO,
            rotation:  Some(Rotation::DAILY),
            temporary: true,
        }
    }

    pub fn new(log_path: Option<PathBuf>, log_level: Level, rotation: Option<Rotation>) -> Self {
        Self {
            log_path,
            log_level,
            rotation,
            temporary: false,
        }
    }

    pub fn new_temporary(
        log_path:  Option<PathBuf>, 
        log_level: Level, 
        rotation:  Option<Rotation>

    ) -> Self {

        Self {
            log_path,
            log_level,
            rotation,
            temporary: true,
        }
    }

    pub fn create_writer(&self) -> BoxMakeWriter {
        match &self.log_path {
            Some(log_path) => {
                if let Some(rotation) = &self.rotation {
                    let file_appender = RollingFileAppender::new(rotation.clone(), ".", log_path);
                    BoxMakeWriter::new(file_appender)
                } else {
                    let file = File::create(log_path).expect("Could not create log file");
                    BoxMakeWriter::new(Arc::new(file))
                }
            }
            None => BoxMakeWriter::new(io::stderr),
        }
    }

    //----------------------------------------[access]
    pub fn log_level(&self) -> &Level {
        &self.log_level
    }

    pub fn log_path(&self) -> &Option<PathBuf> {
        &self.log_path
    }

    pub fn rotation(&self) -> &Option<Rotation> {
        &self.rotation
    }

    pub fn info_level(&self) -> bool {
        self.log_level == Level::INFO
    }

    pub fn log_path_root_is(&self, path: impl AsRef<Path>) -> bool {
        self.log_path == Some(path.as_ref().to_path_buf())
    }

    pub fn rotates_daily(&self) -> bool {
        matches!(self.rotation(), Some(Rotation::DAILY))
    }

    pub fn remove_logs(&self) {
        if let Some(path) = &self.log_path {
            match std::fs::remove_file(path) {
                Ok(()) => {},
                Err(ref e) if e.kind() == std::io::ErrorKind::NotFound => {
                    // File does not exist, no problem
                },
                Err(e) => {
                    panic!("Failed to remove log file: {:?}", e);
                }
            }
        }
    }

    pub fn is_temporary(&self) -> bool {
        self.temporary
    }
}

impl Default for FileLoggingConfiguration {

    fn default() -> Self {
        Self {
            log_path: Some(PathBuf::from("default.log")),
            log_level: Level::INFO,
            rotation: Some(Rotation::DAILY),
            temporary: false,
        }
    }
}

impl Drop for FileLoggingConfiguration {

    fn drop(&mut self) {
        if self.is_temporary() {
            self.remove_logs();
        }
    }
}