From b318ac84e6584e4c2b3c67de243fe6c06f055aee Mon Sep 17 00:00:00 2001 From: Henrik Friedrichsen Date: Sat, 23 Mar 2019 15:48:54 +0100 Subject: [PATCH] remove debug view + implement debug logging to file closes #41 --- Cargo.toml | 3 +++ README.md | 3 +-- src/commands.rs | 12 ------------ src/main.rs | 50 +++++++++++++++++++++++++++++++++++++++++-------- 4 files changed, 46 insertions(+), 22 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e9bea54..f2a99d5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,9 +13,12 @@ travis-ci = { repository = "hrkfdn/ncspot", branch = "master" } maintenance = { status = "experimental" } [dependencies] +clap = "2.32.0" +chrono = "0.4" crossbeam-channel = "0.3.8" directories = "1.0" failure = "0.1.3" +fern = "0.5" futures = "0.1" log = "0.4.0" serde = "1.0" diff --git a/README.md b/README.md index 4dca87d..dfd333c 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ future. Contributions welcome, but please be kind ;) * Set your login credentials (see configuration) * Build using `cargo build --release` -* The initial screen is the debug log. Press `F2` for the queue and `F3` to +* The initial screen is the debug log. Press `F1` for the queue and `F2` to search for a track. More key bindings are described below. ### Key Bindings @@ -39,7 +39,6 @@ have them configurable. * `F3`: Playlists * `d` deletes the currently selected track * `c` clears the entire playlist -* `F9`: Debug log * Tracks and playlists can be played using `Return` and queued using `Space` * `Shift-p` toggles playback of a track * `Shift-s` stops a track diff --git a/src/commands.rs b/src/commands.rs index 2c68bb8..37de3f5 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -129,17 +129,6 @@ impl CommandManager { ); } - self.register( - "log", - Vec::new(), - Box::new(move |s, _args| { - s.call_on_id("main", |v: &mut Layout| { - v.set_view("log"); - }); - Ok(None) - }), - ); - self.register( "move", Vec::new(), @@ -365,7 +354,6 @@ impl CommandManager { kb.insert("F1".into(), "queue".into()); kb.insert("F2".into(), "search".into()); kb.insert("F3".into(), "playlists".into()); - kb.insert("F9".into(), "log".into()); kb.insert("Up".into(), "move up".into()); kb.insert("Down".into(), "move down".into()); diff --git a/src/main.rs b/src/main.rs index e68a8bd..996b42c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +extern crate clap; extern crate crossbeam_channel; #[macro_use] extern crate cursive; @@ -21,14 +22,15 @@ extern crate toml; #[macro_use] extern crate log; +extern crate chrono; +extern crate fern; use std::process; use std::sync::Arc; use std::thread; +use clap::{Arg, App}; use cursive::traits::Identifiable; -use cursive::view::ScrollStrategy; -use cursive::views::*; use cursive::Cursive; mod commands; @@ -50,8 +52,45 @@ use events::{Event, EventManager}; use playlists::Playlists; use spotify::PlayerEvent; +fn setup_logging(filename: &str) -> Result<(), fern::InitError> { + fern::Dispatch::new() + // Perform allocation-free log formatting + .format(|out, message, record| { + out.finish(format_args!( + "{} [{}] [{}] {}", + chrono::Local::now().format("[%Y-%m-%d][%H:%M:%S]"), + record.target(), + record.level(), + message + )) + }) + // Add blanket level filter - + .level(log::LevelFilter::Trace) + // - and per-module overrides + .level_for("librespot", log::LevelFilter::Debug) + // Output to stdout, files, and other Dispatch configurations + .chain(fern::log_file(filename)?) + // Apply globally + .apply()?; + Ok(()) +} + fn main() { - cursive::logger::init(); + let matches = App::new("ncspot") + .version("0.1.0") + .author("Henrik Friedrichsen ") + .about("cross-platform ncurses Spotify client") + .arg(Arg::with_name("debug") + .short("d") + .long("debug") + .value_name("FILE") + .help("Enable debug logging to the specified file") + .takes_value(true)) + .get_matches(); + + if let Some(filename) = matches.value_of("debug") { + setup_logging(filename).expect("can't setup logging"); + } // Things here may cause the process to abort; we must do them before creating curses windows // otherwise the error message will not be seen by a user @@ -74,8 +113,6 @@ fn main() { let theme = theme::load(&cfg); - let logview = DebugView::new(); - let mut cursive = Cursive::default(); cursive.set_theme(theme.clone()); @@ -115,13 +152,10 @@ fn main() { let queueview = ui::queue::QueueView::new(queue.clone()); - let logview_scroller = ScrollView::new(logview).scroll_strategy(ScrollStrategy::StickToBottom); - let status = ui::statusbar::StatusBar::new(queue.clone(), spotify.clone()); let mut layout = ui::layout::Layout::new(status, &event_manager, theme) .view("search", search.with_id("search"), "Search") - .view("log", logview_scroller, "Debug Log") .view("playlists", playlistsview, "Playlists") .view("queue", queueview, "Queue");