diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/db.rs | 7 | ||||
-rw-r--r-- | src/ed.rs | 1 | ||||
-rw-r--r-- | src/logging.rs | 46 | ||||
-rw-r--r-- | src/main.rs | 11 | ||||
-rw-r--r-- | src/user.rs | 4 |
5 files changed, 25 insertions, 44 deletions
diff --git a/src/db.rs b/src/db.rs index 0ad16ca..8ae9f2c 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,8 +1,5 @@ use std::time; -use log::info; -use rusqlite; - const DB_PATH: &str = "/usr/local/clinte/clinte.db"; #[derive(Debug)] @@ -21,7 +18,7 @@ pub struct Conn { impl Conn { pub fn init(path: &str) -> rusqlite::Connection { let start = time::Instant::now(); - info!("Connecting to database"); + log::info!("Connecting to database"); let conn = rusqlite::Connection::open_with_flags( path, rusqlite::OpenFlags::SQLITE_OPEN_FULL_MUTEX @@ -41,7 +38,7 @@ impl Conn { ) .expect("Could not initialize DB"); - info!( + log::info!( "Database connection established in {}ms", start.elapsed().as_millis() ); diff --git a/src/ed.rs b/src/ed.rs index 059e411..fa6d43b 100644 --- a/src/ed.rs +++ b/src/ed.rs @@ -3,7 +3,6 @@ use std::fs; use std::process; use chrono::prelude::*; -use log; use crate::error; use crate::user; 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")); } } diff --git a/src/main.rs b/src/main.rs index e2f793b..faef1b3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,6 @@ use std::time; extern crate lazy_static; use clap; -use log::info; mod db; mod ed; @@ -25,17 +24,17 @@ fn main() -> error::Result<()> { let start = time::Instant::now(); logging::init()?; - info!("clinte starting up!"); + log::info!("clinte starting up!"); println!("clinte v{}", clap::crate_version!()); println!("a community notices system"); println!(); let db = db::Conn::new(); - info!("Startup completed in {:?}ms", start.elapsed().as_millis()); + log::info!("Startup completed in {:?}ms", start.elapsed().as_millis()); if arg_matches.subcommand_matches("post").is_some() { - info!("New post..."); + log::info!("New post..."); posts::create(&db)?; } else if arg_matches.subcommand_matches("update").is_some() { let id: u32 = if let Some(val) = arg_matches.subcommand_matches("update_handler") { @@ -43,7 +42,7 @@ fn main() -> error::Result<()> { } else { 0 }; - info!("Updating post ..."); + log::info!("Updating post ..."); posts::update_handler(&db, id)?; } else if arg_matches.subcommand_matches("delete").is_some() { let id: u32 = if let Some(val) = arg_matches.subcommand_matches("update_handler") { @@ -51,7 +50,7 @@ fn main() -> error::Result<()> { } else { 0 }; - info!("Deleting post"); + log::info!("Deleting post"); posts::delete_handler(&db, id)?; } diff --git a/src/user.rs b/src/user.rs index 7aac1b4..df785be 100644 --- a/src/user.rs +++ b/src/user.rs @@ -2,7 +2,7 @@ use users; lazy_static! { pub static ref NAME: String = users::get_current_username() - .unwrap() + .expect("Could not get username") .into_string() - .unwrap(); + .expect("Could not get username"); } |