From b19b0cb40d3449df214834679a5aa3a1568556e7 Mon Sep 17 00:00:00 2001 From: galister <22305755+galister@users.noreply.github.com> Date: Wed, 10 Apr 2024 16:30:26 +0900 Subject: [PATCH] feat: log to /tmp by default --- README.md | 4 ++++ src/main.rs | 61 ++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 50 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 3c7dec8..d132dbd 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,10 @@ Typing If your bindings are not supported, please reach out. \ We would like to work with you and include additional bindings. +# Troubleshooting + +Check [here](https://github.com/galister/wlx-overlay-s/wiki/Troubleshooting) for tips. + # Known Issues ## Auto-Start not working on SteamVR 2.4.4 diff --git a/src/main.rs b/src/main.rs index 7fcc469..b76289b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,7 +18,7 @@ use std::{ }; use clap::Parser; -use flexi_logger::FileSpec; +use flexi_logger::{Duplicate, FileSpec, LogSpecification}; /// The lightweight desktop overlay for OpenVR and OpenXR #[derive(Parser, Debug)] @@ -44,26 +44,15 @@ struct Args { } fn main() -> Result<(), Box> { - let args = Args::parse(); - let maybe_logfile = std::env::var("WLX_LOGFILE").ok(); - - if let Some(ref log_to) = args.log_to.as_ref().or(maybe_logfile.as_ref()) { - let file_spec = FileSpec::try_from(PathBuf::from(log_to))?; - flexi_logger::Logger::try_with_env_or_str("info")? - .log_to_file(file_spec) - .duplicate_to_stderr(flexi_logger::Duplicate::Info) - .start()?; - println!(" ****** Logging to: {} ******", &log_to); - println!(" ****** Console logs limited to Info ******"); - } else { - flexi_logger::Logger::try_with_env_or_str("info")?.start()?; - } + let mut args = Args::parse(); + logging_init(&mut args)?; log::info!( "Welcome to {} version {}!", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION") ); + log::info!("It is {}.", chrono::Local::now().format("%c")); #[cfg(feature = "openvr")] if args.uninstall { @@ -143,3 +132,45 @@ fn args_get_openxr(_args: &Args) -> bool { ret } + +fn logging_init(args: &mut Args) -> anyhow::Result<()> { + let log_file = args + .log_to + .take() + .or_else(|| std::env::var("WLX_LOGFILE").ok()) + .or_else(|| Some("/tmp/wlx.log".to_string())); + + if let Some(log_to) = log_file.filter(|s| !s.is_empty()) { + if let Err(e) = file_logging_init(&log_to) { + log::error!("Failed to initialize file logging: {}", e); + flexi_logger::Logger::try_with_env_or_str("info")?.start()?; + } + } else { + flexi_logger::Logger::try_with_env_or_str("info")?.start()?; + } + Ok(()) +} + +fn file_logging_init(log_to: &str) -> anyhow::Result<()> { + let file_spec = FileSpec::try_from(PathBuf::from(log_to))?; + let log_spec = LogSpecification::env_or_parse("info")?; + + let duplicate = log_spec + .module_filters() + .iter() + .find(|m| m.module_name.is_none()) + .map(|m| match m.level_filter { + log::LevelFilter::Trace => Duplicate::Trace, + log::LevelFilter::Debug => Duplicate::Debug, + log::LevelFilter::Info => Duplicate::Info, + log::LevelFilter::Warn => Duplicate::Warn, + _ => Duplicate::Error, + }); + + flexi_logger::Logger::with(log_spec) + .log_to_file(file_spec) + .duplicate_to_stderr(duplicate.unwrap_or(Duplicate::Error)) + .start()?; + println!("Logging to: {}", log_to); + Ok(()) +}