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
//! # Logger
//!
//! Loggers are thread-safe and reference counted, so can be freely
//! passed around the code.
//!
//! Each logger is built with a set of key-values.
//!
//! Child loggers are build from existing loggers, and copy
//! all the key-values from their parents
//!
//! Loggers form hierarchies sharing a drain. Setting a drain on
//! any logger will change it on all loggers in given hierarchy.
use super::{DrainRef, OwnedKeyValue, Level, BorrowedKeyValue};
use std::sync::Arc;
use crossbeam::sync::ArcCell;

use drain;

use chrono;

struct LoggerInner {
    drain: DrainRef,
    values: Vec<OwnedKeyValue>,
}


#[derive(Clone)]
/// Logger
pub struct Logger {
    inner: Arc<LoggerInner>,
}

impl Logger {
    /// Build a root logger
    ///
    /// All children and their children and so on form one hierarchy
    /// sharing a common drain.
    ///
    /// Use `o!` macro to help build `values`
    ///
    /// ```
    /// #[macro_use]
    /// extern crate slog;
    ///
    /// fn main() {
    ///     let root = slog::Logger::new_root(o!("key1" => "value1", "key2" => "value2"));
    /// }
    pub fn new_root(values: &[OwnedKeyValue]) -> Logger {
        let drain = Arc::new(
            ArcCell::new(
                Arc::new(
                    Box::new(
                        drain::discard()
                        ) as Box<drain::Drain>
                    )
                )
            );
        Logger {
            inner: Arc::new(LoggerInner {
                drain: drain,
                values: values.to_vec(),
            }),
        }
    }

    /// Build a child logger
    ///
    /// Child logger copies all existing values from the parent.
    ///
    /// All children, their children and so on, form one hierarchy sharing
    /// a common drain.
    ///
    /// Use `o!` macro to help build `values`
    ///
    /// ```
    /// #[macro_use]
    /// extern crate slog;
    ///
    /// fn main() {
    ///     let root = slog::Logger::new_root(o!("key1" => "value1", "key2" => "value2"));
    ///     let log = root.new(o!("key" => "value"));
    /// }

    pub fn new(&self, values: &[OwnedKeyValue]) -> Logger {
        let mut new_values = self.inner.values.clone();
        new_values.extend_from_slice(values);
        Logger {
            inner: Arc::new(LoggerInner {
                drain: self.inner.drain.clone(),
                values: new_values,
            }),
        }
    }

    /// Set the drain for logger and it's hierarchy
    pub fn set_drain<D: drain::Drain>(&self, drain: D) {
        let _ = self.inner.drain.set(Arc::new(Box::new(drain)));
    }

    /// Swap the existing drain with a new one
    ///
    /// As the drains are shared between threads, and might still be
    /// referenced `Arc`s are being used to reference-count them.
    pub fn swap_drain(&self, drain: Arc<Box<drain::Drain>>) -> Arc<Box<drain::Drain>> {
        self.inner.drain.set(drain)
    }

    /// Log one logging record
    ///
    /// Use specific logging functions instead.
    pub fn log<'a>(&'a self, lvl: Level, msg: &'a str, values: &'a [BorrowedKeyValue<'a>]) {

        let info = RecordInfo {
            ts: chrono::UTC::now(),
            msg: msg.to_string(),
            level: lvl,
        };

        self.inner.drain.get().log(&info, self.inner.values.as_slice(), values);
    }

    /// Log critical level record
    ///
    /// Use `b!` macro to help build `values`
    pub fn critical<'a>(&'a self, msg: &'a str, values: &'a [BorrowedKeyValue<'a>]) {
        self.log(Level::Critical, msg, values);
    }

    /// Log error level record
    ///
    /// Use `b!` macro to help build `values`
    pub fn error<'a>(&'a self, msg: &'a str, values: &'a [BorrowedKeyValue<'a>]) {
        self.log(Level::Error, msg, values);
    }

    /// Log warning level record
    ///
    /// Use `b!` macro to help build `values`
    pub fn warn<'a>(&'a self, msg: &'a str, values: &'a [BorrowedKeyValue<'a>]) {
        self.log(Level::Warning, msg, values);
    }

    /// Log info level record
    ///
    /// Use `b!` macro to help build `values`
    pub fn info<'a>(&'a self, msg: &'a str, values: &'a [BorrowedKeyValue<'a>]) {
        self.log(Level::Info, msg, values);
    }

    /// Log debug level record
    ///
    /// Use `b!` macro to help build `values`
    pub fn debug<'a>(&'a self, msg: &'a str, values: &'a [BorrowedKeyValue<'a>]) {
        self.log(Level::Debug, msg, values);
    }

    /// Log trace level record
    ///
    /// Use `b!` macro to help build `values`
    pub fn trace<'a>(&'a self, msg: &'a str, values: &'a [BorrowedKeyValue<'a>]) {
        self.log(Level::Trace, msg, values);
    }
}

/// Common information about a logging record
pub struct RecordInfo {
    /// Timestamp
    pub ts: chrono::DateTime<chrono::UTC>,
    /// Logging level
    pub level: Level,
    /// Message
    pub msg: String,
}