From cf3b6628ce4deb429fad407829b425a7377e2b5d Mon Sep 17 00:00:00 2001 From: Ben Morrison Date: Tue, 26 May 2020 20:37:23 -0400 Subject: hiding some log messages behind -v flag --- src/conf.rs | 23 +++++++++++++++++++++++ src/db.rs | 18 +++++++++++++----- src/ed.rs | 6 +++++- src/main.rs | 22 ++++++++-------------- 4 files changed, 49 insertions(+), 20 deletions(-) create mode 100644 src/conf.rs diff --git a/src/conf.rs b/src/conf.rs new file mode 100644 index 0000000..dd3b298 --- /dev/null +++ b/src/conf.rs @@ -0,0 +1,23 @@ +use clap::{Arg, ArgMatches}; + +lazy_static! { + pub static ref ARGS: ArgMatches<'static> = get_config(); + pub static ref DEBUG: bool = ARGS.is_present("verbose"); +} + +fn get_config() -> clap::ArgMatches<'static> { + clap::App::new("clinte") + .version(clap::crate_version!()) + .author("Ben Morrison ") + .about("Command-line community notices system") + .arg( + Arg::with_name("verbose") + .short("v") + .long("verbose") + .help("Verbose logging"), + ) + .subcommand(clap::SubCommand::with_name("post").about("Post a new notice")) + .subcommand(clap::SubCommand::with_name("update").about("Update a notice you've posted")) + .subcommand(clap::SubCommand::with_name("delete").about("Delete a notice you've posted")) + .get_matches() +} diff --git a/src/db.rs b/src/db.rs index 86875e2..935f1f9 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,5 +1,7 @@ use std::time; +use crate::conf; + const DB_PATH: &str = "/usr/local/clinte/clinte.db"; #[derive(Debug)] @@ -18,7 +20,11 @@ pub struct Conn { impl Conn { pub fn init(path: &str) -> rusqlite::Connection { let start = time::Instant::now(); - log::info!("Connecting to database"); + + if *conf::DEBUG { + log::info!("Connecting to database"); + } + let conn = rusqlite::Connection::open_with_flags( path, rusqlite::OpenFlags::SQLITE_OPEN_FULL_MUTEX @@ -38,10 +44,12 @@ impl Conn { ) .expect("Could not initialize DB"); - log::info!( - "Database connection established in {}ms", - start.elapsed().as_millis() - ); + if *conf::DEBUG { + log::info!( + "Database connection established in {}ms", + start.elapsed().as_millis() + ); + } conn } diff --git a/src/ed.rs b/src/ed.rs index 0e1c4c8..31a27ae 100644 --- a/src/ed.rs +++ b/src/ed.rs @@ -4,6 +4,7 @@ use std::process; use chrono::prelude::*; +use crate::conf; use crate::error; use crate::user; @@ -13,7 +14,10 @@ fn create_tmp_file<'a>() -> Result { match fs::write(&file_name, "") { Ok(_) => Ok(file_name), Err(err) => { - log::warn!("{:?}", err); + log::warn!("Couldn't create tempfile"); + if *conf::DEBUG { + log::warn!("--> {:?}", err); + } Err("Unable to create temp file") } } diff --git a/src/main.rs b/src/main.rs index 83708c4..53cb2f9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,8 +3,7 @@ use std::time; #[macro_use] extern crate lazy_static; -use clap; - +mod conf; mod db; mod ed; mod error; @@ -13,18 +12,11 @@ mod posts; mod user; fn main() -> error::Result<()> { - let arg_matches = clap::App::new("clinte") - .version(clap::crate_version!()) - .author("Ben Morrison ") - .about("Command-line community notices system") - .subcommand(clap::SubCommand::with_name("post").about("Post a new notice")) - .subcommand(clap::SubCommand::with_name("update").about("Update a notice you've posted")) - .subcommand(clap::SubCommand::with_name("delete").about("Delete a notice you've posted")) - .get_matches(); - + let arg_matches = &*conf::ARGS; let start = time::Instant::now(); - let file = format!("/tmp/clinte_{}.log", *user::NAME); - logging::init(&file)?; + let logfile = format!("/tmp/clinte_{}.log", *user::NAME); + logging::init(&logfile)?; + log::info!("clinte starting up!"); println!("clinte v{}", clap::crate_version!()); println!("a community notices system"); @@ -32,7 +24,9 @@ fn main() -> error::Result<()> { let db = db::Conn::new(); - log::info!("Startup completed in {:?}ms", start.elapsed().as_millis()); + if *conf::DEBUG { + log::info!("Startup completed in {:?}ms", start.elapsed().as_millis()); + } if arg_matches.subcommand_matches("post").is_some() { log::info!("New post..."); -- cgit 1.4.1-2-gfad0