@@ -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