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
use {
    super::{Directive, Filter},
    crate::Diagnostics,
    miette::{Diagnostic, SourceSpan},
    std::{cmp, str::FromStr},
    thiserror::Error,
    tracing_core::LevelFilter,
};

impl Filter {
    /// Parse a filter from its string representation.
    ///
    /// Filter compilation can produce warnings even when it succeeds, thus
    /// the nonstandard return type to provide [`Diagnostics`] on success.
    pub fn parse(spec: &str) -> (Option<Filter>, Option<Diagnostics<'_>>) {
        // this code is adapted directly from env_logger 0.9.0
        // env_logger is licensed under MIT OR Apache-2.0

        let recover_span = |substr: &str| {
            let offset = substr.as_ptr() as usize - spec.as_ptr() as usize;
            offset..offset + substr.len()
        };

        let mut directives = Vec::new();
        let mut parts = spec.split('/');
        let dirs = parts.next();
        let regex = parts.next();

        if let Some(after) = parts.next() {
            let regex = recover_span(regex.unwrap());
            let after = recover_span(after);
            let error = Error::MultipleSlash {
                slash: (after.start - 1..after.start).into(),
                regex: (regex.start..spec.len()).into(),
            };
            return (
                None,
                Some(Diagnostics {
                    error: Some(Box::new(error)),
                    ignored: Vec::new(),
                    disabled: None,
                    source: spec.into(),
                }),
            );
        }

        let mut warnings = Vec::new();

        if let Some(dirs) = dirs {
            for dir in dirs.split(',').map(|dir| dir.trim()) {
                if dir.is_empty() {
                    continue;
                }
                let mut parts = dir.split('=');
                let (log_level, name) =
                    match (parts.next(), parts.next().map(str::trim), parts.next()) {
                        (Some(part0), None, None) => {
                            // if the single argument is a log-level string
                            // or number, treat that as a global fallback
                            match part0.parse() {
                                Ok(num) => (num, None),
                                Err(_) => (LevelFilter::TRACE, Some(part0)),
                            }
                        },
                        (Some(part0), Some(""), None) => (LevelFilter::TRACE, Some(part0)),
                        (Some(part0), Some(part1), None) => match part1.parse() {
                            Ok(num) => (num, Some(part0)),
                            _ => {
                                warnings.push(Warning::InvalidLevel {
                                    span: recover_span(part1).into(),
                                });
                                continue;
                            },
                        },
                        (Some(_part0), Some(part1), Some(_part2)) => {
                            let part1 = recover_span(part1);
                            let dir = recover_span(dir);
                            warnings.push(Warning::InvalidLevel {
                                span: (part1.start..dir.end).into(),
                            });
                            continue;
                        },
                        _ => unreachable!(),
                    };
                let directive = Directive {
                    target: name.map(Into::into),
                    level: log_level,
                };
                let ix = directives.binary_search_by(|x: &Directive| {
                    let a = x.target.as_ref().map(|x| x.len()).unwrap_or(0);
                    let b = directive.target.as_ref().map(|x| x.len()).unwrap_or(0);
                    match a.cmp(&b) {
                        cmp::Ordering::Equal => x.target.cmp(&directive.target),
                        ordering => ordering,
                    }
                });
                match ix {
                    Ok(ix) => directives[ix] = directive,
                    Err(ix) => directives.insert(ix, directive),
                }
            }
        }

        let regex = regex.and_then(|regex| match regex::Regex::new(regex) {
            Ok(regex) => Some(regex),
            Err(error) => {
                warnings.push(Warning::InvalidRegex {
                    error,
                    span: recover_span(regex).into(),
                });
                None
            },
        });

        let _ = regex; // mark used for cfg(not(feature = "regex"))
        let filter = Some(Filter { directives, regex });
        let report = if warnings.is_empty() {
            None
        } else {
            Some(Diagnostics {
                error: None,
                ignored: warnings
                    .into_iter()
                    .map(|x| Box::new(x) as Box<dyn Diagnostic + Send + Sync + 'static>)
                    .collect(),
                disabled: None,
                source: spec.into(),
            })
        };

        (filter, report)
    }
}

impl FromStr for Filter {
    type Err = Diagnostics<'static>;

    /// Parse a filter from its string representation, discarding warnings.
    fn from_str(spec: &str) -> Result<Self, Diagnostics<'static>> {
        let (filter, errs) = Self::parse(spec);
        filter.ok_or_else(|| {
            errs.expect("filter compilation failed without any diagnostics")
                .into_owned()
        })
    }
}

#[derive(Debug, Error, Diagnostic)]
#[diagnostic(severity(warning))]
enum Warning {
    #[error("invalid level filter specified")]
    #[diagnostic(help("valid level filters are OFF, ERROR, WARN, INFO, DEBUG, or TRACE"))]
    InvalidLevel {
        #[label]
        span: SourceSpan,
    },
    #[error("invalid regex specified")]
    #[diagnostic(code(tracing_filter::simple::InvalidRegex))]
    InvalidRegex {
        // no, we are not going to parse the formatted regex error
        // in order to translate it into miette span/labels
        // it'd be nice, but it's not worth the brittle hacks
        error: regex::Error,
        #[label("{}", .error)]
        span: SourceSpan,
    },
}

#[derive(Debug, Error, Diagnostic)]
#[diagnostic(severity(error))]
enum Error {
    #[error("logging spec has too many `/`s")]
    #[diagnostic(help("regex filters may not contain `/`"))]
    MultipleSlash {
        #[label("this `/` is not allowed ...")]
        slash: SourceSpan,
        #[label("... in this regex filter")]
        regex: SourceSpan,
    },
}