memoir_logger/
logging_utility.rs

1use chrono;
2use std::fs::OpenOptions;
3use std::io::Write;
4
5#[derive(Debug)]
6pub struct Log {
7    pub level: LogLevel,
8    pub message: String,
9}
10
11#[derive(Debug)]
12pub struct FileLogger {
13    pub filepath: String,
14    pub whitelist: Vec<LogLevel>,
15    pub format: String,
16}
17
18#[derive(Debug, PartialEq)]
19pub enum LogLevel {
20    Info,
21    Debug,
22    Warning,
23    Error,
24}
25
26impl FileLogger {
27    fn log(&mut self, log: Log) {
28        if self.whitelist.contains(&log.level) {
29            let file = OpenOptions::new()
30                .write(true)
31                .create(true)
32                .append(true)
33                .open(self.filepath.clone());
34
35            let mut conformed_message = "".to_string();
36            let mut is_format_char = false;
37
38            for char in self.format.chars() {
39                match char {
40                    '%' => {
41                        is_format_char = true;
42                    }
43                    'd' => {
44                        if is_format_char {
45                            conformed_message.push_str(&*format!("{}", chrono::offset::Utc::now()));
46                        } else {
47                            conformed_message.push_str("d");
48                            is_format_char = false;
49                        }
50                    }
51                    'l' => {
52                        if is_format_char {
53                            conformed_message.push_str(&*format!("{:?}", log.level));
54                        } else {
55                            conformed_message.push_str("l");
56                            is_format_char = false;
57                        }
58                    }
59                    'm' => {
60                        if is_format_char {
61                            conformed_message.push_str(&*log.message);
62                        } else {
63                            conformed_message.push_str("m");
64                            is_format_char = false;
65                        }
66                    }
67                    _ => {
68                        conformed_message.push_str(&*char.to_string());
69                        is_format_char = false;
70                    }
71                }
72            }
73
74            if let Err(e) = writeln!(file.expect(""), "{}", conformed_message) {
75                eprintln!("Couldn't write to file {}", e)
76            }
77        }
78    }
79
80    pub fn set_format(&mut self, format: String) {
81        self.format = format
82    }
83
84    pub fn warn(&mut self, message: String) {
85        self.log(Log {
86            level: LogLevel::Warning,
87            message,
88        });
89    }
90
91    pub fn error(&mut self, message: String) {
92        self.log(Log {
93            level: LogLevel::Error,
94            message,
95        });
96    }
97
98    pub fn info(&mut self, message: String) {
99        self.log(Log {
100            level: LogLevel::Info,
101            message,
102        });
103    }
104
105    pub fn debug(&mut self, message: String) {
106        self.log(Log {
107            level: LogLevel::Debug,
108            message,
109        });
110    }
111}