update project to use log crate and replace println! calls with equivalent log level macro calls

This commit is contained in:
João Oliveira 2017-10-13 01:09:54 +01:00
parent 953dea1929
commit 55d6e412d6
9 changed files with 65 additions and 22 deletions

23
Cargo.lock generated
View file

@ -14,6 +14,7 @@ dependencies = [
"iron 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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)", "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)", "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)", "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)", "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_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)", "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)", "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)", "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)", "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)", "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)", "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]] [[package]]
name = "siphasher" name = "siphasher"
version = "0.2.2" version = "0.2.2"
@ -1460,6 +1472,15 @@ dependencies = [
"rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", "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]] [[package]]
name = "thread_local" name = "thread_local"
version = "0.3.4" 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 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)" = "<none>" "checksum shell32-sys 0.1.1 (git+https://github.com/retep998/winapi-rs?branch=0.2)" = "<none>"
"checksum shell32-sys 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "72f20b8f3c060374edb8046591ba28f62448c369ccbdc7b02075103fb3a9e38d" "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 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 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" "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 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 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 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 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 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" "checksum tokio-core 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "6a20ba4738d283cac7495ca36e045c80c2a8df3e05dd0909b17a06646af5a7ed"

View file

@ -34,6 +34,8 @@ staticfile = "0.4.0"
toml = "0.4.5" toml = "0.4.5"
typemap = "0.3" typemap = "0.3"
url = "1.2.0" url = "1.2.0"
log = "0.3.8"
simplelog = "0.4.2"
[dependencies.rusqlite] [dependencies.rusqlite]
version = "0.12.0" version = "0.12.0"

View file

@ -63,7 +63,7 @@ pub fn parse_json(content: &str) -> Result<Config> {
} }
pub fn parse_toml_file(path: &path::Path) -> Result<Config> { pub fn parse_toml_file(path: &path::Path) -> Result<Config> {
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 = fs::File::open(path)?;
let mut config_file_content = String::new(); let mut config_file_content = String::new();
config_file.read_to_string(&mut config_file_content)?; config_file.read_to_string(&mut config_file_content)?;

View file

@ -27,7 +27,7 @@ pub struct DB {
impl DB { impl DB {
pub fn new(path: &Path) -> Result<DB> { pub fn new(path: &Path) -> Result<DB> {
println!("Database file path: {}", path.to_string_lossy()); info!("Database file path: {}", path.to_string_lossy());
let connection = let connection =
Arc::new(Mutex::new(SqliteConnection::establish(&path.to_string_lossy())?)); Arc::new(Mutex::new(SqliteConnection::establish(&path.to_string_lossy())?));
let db = DB { connection: connection.clone() }; let db = DB { connection: connection.clone() };

View file

@ -66,7 +66,7 @@ fn update_my_ip<T>(config_source: &T) -> Result<(), DDNSError>
{ {
let config = config_source.get_ddns_config()?; let config = config_source.get_ddns_config()?;
if config.host.len() == 0 || config.username.len() == 0 { 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(()); return Ok(());
} }
@ -91,7 +91,7 @@ pub fn run<T>(config_source: &T)
{ {
loop { loop {
if let Err(e) = update_my_ip(config_source) { 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)); thread::sleep(time::Duration::from_secs(60 * 30));
} }

View file

@ -207,7 +207,7 @@ impl<'conn> IndexBuilder<'conn> {
let file_path = match file { let file_path = match file {
Ok(f) => f.path(), Ok(f) => f.path(),
_ => { _ => {
println!("File read error within {}", path_string); error!("File read error within {}", path_string);
break; break;
} }
}; };
@ -378,10 +378,10 @@ pub fn update<T>(db: &T) -> Result<()>
where T: ConnectionSource + VFSSource where T: ConnectionSource + VFSSource
{ {
let start = time::Instant::now(); let start = time::Instant::now();
println!("Beginning library index update"); info!("Beginning library index update");
clean(db)?; clean(db)?;
populate(db)?; populate(db)?;
println!("Library index update took {} seconds", info!("Library index update took {} seconds",
start.elapsed().as_secs()); start.elapsed().as_secs());
Ok(()) Ok(())
} }
@ -392,7 +392,7 @@ pub fn update_loop<T>(db: &T, command_buffer: Receiver<Command>)
loop { loop {
// Wait for a command // Wait for a command
if let Err(e) = command_buffer.recv() { 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; return;
} }
@ -400,7 +400,7 @@ pub fn update_loop<T>(db: &T, command_buffer: Receiver<Command>)
loop { loop {
match command_buffer.try_recv() { match command_buffer.try_recv() {
Err(TryRecvError::Disconnected) => { Err(TryRecvError::Disconnected) => {
println!("Error while flushing index command buffer"); error!("Error while flushing index command buffer");
return; return;
} }
Err(TryRecvError::Empty) => break, Err(TryRecvError::Empty) => break,
@ -410,7 +410,7 @@ pub fn update_loop<T>(db: &T, command_buffer: Receiver<Command>)
// Do the update // Do the update
if let Err(e) = update(db) { 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<T>(db: &T, command_buffer: Arc<Mutex<Sender<Command>>>)
let command_buffer = command_buffer.lock().unwrap(); let command_buffer = command_buffer.lock().unwrap();
let command_buffer = command_buffer.deref(); let command_buffer = command_buffer.deref();
if let Err(e) = command_buffer.send(Command::REINDEX) { 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; return;
} }
} }
@ -434,7 +434,7 @@ pub fn self_trigger<T>(db: &T, command_buffer: Arc<Mutex<Sender<Command>>>)
.get_result(connection.deref()) .get_result(connection.deref())
.map_err(|e| e.into()); .map_err(|e| e.into());
if let Err(ref e) = settings { 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 sleep_duration = settings
.map(|s| s.index_sleep_duration_seconds) .map(|s| s.index_sleep_duration_seconds)

View file

@ -32,6 +32,9 @@ extern crate staticfile;
extern crate toml; extern crate toml;
extern crate typemap; extern crate typemap;
extern crate url; extern crate url;
#[macro_use]
extern crate log;
extern crate simplelog;
#[cfg(windows)] #[cfg(windows)]
extern crate uuid; extern crate uuid;
@ -59,6 +62,7 @@ use staticfile::Static;
use std::path::Path; use std::path::Path;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::sync::mpsc::channel; use std::sync::mpsc::channel;
use simplelog::{Config, TermLogger, LogLevelFilter};
mod api; mod api;
mod config; mod config;
@ -111,6 +115,7 @@ fn run() -> Result<()> {
options.optopt("p", "port", "set polaris to run on a custom port", "PORT"); 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("d", "database", "set the path to index database", "FILE");
options.optopt("w", "web", "set the path to web client files", "DIRECTORY"); 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)] #[cfg(unix)]
options.optflag("f", options.optflag("f",
@ -128,11 +133,23 @@ fn run() -> Result<()> {
return Ok(()); 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)] #[cfg(unix)]
daemonize(&matches)?; daemonize(&matches)?;
// Init DB // Init DB
println!("Starting up database"); info!("Starting up database");
let db_path = matches.opt_str("d"); let db_path = matches.opt_str("d");
let mut default_db_path = utils::get_data_root()?; let mut default_db_path = utils::get_data_root()?;
default_db_path.push("db.sqlite"); default_db_path.push("db.sqlite");
@ -167,7 +184,7 @@ fn run() -> Result<()> {
// Mount API // Mount API
let prefix_url = config.prefix_url.unwrap_or("".to_string()); let prefix_url = config.prefix_url.unwrap_or("".to_string());
let api_url = format!("{}/api", &prefix_url); 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 mut mount = Mount::new();
let handler = api::get_handler(db.clone(), index_sender)?; let handler = api::get_handler(db.clone(), index_sender)?;
mount.mount(&api_url, handler); mount.mount(&api_url, handler);
@ -175,7 +192,7 @@ fn run() -> Result<()> {
// Mount static files // Mount static files
let static_url = format!("/{}", &prefix_url); 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 web_dir_name = matches.opt_str("w");
let mut default_web_dir = utils::get_data_root()?; let mut default_web_dir = utils::get_data_root()?;
default_web_dir.push("web"); default_web_dir.push("web");
@ -185,12 +202,13 @@ fn run() -> Result<()> {
mount.mount(&static_url, Static::new(web_dir_path)); mount.mount(&static_url, Static::new(web_dir_path));
println!("Starting up server"); info!("Starting up server");
let port: u16 = matches let port: u16 = matches
.opt_str("p") .opt_str("p")
.unwrap_or("5050".to_owned()) .unwrap_or("5050".to_owned())
.parse() .parse()
.or(Err("invalid port number"))?; .or(Err("invalid port number"))?;
let mut server = match Iron::new(mount).http(("0.0.0.0", port)) { let mut server = match Iron::new(mount).http(("0.0.0.0", port)) {
Ok(s) => s, Ok(s) => s,
Err(e) => bail!("Error starting up server: {}", e), Err(e) => bail!("Error starting up server: {}", e),
@ -203,7 +221,7 @@ fn run() -> Result<()> {
// Run UI // Run UI
ui::run(); ui::run();
println!("Shutting down server"); info!("Shutting down server");
if let Err(e) = server.close() { if let Err(e) = server.close() {
bail!("Error shutting down server: {}", e); bail!("Error shutting down server: {}", e);
} }

View file

@ -2,7 +2,7 @@ use std::time;
use std::thread; use std::thread;
pub fn run() { pub fn run() {
println!("Starting up UI (headless)"); info!("Starting up UI (headless)");
loop { loop {
thread::sleep(time::Duration::from_secs(10)); thread::sleep(time::Duration::from_secs(10));
} }

View file

@ -158,7 +158,7 @@ fn remove_notification_icon(window: winapi::HWND) {
} }
fn open_notification_context_menu(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(); let quit_string = "Quit Polaris".to_win();
unsafe { unsafe {
@ -187,20 +187,20 @@ fn open_notification_context_menu(window: winapi::HWND) {
std::ptr::null_mut()); std::ptr::null_mut());
user32::PostMessageW(window, 0, 0, 0); user32::PostMessageW(window, 0, 0, 0);
println!("Closing notification context menu"); info!("Closing notification context menu");
user32::DestroyMenu(context_menu); user32::DestroyMenu(context_menu);
} }
} }
fn quit(window: winapi::HWND) { fn quit(window: winapi::HWND) {
println!("Shutting down UI"); info!("Shutting down UI");
unsafe { unsafe {
user32::PostMessageW(window, winapi::winuser::WM_CLOSE, 0, 0); user32::PostMessageW(window, winapi::winuser::WM_CLOSE, 0, 0);
} }
} }
pub fn run() { pub fn run() {
println!("Starting up UI (Windows)"); info!("Starting up UI (Windows)");
create_window().expect("Could not initialize window"); create_window().expect("Could not initialize window");