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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//! Plugin trait and some built-in plugins
use super::define::*;
use super::record::*;

/// The Plugin Trait
///
/// A plugin can be used to customize a record. You can append additional fields to a record before
/// or after the `msg` field.
///
/// You can terminate the log processing in advance, simply return `false` in `pre` or `post`.
#[allow(unused_variables)]
pub trait Plugin: Any + Sync + Send + 'static {
    /// Invoked before the `msg` field is appended to a record
    #[inline]
    #[must_use]
    fn pre(&self, record: &mut Record) -> bool {
        true
    }

    /// Invoked after the `msg` field is appended to a record
    #[inline]
    #[must_use]
    fn post(&self, record: &mut Record) -> bool {
        true
    }
}

/// Add a level string to a record
///
/// ```json,no_run
/// {"level":"info"}
/// ```
pub struct LevelPlugin;

impl Plugin for LevelPlugin {
    #[inline]
    fn pre(&self, record: &mut Record) -> bool {
        match level_to_str(record.level()) {
            None => record.append("level", &record.level().to_string()),
            Some(level) => record.append("level", &level),
        };

        true
    }
}

/// Add a rfc3339 datetime string to a record
pub struct TimePlugin {
    /// time format
    pub format: chrono::SecondsFormat,
}

impl TimePlugin {
    /// Second-level precision
    ///
    /// ```json,no_run
    /// {"time":"2024-01-03T11:01:00+08:00"}
    /// ```
    pub fn from_secs() -> Self {
        Self {format: chrono::SecondsFormat::Secs}
    }

    /// Millisecond-level precision
    ///
    /// ```json,no_run
    /// {"time":"2024-01-03T11:01:00.123+08:00"}
    /// ```
    pub fn from_millis() -> Self {
        Self {format: chrono::SecondsFormat::Millis}
    }

    /// Microsecond-level precision
    ///
    /// ```json,no_run
    /// {"time":"2024-01-03T11:01:00.123456+08:00"}
    /// ```
    pub fn from_micros() -> Self {
        Self {format: chrono::SecondsFormat::Micros}
    }

    /// Nanosecond-level precision
    ///
    /// ```json,no_run
    /// {"time":"2024-01-03T11:01:00.123456789+08:00"}
    /// ```
    pub fn from_nanos() -> Self {
        Self {format: chrono::SecondsFormat::Nanos}
    }
}

impl Plugin for TimePlugin {
    #[inline]
    fn pre(&self, record: &mut Record) -> bool {
        let now = chrono::Local::now();
        record.append("time", &now.to_rfc3339_opts(self.format, false));
        true
    }
}

/// Represent a stack trace frame
#[derive(Debug, Default, Clone)]
pub struct StackFrame {
    /// function name
    pub funcname: String,

    /// file name
    pub filename: String,

    /// line number
    pub lineno: u32,
}

impl Encode for StackFrame {
    #[inline]
    fn encode(&self, buf: &mut Vec<u8>) {
        buf.push(b'{');

        "funcname".encode(buf);
        buf.push(b':');
        self.funcname.encode(buf);
        buf.push(b',');

        "filename".encode(buf);
        buf.push(b':');
        self.filename.encode(buf);
        buf.push(b',');

        "lineno".encode(buf);
        buf.push(b':');
        self.lineno.encode(buf);

        buf.push(b'}');
    }
}

/// Add a stack trace to a record
///
/// Note that this plugin disregards frames internal to Rust and this crate.
///
/// ```json,no_run
/// {"stack":[{"funcname":"hello_world::main::h95297a3226de826e","filename":"/logkit/examples/hello_world.rs","lineno":9}]}
/// ```
pub struct StackPlugin {
    /// logs equal to this level will include a stack trace
    pub level: Level,
}

impl StackPlugin {
    /// Create from level
    pub fn from_level(level: Level) -> Self {
        Self {level}
    }
}

impl Plugin for StackPlugin {
    fn post(&self, record: &mut Record) -> bool {
        if record.level() != self.level {
            return true;
        }

        match std::env::var("RUST_BACKTRACE") {
            Ok(val) if val != "0" => {}
            _ => return true,
        }

        let mut frames = vec![];

        // todo pretty
        backtrace::trace(|frame| {
            backtrace::resolve_frame(frame, |symbol| {
                if let (Some(funcname), Some(filename), Some(lineno)) = (symbol.name(), symbol.filename(), symbol.lineno()) {
                    let funcname = funcname.to_string();
                    let filename = filename.to_string_lossy().to_string();

                    if filename.starts_with("/rustc/") ||
                        funcname.starts_with("backtrace::") ||
                        funcname.starts_with(concat!(env!("CARGO_PKG_NAME"), "::")) ||
                        funcname.starts_with(concat!("<", env!("CARGO_PKG_NAME"))) {
                        return;
                    }

                    frames.push(StackFrame {funcname, filename, lineno});
                }
            });

            true
        });

        record.append("stack", &frames);

        true
    }
}