Repository: Spotifyd/spotifyd (language: rust)

Issue:
Thread 'main' panicked at 'Couldn't initialize logger'...
**Description**

I am using `Linux OTM 5.19.2-artix1-2 #1 SMP PREEMPT_DYNAMIC Sun, 21 Aug 2022 10:48:27 +0000 x86_64 GNU/Linux` from base and when I configured spotifyd, it showed the following error:
`thread 'main' panicked at 'Couldn't initialize logger: Error(Initialization, State { next_error: Some(Error(Io(Os { code: 2, kind: NotFound, message: "No such file or directory" }), State { next_error: None, backtrace: None })), backtrace: None })', src/main.rs:39:51
note: run with RUST_BACKTRACE=1 environment variable to display a backtrace`

**To Reproduce**

Steps to reproduce the behavior:
1. Run spotifyd without [syslog-ng](https://github.com/syslog-ng/syslog-ng) installed
2. See error

**Expected behavior**

spotifyd working

**Logs**

<details><summary>Click to show logs</summary>

```
thread 'main' panicked at 'Couldn't initialize logger: Error(Initialization, State { next_error: Some(Error(Io(Os { code: 111, kind: ConnectionRefused, message: "Connection refused" }), State { next_error: None, backtrace: Some(   0: <unknown>
   1: <unknown>
   2: <unknown>
   3: <unknown>
   4: <unknown>
   5: <unknown>
   6: <unknown>
   7: <unknown>
   8: <unknown>
   9: main
  10: <unknown>
  11: __libc_start_main
  12: <unknown>
) })), backtrace: Some(   0: <unknown>
   1: <unknown>
   2: <unknown>
   3: <unknown>
   4: <unknown>
   5: <unknown>
   6: <unknown>
   7: <unknown>
   8: <unknown>
   9: main
  10: <unknown>
  11: __libc_start_main
  12: <unknown>
) })', src/main.rs:39:51
stack backtrace:
   0:     0x561c92caba4d - <unknown>
   1:     0x561c92cd3f6c - <unknown>
   2:     0x561c92ca4c61 - <unknown>
   3:     0x561c92cad4a5 - <unknown>
   4:     0x561c92cad1c6 - <unknown>
   5:     0x561c92cada36 - <unknown>
   6:     0x561c92cad927 - <unknown>
   7:     0x561c92cabf04 - <unknown>
   8:     0x561c92cad659 - <unknown>
   9:     0x561c925e53b3 - <unknown>
  10:     0x561c925e54a3 - <unknown>
  11:     0x561c926764cb - <unknown>
  12:     0x561c9265a873 - <unknown>
  13:     0x561c92638a29 - <unknown>
  14:     0x561c92c9d92e - <unknown>
  15:     0x561c926770c2 - main
  16:     0x7f721d1342d0 - <unknown>
  17:     0x7f721d13438a - __libc_start_main
  18:     0x561c925e5655 - <unknown>
  19:                0x0 - <unknown>
```

</details>

**Versions (please complete the following information):**
<!-- DO NOT use words like `latest`. Please specify the exact version/commit hash -->
- OS: <!-- e.g. Ubuntu 18.04 LTS, Windows 10 -->
- Spotifyd: [spotifyd-full-git 0.2.17.7.g8a82bec-1](https://aur.archlinux.org/packages/spotifyd-full-git) & [spotifyd 0.3.3-1](https://archlinux.org/packages/community/x86_64/spotifyd/)
- cargo: 1.63.0

**Additional context**
I think it'd be nice if a more specific error was written. I looked up the line of code where the problem was and I figured out it was because I didn't have **syslog-ng** installed. Since I tested both programs on the latest Artix Linux with Runit, here are the steps I made in order to solve this problem:
`sudo pacman -Syu syslog-ng syslog-ng-runit`
`sudo ln -s /etc/runit/sv/syslog-ng /etc/runit/runsvdir/default`
`sudo sv start syslog-ng`

Once done that, I was able to launch spotifyd without further issues.



Discussion / hints:

Fix patch:
diff --git a/src/main.rs b/src/main.rs
index a73f2712..4f7a0476 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -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;
 
@@ -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 {
@@ -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;
@@ -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()
@@ -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);


Test patch: