From 44d074d8647a273219c3abac930707fed51ace07 Mon Sep 17 00:00:00 2001 From: Observer KRypt0n_ Date: Sat, 11 Feb 2023 12:34:39 +0200 Subject: [PATCH] Changed debug logging - stdout logs got rid of rustls messages and now are pretty styled - logs are saved into the `debug.log` file in the launcher folder they're not filtered and contain all the messages - toasts timeout fixed to 5 seconds - added "save" button which will, well, not save logs but open `debug.log` file using `xdg-open` --- assets/locales/en/main.ftl | 1 + assets/locales/ru/main.ftl | 1 + src/main.rs | 37 +++++++++++++++++++++++++++++++------ src/ui/preferences/main.rs | 16 +++++++++++++++- 4 files changed, 48 insertions(+), 7 deletions(-) diff --git a/assets/locales/en/main.ftl b/assets/locales/en/main.ftl index 2e749ed..11dd709 100644 --- a/assets/locales/en/main.ftl +++ b/assets/locales/en/main.ftl @@ -3,6 +3,7 @@ none = None default = Default details = Details close = Close +save = Save checking-free-space = Checking free space diff --git a/assets/locales/ru/main.ftl b/assets/locales/ru/main.ftl index f06f0c2..8b613a0 100644 --- a/assets/locales/ru/main.ftl +++ b/assets/locales/ru/main.ftl @@ -3,6 +3,7 @@ none = Нет default = По умолчанию details = Подробнее close = Закрыть +save = Сохранить checking-free-space = Проверка свободного места diff --git a/src/main.rs b/src/main.rs index b0ff9d1..795a7bb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,8 @@ use anime_launcher_sdk::config; use anime_launcher_sdk::anime_game_core::prelude::*; use anime_launcher_sdk::anime_game_core::genshin::prelude::*; +use tracing_subscriber::prelude::*; + pub mod i18n; pub mod ui; @@ -25,6 +27,9 @@ pub fn is_ready() -> bool { } lazy_static::lazy_static! { + /// Path to `debug.log` file. Standard is `$HOME/.local/share/anime-game-launcher/debug.log` + pub static ref DEBUG_FILE: std::path::PathBuf = anime_launcher_sdk::consts::launcher_dir().unwrap_or_default().join("debug.log"); + /// Config loaded on the app's start. Use `config::get()` to get up to date config instead. /// This one is used to prepare some launcher UI components on start pub static ref CONFIG: config::Config = config::get().expect("Failed to load config"); @@ -56,12 +61,32 @@ lazy_static::lazy_static! { } fn main() { - tracing_subscriber::fmt() - .with_span_events(tracing_subscriber::fmt::format::FmtSpan::FULL) - .with_max_level(if APP_DEBUG || std::env::args().any(|arg| &arg == "--debug") { - tracing::Level::TRACE - } else { - tracing::Level::WARN + let stdout = tracing_subscriber::fmt::layer().pretty(); + + let file = match std::fs::File::create(DEBUG_FILE.as_path()) { + Ok(file) => file, + Err(error) => panic!("Failed to create debug.log file: {:?}", error) + }; + + let mut debug_log = tracing_subscriber::fmt::layer() + .with_writer(std::sync::Arc::new(file)); + + debug_log.set_ansi(false); + + tracing_subscriber::registry() + .with({ + stdout + .with_filter(tracing_subscriber::filter::LevelFilter::from_level({ + if APP_DEBUG || std::env::args().any(|arg| &arg == "--debug") { + tracing::Level::TRACE + } else { + tracing::Level::WARN + } + })) + .with_filter(tracing_subscriber::filter::filter_fn(|metadata| { + !metadata.target().starts_with("rustls") + })) + .and_then(debug_log) }) .init(); diff --git a/src/ui/preferences/main.rs b/src/ui/preferences/main.rs index 7b10b22..0ed5fe0 100644 --- a/src/ui/preferences/main.rs +++ b/src/ui/preferences/main.rs @@ -222,7 +222,7 @@ impl SimpleAsyncComponent for App { AppMsg::Toast { title, description } => unsafe { let toast = adw::Toast::new(&title); - toast.set_timeout(5000); + toast.set_timeout(5); if let Some(description) = description { toast.set_button_label(Some(&tr("details"))); @@ -230,6 +230,20 @@ impl SimpleAsyncComponent for App { let dialog = adw::MessageDialog::new(PREFERENCES_WINDOW.as_ref(), Some(&title), Some(&description)); dialog.add_response("close", &tr("close")); + dialog.add_response("save", &tr("save")); + + dialog.set_response_appearance("save", adw::ResponseAppearance::Suggested); + + #[allow(unused_must_use)] + dialog.connect_response(Some("save"), |_, _| { + let result = std::process::Command::new("xdg-open") + .arg(crate::DEBUG_FILE.as_os_str()) + .output(); + + if let Err(err) = result { + tracing::error!("Failed to open debug file: {}", err); + } + }); toast.connect_button_clicked(move |_| { dialog.show();