From 55d6e412d6fc41dd9a3c36b196414b9eda43beb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Oliveira?= Date: Fri, 13 Oct 2017 01:09:54 +0100 Subject: [PATCH] update project to use log crate and replace println! calls with equivalent log level macro calls --- Cargo.lock | 23 +++++++++++++++++++++++ Cargo.toml | 2 ++ src/config.rs | 2 +- src/db/mod.rs | 2 +- src/ddns.rs | 4 ++-- src/index.rs | 16 ++++++++-------- src/main.rs | 28 +++++++++++++++++++++++----- src/ui/headless.rs | 2 +- src/ui/windows.rs | 8 ++++---- 9 files changed, 65 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4ac804a..c7c62dd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14,6 +14,7 @@ dependencies = [ "iron 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "kernel32-sys 0.2.2 (git+https://github.com/retep998/winapi-rs?branch=0.2)", "lewton 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "metaflac 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", "mount 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "params 0.6.0 (git+https://github.com/euclio/params?branch=update)", @@ -28,6 +29,7 @@ dependencies = [ "serde_derive 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "shell32-sys 0.1.1 (git+https://github.com/retep998/winapi-rs?branch=0.2)", + "simplelog 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "staticfile 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", "typemap 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1403,6 +1405,16 @@ dependencies = [ "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "simplelog" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", + "term 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "siphasher" version = "0.2.2" @@ -1460,6 +1472,15 @@ dependencies = [ "rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "term" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "thread_local" version = "0.3.4" @@ -1875,6 +1896,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum serde_urlencoded 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ce0fd303af908732989354c6f02e05e2e6d597152870f2c6990efb0577137480" "checksum shell32-sys 0.1.1 (git+https://github.com/retep998/winapi-rs?branch=0.2)" = "" "checksum shell32-sys 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "72f20b8f3c060374edb8046591ba28f62448c369ccbdc7b02075103fb3a9e38d" +"checksum simplelog 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2c3a63f2b74bc3359ed6050da62e32cc0e10a162c4f62a0c1d80cd89d432234b" "checksum siphasher 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0df90a788073e8d0235a67e50441d47db7c8ad9debd91cbf43736a2a92d36537" "checksum slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "17b4fcaed89ab08ef143da37bc52adbcc04d4a69014f4c1208d6b51f0c47bc23" "checksum smallvec 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4c8cbcd6df1e117c2210e13ab5109635ad68a929fcbb8964dc965b76cb5ee013" @@ -1883,6 +1905,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" "checksum take 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b157868d8ac1f56b64604539990685fa7611d8fa9e5476cf0c02cf34d32917c5" "checksum tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "87974a6f5c1dfb344d733055601650059a3363de2a6104819293baff662132d6" +"checksum term 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "fa63644f74ce96fbeb9b794f66aff2a52d601cbd5e80f4b97123e3899f4570f1" "checksum thread_local 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "1697c4b57aeeb7a536b647165a2825faddffb1d3bad386d507709bd51a90bb14" "checksum time 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)" = "ffd7ccbf969a892bf83f1e441126968a07a3941c24ff522a26af9f9f4585d1a3" "checksum tokio-core 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "6a20ba4738d283cac7495ca36e045c80c2a8df3e05dd0909b17a06646af5a7ed" diff --git a/Cargo.toml b/Cargo.toml index d62d1ae..efcea27 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,6 +34,8 @@ staticfile = "0.4.0" toml = "0.4.5" typemap = "0.3" url = "1.2.0" +log = "0.3.8" +simplelog = "0.4.2" [dependencies.rusqlite] version = "0.12.0" diff --git a/src/config.rs b/src/config.rs index d9d755b..2291d3b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -63,7 +63,7 @@ pub fn parse_json(content: &str) -> Result { } pub fn parse_toml_file(path: &path::Path) -> Result { - println!("Config file path: {}", path.to_string_lossy()); + info!("Config file path: {}", path.to_string_lossy()); let mut config_file = fs::File::open(path)?; let mut config_file_content = String::new(); config_file.read_to_string(&mut config_file_content)?; diff --git a/src/db/mod.rs b/src/db/mod.rs index adf66c3..a787f5d 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -27,7 +27,7 @@ pub struct DB { impl DB { pub fn new(path: &Path) -> Result { - println!("Database file path: {}", path.to_string_lossy()); + info!("Database file path: {}", path.to_string_lossy()); let connection = Arc::new(Mutex::new(SqliteConnection::establish(&path.to_string_lossy())?)); let db = DB { connection: connection.clone() }; diff --git a/src/ddns.rs b/src/ddns.rs index 85c241c..3e0df7e 100644 --- a/src/ddns.rs +++ b/src/ddns.rs @@ -66,7 +66,7 @@ fn update_my_ip(config_source: &T) -> Result<(), DDNSError> { let config = config_source.get_ddns_config()?; if config.host.len() == 0 || config.username.len() == 0 { - println!("Skipping DDNS update because credentials are missing"); + info!("Skipping DDNS update because credentials are missing"); return Ok(()); } @@ -91,7 +91,7 @@ pub fn run(config_source: &T) { loop { if let Err(e) = update_my_ip(config_source) { - println!("Dynamic DNS update error: {:?}", e); + error!("Dynamic DNS update error: {:?}", e); } thread::sleep(time::Duration::from_secs(60 * 30)); } diff --git a/src/index.rs b/src/index.rs index 420c3a6..d3b87ba 100644 --- a/src/index.rs +++ b/src/index.rs @@ -207,7 +207,7 @@ impl<'conn> IndexBuilder<'conn> { let file_path = match file { Ok(f) => f.path(), _ => { - println!("File read error within {}", path_string); + error!("File read error within {}", path_string); break; } }; @@ -378,10 +378,10 @@ pub fn update(db: &T) -> Result<()> where T: ConnectionSource + VFSSource { let start = time::Instant::now(); - println!("Beginning library index update"); + info!("Beginning library index update"); clean(db)?; populate(db)?; - println!("Library index update took {} seconds", + info!("Library index update took {} seconds", start.elapsed().as_secs()); Ok(()) } @@ -392,7 +392,7 @@ pub fn update_loop(db: &T, command_buffer: Receiver) loop { // Wait for a command if let Err(e) = command_buffer.recv() { - println!("Error while waiting on index command buffer: {}", e); + error!("Error while waiting on index command buffer: {}", e); return; } @@ -400,7 +400,7 @@ pub fn update_loop(db: &T, command_buffer: Receiver) loop { match command_buffer.try_recv() { Err(TryRecvError::Disconnected) => { - println!("Error while flushing index command buffer"); + error!("Error while flushing index command buffer"); return; } Err(TryRecvError::Empty) => break, @@ -410,7 +410,7 @@ pub fn update_loop(db: &T, command_buffer: Receiver) // Do the update if let Err(e) = update(db) { - println!("Error while updating index: {}", e); + error!("Error while updating index: {}", e); } } } @@ -423,7 +423,7 @@ pub fn self_trigger(db: &T, command_buffer: Arc>>) let command_buffer = command_buffer.lock().unwrap(); let command_buffer = command_buffer.deref(); if let Err(e) = command_buffer.send(Command::REINDEX) { - println!("Error while writing to index command buffer: {}", e); + error!("Error while writing to index command buffer: {}", e); return; } } @@ -434,7 +434,7 @@ pub fn self_trigger(db: &T, command_buffer: Arc>>) .get_result(connection.deref()) .map_err(|e| e.into()); if let Err(ref e) = settings { - println!("Could not retrieve index sleep duration: {}", e); + error!("Could not retrieve index sleep duration: {}", e); } sleep_duration = settings .map(|s| s.index_sleep_duration_seconds) diff --git a/src/main.rs b/src/main.rs index a459d9e..d307e1c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,6 +32,9 @@ extern crate staticfile; extern crate toml; extern crate typemap; extern crate url; +#[macro_use] +extern crate log; +extern crate simplelog; #[cfg(windows)] extern crate uuid; @@ -59,6 +62,7 @@ use staticfile::Static; use std::path::Path; use std::sync::{Arc, Mutex}; use std::sync::mpsc::channel; +use simplelog::{Config, TermLogger, LogLevelFilter}; mod api; mod config; @@ -111,6 +115,7 @@ fn run() -> Result<()> { options.optopt("p", "port", "set polaris to run on a custom port", "PORT"); options.optopt("d", "database", "set the path to index database", "FILE"); options.optopt("w", "web", "set the path to web client files", "DIRECTORY"); + options.optopt("l", "log", "set the log level to a value between 0 (off) and 3 (debug)", "LEVEL"); #[cfg(unix)] options.optflag("f", @@ -128,11 +133,23 @@ fn run() -> Result<()> { return Ok(()); } + let log_level = match matches.opt_str("l").as_ref().map(String::as_ref) { + Some("0") => LogLevelFilter::Off, + Some("1") => LogLevelFilter::Error, + Some("2") => LogLevelFilter::Info, + Some("3") => LogLevelFilter::Debug, + _ => LogLevelFilter::Info, + }; + + if let Err(e) = TermLogger::init(log_level, Config::default()) { + bail!("Error starting logger: {}", e); + }; + #[cfg(unix)] daemonize(&matches)?; // Init DB - println!("Starting up database"); + info!("Starting up database"); let db_path = matches.opt_str("d"); let mut default_db_path = utils::get_data_root()?; default_db_path.push("db.sqlite"); @@ -167,7 +184,7 @@ fn run() -> Result<()> { // Mount API let prefix_url = config.prefix_url.unwrap_or("".to_string()); let api_url = format!("{}/api", &prefix_url); - println!("Mounting API on {}", api_url); + info!("Mounting API on {}", api_url); let mut mount = Mount::new(); let handler = api::get_handler(db.clone(), index_sender)?; mount.mount(&api_url, handler); @@ -175,7 +192,7 @@ fn run() -> Result<()> { // Mount static files let static_url = format!("/{}", &prefix_url); - println!("Mounting static files on {}", static_url); + info!("Mounting static files on {}", static_url); let web_dir_name = matches.opt_str("w"); let mut default_web_dir = utils::get_data_root()?; default_web_dir.push("web"); @@ -185,12 +202,13 @@ fn run() -> Result<()> { mount.mount(&static_url, Static::new(web_dir_path)); - println!("Starting up server"); + info!("Starting up server"); let port: u16 = matches .opt_str("p") .unwrap_or("5050".to_owned()) .parse() .or(Err("invalid port number"))?; + let mut server = match Iron::new(mount).http(("0.0.0.0", port)) { Ok(s) => s, Err(e) => bail!("Error starting up server: {}", e), @@ -203,7 +221,7 @@ fn run() -> Result<()> { // Run UI ui::run(); - println!("Shutting down server"); + info!("Shutting down server"); if let Err(e) = server.close() { bail!("Error shutting down server: {}", e); } diff --git a/src/ui/headless.rs b/src/ui/headless.rs index 1e5606f..e25d493 100644 --- a/src/ui/headless.rs +++ b/src/ui/headless.rs @@ -2,7 +2,7 @@ use std::time; use std::thread; pub fn run() { - println!("Starting up UI (headless)"); + info!("Starting up UI (headless)"); loop { thread::sleep(time::Duration::from_secs(10)); } diff --git a/src/ui/windows.rs b/src/ui/windows.rs index 87ac0a8..5eb2bad 100644 --- a/src/ui/windows.rs +++ b/src/ui/windows.rs @@ -158,7 +158,7 @@ fn remove_notification_icon(window: winapi::HWND) { } fn open_notification_context_menu(window: winapi::HWND) { - println!("Opening notification icon context menu"); + info!("Opening notification icon context menu"); let quit_string = "Quit Polaris".to_win(); unsafe { @@ -187,20 +187,20 @@ fn open_notification_context_menu(window: winapi::HWND) { std::ptr::null_mut()); user32::PostMessageW(window, 0, 0, 0); - println!("Closing notification context menu"); + info!("Closing notification context menu"); user32::DestroyMenu(context_menu); } } fn quit(window: winapi::HWND) { - println!("Shutting down UI"); + info!("Shutting down UI"); unsafe { user32::PostMessageW(window, winapi::winuser::WM_CLOSE, 0, 0); } } pub fn run() { - println!("Starting up UI (Windows)"); + info!("Starting up UI (Windows)"); create_window().expect("Could not initialize window");