use std::time::{SystemTime, Duration};
use mentor::configs::LogConfig;
#[derive(Debug, Copy, Clone)]
pub struct Stats {
pub iterations : u64,
pub elapsed_time: Duration,
pub latest_mse : f64,
pub recent_mse : f64
}
#[derive(Debug, Clone)]
pub enum Logger {
Never,
TimeSteps{
last_log: SystemTime,
interval: Duration
},
Iterations(u64)
}
impl From<LogConfig> for Logger {
fn from(config: LogConfig) -> Self {
use self::LogConfig::*;
match config {
Never => Logger::Never,
TimeSteps(duration) => Logger::TimeSteps{
last_log: SystemTime::now(),
interval: duration
},
Iterations(interval) => Logger::Iterations(interval)
}
}
}
impl Logger {
fn log(stats: Stats) {
info!("{:?}\n", stats);
println!("{:?}", stats)
}
pub fn try_log(&mut self, stats: Stats) {
use self::Logger::*;
match *self {
TimeSteps{ref mut last_log, interval} => {
if last_log.elapsed().expect("expected valid duration") >= interval {
Self::log(stats);
*last_log = SystemTime::now();
}
},
Iterations(interval) => {
if stats.iterations % interval == 0 {
Self::log(stats)
}
},
_ => {
}
}
}
}