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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
use LogLevelFilter;
use regex::Regex;
use std::env;

/// Immutable struct that defines which loglines are to be written,
/// based on the module, the log level, and the text.
///
/// The loglevel specification via string (relevant for methods parse() and env())
/// works essentially like with env_logger,
/// but we are a bit more tolerant with spaces. Its functionality can be
/// described with some Backus-Naur-form:
///
/// ```text
/// <log_level_spec> ::= single_log_level_spec[{,single_log_level_spec}][/<text_filter>]
/// <single_log_level_spec> ::= <path_to_module>|<log_level>|<path_to_module>=<log_level>
/// <text_filter> ::= <regex>
/// ```
///
/// * If you just specify the module, without log_level, all levels will be traced for this module.
/// * If you just specify a log level, this will be applied as default to all modules without
///   explicit log level assigment.
///   (You see that for modules named error, warn, info, debug or trace,
///   it is necessary to specify their loglevel explicit).
/// * The module names are compared as Strings, with the side effect that a specified module filter
///   affects all modules whose name starts with this String.<br>
///   Example: "foo" affects e.g.
///
///   * foo
///   * foo::bar
///   * foobaz (!)
///   * foobaz::bar (!)
///
/// The optional text filter is applied for all modules.
///
/// Note that external module names are to be specified like in "extern crate ...".
/// For crates with a dash in their name this means: the dash is to be replaced with
/// the underscore (e.g. karl_heinz, not karl-heinz).
pub struct LogSpecification {
    module_filters: Vec<ModuleFilter>,
    textfilter: Option<Regex>,
}

/// Defines which loglevel filter to use for a given module (or as default, if no module is given)
pub struct ModuleFilter {
    pub module_name: Option<String>,
    pub level_filter: LogLevelFilter,
}

impl LogSpecification {
    /// Returns a log specification from a String
    /// (e.g: "crate1, crate2::mod_a, crate3::mod_x = error /foo").
    pub fn parse(spec: &str) -> LogSpecification {
        let mut dirs = Vec::<ModuleFilter>::new();

        let mut parts = spec.split('/');
        let mods = parts.next();
        let filter = parts.next();
        if parts.next().is_some() {
            print_err!("warning: invalid logging spec '{}', ignoring it (too many '/'s)", spec);
            return LogSpecification::default(LogLevelFilter::Off).finalize();
        }
        mods.map(|m| {
            for s in m.split(',') {
                let s = s.trim();
                if s.len() == 0 {
                    continue;
                }
                let mut parts = s.split('=');
                let (log_level, name) = match (parts.next(),
                                               parts.next().map(|s| s.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.trim().parse() {
                            Ok(num) => (num, None),
                            Err(_) => (LogLevelFilter::max(), Some(part0)),
                        }
                    }
                    (Some(part0), Some(""), None) => (LogLevelFilter::max(), Some(part0)),
                    (Some(part0), Some(part1), None) => {
                        match part1.trim().parse() {
                            Ok(num) => (num, Some(part0.trim())),
                            _ => {
                                print_err!("warning: invalid part in logging spec '{}', ignoring \
                                            it",
                                           part1);
                                continue;
                            }
                        }
                    }
                    _ => {
                        print_err!("warning: invalid part in logging spec '{}', ignoring it", s);
                        continue;
                    }
                };
                dirs.push(ModuleFilter {
                    module_name: name.map(|s| s.to_string()),
                    level_filter: log_level,
                });
            }
        });

        let textfilter = filter.map_or(None, |filter| match Regex::new(filter) {
            Ok(re) => Some(re),
            Err(e) => {
                print_err!("warning: invalid regex filter - {}", e);
                None
            }
        });

        LogSpecification {
            module_filters: dirs.level_sort(),
            textfilter: textfilter,
        }
    }

    /// Returns a log specification based on the value of the environment variable RUST_LOG,
    /// or an empty one.
    pub fn env() -> LogSpecification {
        match env::var("RUST_LOG") {
            Ok(spec) => LogSpecification::parse(&spec),
            Err(..) => LogSpecification::default(LogLevelFilter::Off).finalize(),
        }
    }

    /// Creates a LogSpecBuilder, setting the default log level.
    pub fn default(llf: LogLevelFilter) -> LogSpecBuilder {
        LogSpecBuilder {
            module_filters: vec![ModuleFilter {
                                     module_name: None,
                                     level_filter: llf,
                                 }],
        }
    }

    /// Provides a reference to the module filters.
    pub fn module_filters(&self) -> &Vec<ModuleFilter> {
        &self.module_filters
    }

    /// Provides a reference to the text filter.
    pub fn text_filter(&self) -> &Option<Regex> {
        &self.textfilter
    }
}

/// Builder for LogSpecification.
pub struct LogSpecBuilder {
    module_filters: Vec<ModuleFilter>,
}

impl LogSpecBuilder {
    /// Adds a log level filter for a module.
    pub fn module(&mut self, module_name: &str, lf: LogLevelFilter) -> &mut LogSpecBuilder {
        self.module_filters.push(ModuleFilter {
            module_name: Some(module_name.to_string()),
            level_filter: lf,
        });
        self
    }

    /// Creates a log specification without text filter.
    pub fn finalize(self) -> LogSpecification {
        LogSpecification {
            module_filters: self.module_filters.level_sort(),
            textfilter: None,
        }
    }

    /// Creates a log specification with text filter.
    pub fn finalize_with_textfilter(self, tf: Regex) -> LogSpecification {
        LogSpecification {
            module_filters: self.module_filters.level_sort(),
            textfilter: Some(tf),
        }
    }
}

trait LevelSort {
    fn level_sort(self) -> Vec<ModuleFilter>;
}
impl LevelSort for Vec<ModuleFilter> {
    /// Sort the module filters by length of their name,
    /// this allows a little more efficient lookup at runtime.
    fn level_sort(mut self) -> Vec<ModuleFilter> {
        self.sort_by(|a, b| {
            // let alen = a.module_name.as_ref().map(|a| a.len()).unwrap_or(0);
            // let blen = b.module_name.as_ref().map(|b| b.len()).unwrap_or(0);
            // alen.cmp(&blen)
            a.module_name.cmp(&b.module_name)
        });
        self
    }
}


#[cfg(test)]
mod tests {
    extern crate log;
    use LogSpecification;
    use log::LogLevelFilter;

    #[test]
    fn parse_logging_spec_valid() {
        let spec = LogSpecification::parse("crate1::mod1=error,crate1::mod2,crate2=debug");
        assert_eq!(spec.module_filters().len(), 3);
        assert_eq!(spec.module_filters()[0].module_name, Some("crate1::mod1".to_string()));
        assert_eq!(spec.module_filters()[0].level_filter, LogLevelFilter::Error);

        assert_eq!(spec.module_filters()[1].module_name, Some("crate1::mod2".to_string()));
        assert_eq!(spec.module_filters()[1].level_filter, LogLevelFilter::max());

        assert_eq!(spec.module_filters()[2].module_name, Some("crate2".to_string()));
        assert_eq!(spec.module_filters()[2].level_filter, LogLevelFilter::Debug);

        assert!(spec.text_filter().is_none());
    }

    #[test]
    fn parse_logging_spec_invalid_crate() {
        // test parse_logging_spec with multiple = in specification
        let spec = LogSpecification::parse("crate1::mod1=warn=info,crate2=debug");
        assert_eq!(spec.module_filters().len(), 1);
        assert_eq!(spec.module_filters()[0].module_name, Some("crate2".to_string()));
        assert_eq!(spec.module_filters()[0].level_filter, LogLevelFilter::Debug);
        assert!(spec.text_filter().is_none());
    }

    #[test]
    fn parse_logging_spec_invalid_log_level() {
        // test parse_logging_spec with 'noNumber' as log level
        let spec = LogSpecification::parse("crate1::mod1=noNumber,crate2=debug");
        assert_eq!(spec.module_filters().len(), 1);
        assert_eq!(spec.module_filters()[0].module_name, Some("crate2".to_string()));
        assert_eq!(spec.module_filters()[0].level_filter, LogLevelFilter::Debug);
        assert!(spec.text_filter().is_none());
    }

    #[test]
    fn parse_logging_spec_string_log_level() {
        // test parse_logging_spec with 'warn' as log level
        let spec = LogSpecification::parse("crate1::mod1=wrong, crate2=warn");
        assert_eq!(spec.module_filters().len(), 1);
        assert_eq!(spec.module_filters()[0].module_name, Some("crate2".to_string()));
        assert_eq!(spec.module_filters()[0].level_filter, LogLevelFilter::Warn);
        assert!(spec.text_filter().is_none());
    }

    #[test]
    fn parse_logging_spec_empty_log_level() {
        // test parse_logging_spec with '' as log level
        let spec = LogSpecification::parse("crate1::mod1=wrong, crate2=");
        assert_eq!(spec.module_filters().len(), 1);
        assert_eq!(spec.module_filters()[0].module_name, Some("crate2".to_string()));
        assert_eq!(spec.module_filters()[0].level_filter, LogLevelFilter::max());
        assert!(spec.text_filter().is_none());
    }

    #[test]
    fn parse_logging_spec_global() {
        // test parse_logging_spec with no crate
        let spec = LogSpecification::parse("warn,crate2=debug");
        assert_eq!(spec.module_filters().len(), 2);
        assert_eq!(spec.module_filters()[0].module_name, None);
        assert_eq!(spec.module_filters()[0].level_filter, LogLevelFilter::Warn);
        assert_eq!(spec.module_filters()[1].module_name, Some("crate2".to_string()));
        assert_eq!(spec.module_filters()[1].level_filter, LogLevelFilter::Debug);
        assert!(spec.text_filter().is_none());
    }

    #[test]
    fn parse_logging_spec_valid_filter() {
        let spec = LogSpecification::parse(" crate1::mod1 = error , crate1::mod2,crate2=debug/abc");
        assert_eq!(spec.module_filters().len(), 3);
        assert_eq!(spec.module_filters()[0].module_name, Some("crate1::mod1".to_string()));
        assert_eq!(spec.module_filters()[0].level_filter, LogLevelFilter::Error);

        assert_eq!(spec.module_filters()[1].module_name, Some("crate1::mod2".to_string()));
        assert_eq!(spec.module_filters()[1].level_filter, LogLevelFilter::max());

        assert_eq!(spec.module_filters()[2].module_name, Some("crate2".to_string()));
        assert_eq!(spec.module_filters()[2].level_filter, LogLevelFilter::Debug);
        assert!(spec.text_filter().is_some() &&
                spec.text_filter().as_ref().unwrap().to_string() == "abc");
    }

    #[test]
    fn parse_logging_spec_invalid_crate_filter() {
        let spec = LogSpecification::parse("crate1::mod1=error=warn,crate2=debug/a.c");
        assert_eq!(spec.module_filters().len(), 1);
        assert_eq!(spec.module_filters()[0].module_name, Some("crate2".to_string()));
        assert_eq!(spec.module_filters()[0].level_filter, LogLevelFilter::Debug);
        assert!(spec.text_filter().is_some() &&
                spec.text_filter().as_ref().unwrap().to_string() == "a.c");
    }

    #[test]
    fn parse_logging_spec_empty_with_filter() {
        let spec = LogSpecification::parse("crate1/a*c");
        assert_eq!(spec.module_filters().len(), 1);
        assert_eq!(spec.module_filters()[0].module_name, Some("crate1".to_string()));
        assert_eq!(spec.module_filters()[0].level_filter, LogLevelFilter::max());
        assert!(spec.text_filter().is_some() &&
                spec.text_filter().as_ref().unwrap().to_string() == "a*c");
    }
}