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
187
188
189
// Copyright 2016-2017 The Perceptia Project Developers
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
// and associated documentation files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

//! `Timber` is simple logger facility. It provides means to write logs to given file in concurrent
//! applications.
//!
//! `timber!` macro takes as argument level number and writes log only if it is greater than zero.
//! If user defines log levels as constants compiler will be able to ignore strings passed to unused
//! logs and make application smaller. This way user can keep set of debugging logs, but compile
//! them out for release.
//!
//! By default `timber` writes logs to `stdout`. To write to a file one have to pass file path with
//! `timber::init(path)`.
//!
//! Example wrapper for `timber` could look like:
//!
//! ```
//! #[macro_use(timber)]
//! use timber;
//!
//! #[cfg(debug)]
//! pub mod level {
//!     pub const ERR: i32 = 1;
//!     pub const DEB: i32 = 2;
//!     pub const INF: i32 = 7;
//! }
//!
//! #[cfg(not(debug))]
//! pub mod level {
//!     pub const ERR: i32 = 1;
//!     pub const DEB: i32 = 0;
//!     pub const INF: i32 = 3;
//! }
//!
//! macro_rules! log_err{($($arg:tt)*) => {timber!($crate::level::ERR, "ERR", $($arg)*)}}
//! macro_rules! log_deb{($($arg:tt)*) => {timber!($crate::level::DEB, "DEB", $($arg)*)}}
//! macro_rules! log_inf{($($arg:tt)*) => {timber!($crate::level::INF, "INF", $($arg)*)}}
//!
//! //log_err!("This is error! I'm visible!");
//! //log_deb!("I'm debug. I'm visible only in debug mode.");
//! ```

// -------------------------------------------------------------------------------------------------

extern crate time;

use std::sync::{Arc, Mutex, MutexGuard, PoisonError, Once, ONCE_INIT};
use std::io::Write;

// -------------------------------------------------------------------------------------------------

/// Prints timber (processed log). Timber prints time (with microseconds), name of current thread,
/// line number and module name + log text.
#[macro_export]
macro_rules! timber {
    ($lnum:expr, $lname:expr, $($arg:tt)*) => {
        if $lnum > 0 {
            $crate::timber($lname, module_path!(), line!(), format_args!($($arg)*))
        }
    };
}

// -------------------------------------------------------------------------------------------------

/// Timber struct - used as singleton.
pub struct Timber {
    log_file: Option<std::fs::File>,
}

// -------------------------------------------------------------------------------------------------

/// Wrapper for `Timber` struct ensuring thread safety.
struct Wrapper {
    inner: Arc<Mutex<Timber>>,
}

// -------------------------------------------------------------------------------------------------

impl Timber {
    /// Print not formated log.
    pub fn log(&mut self, args: std::fmt::Arguments) {
        match self.log_file {
            Some(ref mut log_file) => {
                log_file.write(format!("{}", args).as_bytes()).expect("Failed to log!");
            }
            None => {
                print!("{}", args);
            }
        }
    }

    /// Print formated log.
    pub fn timber(&mut self, level: &str, module: &str, line: u32, args: std::fmt::Arguments) {
        // Get local time
        let tm = time::now().to_local();

        // Get current thread name
        let current_thread = std::thread::current();
        let thread = current_thread.name().unwrap_or("<unknown>");

        // Format log entry
        let entry = format!("{:02}:{:02}:{:02}.{:06} | {} | {:16} | {:4} | {:40} | {}",
                            tm.tm_hour,
                            tm.tm_min,
                            tm.tm_sec,
                            tm.tm_nsec / 1000,
                            level,
                            thread,
                            line,
                            module,
                            args);

        // Write log entry
        match self.log_file {
            Some(ref mut log_file) => {
                log_file.write(entry.as_bytes()).expect("Failed to timber!");
                log_file.write("\n".as_bytes()).expect("Failed to timber!");
            }
            None => {
                println!("{}", entry);
            }
        }
    }

    /// Initialize logger by providing output log file. Before call to this method logs will be
    /// printed to standard output.
    pub fn init(&mut self, path: &std::path::Path) -> Result<(), std::io::Error> {
        self.log_file = Some(std::fs::File::create(path)?);
        Ok(())
    }
}

// -------------------------------------------------------------------------------------------------

/// Get instance of logger singleton.
fn get_instance() -> &'static Wrapper {
    static mut LOGGER: *const Wrapper = 0 as *const Wrapper;
    static ONCE: Once = ONCE_INIT;

    unsafe {
        ONCE.call_once(|| {
            let logger = Wrapper { inner: Arc::new(Mutex::new(Timber { log_file: None })) };

            LOGGER = std::mem::transmute(Box::new(logger));
        });

        &(*LOGGER)
    }
}

// -------------------------------------------------------------------------------------------------

/// Get locked instance of `Timber` for guarded loging.
pub fn lock<'a>() -> Result<MutexGuard<'a, Timber>, PoisonError<MutexGuard<'a, Timber>>> {
    get_instance().inner.lock()
}

// -------------------------------------------------------------------------------------------------

/// Print formated log.
pub fn timber(level: &str, module: &str, line: u32, args: std::fmt::Arguments) {
    let mut timber = get_instance().inner.lock().unwrap();
    timber.timber(level, module, line, args);
}

// -------------------------------------------------------------------------------------------------

/// Initialize logger by providing output log file. Before call to this method logs will be printed
/// to standard output.
pub fn init(path: &std::path::Path) -> Result<(), std::io::Error> {
    let mut timber = get_instance().inner.lock().unwrap();
    timber.init(path)
}

// -------------------------------------------------------------------------------------------------