diff options
Diffstat (limited to 'src/logging.rs')
-rw-r--r-- | src/logging.rs | 46 |
1 files changed, 16 insertions, 30 deletions
diff --git a/src/logging.rs b/src/logging.rs index b42c6b1..5699f59 100644 --- a/src/logging.rs +++ b/src/logging.rs @@ -1,38 +1,23 @@ -use std::fs; -use std::fs::File; +use std::fs::OpenOptions; -use chrono::offset::Utc; use simplelog::*; use crate::error; use crate::user; -lazy_static! { - static ref FILE: String = format!("/tmp/clinte_{}.log", *user::NAME); -} - pub fn init() -> error::Result<()> { - // If the log file exists on startup, - // move and timestamp it so we get a - // fresh log file. - if fs::metadata(FILE.clone()).is_ok() { - let mut new_file = FILE.clone(); - let time = Utc::now().to_rfc3339(); - new_file.push_str("."); - new_file.push_str(&time); - fs::rename(FILE.clone(), new_file)?; + let file = format!("/tmp/clinte_{}.log", *user::NAME); + let logfile = match OpenOptions::new().append(true).create(true).open(file) { + Err(e) => { + panic!("Could not open log file: {}", e); + } + Ok(f) => f, + }; + + if let Err(e) = WriteLogger::init(LevelFilter::Info, Config::default(), logfile) { + panic!("Could not initiate logging: {}", e); } - CombinedLogger::init(vec![ - TermLogger::new(LevelFilter::Warn, Config::default(), TerminalMode::Stderr).unwrap(), - WriteLogger::new( - LevelFilter::Info, - Config::default(), - File::create(FILE.clone())?, - ), - ]) - .expect("Unable to initialize logging"); - Ok(()) } @@ -40,16 +25,17 @@ pub fn init() -> error::Result<()> { mod tests { use super::*; - use log::info; + use std::fs; #[test] fn init_logs() { + let file = format!("/tmp/clinte_{}.log", *user::NAME); let blank = " ".bytes().collect::<Vec<u8>>(); - fs::write(&*FILE, &blank).unwrap(); + fs::write(&file, &blank).unwrap(); init().unwrap(); - info!("TEST LOG MESSAGE"); - let logfile = fs::read_to_string(&*FILE).unwrap(); + log::info!("TEST LOG MESSAGE"); + let logfile = fs::read_to_string(&file).unwrap(); assert!(logfile.contains("TEST LOG MESSAGE")); } } |