feat(dtmm): Log to file

This commit is contained in:
Lucas Schwiderski 2023-04-06 14:49:43 +02:00
parent 9ab92499a8
commit a246e5acb6
Signed by: lucas
GPG key ID: AA12679AAA6DF4D8
3 changed files with 35 additions and 3 deletions

View file

@ -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<Init
.await
.wrap_err("Failed to read config file")?;
// Create or truncate the log file
let log_path = config.data_dir.join("dtmm.log");
tokio::spawn(async move {
let _ = File::create(&log_path).await;
tracing::debug!("Truncated log file");
});
let game_info = tokio::task::spawn_blocking(dtmt_shared::collect_game_info)
.await
.wrap_err("Failed to collect Steam game info")?;

View file

@ -5,7 +5,10 @@ use color_eyre::Help;
use color_eyre::Report;
use color_eyre::Result;
use druid::{ExtEventSink, SingleUse, Target};
use tokio::fs::OpenOptions;
use tokio::io::AsyncWriteExt;
use tokio::runtime::Runtime;
use tokio::sync::mpsc::UnboundedReceiver;
use tokio::sync::RwLock;
@ -168,6 +171,15 @@ async fn handle_action(
)
.expect("failed to send command");
}),
AsyncAction::Log((state, line)) => 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;
}
}),
};
}
}

View file

@ -92,6 +92,7 @@ pub(crate) enum AsyncAction {
SaveSettings(ActionState),
CheckUpdates(ActionState),
LoadInitial((PathBuf, bool)),
Log((ActionState, Vec<u8>)),
}
pub(crate) struct Delegate {
@ -253,10 +254,22 @@ impl AppDelegate<State> 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()));
}
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) => {