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
use time;
use crate::filter::FilterLevel;
use std::fmt;

pub struct LogicEvent {
    pub level: FilterLevel,
    pub content: String,
}

pub struct Event {
    pub time_spec: time::Timespec,
    pub tm: time::Tm,
    pub level: FilterLevel,
    pub thread_tag: String,
    pub file: &'static str,
    pub line: u32,
    pub msg: String,
}

impl Event {
    pub fn new(level: FilterLevel, thread_tag: String, file: &'static str, line: u32, msg: fmt::Arguments) -> Self {
        Self {
            time_spec: time::get_time(),
            tm: time::now(),
            level,
            thread_tag,
            file,
            line,
            msg: msg.to_string(),
        }
    }

    pub fn format_by_default(&self) -> String {
        let t = self.tm.strftime("[%Y-%m-%d %H:%M:%S]").unwrap();
        format!("{}-{}-[{}]-{}:{}  {}\n", t, self.thread_tag, self.level.to_str(), self.file, self.line, self.msg)
    }

    pub fn to_logic(self) -> LogicEvent {
        LogicEvent {
            content: self.format_by_default(),
            level: self.level,
        }
    }
}

impl fmt::Display for Event {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f,
               "(tm:{:?} level:{} thread_tag:{} file:{} line:{} msg:{})",
               self.tm,
               self.level.to_str(),
               self.thread_tag,
               self.file,
               self.line,
               self.msg
        )
    }
}