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
use serde::{Deserialize, Serialize};
use std::{fmt, str::FromStr};
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub struct Metadata {
#[serde(skip_serializing)]
level: Level,
package: &'static str,
#[serde(skip)]
module_path: &'static str,
file: &'static str,
}
impl Metadata {
pub const fn new(
level: Level,
target: &'static str,
module_path: &'static str,
source_path: &'static str,
) -> Self {
Self {
level,
package: target,
module_path,
file: source_path,
}
}
pub fn enabled(&self) -> bool {
crate::logger::enabled(self)
}
pub fn level(&self) -> Level {
self.level
}
pub fn target(&self) -> &'static str {
self.package
}
pub fn module_path(&self) -> &'static str {
self.module_path
}
pub fn source_path(&self) -> &'static str {
self.file
}
}
static LOG_LEVEL_NAMES: &[&str] = &["ERROR", "WARN", "INFO", "DEBUG", "TRACE"];
#[repr(usize)]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum Level {
Error = 0,
Warn,
Info,
Debug,
Trace,
}
impl Level {
fn from_usize(idx: usize) -> Option<Self> {
let lvl = match idx {
0 => Level::Error,
1 => Level::Warn,
2 => Level::Info,
3 => Level::Debug,
4 => Level::Trace,
_ => return None,
};
Some(lvl)
}
}
#[derive(Debug)]
pub struct LevelParseError;
impl FromStr for Level {
type Err = LevelParseError;
fn from_str(level: &str) -> Result<Level, Self::Err> {
LOG_LEVEL_NAMES
.iter()
.position(|name| name.eq_ignore_ascii_case(level))
.map(|idx| Level::from_usize(idx).unwrap())
.ok_or(LevelParseError)
}
}
impl fmt::Display for Level {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.pad(LOG_LEVEL_NAMES[*self as usize])
}
}