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
use std::io;
use std::sync;
use std::error;
use std::convert;
use std::fmt;

use log;

/// Error that may occur within logging
#[derive(Debug)]
pub enum LogError {
    /// IO Error
    Io(io::Error),
    /// Poison error - this will only occur within fern logger implementations if write!() panics.
    Poison(String),
}

impl convert::From<io::Error> for LogError {
    fn from(error: io::Error) -> LogError {
        LogError::Io(error)
    }
}

impl <T> convert::From<sync::PoisonError<T>> for LogError {
    fn from(error: sync::PoisonError<T>) -> LogError {
        LogError::Poison(format!("{}", error))
    }
}

impl error::Error for LogError {
    fn description(&self) -> &str {
        match self {
            &LogError::Io(..) => "IO error while logging",
            &LogError::Poison(..) => "lock within logger poisoned",
        }
    }

    fn cause(&self) -> Option<&error::Error> {
        match self {
            &LogError::Io(ref e) => Some(e),
            &LogError::Poison(..) => None,
        }
    }
}

impl fmt::Display for LogError {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        match self {
            &LogError::Io(ref e) => write!(f, "IO Error: {}", e),
            &LogError::Poison(ref e) => write!(f, "Poison Error: {}", e),
        }
    }
}

/// Error that may occur within init_global_logger()
#[derive(Debug)]
pub enum InitError {
    /// IO Error - this will only occur within fern logger implementations when opening files
    Io(io::Error),
    /// SetLoggerError - this occurs if the log crate has already been initialized when
    /// init_global_logger() is called.
    SetLoggerError(log::SetLoggerError),
}

impl convert::From<io::Error> for InitError {
    fn from(error: io::Error) -> InitError {
        InitError::Io(error)
    }
}

impl convert::From<log::SetLoggerError> for InitError {
    fn from(error: log::SetLoggerError) -> InitError {
        InitError::SetLoggerError(error)
    }
}

impl fmt::Display for InitError {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        match self {
            &InitError::Io(ref e) => write!(f, "IO Error: {}", e),
            &InitError::SetLoggerError(ref e) => write!(f, "SetLoggerError: {}", e),
        }
    }
}

impl error::Error for InitError {
    fn description(&self) -> &str {
        match self {
            &InitError::Io(..) => "IO error while initializing",
            &InitError::SetLoggerError(..) => "global logger already initialized",
        }
    }

    fn cause(&self) -> Option<&error::Error> {
        match self {
            &InitError::Io(ref e) => Some(e),
            &InitError::SetLoggerError(ref e) => Some(e),
        }
    }
}