From a246e5acb64981b70d670f980baa18dc92f1b621 Mon Sep 17 00:00:00 2001 From: Lucas Schwiderski Date: Thu, 6 Apr 2023 14:49:43 +0200 Subject: [PATCH] feat(dtmm): Log to file --- crates/dtmm/src/controller/app.rs | 9 ++++++++- crates/dtmm/src/controller/worker.rs | 12 ++++++++++++ crates/dtmm/src/state/delegate.rs | 17 +++++++++++++++-- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/crates/dtmm/src/controller/app.rs b/crates/dtmm/src/controller/app.rs index 3073b9b..d732801 100644 --- a/crates/dtmm/src/controller/app.rs +++ b/crates/dtmm/src/controller/app.rs @@ -9,7 +9,7 @@ use druid::im::Vector; use druid::{FileInfo, ImageBuf}; use dtmt_shared::ModConfig; use nexusmods::Api as NexusApi; -use tokio::fs::{self, DirEntry}; +use tokio::fs::{self, DirEntry, File}; use tokio_stream::wrappers::ReadDirStream; use tokio_stream::StreamExt; use zip::ZipArchive; @@ -424,6 +424,13 @@ pub(crate) async fn load_initial(path: PathBuf, is_default: bool) -> Result tokio::spawn(async move { + if let Ok(mut f) = OpenOptions::new() + .append(true) + .open(state.data_dir.join("dtmm.log")) + .await + { + let _ = f.write_all(&line).await; + } + }), }; } } diff --git a/crates/dtmm/src/state/delegate.rs b/crates/dtmm/src/state/delegate.rs index 1cf3cda..d5ae042 100644 --- a/crates/dtmm/src/state/delegate.rs +++ b/crates/dtmm/src/state/delegate.rs @@ -92,6 +92,7 @@ pub(crate) enum AsyncAction { SaveSettings(ActionState), CheckUpdates(ActionState), LoadInitial((PathBuf, bool)), + Log((ActionState, Vec)), } pub(crate) struct Delegate { @@ -253,10 +254,22 @@ impl AppDelegate for Delegate { let line = cmd .get(ACTION_LOG) .expect("command type matched but didn't contain the expected value"); + if let Some(line) = line.take() { - let line = String::from_utf8_lossy(&line); - state.log.push_back(ansi_to_rich_text(line.trim())); + { + let line = String::from_utf8_lossy(&line); + state.log.push_back(ansi_to_rich_text(line.trim())); + } + + if self + .sender + .send(AsyncAction::Log((state.clone().into(), line))) + .is_err() + { + tracing::error!("Failed to queue action to add mod"); + } } + Handled::Yes } cmd if cmd.is(ACTION_START_SAVE_SETTINGS) => {