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
use anyhow::{bail, Context};
use serde::{de, ser::SerializeSeq, Deserialize, Serialize};
use std::{ops::AddAssign, str::FromStr};
use tracing_subscriber::{filter::Directive, EnvFilter};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoggingSettings {
pub levels: DirectiveWrapper,
pub format: LogFormat,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum LogFormat {
Pretty,
MozLog,
Compact,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DirectiveWrapper(Vec<String>);
impl Serialize for DirectiveWrapper {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut seq = serializer.serialize_seq(Some(self.0.len()))?;
for directive in &self.0 {
seq.serialize_element(&directive)?;
}
seq.end()
}
}
impl<'de> Deserialize<'de> for DirectiveWrapper {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct Visitor;
impl<'de> de::Visitor<'de> for Visitor {
type Value = DirectiveWrapper;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(formatter, "directive or list of directives")
}
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
s.parse().map_err(|_err| {
de::Error::invalid_value(de::Unexpected::Str(s), &"valid directive")
})
}
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: de::SeqAccess<'de>,
{
let mut rv = DirectiveWrapper(vec![]);
while let Some(item) = seq.next_element::<String>()? {
let parsed: DirectiveWrapper = item.parse().map_err(|err: anyhow::Error| {
de::Error::invalid_value(
de::Unexpected::Str(&item),
&err.to_string().as_str(),
)
})?;
rv += parsed;
}
Ok(rv)
}
}
let mut rv = deserializer.deserialize_any(Visitor)?;
if let Some(rust_log) = std::option_env!("RUST_LOG") {
let from_env: DirectiveWrapper = rust_log.parse().map_err(|_err| {
de::Error::invalid_value(de::Unexpected::Str(rust_log), &"valid directive")
})?;
rv += from_env;
}
Ok(rv)
}
}
impl FromStr for DirectiveWrapper {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let parts: Vec<String> = s.split(',').map(|s| s.to_string()).collect();
if let Some(err) = parts.iter().find_map(|p| p.parse::<Directive>().err()) {
return Err(err).context("valid syntax");
}
if parts.iter().any(|p| p.contains('-')) {
bail!("log targets must not include hyphens");
}
Ok(Self(parts))
}
}
impl AddAssign for DirectiveWrapper {
fn add_assign(&mut self, rhs: Self) {
self.0.extend(rhs.0)
}
}
impl From<&DirectiveWrapper> for EnvFilter {
fn from(val: &DirectiveWrapper) -> Self {
let mut rv = EnvFilter::default();
for directive in &val.0 {
rv = rv.add_directive(directive.parse().unwrap());
}
rv
}
}