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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// Copyright 2017 Dmytro Milinevskyi <dmilinevskyi@gmail.com>

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! The logger spec might also be either [env_logger](https://doc.rust-lang.org/log/env_logger) spec or
//! a JSON string which defines global and per-module log level.
//!
//! # Simple env_logger spec
//!
//! The [env_logger](https://doc.rust-lang.org/log/env_logger/#enabling-logging) spec defines
//! the way to set global logging level or on per module basis.
//!
//! The spec string has a form of:
//!
//! ```ignore
//! [level],[module=[level]],...
//! ```
//!
//! The level must be a valid logging [level](levels/enum.LogLevel.html) level in the range
//! from [TRACE](levels/enum.LogLevel.html) to [CRITICAL](levels/enum.LogLevel.html).
//!
//! If `level` doesn't match any known logging level it's treated as a module path.
//!
//! In this case if `level` is not specified the log level is set to [TRACE](levels/enum.LogLevel.html).
//!
//! # Extended JSON spec
//!
//! The JSON logging spec allows to specify a fine grained logging settings.
//!
//! It doesn't suffer from the issue when module or crate path match the logging level.
//!
//! Also it allows to define logging level for the ranges of lines and potentially define other
//! features supported by `woodpecker`.
//!
//! The definition of the JSON spec:
//!
//! ```json
//! {
//!     "level": "<global logging level>",
//!     "modules": [
//!         {
//!             "path": "<path to the module>",
//!             "level": "<module logging level>"
//!         },
//!         {
//!             "path": "<path to the module>",
//!             "lines": [[<from>, <to>], ...]
//!         },
//!         ...
//!     ]
//! }
//! ```
//!
//! If the global level is not specified then it's left untouched.
//!
//! The `path` field of the module object is mandatory while
//! the `level` and `lines` fields are optional.
//!
//! If module `level` is not specified then it defaults to [TRACE](levels/enum.LogLevel.html).
//!
//! In case the ranges of lines is omitted the logging for the whole file is defined.
//!
//! See documentation for the [wp_set_level](../macro.wp_set_level.html)
//! for examples.


extern crate serde_json;
use self::serde_json::Value;

use std;

use levels::LogLevel;

#[doc(hidden)]
#[derive(PartialEq, PartialOrd, Clone, Debug)]
pub struct Module {
    pub path: String,
    pub level: LogLevel,
    pub lranges: Option<Vec<(u32, u32)>>,
}

impl Module {
    fn with_level(path: &str, level: LogLevel) -> Self {
        Module {
            path: path.to_string(),
            level: level,
            lranges: None,
        }
    }

    fn with_lranges(path: &str, level: LogLevel, lranges: Vec<(u32, u32)>) -> Self {
        Module {
            path: path.to_string(),
            level: level,
            lranges: Some(lranges),
        }
    }
}

#[doc(hidden)]
#[derive(PartialEq, PartialOrd, Clone, Debug)]
pub struct Root {
    pub level: Option<LogLevel>,
    pub modules: Vec<Module>,
}

impl Root {
    fn new() -> Self {
        Root {
            level: None,
            modules: Vec::new(),
        }
    }

    #[cfg(test)]
    fn with_level(level: LogLevel) -> Self {
        Root {
            level: Some(level),
            modules: Vec::new(),
        }
    }

    #[cfg(test)]
    fn module(mut self, module: Module) -> Self {
        self.modules.push(module);
        self
    }
}

/// JSON log spec parse failure.
#[derive(PartialEq, PartialOrd, Clone, Debug)]
pub enum JsonError {
    /// Invalid JSON string.
    Json,
    /// The root is invalid.
    Root,
    /// The array of modules is invalid.
    Module,
    /// The log level of the root is invalid.
    RootLogLevel,
    /// The log level of the module is invalid.
    ModuleLogLevel,
    /// The line range of the module is invalid.
    LineRange,
}

/// Generic log spec parse failure.
#[derive(PartialEq, PartialOrd, Clone, Debug)]
pub enum ParseError {
    /// Invalid spec.
    Spec,
    /// Invalid log level.
    LogLevel,
    /// JSON parse error.
    Json(JsonError),
}

impl ToString for ParseError {
    fn to_string(&self) -> String {
        format!("{:?}", self)
    }
}

fn parse_json(json: &str) -> Result<Root, ParseError> {
    let spec: Value = serde_json::from_str(json)
        .or(Err(ParseError::Json(JsonError::Json)))?;
    let spec = spec.as_object()
        .ok_or(ParseError::Json(JsonError::Root))?;
    if spec.is_empty() {
        return Err(ParseError::Json(JsonError::Root));
    }

    let mut root = Root::new();

    if let Some(level) = spec.get("level") {
        root.level = Some(
            level.as_str()
                .ok_or(ParseError::Json(JsonError::RootLogLevel))?.parse()
                .or(Err(ParseError::Json(JsonError::RootLogLevel)))?
        );
    }

    if let Some(modules) = spec.get("modules") {
        let modules = modules.as_array()
            .ok_or(ParseError::Json(JsonError::Module))?;
        if modules.is_empty() {
            return Err(ParseError::Json(JsonError::Module));
        }
        for module in modules {
            let module = module.as_object()
                .ok_or(ParseError::Json(JsonError::Module))?;
            let path = module.get("path")
                .ok_or(ParseError::Json(JsonError::Module))?.as_str()
                .ok_or(ParseError::Json(JsonError::Module))?;
            let level = if let Some(level) = module.get("level") {
                level.as_str()
                    .ok_or(ParseError::Json(JsonError::ModuleLogLevel))?.parse()
                    .or(Err(ParseError::Json(JsonError::ModuleLogLevel)))?
            } else {
                LogLevel::TRACE
            };

            let module = if let Some(lines) = module.get("lines") {
                let lines = lines.as_array()
                    .ok_or(ParseError::Json(JsonError::LineRange))?;
                let mut lranges = Vec::new();
                if lines.is_empty() {
                    return Err(ParseError::Json(JsonError::LineRange));
                }
                for line in lines {
                    let line = line.as_array()
                        .ok_or(ParseError::Json(JsonError::LineRange))?;
                    if line.len() != 2 {
                        return Err(ParseError::Json(JsonError::LineRange));
                    }
                    let from = line[0].as_u64()
                        .ok_or(ParseError::Json(JsonError::LineRange))?;
                    if from > std::u32::MAX as u64 {
                        return Err(ParseError::Json(JsonError::LineRange))
                    }
                    let to = line[1].as_u64()
                        .ok_or(ParseError::Json(JsonError::LineRange))?;
                    if to > std::u32::MAX as u64 {
                        return Err(ParseError::Json(JsonError::LineRange))
                    }
                    lranges.push((from as u32, to as u32));
                }
                Module::with_lranges(path, level, lranges)
            } else {
                Module::with_level(path, level)
            };
            root.modules.push(module);
        }
    }

    Ok(root)
}

fn parse_token(root: &mut Root, token: &str) -> Result<(), ParseError> {
    if token.is_empty() {
        return Err(ParseError::Spec);
    }

    let mut kv = token.split('=');
    let (k, v) = (kv.next(), kv.next());

    if kv.next().is_some() {
        return Err(ParseError::Spec);
    }

    if k.is_none() {
        return Err(ParseError::Spec);
    }
    let k = k.unwrap().trim();
    if k.is_empty() {
        return Err(ParseError::Spec);
    }

    if v.is_none() {
        // `k` is either global log level or path for which log level is `TRACE`
        if let Ok(level) = k.to_uppercase().parse() {
            root.level = Some(level);
        } else {
            root.modules.push(Module::with_level(k, LogLevel::TRACE));
        }
    } else {
        let v = v.unwrap().trim();
        if v.is_empty() {
            return Err(ParseError::Spec);
        }

        if let Ok(level) = v.to_uppercase().parse() {
            root.modules.push(Module::with_level(k, level));
        } else {
            return Err(ParseError::Spec);
        }
    }

    Ok(())
}

#[doc(hidden)]
pub fn parse(spec: &str) -> Result<Root, ParseError> {
    let spec = spec.trim();
    if spec.is_empty() {
        return Err(ParseError::Spec);
    }

    if spec.starts_with('{') {
        return parse_json(spec);
    }

    let mut root = Root::new();
    for token in spec.split(',') {
        parse_token(&mut root, token)?;
    }

    Ok(root)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_spec_invalid() {
        assert_eq!(Err(ParseError::Spec), parse(""));
        assert_eq!(Err(ParseError::Spec), parse(","));
        assert_eq!(Err(ParseError::Spec), parse("="));
        assert_eq!(Err(ParseError::Spec), parse("foo="));
        assert_eq!(Err(ParseError::Spec), parse("=error"));
        assert_eq!(Err(ParseError::Spec), parse("foo,"));
        assert_eq!(Err(ParseError::Spec), parse(",foo"));
        assert_eq!(Err(ParseError::Spec), parse("foo=bar"));
    }

    #[test]
    fn test_spec_global() {
        let expect = Root::with_level(LogLevel::ERROR);
        assert_eq!(Ok(expect), parse("error"));
    }

    #[test]
    fn test_spec_module() {
        let expect = Root::new()
            .module(Module::with_level("foo", LogLevel::ERROR));
        assert_eq!(Ok(expect), parse("foo=error"));

        let expect = Root::new()
            .module(Module::with_level("foo", LogLevel::TRACE));
        assert_eq!(Ok(expect), parse("foo"));
    }

    #[test]
    fn test_spec_combined() {
        let expect = Root::with_level(LogLevel::CRITICAL)
            .module(Module::with_level("foo", LogLevel::TRACE))
            .module(Module::with_level("bar", LogLevel::TRACE))
            .module(Module::with_level("qux", LogLevel::ERROR));
        assert_eq!(Ok(expect), parse("critical,foo,bar,qux=error"));
    }

    #[test]
    fn test_spec_json() {
        let expect = Root::with_level(LogLevel::CRITICAL)
            .module(Module::with_level("foo", LogLevel::TRACE))
            .module(Module::with_level("bar", LogLevel::TRACE))
            .module(Module::with_lranges("bar", LogLevel::ERROR,
                                       vec!((10, 100), (120, 130))));
        assert_eq!(Ok(expect), parse(r#"{
                                            "level": "critical",
                                            "modules": [
                                                {
                                                    "path": "foo"
                                                },
                                                {
                                                    "path": "bar"
                                                },
                                                {
                                                    "path": "bar",
                                                    "level": "error",
                                                    "lines": [
                                                        [10, 100], [120, 130]
                                                    ]
                                                }
                                            ]
                                        }"#));
    }

    #[test]
    fn test_spec_json_invalid() {
        assert_eq!(Err(ParseError::Json(JsonError::Json)), parse("{"));

        assert_eq!(Err(ParseError::Json(JsonError::Root)),
                   parse(r#"{}"#));

        assert_eq!(Err(ParseError::Json(JsonError::RootLogLevel)),
                   parse(r#"{"level": "foo"}"#));

        assert_eq!(Err(ParseError::Json(JsonError::Module)),
                   parse(r#"{"modules": {}}"#));
        assert_eq!(Err(ParseError::Json(JsonError::Module)),
                   parse(r#"{"modules": []}"#));
        assert_eq!(Err(ParseError::Json(JsonError::Module)),
                   parse(r#"{"modules": [{"level": "critical"}]}"#));

        assert_eq!(Err(ParseError::Json(JsonError::ModuleLogLevel)),
                   parse(r#"{"modules": [{"path": "bar", "level": "foo"}]}"#));

        assert_eq!(Err(ParseError::Json(JsonError::LineRange)),
                   parse(r#"{"modules": [{"path": "bar", "lines": {}}]}"#));
        assert_eq!(Err(ParseError::Json(JsonError::LineRange)),
                   parse(r#"{"modules": [{"path": "bar", "lines": []}]}"#));
        assert_eq!(Err(ParseError::Json(JsonError::LineRange)),
                   parse(r#"{"modules": [{"path": "bar", "lines": [[]]}]}"#));
        assert_eq!(Err(ParseError::Json(JsonError::LineRange)),
                   parse(r#"{"modules": [{"path": "bar", "lines": [[1]]}]}"#));
        assert_eq!(Err(ParseError::Json(JsonError::LineRange)),
                   parse(r#"{"modules": [{"path": "bar", "lines": [[1, 10, 20]]}]}"#));
    }
}