diff --git a/src-tauri/src/commands/game.rs b/src-tauri/src/commands/game.rs index 5b6a1b9..8045558 100644 --- a/src-tauri/src/commands/game.rs +++ b/src-tauri/src/commands/game.rs @@ -273,7 +273,7 @@ pub async fn act( // A DLC install is just that DLC. let with_dlc = dlc.is_none().then_some(true); blocking(move || { - act_blocking( + let result = act_blocking( &app, action, &game, @@ -281,7 +281,11 @@ pub async fn act( dlc.as_deref(), keep, dest.as_deref(), - ) + ); + if let (Action::Play | Action::PlayOutdated, Err(e)) = (action, &result) { + tracing::warn!("{key}: Play failed: {e}"); + } + result }) .await } diff --git a/src-tauri/src/commands/library.rs b/src-tauri/src/commands/library.rs index a227fa0..8fe766c 100644 --- a/src-tauri/src/commands/library.rs +++ b/src-tauri/src/commands/library.rs @@ -961,7 +961,11 @@ fn scan_store( Some(creds) => { gog::list(http, dirs, &creds).map_err(|e| ScanError::Quiet(e.into())) } - None => Err(ScanError::Quiet(Msg::system(format!("{login:?}")))), + None => Err(ScanError::Quiet(Msg::system(match login { + StoreLogin::NoTool => "gogdl isn't installed", + StoreLogin::Unreachable => "gogdl couldn't reach GOG", + _ => "not logged in", + }))), }; (store, games, login) } @@ -1233,30 +1237,46 @@ pub async fn poll(app: AppHandle) -> Poll { blocking(move || poll_blocking(&app)).await } +/// A watch whose game isn't seen running ends when our own process exited, else once it had `START_GRACE` to start. +fn watch_ends(exit: Option, since: Instant) -> bool { + exit.is_some() || since.elapsed() > START_GRACE +} + fn poll_blocking(app: &AppHandle) -> Poll { let state = app.state::(); let steam_running = steam::running_game().map(|id| ProviderKind::Steam.key(&id)); // Our own process answers at once; an Epic game's name is looked up after the lock is let go. let watch = state.lock().watch.as_mut().map(|w| { - let running = match &mut w.process { - Process::Ours(child) => Ok(matches!(child.try_wait(), Ok(None))), - Process::Named(exe) => Err(exe.clone()), + let (running, exit) = match &mut w.process { + Process::Ours(child) => { + let waited = child.try_wait(); + (Ok(matches!(waited, Ok(None))), waited.ok().flatten()) + } + Process::Named(exe) => (Err(exe.clone()), None), }; - (w.key.clone(), running, w.since) + (w.key.clone(), running, exit, w.since) }); - let watched = watch.and_then(|(key, running, since)| { + let watched = watch.and_then(|(key, running, exit, since)| { if running.unwrap_or_else(|exe| epic::is_running(&exe)) { if let Some(w) = state.lock().watch.as_mut().filter(|w| w.since == since) { w.seen = true; } return Some(key); } - // Gone, or never showed up: stop watching once it had time to start, time the session if it ran, - // and upload its saves. + // Gone, or never showed up: stop watching once it had time to start (at once when our own process exited), + // time the session if it ran, and upload its saves. // shortcut: one that never showed up (a bootstrapper exe) has its saves uploaded while it may still run. - if since.elapsed() > START_GRACE { + if watch_ends(exit, since) { let ended = state.lock().watch.take(); if let Some(w) = ended { + if let Some(status) = exit.filter(|s| !s.success() || !w.seen) { + tracing::warn!( + "{}: the game exited ({status}) after {} s{}", + w.key, + w.since.elapsed().as_secs(), + crate::log::game_output_tail(&w.key, 20) + ); + } if w.seen { record_session(&state, &w.key, w.since.elapsed()); } @@ -1895,4 +1915,12 @@ mod tests { }; insta::assert_json_snapshot!(poll); } + + #[cfg(unix)] + #[test] + fn a_watch_ends_as_soon_as_our_process_exits() { + let status = std::process::Command::new("false").status().unwrap(); + assert!(watch_ends(Some(status), Instant::now())); + assert!(!watch_ends(None, Instant::now())); + } } diff --git a/src-tauri/src/log.rs b/src-tauri/src/log.rs index 7281b74..20df27d 100644 --- a/src-tauri/src/log.rs +++ b/src-tauri/src/log.rs @@ -114,6 +114,29 @@ fn stamp(at: Timestamp, tz: TimeZone) -> String { at.to_zoned(tz).strftime("%Y-%m-%d %H:%M:%S").to_string() } +/// Where a Windows game's output (umu-run, pressure-vessel, Proton) goes: `logs/game-.log`, rewritten at +/// each launch, so a launch that fails can be explained in launcher.log. +pub fn game_output(key: &str) -> PathBuf { + crate::cache::Dirs::new() + .logs + .join(format!("game-{key}.log")) +} + +/// The last `lines` lines of a copy's game output, each indented; empty when it has none. +pub fn game_output_tail(key: &str, lines: usize) -> String { + tail(&game_output(key), lines) +} + +fn tail(path: &Path, lines: usize) -> String { + let text = std::fs::read(path).unwrap_or_default(); + let text = String::from_utf8_lossy(&text); + let all: Vec<&str> = text.lines().collect(); + all[all.len().saturating_sub(lines)..] + .iter() + .map(|l| format!("\n {l}")) + .collect() +} + /// Sends this crate's info and above to the log file in `dir` (and stderr in debug builds). The file is /// written unbuffered, so nothing waits to be flushed when the app exits. pub fn init(dir: &Path) { @@ -194,4 +217,13 @@ mod tests { "2024-07-01 14:00:00" ); } + + #[test] + fn a_game_output_tail_is_its_last_lines_indented() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("game.log"); + assert_eq!(tail(&path, 2), ""); + fs::write(&path, "a\nb\nc\n").unwrap(); + assert_eq!(tail(&path, 2), "\n b\n c"); + } } diff --git a/src-tauri/src/wine.rs b/src-tauri/src/wine.rs index ea97169..8a5cd32 100644 --- a/src-tauri/src/wine.rs +++ b/src-tauri/src/wine.rs @@ -272,7 +272,11 @@ fn wrap(game: &Command, key: &str, proton: Option<&str>) -> Result<(Command, Pro /// A game's launch through `wrap`. When umu still has to fetch the Steam Linux Runtime or set the prefix up, the copy /// counts as setting up (`setting_up`) until the game's own program runs. pub fn game_command(game: Command, key: &str, proton: Option<&str>) -> Result { - let (command, proton) = wrap(&game, key, proton)?; + let (mut command, proton) = wrap(&game, key, proton)?; + match fs_err::File::create(crate::log::game_output(key)) { + Ok(file) => _ = command.stderr(file.into_file()), + Err(e) => tracing::warn!("game output: {e}"), + } let prefix = prefix(key).ok_or(msg!(gameNotInLibrary))?; let mut setups = SETUPS.lock().unwrap_or_else(|e| e.into_inner()); let exe = Path::new(game.get_program()).file_name();