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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
//! log4rs configuration

use std::collections::HashSet;
use std::fmt;
use std::iter::IntoIterator;
use std::error;
use log::LevelFilter;

use append::Append;
use filter::Filter;
use {ConfigPrivateExt, PrivateConfigAppenderExt};

/// Configuration for the root logger.
#[derive(Debug)]
pub struct Root {
    level: LevelFilter,
    appenders: Vec<String>,
}

impl Root {
    /// Creates a new `RootBuilder` with no appenders.
    pub fn builder() -> RootBuilder {
        RootBuilder { appenders: vec![] }
    }

    /// Returns the minimum level of log messages that the root logger will accept.
    pub fn level(&self) -> LevelFilter {
        self.level
    }

    /// Returns the list of names of appenders that will be attached to the root logger.
    pub fn appenders(&self) -> &[String] {
        &self.appenders
    }
}

/// A builder for `Root`s.
#[derive(Debug)]
pub struct RootBuilder {
    appenders: Vec<String>,
}

impl RootBuilder {
    /// Adds an appender.
    pub fn appender<T>(mut self, appender: T) -> RootBuilder
    where
        T: Into<String>,
    {
        self.appenders.push(appender.into());
        self
    }

    /// Adds appenders.
    pub fn appenders<I>(mut self, appenders: I) -> RootBuilder
    where
        I: IntoIterator,
        I::Item: Into<String>,
    {
        self.appenders.extend(appenders.into_iter().map(Into::into));
        self
    }

    /// Consumes the `RootBuilder`, returning the `Root`.
    pub fn build(self, level: LevelFilter) -> Root {
        Root {
            level: level,
            appenders: self.appenders,
        }
    }
}

/// Configuration for an appender.
#[derive(Debug)]
pub struct Appender {
    name: String,
    appender: Box<Append>,
    filters: Vec<Box<Filter>>,
}

impl Appender {
    /// Creates a new `AppenderBuilder` with the specified name and `Append` trait object.
    pub fn builder() -> AppenderBuilder {
        AppenderBuilder { filters: vec![] }
    }

    /// Returns the name of the appender.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Returns the appender.
    pub fn appender(&self) -> &Append {
        &*self.appender
    }

    /// Returns the filters attached to the appender.
    pub fn filters(&self) -> &[Box<Filter>] {
        &self.filters
    }
}

impl PrivateConfigAppenderExt for Appender {
    fn unpack(self) -> (String, Box<Append>, Vec<Box<Filter>>) {
        let Appender {
            name,
            appender,
            filters,
        } = self;
        (name, appender, filters)
    }
}

/// A builder for `Appender`s.
#[derive(Debug)]
pub struct AppenderBuilder {
    filters: Vec<Box<Filter>>,
}

impl AppenderBuilder {
    /// Adds a filter.
    pub fn filter(mut self, filter: Box<Filter>) -> AppenderBuilder {
        self.filters.push(filter);
        self
    }

    /// Adds filters.
    pub fn filters<I>(mut self, filters: I) -> AppenderBuilder
    where
        I: IntoIterator<Item = Box<Filter>>,
    {
        self.filters.extend(filters);
        self
    }

    /// Consumes the `AppenderBuilder`, returning the `Appender`.
    pub fn build<T>(self, name: T, appender: Box<Append>) -> Appender
    where
        T: Into<String>,
    {
        Appender {
            name: name.into(),
            appender: appender,
            filters: self.filters,
        }
    }
}

/// Configuration for a logger.
#[derive(Debug)]
pub struct Logger {
    name: String,
    level: LevelFilter,
    appenders: Vec<String>,
    additive: bool,
}

impl Logger {
    /// Creates a new `LoggerBuilder` with the specified name and level.
    ///
    /// There are initially no appenders attached and `additive` is `true`.
    pub fn builder() -> LoggerBuilder {
        LoggerBuilder {
            appenders: vec![],
            additive: true,
        }
    }

    /// Returns the name of the logger.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Returns the minimum level of log messages that the logger will accept.
    pub fn level(&self) -> LevelFilter {
        self.level
    }

    /// Returns the list of names of appenders that will be attached to the logger.
    pub fn appenders(&self) -> &[String] {
        &self.appenders
    }

    /// Determines if appenders of parent loggers will also be attached to this logger.
    pub fn additive(&self) -> bool {
        self.additive
    }
}

/// A builder for `Logger`s.
#[derive(Debug)]
pub struct LoggerBuilder {
    appenders: Vec<String>,
    additive: bool,
}

impl LoggerBuilder {
    /// Adds an appender.
    pub fn appender<T>(mut self, appender: T) -> LoggerBuilder
    where
        T: Into<String>,
    {
        self.appenders.push(appender.into());
        self
    }

    /// Adds appenders.
    pub fn appenders<I>(mut self, appenders: I) -> LoggerBuilder
    where
        I: IntoIterator,
        I::Item: Into<String>,
    {
        self.appenders.extend(appenders.into_iter().map(Into::into));
        self
    }

    /// Sets the additivity of the logger.
    pub fn additive(mut self, additive: bool) -> LoggerBuilder {
        self.additive = additive;
        self
    }

    /// Consumes the `LoggerBuilder`, returning the `Logger`.
    pub fn build<T>(self, name: T, level: LevelFilter) -> Logger
    where
        T: Into<String>,
    {
        Logger {
            name: name.into(),
            level: level,
            appenders: self.appenders,
            additive: self.additive,
        }
    }
}

/// A log4rs configuration.
#[derive(Debug)]
pub struct Config {
    appenders: Vec<Appender>,
    root: Root,
    loggers: Vec<Logger>,
}

impl Config {
    /// Creates a new `ConfigBuilder`.
    pub fn builder() -> ConfigBuilder {
        ConfigBuilder {
            appenders: vec![],
            loggers: vec![],
        }
    }

    /// Returns the `Appender`s associated with the `Config`.
    pub fn appenders(&self) -> &[Appender] {
        &self.appenders
    }

    /// Returns the `Root` associated with the `Config`.
    pub fn root(&self) -> &Root {
        &self.root
    }

    /// Returns the `Logger`s associated with the `Config`.
    pub fn loggers(&self) -> &[Logger] {
        &self.loggers
    }
}

/// A builder for `Config`s.
pub struct ConfigBuilder {
    appenders: Vec<Appender>,
    loggers: Vec<Logger>,
}

impl ConfigBuilder {
    /// Adds an appender.
    pub fn appender(mut self, appender: Appender) -> ConfigBuilder {
        self.appenders.push(appender);
        self
    }

    /// Adds appenders.
    pub fn appenders<I>(mut self, appenders: I) -> ConfigBuilder
    where
        I: IntoIterator<Item = Appender>,
    {
        self.appenders.extend(appenders);
        self
    }

    /// Adds a logger.
    pub fn logger(mut self, logger: Logger) -> ConfigBuilder {
        self.loggers.push(logger);
        self
    }

    /// Adds loggers.
    pub fn loggers<I>(mut self, loggers: I) -> ConfigBuilder
    where
        I: IntoIterator<Item = Logger>,
    {
        self.loggers.extend(loggers);
        self
    }

    /// Consumes the `ConfigBuilder`, returning the `Config`.
    ///
    /// Unlike `build`, this method will always return a `Config` by stripping
    /// portions of the configuration that are incorrect.
    pub fn build_lossy(self, mut root: Root) -> (Config, Vec<Error>) {
        let mut errors = vec![];

        let ConfigBuilder { appenders, loggers } = self;

        let mut ok_appenders = vec![];
        let mut appender_names = HashSet::new();
        for appender in appenders {
            if appender_names.insert(appender.name.clone()) {
                ok_appenders.push(appender);
            } else {
                errors.push(Error::DuplicateAppenderName(appender.name));
            }
        }

        let mut ok_root_appenders = vec![];
        for appender in root.appenders {
            if appender_names.contains(&appender) {
                ok_root_appenders.push(appender);
            } else {
                errors.push(Error::NonexistentAppender(appender));
            }
        }
        root.appenders = ok_root_appenders;

        let mut ok_loggers = vec![];
        let mut logger_names = HashSet::new();
        for mut logger in loggers {
            if !logger_names.insert(logger.name.clone()) {
                errors.push(Error::DuplicateLoggerName(logger.name));
                continue;
            }

            if let Err(err) = check_logger_name(&logger.name) {
                errors.push(err);
                continue;
            }

            let mut ok_logger_appenders = vec![];
            for appender in logger.appenders {
                if appender_names.contains(&appender) {
                    ok_logger_appenders.push(appender);
                } else {
                    errors.push(Error::NonexistentAppender(appender));
                }
            }
            logger.appenders = ok_logger_appenders;

            ok_loggers.push(logger);
        }

        let config = Config {
            appenders: ok_appenders,
            root: root,
            loggers: ok_loggers,
        };

        (config, errors)
    }

    /// Consumes the `ConfigBuilder`, returning the `Config`.
    pub fn build(self, root: Root) -> Result<Config, Errors> {
        let (config, errors) = self.build_lossy(root);
        if errors.is_empty() {
            Ok(config)
        } else {
            Err(Errors { errors: errors })
        }
    }
}

fn check_logger_name(name: &str) -> Result<(), Error> {
    if name.is_empty() {
        return Err(Error::InvalidLoggerName(name.to_owned()));
    }

    let mut streak = 0;
    for ch in name.chars() {
        if ch == ':' {
            streak += 1;
            if streak > 2 {
                return Err(Error::InvalidLoggerName(name.to_owned()));
            }
        } else {
            if streak > 0 && streak != 2 {
                return Err(Error::InvalidLoggerName(name.to_owned()));
            }
            streak = 0;
        }
    }

    if streak > 0 {
        Err(Error::InvalidLoggerName(name.to_owned()))
    } else {
        Ok(())
    }
}

impl ConfigPrivateExt for Config {
    fn unpack(self) -> (Vec<Appender>, Root, Vec<Logger>) {
        let Config {
            appenders,
            root,
            loggers,
        } = self;
        (appenders, root, loggers)
    }
}

/// Errors encountered when validating a log4rs `Config`.
#[derive(Debug)]
pub struct Errors {
    errors: Vec<Error>,
}

impl Errors {
    /// Returns a slice of `Error`s.
    pub fn errors(&self) -> &[Error] {
        &self.errors
    }
}

impl fmt::Display for Errors {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        for error in &self.errors {
            writeln!(fmt, "{}", error)?;
        }
        Ok(())
    }
}

impl error::Error for Errors {
    fn description(&self) -> &str {
        "Errors encountered when validating a log4rs `Config`"
    }
}

/// An error validating a log4rs `Config`.
#[derive(Debug)]
pub enum Error {
    /// Multiple appenders were registered with the same name.
    DuplicateAppenderName(String),
    /// A reference to a nonexistant appender.
    NonexistentAppender(String),
    /// Multiple loggers were registered with the same name.
    DuplicateLoggerName(String),
    /// A logger name was invalid.
    InvalidLoggerName(String),
    #[doc(hidden)] __Extensible,
}

impl fmt::Display for Error {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Error::DuplicateAppenderName(ref n) => write!(fmt, "Duplicate appender name `{}`", n),
            Error::NonexistentAppender(ref n) => {
                write!(fmt, "Reference to nonexistent appender: `{}`", n)
            }
            Error::DuplicateLoggerName(ref n) => write!(fmt, "Duplicate logger name `{}`", n),
            Error::InvalidLoggerName(ref n) => write!(fmt, "Invalid logger name `{}`", n),
            Error::__Extensible => unreachable!(),
        }
    }
}

impl error::Error for Error {
    fn description(&self) -> &str {
        "An error constructing a log4rs `Config`"
    }
}

#[cfg(test)]
mod test {
    #[test]
    fn check_logger_name() {
        let tests = [
            ("", false),
            ("asdf", true),
            ("asdf::jkl", true),
            ("::", false),
            ("asdf::jkl::", false),
            ("asdf:jkl", false),
            ("asdf:::jkl", false),
            ("asdf::jkl::", false),
        ];

        for &(ref name, expected) in &tests {
            assert!(
                expected == super::check_logger_name(name).is_ok(),
                "{}",
                name
            );
        }
    }
}