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
//! Module providing the TermLogger Implementation

use log::{LogLevel, LogLevelFilter, LogMetadata, LogRecord, SetLoggerError, set_logger, Log};
use term;
use term::{StderrTerminal, StdoutTerminal, Terminal, color};
use std::error;
use std::fmt;
use std::sync::{Mutex, MutexGuard};
use std::io::{Write, Error};

use self::TermLogError::{SetLogger, Term};
use super::logging::*;

use ::{Config, SharedLogger};

/// TermLogger error type.
///
/// TermLogger initialization might also fail if stdout or stderr could not be opened,
/// e.g. when no tty is attached to the process, it runs detached in the background, etc
/// This is represented by the `Term` Kind
#[derive(Debug)]
pub enum TermLogError {
    SetLogger(SetLoggerError),
    Term,
}

impl fmt::Display for TermLogError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use std::error::Error as FmtError;

        write!(f, "{}", self.description())
    }
}

impl error::Error for TermLogError {
    fn description(&self) -> &str {
        match * self {
            SetLogger(ref err) => err.description(),
            Term => "A terminal could not be opened",
        }
    }

    fn cause(&self) -> Option<&error::Error> {
        match *self {
            SetLogger(ref err) => Some(err),
            Term => None,
        }
    }
}

impl From<SetLoggerError> for TermLogError {
    fn from(error: SetLoggerError) -> Self {
        SetLogger(error)
    }
}

/// The TermLogger struct. Provides a stderr/out based Logger implementation
///
/// Supports colored output
pub struct TermLogger {
    level: LogLevelFilter,
    config: Config,
    stderr: Mutex<Box<StderrTerminal>>,
    stdout: Mutex<Box<StdoutTerminal>>,
}

impl TermLogger
{
    /// init function. Globally initializes the TermLogger as the one and only used log facility.
    ///
    /// Takes the desired `LogLevel` and `Config` as arguments. They cannot be changed later on.
    /// Fails if another Logger was already initialized.
    ///
    /// # Examples
    /// ```
    /// # extern crate simplelog;
    /// # use simplelog::*;
    /// # fn main() {
    /// let _ = TermLogger::init(LogLevelFilter::Info, Config::default());
    /// # }
    /// ```
    pub fn init(log_level: LogLevelFilter, config: Config) -> Result<(), TermLogError> {
        let logger = try!(TermLogger::new(log_level, config).ok_or(Term));
        try!(set_logger(|max_log_level| {
            max_log_level.set(log_level.clone());
            logger
        }));
        Ok(())
    }

    /// allows to create a new logger, that can be independently used, no matter whats globally set.
    ///
    /// no macros are provided for this case and you probably
    /// dont want to use this function, but `init()``, if you dont want to build a `CombinedLogger`.
    ///
    /// Takes the desired `LogLevel` and `Config` as arguments. They cannot be changed later on.
    ///
    /// # Examples
    /// ```
    /// # extern crate simplelog;
    /// # use simplelog::*;
    /// # fn main() {
    /// let term_logger = TermLogger::new(LogLevelFilter::Info, Config::default()).unwrap();
    /// # }
    /// ```
    pub fn new(log_level: LogLevelFilter, config: Config) -> Option<Box<TermLogger>> {
        term::stderr().and_then(|stderr|
            term::stdout().map(|stdout| {
                Box::new(TermLogger { level: log_level, config: config, stderr: Mutex::new(stderr), stdout: Mutex::new(stdout) })
            })
        )
    }

    fn try_log_term<W>(&self, record: &LogRecord, mut term_lock: MutexGuard<Box<Terminal<Output=W> + Send>>) -> Result<(), Error>
        where W: Write + Sized
    {
        let color = match record.level() {
            LogLevel::Error => color::RED,
            LogLevel::Warn => color::YELLOW,
            LogLevel::Info => color::BLUE,
            LogLevel::Debug => color::CYAN,
            LogLevel::Trace => color::WHITE
        };

        if self.config.time >= record.level() {
            try!(write_time(&mut *term_lock));
        }

        if self.config.level >= record.level() {
            try!(term_lock.fg(color));
            try!(write_level(record, &mut *term_lock));
            try!(term_lock.reset());
        }

        if self.config.target >= record.level() {
            try!(write_target(record, &mut *term_lock));
        }

        if self.config.location >= record.level() {
            try!(write_location(record, &mut *term_lock));
        }

        try!(write_args(record, &mut *term_lock));
        try!(term_lock.flush());
        Ok(())
    }

    fn try_log(&self, record: &LogRecord) -> Result<(), Error> {
        if self.enabled(record.metadata()) {
            if record.level() == LogLevel::Error {
                self.try_log_term(record, self.stderr.lock().unwrap())
            } else {
                self.try_log_term(record, self.stdout.lock().unwrap())
            }
        } else {
            Ok(())
        }
    }
}

impl Log for TermLogger
{
    fn enabled(&self, metadata: &LogMetadata) -> bool {
        metadata.level() <= self.level
    }

    fn log(&self, record: &LogRecord) {
        let _ = self.try_log(record);
    }
}

impl SharedLogger for TermLogger
{
    fn level(&self) -> LogLevelFilter {
        self.level
    }

    fn config(&self) -> Option<&Config>
    {
        Some(&self.config)
    }

    fn as_log(self: Box<Self>) -> Box<Log> {
        Box::new(*self)
    }
}