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
use log;
use std::fmt;
use std::io;
use std::error::Error;


/// Describes errors in the initialization of flexi_logger.
#[derive(Debug)]
pub enum FlexiLoggerError {
    /// "Log cannot be written, most likely due to permission errors on OS level"
    BadDirectory,
    /// "Log cannot be written because the configured output directory is not accessible"
    Io(io::Error),
    /// "Logger initialization failed"
    Log(log::SetLoggerError),
}


impl fmt::Display for FlexiLoggerError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            FlexiLoggerError::BadDirectory => Ok(()),
            FlexiLoggerError::Io(ref err) => err.fmt(f),
            FlexiLoggerError::Log(ref err) => err.fmt(f),
        }
    }
}

impl Error for FlexiLoggerError {
    fn description(&self) -> &str {
        match *self {
            FlexiLoggerError::BadDirectory => "not a directory", // ""
            FlexiLoggerError::Io(ref err) => err.description(),  // "Log cannot be written"
            FlexiLoggerError::Log(ref err) => err.description(), // "Logger initialization failed"
        }
    }

    fn cause(&self) -> Option<&Error> {
        match *self {
            FlexiLoggerError::BadDirectory => None,
            FlexiLoggerError::Io(ref err) => Some(err),
            FlexiLoggerError::Log(ref err) => Some(err),
        }
    }
}

impl From<log::SetLoggerError> for FlexiLoggerError {
    fn from(err: log::SetLoggerError) -> FlexiLoggerError {
        FlexiLoggerError::Log(err)
    }
}
impl From<io::Error> for FlexiLoggerError {
    fn from(err: io::Error) -> FlexiLoggerError {
        FlexiLoggerError::Io(err)
    }
}