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
use log;
#[cfg(feature = "specfile")]
use notify;
#[cfg(feature = "specfile")]
use toml;
use std::fmt;
use std::io;
use std::error::Error;

/// Describes errors in the initialization of `flexi_logger`.
#[derive(Debug)]
pub enum FlexiLoggerError {
    /// Log file cannot be written because the specified path is not a directory.
    BadDirectory,
    /// Log cannot be written because the configured output directory is not accessible.
    Io(io::Error),
    /// Error with fs-notifications for the specfile
    #[cfg(feature = "specfile")]
    Notify(notify::Error),
    /// The configured logspec file cannot be read
    #[cfg(feature = "specfile")]
    Toml(toml::de::Error),
    /// Some error occured during parsing
    Parse(String),
    /// 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),
            #[cfg(feature = "specfile")]
            FlexiLoggerError::Notify(ref err) => err.fmt(f),
            #[cfg(feature = "specfile")]
            FlexiLoggerError::Toml(ref err) => err.fmt(f),
            FlexiLoggerError::Parse(ref s) => f.write_str(s),
            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"
            #[cfg(feature = "specfile")]
            FlexiLoggerError::Notify(ref err) => err.description(), // "Log cannot be written"
            #[cfg(feature = "specfile")]
            FlexiLoggerError::Toml(ref err) => err.description(), // "Log cannot be written"
            FlexiLoggerError::Parse(_) => "Error during parsing",
            FlexiLoggerError::Log(ref err) => err.description(), // "Logger initialization failed"
        }
    }

    fn cause(&self) -> Option<&Error> {
        match *self {
            FlexiLoggerError::BadDirectory | FlexiLoggerError::Parse(_) => None,
            FlexiLoggerError::Io(ref err) => Some(err),
            #[cfg(feature = "specfile")]
            FlexiLoggerError::Notify(ref err) => Some(err),
            #[cfg(feature = "specfile")]
            FlexiLoggerError::Toml(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)
    }
}
#[cfg(feature = "specfile")]
impl From<toml::de::Error> for FlexiLoggerError {
    fn from(err: toml::de::Error) -> FlexiLoggerError {
        FlexiLoggerError::Toml(err)
    }
}
#[cfg(feature = "specfile")]
impl From<notify::Error> for FlexiLoggerError {
    fn from(err: notify::Error) -> FlexiLoggerError {
        FlexiLoggerError::Notify(err)
    }
}