Skip to content

Commit

Permalink
Merge pull request Spotifyd#1108 from eladyn/improve_error_reporting
Browse files Browse the repository at this point in the history
improve error reporting
  • Loading branch information
eladyn authored Sep 26, 2022
2 parents cb976ce + e555ef6 commit a4316df
Showing 1 changed file with 14 additions and 22 deletions.
36 changes: 14 additions & 22 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#![cfg(unix)]

use crate::config::CliConfig;
use color_eyre::{eyre::Context, Help, Report, SectionExt};
use color_eyre::{
eyre::{self, eyre, Context},
Help, SectionExt,
};
use daemonize::Daemonize;
use log::{error, info, trace, LevelFilter};
use std::panic;
use structopt::StructOpt;
use tokio::runtime::Runtime;

Expand All @@ -24,7 +26,7 @@ enum LogTarget {
Syslog,
}

fn setup_logger(log_target: LogTarget, verbose: bool) {
fn setup_logger(log_target: LogTarget, verbose: bool) -> eyre::Result<()> {
let log_level = if verbose {
LevelFilter::Trace
} else {
Expand All @@ -46,14 +48,19 @@ fn setup_logger(log_target: LogTarget, verbose: bool) {
process: "spotifyd".to_owned(),
pid: 0,
};
logger.chain(syslog::unix(log_format).expect("Couldn't initialize logger"))
logger.chain(
syslog::unix(log_format)
.map_err(|e| eyre!("Couldn't connect to syslog instance: {}", e))?,
)
}
};

logger.apply().expect("Couldn't initialize logger");
logger.apply().wrap_err("Couldn't initialize logger")
}

fn main() -> Result<(), Report> {
fn main() -> eyre::Result<()> {
color_eyre::install().wrap_err("Couldn't initialize error reporting")?;

let mut cli_config: CliConfig = CliConfig::from_args();

let is_daemon = !cli_config.no_daemon;
Expand All @@ -64,8 +71,7 @@ fn main() -> Result<(), Report> {
LogTarget::Terminal
};

setup_logger(log_target, cli_config.verbose);
color_eyre::install().expect("Coundn't initialize error reporting");
setup_logger(log_target, cli_config.verbose)?;

cli_config
.load_config_file_values()
Expand Down Expand Up @@ -95,20 +101,6 @@ fn main() -> Result<(), Report> {
};
}

panic::set_hook(Box::new(|panic_info| {
error!(
"PANIC: Shutting down spotifyd. Error message: {}",
match (
panic_info.payload().downcast_ref::<String>(),
panic_info.payload().downcast_ref::<&str>(),
) {
(Some(s), _) => &**s,
(_, Some(&s)) => s,
_ => "Unknown error type, can't produce message.",
}
);
}));

let runtime = Runtime::new().unwrap();
runtime.block_on(async {
let mut initial_state = setup::initial_state(internal_config);
Expand Down

0 comments on commit a4316df

Please sign in to comment.