From 7ba3d766c03df8b93a06608f04d67d831311a288 Mon Sep 17 00:00:00 2001 From: Paul Lietar Date: Thu, 5 Jan 2017 14:25:14 +0100 Subject: [PATCH] Reduce log verbosity by default. Fixes #141 --- src/lib.rs | 1 + src/main.rs | 17 +++++------------ src/main_helper.rs | 40 ++++++++++++++++++++++++++++++++-------- src/player.rs | 4 +++- src/stream.rs | 2 +- 5 files changed, 42 insertions(+), 22 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 336af4d0..bb97d216 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,6 +14,7 @@ extern crate bit_set; extern crate byteorder; extern crate crypto; +extern crate env_logger; extern crate eventual; extern crate getopts; extern crate hyper; diff --git a/src/main.rs b/src/main.rs index 69d7eff8..69fbace4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,10 @@ extern crate getopts; extern crate librespot; -extern crate env_logger; -#[macro_use] -extern crate log; extern crate ctrlc; +use std::io::{stderr, Write}; use std::process::exit; use std::thread; -use std::env; use librespot::spirc::SpircManager; use librespot::main_helper; @@ -18,27 +15,23 @@ fn usage(program: &str, opts: &getopts::Options) -> String { } fn main() { - if env::var("RUST_LOG").is_err() { - env::set_var("RUST_LOG", "mdns=info,librespot=trace") - } - env_logger::init().unwrap(); - let mut opts = getopts::Options::new(); main_helper::add_session_arguments(&mut opts); main_helper::add_authentication_arguments(&mut opts); main_helper::add_player_arguments(&mut opts); - main_helper::add_program_arguments(&mut opts); let args: Vec = std::env::args().collect(); let matches = match opts.parse(&args[1..]) { Ok(m) => m, Err(f) => { - error!("Error: {}\n{}", f.to_string(), usage(&args[0], &opts)); - exit(1) + writeln!(stderr(), "error: {}\n{}", f.to_string(), usage(&args[0], &opts)).unwrap(); + exit(1); } }; + main_helper::setup_logging(&matches); + let session = main_helper::create_session(&matches); let credentials = main_helper::get_credentials(&session, &matches); session.login(credentials).unwrap(); diff --git a/src/main_helper.rs b/src/main_helper.rs index 43c9d50d..9982bd4f 100644 --- a/src/main_helper.rs +++ b/src/main_helper.rs @@ -1,5 +1,7 @@ +use env_logger::LogBuilder; use getopts; use rpassword; +use std::env; use std::io::{stderr, Write}; use std::path::PathBuf; use std::process::exit; @@ -37,7 +39,10 @@ pub fn find_backend(name: Option<&str>) -> &'static (Fn(Option<&str>) -> Box Session { @@ -136,3 +136,27 @@ pub fn create_player(session: &Session, matches: &getopts::Matches) -> Player { make_backend(device_name.as_ref().map(AsRef::as_ref)) }) } + +pub fn setup_logging(matches: &getopts::Matches) { + let verbose = matches.opt_present("verbose"); + let mut builder = LogBuilder::new(); + + match env::var("RUST_LOG") { + Ok(config) => { + builder.parse(&config); + builder.init().unwrap(); + + if verbose { + warn!("`--verbose` flag overidden by `RUST_LOG` environment variable"); + } + } + Err(_) => { + if verbose { + builder.parse("mdns=info,librespot=trace"); + } else { + builder.parse("mdns=info,librespot=info"); + } + builder.init().unwrap(); + } + } +} diff --git a/src/player.rs b/src/player.rs index 822b6d10..ca496dbe 100644 --- a/src/player.rs +++ b/src/player.rs @@ -180,10 +180,12 @@ fn find_available_alternative<'a>(session: &Session, track: &'a Track) -> Option fn load_track(session: &Session, track_id: SpotifyId) -> Option>>>> { let track = session.metadata::(track_id).await().unwrap(); + info!("Loading track {:?}", track.name); + let track = match find_available_alternative(session, &track) { Some(track) => track, None => { - warn!("Track \"{}\" is not available", track.name); + warn!("Track {:?} is not available", track.name); return None; } }; diff --git a/src/stream.rs b/src/stream.rs index 83d6ef23..4b802ffd 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -51,7 +51,7 @@ impl Channel { packet.read_u16::().unwrap(); // Skip channel id if cmd == 0xa { - error!("error: {} {}", data.len(), packet.read_u16::().unwrap()); + trace!("error: {} {}", data.len(), packet.read_u16::().unwrap()); return match handler.box_on_error(session) { Response::Continue(_) => Response::Close, Response::Spawn(f) => Response::Spawn(f),