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
//! `simplelog`-based logging subsystem

use simplelog::{self, CombinedLogger, LevelFilter, TermLogger};

use error::FrameworkError;

/// Logging configuration
// TODO: make things configurable via this newtype
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct LoggingConfig {
    level_filter: LevelFilter,
    simplelog_config: simplelog::Config,
}

impl LoggingConfig {
    /// Create a new LoggingConfig object with verbose logging
    pub fn verbose() -> Self {
        LevelFilter::Debug.into()
    }
}

impl Default for LoggingConfig {
    fn default() -> Self {
        LevelFilter::Info.into()
    }
}

impl From<LevelFilter> for LoggingConfig {
    fn from(level_filter: LevelFilter) -> LoggingConfig {
        Self {
            level_filter,
            simplelog_config: Default::default(),
        }
    }
}

/// Initialize the logging subsystem (i.e. simplelog) using this configuration
pub fn init(config: LoggingConfig) -> Result<(), FrameworkError> {
    let LoggingConfig {
        level_filter,
        simplelog_config,
    } = config;

    if let Some(logger) = TermLogger::new(level_filter, simplelog_config) {
        CombinedLogger::init(vec![logger]).unwrap()
    } // TODO: handle the case we don't get the logger?

    Ok(())
}