summary refs log tree commit diff stats
path: root/src/logging.rs
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2020-05-26 19:43:37 -0400
committerBen Morrison <ben@gbmor.dev>2020-05-26 19:43:37 -0400
commitc5ea65393d9b8000d68cf8c2f887f22bdae81222 (patch)
tree4a044312814ce7b70c6d9af0a7e32b3860ee9425 /src/logging.rs
parenta24e25977e713fbc5a06778ada4c21202209a598 (diff)
downloadclinte-c5ea65393d9b8000d68cf8c2f887f22bdae81222.tar.gz
logging changes:
Rather than using a new logfile for each invocation, a single logfile
called `/tmp/clinte_$USER.log` will be used, with later invocations
appending to the previous messages.

Also removed some unnecessary `use` statements relating to the `log`
crate and its macros. Leaving the macro calls namespaced to `log::`
makes it clearer what the behavior is.
Diffstat (limited to 'src/logging.rs')
-rw-r--r--src/logging.rs46
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"));
     }
 }