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
// Copyright 2018 Stefan Kroboth
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

//! # Loggers based on the `slog` crate

use crate::{ArgminKV, ArgminLog, Error};
use slog;
use slog::{info, o, Drain, Record, Serializer, KV};
use slog_async;
use slog_async::OverflowStrategy;
use slog_json;
use slog_term;
use std::fs::OpenOptions;
// use std::rc::Rc;
use std::sync::Arc;
use std::sync::Mutex;

/// A logger based on `slog`
#[derive(Clone)]
pub struct ArgminSlogLogger {
    /// the logger
    logger: slog::Logger,
}

impl ArgminSlogLogger {
    /// Log to the terminal in a blocking way
    pub fn term() -> Arc<Self> {
        ArgminSlogLogger::term_internal(OverflowStrategy::Block)
    }

    /// Log to the terminal in a non-blocking way (in case of overflow, messages are dropped)
    pub fn term_noblock() -> Arc<Self> {
        ArgminSlogLogger::term_internal(OverflowStrategy::Drop)
    }

    /// Actual implementation of the logging to the terminal
    fn term_internal(overflow_strategy: OverflowStrategy) -> Arc<Self> {
        let decorator = slog_term::TermDecorator::new().build();
        let drain = slog_term::FullFormat::new(decorator)
            .use_original_order()
            .build()
            .fuse();
        let drain = slog_async::Async::new(drain)
            .overflow_strategy(overflow_strategy)
            .build()
            .fuse();
        Arc::new(ArgminSlogLogger {
            logger: slog::Logger::root(drain, o!()),
        })
    }

    /// Log JSON to a file in a blocking way
    pub fn file(file: &str) -> Result<Arc<Self>, Error> {
        ArgminSlogLogger::file_internal(file, OverflowStrategy::Block)
    }

    /// Log JSON to a file in a non-blocking way (in case of overflow, messages are dropped)
    pub fn file_noblock(file: &str) -> Result<Arc<Self>, Error> {
        ArgminSlogLogger::file_internal(file, OverflowStrategy::Drop)
    }

    /// Actual implementaiton of logging JSON to file
    fn file_internal(file: &str, overflow_strategy: OverflowStrategy) -> Result<Arc<Self>, Error> {
        // Logging to file
        let file = OpenOptions::new()
            .create(true)
            .write(true)
            .truncate(true)
            .open(file)?;
        let drain = Mutex::new(slog_json::Json::new(file).build()).map(slog::Fuse);
        let drain = slog_async::Async::new(drain)
            .overflow_strategy(overflow_strategy)
            .build()
            .fuse();
        Ok(Arc::new(ArgminSlogLogger {
            logger: slog::Logger::root(drain, o!()),
        }))
    }
}

/// This type is necessary in order to be able to implement `slog::KV` on `ArgminKV`
pub struct ArgminSlogKV {
    pub kv: Vec<(&'static str, String)>,
}

impl KV for ArgminSlogKV {
    fn serialize(&self, _record: &Record, serializer: &mut Serializer) -> slog::Result {
        for idx in self.kv.clone().iter().rev() {
            serializer.emit_str(idx.0, &idx.1.to_string())?;
        }
        Ok(())
    }
}

impl<'a> From<&'a ArgminKV> for ArgminSlogKV {
    fn from(i: &'a ArgminKV) -> ArgminSlogKV {
        ArgminSlogKV { kv: i.kv.clone() }
    }
}

impl ArgminLog for ArgminSlogLogger {
    /// Log general info
    fn log_info(&self, msg: &str, kv: &ArgminKV) -> Result<(), Error> {
        info!(self.logger, "{}", msg; ArgminSlogKV::from(kv));
        Ok(())
    }

    /// This should be used to log iteration data only (because this is what may be saved in a CSV
    /// file or a database)
    fn log_iter(&self, kv: &ArgminKV) -> Result<(), Error> {
        info!(self.logger, ""; ArgminSlogKV::from(kv));
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    send_sync_test!(argmin_slog_loggerv, ArgminSlogLogger);
}