~gbmor/clinte

ref: fdf322b2d2e93428768e9207f1c62a866e7e8ef4 clinte/src/logging.rs -rw-r--r-- 955 bytes
fdf322b2Ben Morrison updated makefile to use printf instead of echo 1 year, 4 months ago
                                                                                
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
use std::fs::OpenOptions;

use simplelog::*;

use crate::error;

pub fn init(path: &str) -> error::Result<()> {
    let logfile = match OpenOptions::new().append(true).create(true).open(path) {
        Err(e) => {
            panic!("Could not open log file: {}", e);
        }
        Ok(f) => f,
    };

    if let Err(e) = WriteLogger::init(LevelFilter::Info, Config::default(), logfile) {
        panic!("Could not initiate logging: {}", e);
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    use std::fs;

    #[test]
    fn init_logs() {
        let file = "clinte_test.log";
        let blank = " ".bytes().collect::<Vec<u8>>();
        fs::write(&file, &blank).unwrap();
        init("clinte_test.log").unwrap();

        log::info!("TEST LOG MESSAGE");
        let logfile = fs::read_to_string(&file).unwrap();
        assert!(logfile.contains("TEST LOG MESSAGE"));

        fs::remove_file("clinte_test.log").unwrap();
    }
}