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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//! Standard slog-rs extensions.
#![warn(missing_docs)]

extern crate slog;
extern crate thread_local;

use slog::Drain;

use std::sync::{mpsc, Mutex};
use std::fmt;
use std::{io, thread};
use slog::{Record, RecordStatic, Level};
use slog::ser::{self, Serialize, Serializer};

use slog::OwnedKeyValueList;


/// `Async` drain
///
/// `Async` will send all the logging records to a wrapped drain running in another thread.
///
/// Note: Dropping `Async` waits for it's worker-thread to finish (thus handle all previous
/// requests). If you can't tolerate the delay, make sure you drop `Async` drain instance eg. in
/// another thread.
pub struct Async {
    ref_sender: Mutex<mpsc::Sender<AsyncMsg>>,
    tl_sender: thread_local::ThreadLocal<mpsc::Sender<AsyncMsg>>,
    join: Mutex<Option<thread::JoinHandle<()>>>,
}

impl Async {
    /// Create `Async` drain
    ///
    /// The wrapped drain must handle all error conditions (`Drain<Error=Never>`). See
    /// `slog::DrainExt::fuse()` and `slog::DrainExt::ignore_err()` for typical error handling
    /// strategies.
    pub fn new<D: slog::Drain<Error=slog::Never> + Send + 'static>(drain: D) -> Self {
        let (tx, rx) = mpsc::channel();
        let join = thread::spawn(move || {
                loop {
                    match rx.recv().unwrap() {
                        AsyncMsg::Record(r) => {
                            let rs = RecordStatic {
                                level: r.level,
                                file: r.file,
                                line: r.line,
                                column: r.column,
                                function: r.function,
                                module: r.module,
                                target: &r.target,
                            };
                            let record_values: Vec<_> = r.record_values
                                .iter()
                                .map(|&(k, ref v)| (k, v as &Serialize))
                                .collect();

                            drain.log(
                                &Record::new(&rs,
                                             format_args!("{}", r.msg),
                                             record_values.as_slice()
                                            ),
                                            &r.logger_values
                                            ).unwrap();
                        }
                        AsyncMsg::Finish => return,
                    }
                }
        });

        Async{
            ref_sender: Mutex::new(tx),
            tl_sender: thread_local::ThreadLocal::new(),
            join: Mutex::new(Some(join)),
        }
    }

    fn get_sender(&self) -> &mpsc::Sender<AsyncMsg> {
        self.tl_sender.get_or(|| {
            // TODO: Change to `get_or_try` https://github.com/Amanieu/thread_local-rs/issues/2
            Box::new(self.ref_sender.lock().unwrap().clone())
        })
    }

    /// Send `AsyncRecord` to a worker thread.
    fn send(&self, r: AsyncRecord) -> io::Result<()> {
        let sender = self.get_sender();

        sender.send(AsyncMsg::Record(r))
            .map_err(|_| io::Error::new(io::ErrorKind::BrokenPipe, "Send failed"))
    }

}

type RecordValues = Vec<(&'static str, Box<Serialize + Send>)>;

struct ToSendSerializer {
    record_values: RecordValues,
}

impl ToSendSerializer {
    fn new() -> Self {
        ToSendSerializer { record_values: Vec::new() }
    }

    fn finish(self) -> RecordValues {
        self.record_values
    }
}

impl Serializer for ToSendSerializer {
    fn emit_bool(&mut self, key: &'static str, val: bool) -> ser::Result {
        self.record_values.push((key, Box::new(val)));
        Ok(())
    }
    fn emit_unit(&mut self, key: &'static str) -> ser::Result {
        self.record_values.push((key, Box::new(())));
        Ok(())
    }
    fn emit_none(&mut self, key: &'static str) -> ser::Result {
        let val: Option<()> = None;
        self.record_values.push((key, Box::new(val)));
        Ok(())
    }
    fn emit_char(&mut self, key: &'static str, val: char) -> ser::Result {
        self.record_values.push((key, Box::new(val)));
        Ok(())
    }
    fn emit_u8(&mut self, key: &'static str, val: u8) -> ser::Result {
        self.record_values.push((key, Box::new(val)));
        Ok(())
    }
    fn emit_i8(&mut self, key: &'static str, val: i8) -> ser::Result {
        self.record_values.push((key, Box::new(val)));
        Ok(())
    }
    fn emit_u16(&mut self, key: &'static str, val: u16) -> ser::Result {
        self.record_values.push((key, Box::new(val)));
        Ok(())
    }
    fn emit_i16(&mut self, key: &'static str, val: i16) -> ser::Result {
        self.record_values.push((key, Box::new(val)));
        Ok(())
    }
    fn emit_u32(&mut self, key: &'static str, val: u32) -> ser::Result {
        self.record_values.push((key, Box::new(val)));
        Ok(())
    }
    fn emit_i32(&mut self, key: &'static str, val: i32) -> ser::Result {
        self.record_values.push((key, Box::new(val)));
        Ok(())
    }
    fn emit_f32(&mut self, key: &'static str, val: f32) -> ser::Result {
        self.record_values.push((key, Box::new(val)));
        Ok(())
    }
    fn emit_u64(&mut self, key: &'static str, val: u64) -> ser::Result {
        self.record_values.push((key, Box::new(val)));
        Ok(())
    }
    fn emit_i64(&mut self, key: &'static str, val: i64) -> ser::Result {
        self.record_values.push((key, Box::new(val)));
        Ok(())
    }
    fn emit_f64(&mut self, key: &'static str, val: f64) -> ser::Result {
        self.record_values.push((key, Box::new(val)));
        Ok(())
    }
    fn emit_usize(&mut self, key: &'static str, val: usize) -> ser::Result {
        self.record_values.push((key, Box::new(val)));
        Ok(())
    }
    fn emit_isize(&mut self, key: &'static str, val: isize) -> ser::Result {
        self.record_values.push((key, Box::new(val)));
        Ok(())
    }
    fn emit_str(&mut self, key: &'static str, val: &str) -> ser::Result {
        self.record_values.push((key, Box::new(String::from(val))));
        Ok(())
    }
    fn emit_arguments(&mut self, key: &'static str, val: &fmt::Arguments) -> ser::Result {
        self.record_values.push((key, Box::new(fmt::format(*val))));
        Ok(())
    }
}


impl Drain for Async {
    type Error = io::Error;

    fn log(&self, record: &Record, logger_values: &OwnedKeyValueList) -> io::Result<()> {

        let mut ser = ToSendSerializer::new();
        for &(k, v) in record.values() {
            try!(v.serialize(record, k, &mut ser))
        }

        self.send(AsyncRecord {
            msg: fmt::format(record.msg()),
            level: record.level(),
            file: record.file(),
            line: record.line(),
            column: record.column(),
            function: record.function(),
            module: record.module(),
            target: String::from(record.target()),
            logger_values: logger_values.clone(),
            record_values: ser.finish(),
        })
    }
}

struct AsyncRecord {
    msg: String,
    level: Level,
    file: &'static str,
    line: u32,
    column: u32,
    function: &'static str,
    module: &'static str,
    target: String,
    logger_values: OwnedKeyValueList,
    record_values: Vec<(&'static str, Box<Serialize + Send>)>,
}

enum AsyncMsg {
    Record(AsyncRecord),
    Finish,
}

impl Drop for Async {
    fn drop(&mut self) {
        let sender = self.get_sender();

        let _ = sender.send(AsyncMsg::Finish);
        let _ = self.join.lock().unwrap().take().unwrap().join();
    }
}