summary refs log tree commit diff stats
path: root/src/logging.rs
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-08-27 01:10:51 -0400
committerBen Morrison <ben@gbmor.dev>2019-08-27 01:10:51 -0400
commit5b59ba3aee3e874b2e67cf529680d19910a3ec7d (patch)
treeaf9d8c630c144bc875a75fb77f380812b68b69fa /src/logging.rs
parent9ee17f15bb380d31d55d83586ebdf3644cf326af (diff)
downloadclinte-5b59ba3aee3e874b2e67cf529680d19910a3ec7d.tar.gz
db connected, logging up
Diffstat (limited to 'src/logging.rs')
-rw-r--r--src/logging.rs48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/logging.rs b/src/logging.rs
new file mode 100644
index 0000000..332de6c
--- /dev/null
+++ b/src/logging.rs
@@ -0,0 +1,48 @@
+use std::fs;
+use std::fs::File;
+
+use chrono::offset::Utc;
+use simplelog::*;
+
+pub const FILE: &str = "/tmp/clinte.log";
+
+pub fn init() {
+    // If the log file exists on startup,
+    // move and timestamp it so we get a
+    // fresh log file.
+    if fs::metadata(FILE).is_ok() {
+        let mut newpath = FILE.to_string();
+        let time = Utc::now().to_rfc3339();
+        newpath.push_str(".");
+        newpath.push_str(&time);
+        fs::rename(FILE, newpath).unwrap();
+    }
+
+    CombinedLogger::init(vec![
+        TermLogger::new(LevelFilter::Warn, Config::default(), TerminalMode::Stderr).unwrap(),
+        WriteLogger::new(
+            LevelFilter::Info,
+            Config::default(),
+            File::create(FILE).unwrap(),
+        ),
+    ])
+    .expect("Unable to initialize logging");
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    use log::info;
+
+    #[test]
+    fn init_logs() {
+        let blank = " ".bytes().collect::<Vec<u8>>();
+        fs::write("/tmp/dirtmud.log", &blank).unwrap();
+        init();
+
+        info!("TEST LOG MESSAGE");
+        let logfile = fs::read_to_string("/tmp/dirtmud.log").unwrap();
+        assert!(logfile.contains("TEST LOG MESSAGE"));
+    }
+}