extern crate connectr;
use connectr::SpotifyResponse;
+#[macro_use]
+extern crate log;
+extern crate log4rs;
+
+use std::env;
use std::ptr;
use std::thread::sleep;
use std::time::Duration;
status.clear_items();
}
+fn create_logger() {
+ use log::LogLevelFilter;
+ use log4rs::append::console::ConsoleAppender;
+ use log4rs::append::file::FileAppender;
+ use log4rs::encode::pattern::PatternEncoder;
+ use log4rs::config::{Appender, Config, Logger, Root};
+
+ let log_path = format!("{}/{}", env::home_dir().unwrap().display(), ".connectr.log");
+ let stdout = ConsoleAppender::builder()
+ .encoder(Box::new(PatternEncoder::new("{m}{n}")))
+ .build();
+ let requests = FileAppender::builder()
+ .build(&log_path)
+ .unwrap();
+
+ let config = Config::builder()
+ .appender(Appender::builder().build("stdout", Box::new(stdout)))
+ .appender(Appender::builder().build("requests", Box::new(requests)))
+ .logger(Logger::builder().build("app::backend::db", LogLevelFilter::Info))
+ .logger(Logger::builder()
+ .appender("requests")
+ .additive(false)
+ .build("app::requests", LogLevelFilter::Info))
+ .build(Root::builder().appender("stdout").appender("requests").build(LogLevelFilter::Info))
+ .unwrap();
+ let _ = log4rs::init_config(config).unwrap();
+}
+
fn main() {
+ create_logger();
+ info!("Started Connectr");
+
let mut app = ConnectrApp {
menu: MenuItems {
device: Vec::<(MenuItem, String)>::new(),
let mut refresh_time_utc = 0;
let (tx,rx) = channel::<String>();
let mut spotify = connectr::SpotifyConnectr::new();
+ info!("Created Spotify controller.");
spotify.connect();
+ info!("Created Spotify connection.");
spotify.set_target_device(None);
let mut status = osx::OSXStatusBar::new(tx);
+ info!("Created status bar.");
loop {
let now = time::now_utc().to_timespec().sec as i64;
clear_menu(&mut app, &mut spotify, &mut status);
fill_menu(&mut app, &mut spotify, &mut status);
refresh_time_utc = now + 30;
+ info!("Refreshed Spotify state.");
}
spotify.await_once(false);
if let Ok(s) = rx.try_recv() {
println!("Received {}", s);
let cmd: MenuCallbackCommand = json::decode(&s).unwrap();
+ info!("Executed action: {:?}", cmd.action);
match cmd.action {
CallbackAction::SelectDevice => {
let device = &app.menu.device;
// Try to load INI file from home directory
let path = format!("{}/{}", env::home_dir().unwrap().display(), INIFILE);
if path::Path::new(&path).exists() {
+ info!("Found config: {}", path);
return path
}
// such a thing exists.
let bundle_ini = bundled_ini();
if path::Path::new(&bundle_ini).exists() {
+ info!("Copied config: {}", bundle_ini);
let _ = fs::copy(bundle_ini, path.clone());
}
path
}
pub fn read_settings() -> Option<Settings> {
+ info!("Attempting to read config file.");
let conf = match Ini::load_from_file(&inifile()) {
Ok(c) => c,
Err(_) => {
// No connectr.ini found. Generate a junk one in-memory, which
// will fail shortly after with the nice error message.
let mut c = Ini::new();
+ info!("No config file found.");
c.with_section(Some("connectr".to_owned()))
.set("port", 5657.to_string());
c.with_section(Some("application".to_owned()))
let secret = section.get("secret").unwrap();
let client_id = section.get("client_id").unwrap();
if client_id.starts_with('<') || secret.starts_with('<') {
+ error!("No config file found. Exiting.");
println!("");
println!("ERROR: Spotify Client ID or Secret not set in connectr.ini!");
println!("");