From e08ed545eec0a04fb9442d459a7aed875ec701bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Tue, 25 Aug 2015 23:51:49 +0200 Subject: [PATCH 1/2] Use log crate instead of println Allows more granular control over what gets logged to the console. --- Cargo.lock | 11 +++++++++++ Cargo.toml | 2 ++ src/audio_file.rs | 2 +- src/lib.rs | 2 ++ src/main.rs | 9 +++++++++ src/mercury.rs | 2 +- src/player.rs | 2 +- 7 files changed, 27 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d0b82371..78b37840 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,6 +6,7 @@ dependencies = [ "byteorder 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "clippy 0.0.55 (registry+https://github.com/rust-lang/crates.io-index)", "dns-sd 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "env_logger 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "eventual 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", "getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", "hyper 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -14,6 +15,7 @@ dependencies = [ "libpulse-sys 0.0.0 (git+https://github.com/astro/libpulse-sys)", "librespot-protocol 0.1.0", "lmdb-rs 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", "num 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)", "openssl 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)", "portaudio 0.2.0 (git+https://github.com/mvdnes/portaudio-rs)", @@ -123,6 +125,15 @@ dependencies = [ "pkg-config 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "env_logger" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "log 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 0.1.58 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "eventual" version = "0.1.6" diff --git a/Cargo.toml b/Cargo.toml index 44e8e5e4..39cc5e90 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,6 +33,8 @@ rustc-serialize = "~0.3.16" tempfile = "~2.0.0" time = "~0.1.34" url = "~0.5.2" +log = "0.3" +env_logger = "0.3" shannon = { git = "https://github.com/plietar/rust-shannon" } vorbis = "~0.0.14" diff --git a/src/audio_file.rs b/src/audio_file.rs index 0c47063c..f61937a8 100644 --- a/src/audio_file.rs +++ b/src/audio_file.rs @@ -122,7 +122,7 @@ impl AudioFile { (index * CHUNK_SIZE / 4) as u32, (CHUNK_SIZE / 4) as u32); - println!("Chunk {}", index); + trace!("Chunk {}", index); write_file.seek(SeekFrom::Start((index * CHUNK_SIZE) as u64)).unwrap(); diff --git a/src/lib.rs b/src/lib.rs index 5c293a31..8fa3c701 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,6 +25,8 @@ extern crate time; extern crate tempfile; extern crate url; +#[macro_use] extern crate log; + #[cfg(not(feature = "with-tremor"))] extern crate vorbis; #[cfg(feature = "with-tremor")] diff --git a/src/main.rs b/src/main.rs index 82875ba6..3c0d384f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ extern crate getopts; extern crate librespot; extern crate rpassword; +extern crate env_logger; use rpassword::read_password; use std::clone::Clone; @@ -8,6 +9,7 @@ use std::fs::File; use std::io::{stdout, Read, Write}; use std::path::PathBuf; use std::thread; +use std::env; use librespot::audio_backend::BACKENDS; use librespot::authentication::{Credentials, facebook_login, discovery_login}; @@ -30,6 +32,13 @@ static APPKEY: Option<&'static [u8]> = Some(include_bytes!(concat!(env!("CARGO_M static APPKEY: Option<&'static [u8]> = None; fn main() { + let rust_log = "RUST_LOG"; + if let Err(_) = env::var(rust_log) { + env::set_var(rust_log, "info") + } + + env_logger::init().unwrap(); + println!("librespot {} ({}). Built on {}.", version::short_sha(), version::commit_date(), diff --git a/src/mercury.rs b/src/mercury.rs index 3cbf1139..69b5c3bd 100644 --- a/src/mercury.rs +++ b/src/mercury.rs @@ -207,7 +207,7 @@ impl PacketHandler for MercuryManager { callback: MercuryCallback::Channel, } } else { - println!("Ignore seq {:?} cmd {}", seq, cmd); + warn!("Ignore seq {:?} cmd {}", seq, cmd); return; }; diff --git a/src/player.rs b/src/player.rs index 48becb9b..7f381470 100644 --- a/src/player.rs +++ b/src/player.rs @@ -224,7 +224,7 @@ impl PlayerInternal { true }); - println!("Load Done"); + info!("Load Done"); } Some(PlayerCommand::Seek(position)) => { vorbis_time_seek_ms(decoder.as_mut().unwrap(), position as i64).unwrap(); From b0ea636179c771eaecfd52a02b3ad754f598c397 Mon Sep 17 00:00:00 2001 From: Marcus Thiesen Date: Thu, 24 Mar 2016 10:31:33 +0100 Subject: [PATCH 2/2] Replace some more println with log --- Cargo.toml | 4 ++-- src/audio_backend/portaudio.rs | 2 +- src/audio_backend/pulseaudio.rs | 2 +- src/lib.rs | 3 ++- src/main.rs | 8 +++++--- src/session.rs | 8 ++++---- src/spirc.rs | 2 +- 7 files changed, 16 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 39cc5e90..9a28d11e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,8 +33,8 @@ rustc-serialize = "~0.3.16" tempfile = "~2.0.0" time = "~0.1.34" url = "~0.5.2" -log = "0.3" -env_logger = "0.3" +log = "0.3.5" +env_logger = "0.3.2" shannon = { git = "https://github.com/plietar/rust-shannon" } vorbis = "~0.0.14" diff --git a/src/audio_backend/portaudio.rs b/src/audio_backend/portaudio.rs index a3c6c1bd..1c96bbfa 100644 --- a/src/audio_backend/portaudio.rs +++ b/src/audio_backend/portaudio.rs @@ -30,7 +30,7 @@ impl <'a> Sink for PortAudioSink<'a> { fn write(&self, data: &[i16]) -> io::Result<()> { match self.0.write(&data) { Ok(_) => (), - Err(portaudio::PaError::OutputUnderflowed) => eprintln!("Underflow"), + Err(portaudio::PaError::OutputUnderflowed) => error!("PortAudio write underflow"), Err(e) => panic!("PA Error {}", e), }; diff --git a/src/audio_backend/pulseaudio.rs b/src/audio_backend/pulseaudio.rs index c76643b0..ba7a2e42 100644 --- a/src/audio_backend/pulseaudio.rs +++ b/src/audio_backend/pulseaudio.rs @@ -9,7 +9,7 @@ pub struct PulseAudioSink(*mut pa_simple); impl Open for PulseAudioSink { fn open() -> PulseAudioSink { - println!("Using PulseAudioSink"); + info!("Using PulseAudioSink"); let ss = pa_sample_spec { format: PA_SAMPLE_S16LE, diff --git a/src/lib.rs b/src/lib.rs index 8fa3c701..76cbb190 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,7 +25,8 @@ extern crate time; extern crate tempfile; extern crate url; -#[macro_use] extern crate log; +#[macro_use] +extern crate log; #[cfg(not(feature = "with-tremor"))] extern crate vorbis; diff --git a/src/main.rs b/src/main.rs index 3c0d384f..42cbe47e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,8 @@ extern crate getopts; extern crate librespot; extern crate rpassword; extern crate env_logger; +#[macro_use] +extern crate log; use rpassword::read_password; use std::clone::Clone; @@ -34,12 +36,12 @@ static APPKEY: Option<&'static [u8]> = None; fn main() { let rust_log = "RUST_LOG"; if let Err(_) = env::var(rust_log) { - env::set_var(rust_log, "info") + env::set_var(rust_log, "debug") } env_logger::init().unwrap(); - println!("librespot {} ({}). Built on {}.", + info!("librespot {} ({}). Built on {}.", version::short_sha(), version::commit_date(), version::short_now()); @@ -150,7 +152,7 @@ fn main() { }).or(stored_credentials) .or_else(|| { if cfg!(feature = "discovery") { - println!("No username provided and no stored credentials, starting discovery ..."); + info!("No username provided and no stored credentials, starting discovery ..."); Some(discovery_login(&session.config().device_name, session.device_id()).unwrap()) } else { diff --git a/src/session.rs b/src/session.rs index 13e87028..d67efdb0 100644 --- a/src/session.rs +++ b/src/session.rs @@ -95,7 +95,7 @@ impl Session { let aps = apresolve().unwrap(); let ap = thread_rng().choose(&aps).expect("No APs found"); - println!("Connecting to AP {}", ap); + info!("Connecting to AP {}", ap); let mut connection = PlainConnection::connect(ap).unwrap(); let request = protobuf_init!(protocol::keyexchange::ClientHello::new(), { @@ -217,7 +217,7 @@ impl Session { *self.0.rx_connection.lock().unwrap() = Some(connection.clone()); *self.0.tx_connection.lock().unwrap() = Some(connection); - eprintln!("Authenticated !"); + info!("Authenticated !"); let reusable_credentials = Credentials { username: username, @@ -231,11 +231,11 @@ impl Session { 0xad => { let msg: protocol::keyexchange::APLoginFailed = protobuf::parse_from_bytes(&data).unwrap(); - eprintln!("Authentication failed, {:?}", msg); + error!("Authentication failed, {:?}", msg); Err(()) } _ => { - println!("Unexpected message {:x}", cmd); + error!("Unexpected message {:x}", cmd); Err(()) } } diff --git a/src/spirc.rs b/src/spirc.rs index 66b28580..319e1312 100644 --- a/src/spirc.rs +++ b/src/spirc.rs @@ -100,7 +100,7 @@ impl SpircManager { let data = pkt.payload.first().unwrap(); let frame = protobuf::parse_from_bytes::(data).unwrap(); - println!("{:?} {} {} {} {}", + debug!("{:?} {} {} {} {}", frame.get_typ(), frame.get_device_state().get_name(), frame.get_ident(),