Skip to content

Commit 49822a1

Browse files
acuteenvyaome510
andauthored
Allow configuring the log directory (#783)
New config option in `app.toml`: `log_folder` This allows the user to change the location of log and backtrace files. The default is still the same (cache directory). When `RUST_LOG` is set to `off`, the log and backtrace files won't be created. Closes #774. --------- Co-authored-by: Thang Pham <[email protected]>
1 parent 8e69e8c commit 49822a1

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

spotify_player/src/config/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ pub struct AppConfig {
5757

5858
pub login_redirect_uri: String,
5959

60+
pub log_folder: Option<PathBuf>,
61+
6062
pub player_event_hook_command: Option<Command>,
6163

6264
pub playback_format: String,
@@ -268,6 +270,8 @@ impl Default for AppConfig {
268270

269271
login_redirect_uri: "http://127.0.0.1:8989/login".to_string(),
270272

273+
log_folder: None,
274+
271275
tracks_playback_limit: 50,
272276

273277
playback_format: String::from(

spotify_player/src/main.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ fn init_spotify(
3636
Ok(())
3737
}
3838

39-
fn init_logging(cache_folder: &std::path::Path) -> Result<()> {
39+
fn init_logging(log_folder: &std::path::Path) -> Result<()> {
40+
if std::env::var_os("RUST_LOG").is_some_and(|x| x == "off") {
41+
// Don't create log files if logging is disabled.
42+
return Ok(());
43+
}
44+
4045
let log_prefix = format!(
4146
"spotify-player-{}",
4247
chrono::Local::now().format("%y-%m-%d-%H-%M")
@@ -47,7 +52,10 @@ fn init_logging(cache_folder: &std::path::Path) -> Result<()> {
4752
// default to log the current crate and librespot crates
4853
std::env::set_var("RUST_LOG", "spotify_player=info,librespot=info");
4954
}
50-
let log_file = std::fs::File::create(cache_folder.join(format!("{log_prefix}.log")))
55+
if !log_folder.exists() {
56+
std::fs::create_dir_all(log_folder)?;
57+
}
58+
let log_file = std::fs::File::create(log_folder.join(format!("{log_prefix}.log")))
5159
.context("failed to create log file")?;
5260
tracing_subscriber::fmt::fmt()
5361
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
@@ -56,9 +64,8 @@ fn init_logging(cache_folder: &std::path::Path) -> Result<()> {
5664
.init();
5765

5866
// initialize the application's panic backtrace
59-
let backtrace_file =
60-
std::fs::File::create(cache_folder.join(format!("{log_prefix}.backtrace")))
61-
.context("failed to create backtrace file")?;
67+
let backtrace_file = std::fs::File::create(log_folder.join(format!("{log_prefix}.backtrace")))
68+
.context("failed to create backtrace file")?;
6269
let backtrace_file = std::sync::Mutex::new(backtrace_file);
6370
std::panic::set_hook(Box::new(move |info| {
6471
let mut file = backtrace_file.lock().unwrap();
@@ -253,6 +260,10 @@ fn main() -> Result<()> {
253260
// initialize the application configs
254261
{
255262
let mut configs = config::Configs::new(&config_folder, &cache_folder)?;
263+
if configs.app_config.log_folder.is_none() {
264+
// set the log folder to be the cache folder if it is not set
265+
configs.app_config.log_folder = Some(cache_folder);
266+
}
256267
if let Some(theme) = args.get_one::<String>("theme") {
257268
// override the theme config if user specifies a `theme` cli argument
258269
theme.clone_into(&mut configs.app_config.theme);
@@ -263,7 +274,13 @@ fn main() -> Result<()> {
263274
match args.subcommand() {
264275
None => {
265276
// initialize the application's log
266-
init_logging(&cache_folder).context("failed to initialize application's logging")?;
277+
let log_folder = config::get_config()
278+
.app_config
279+
.log_folder
280+
.as_deref()
281+
.expect("log_folder is set");
282+
283+
init_logging(log_folder).context("failed to initialize application's logging")?;
267284

268285
// log the application's configurations
269286
tracing::info!("Configurations: {:?}", config::get_config());

0 commit comments

Comments
 (0)