From 0e78456ebad2d4ba30551ffc8f47eac8b3999679 Mon Sep 17 00:00:00 2001 From: openhands Date: Wed, 25 Nov 2026 04:43:42 +0000 Subject: [PATCH] VICTORY_LOG_FILE: append a run's log to a file A second fmt layer appends every record to the file the env var names, alongside the console, so a run's output survives the window closing. Opened for appending, flushed per record; an unusable path is warned about and the run continues on the console alone. Co-authored-by: openhands --- AGENTS.md | 8 +++ Cargo.lock | 1 + README.md | 5 +- crates/victory-core/Cargo.toml | 1 + crates/victory-core/src/logging.rs | 84 +++++++++++++++++++++++++++++- 5 files changed, 96 insertions(+), 3 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 50453ee8da..2b44769161 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -67,3 +67,11 @@ Fork: MagiccConch/VIctory, upstream: Marvisak/VIctory. CI: windows-latest only ( - Ported as fork PR #11 (branch new-cap, commit b8388e9d, CI 45545098162 green, mergeable CLEAN). Merge conflict was only the heap-size chain in descriptor/host.rs: kept BOTH upstream's `.max(views)` rungs first (small title heaps must span the full capped range) AND the fork's 6ac8fbe plain rungs (`count + DYNAMIC`, `count + MIN_DYNAMIC`), then the cap path. Guard arithmetic: when `cap == Some(count + DYNAMIC)` (or `+MIN_DYNAMIC`), the matching `.max(views)` rung always succeeds first, so the plain rungs can never hide a needed guard trim. - Mingw test-run limitation confirmed — WSL interop intercepts the .exe (`UtilGetPpid` error); local signal stays check+clippy+test --no-run on x86_64-pc-windows-gnu. - After merging to main: rebuild, re-run pre-build (pipeline library rebuilds — emitted shaders change with the new clamp constant), then run the title. Expect the `host view heaps capped` info line, no slot-past-heap flood, and no null-write crash at world entry. Old cache dirs are keyed out automatically (cap in the key). + +## Hardware run 2 with the cap (2026-11-25): no crash; log-file feature added + +- Pre-build on the 5080 at bb56f8a3: `host view heaps capped ... cap=1000000` confirmed; fresh shader cache dir (cap in key, 0 cached); 28,164 pack pipelines: 23,019 made + 3,622 found in the library (safe: library entries are named by a hash of code+root+state, so byte-identical DXIL reuses, changed DXIL remakes; 23,019+3,622 = 28,164 − 1,523 skipped) + 1,523 skipped. Skips: 994 unnamed states (852 rasteriser, 139 depth-stencil, 3 blend), 101 untranslated (61 compute + 40 pixel), 403 E_INVALIDARG (394 compute, 8 graphics, 1 mesh — new small bucket to investigate someday, possibly oversized root signatures), 23 shader-not-found, 2 exotic stages. The 15,863 root-signature refusals stayed gone. +- Title run: `title slot past the host heap index=1113775+ count=934464` warnings REAPPEARED but are now EXPECTED and benign — that is `descriptor::sync_heaps` mirroring title-written heap slots the host heap cannot hold (≥ 934,464); the clamp keeps every shader read below 1,000,000 (guard slot), so those slots can't be read and can't fault. The Breakpad `http_upload` + blocked `submit.backtrace.io` line is the title's own reporter, benign. +- **No crash log ≠ lost log**: `crashes.log` is per-title at `%LOCALAPPDATA%\victory\data\<8-hex title id>\temp\crashes.log` (apps/victory/src/run.rs `log_crashes_to(temp.with_file_name(...))`), NOT in the repo/exe folder. Find it: `Get-ChildItem "$env:LOCALAPPDATA\victory\data" -Recurse -Filter crashes.log`. Absent file = no unhandled exception/panic/GPU failure that run. +- Added `VICTORY_LOG_FILE=` (victory-core/src/logging.rs): a second fmt layer appends every record to a file alongside stderr, so a run's console survives the window closing. Env-less core `init_with(default, filter, file)` for tests (Rust 2024 `set_var` is unsafe; no env mutation in tests). FileLog = shared Arc> handle opened append+create, flushed per write; bad path → warn after the subscriber is live, run continues. README documents it. + diff --git a/Cargo.lock b/Cargo.lock index 8a16d3e729..6abf16e4b7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -675,6 +675,7 @@ dependencies = [ "tempfile", "thiserror", "toml", + "tracing", "tracing-subscriber", ] diff --git a/README.md b/README.md index 4d49d484db..9aa25d3558 100644 --- a/README.md +++ b/README.md @@ -134,8 +134,9 @@ victory run [--execute] [--unimplemented trap|zero] [--strict] victory config ``` -Logging: `VICTORY_LOG=victory_loader=trace,info` (a release build logs -info and above). +| `VICTORY_LOG=victory_loader=trace,info` | a release build logs +info and above; with the `diagnostics` feature, debug and trace too | +| `VICTORY_LOG_FILE=` | also appends every log record to a file (opened for appending; an unusable path is warned about and ignored) | ### Windows shader-test validator diff --git a/crates/victory-core/Cargo.toml b/crates/victory-core/Cargo.toml index 19f69d58bf..52aaa9aed2 100644 --- a/crates/victory-core/Cargo.toml +++ b/crates/victory-core/Cargo.toml @@ -12,6 +12,7 @@ anyhow.workspace = true thiserror.workspace = true serde.workspace = true toml.workspace = true +tracing.workspace = true tracing-subscriber.workspace = true [dev-dependencies] diff --git a/crates/victory-core/src/logging.rs b/crates/victory-core/src/logging.rs index b4175420b1..51ae270a39 100644 --- a/crates/victory-core/src/logging.rs +++ b/crates/victory-core/src/logging.rs @@ -4,7 +4,12 @@ //! `VICTORY_LOG=victory_loader=trace,info`. Debug and trace output exist //! only in builds with the `diagnostics` feature; a plain release build //! logs info and above whatever the filter asks for. +//! +//! Set `VICTORY_LOG_FILE=` to also append every record to a file, so +//! a run's console output survives the window closing. +use std::path::PathBuf; +use std::sync::{Arc, Mutex}; use tracing_subscriber::EnvFilter; use tracing_subscriber::filter::LevelFilter; use tracing_subscriber::layer::SubscriberExt; @@ -17,12 +22,48 @@ const CAP: LevelFilter = if cfg!(feature = "diagnostics") { LevelFilter::INFO }; +/// Appends to the log file. Shared handle behind a lock: a record's bytes +/// are written whole (and flushed), from whichever thread logs them. +struct FileLog(Arc>); + +impl std::io::Write for FileLog { + fn write(&mut self, buf: &[u8]) -> std::io::Result { + let mut file = self.0.lock().unwrap_or_else(|e| e.into_inner()); + file.write_all(buf)?; + file.flush()?; + Ok(buf.len()) + } + fn flush(&mut self) -> std::io::Result<()> { + Ok(()) + } +} + pub fn init(default_level: &str) { let filter = std::env::var("VICTORY_LOG") .or_else(|_| std::env::var("RUST_LOG")) - .ok() + .ok(); + let file = std::env::var_os("VICTORY_LOG_FILE").map(PathBuf::from); + init_with(default_level, filter, file); +} + +/// The env-less core of [`init`]: the filter from `VICTORY_LOG` (else the +/// default), and the log file path, if any. +fn init_with(default_level: &str, filter: Option, file: Option) { + let filter = filter .and_then(|v| EnvFilter::try_new(v).ok()) .unwrap_or_else(|| EnvFilter::new(default_level)); + let (log, file_error) = match file { + Some(path) => match std::fs::OpenOptions::new().create(true).append(true).open(&path) { + Ok(f) => (Some(Arc::new(Mutex::new(f))), None), + Err(e) => (None, Some((path, e))), + }, + None => (None, None), + }; + let file = log.map(|log| { + tracing_subscriber::fmt::layer() + .with_target(true) + .with_writer(move || FileLog(Arc::clone(&log))) + }); let _ = tracing_subscriber::registry() .with(CAP) .with(filter) @@ -31,5 +72,46 @@ pub fn init(default_level: &str) { .with_target(true) .with_writer(std::io::stderr), ) + .with(file) .try_init(); + if let Some((path, e)) = file_error { + // The subscriber is live: this is seen on the console. + tracing::warn!(error = %e, path = %path.display(), "no log file; logging to the console only"); + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::io::Write as _; + + /// The file writer appends every write and keeps what earlier runs + /// wrote (the file is opened for appending, never truncated). + #[test] + fn file_log_appends() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("run.log"); + std::fs::write(&path, b"kept\n").unwrap(); + let log = Arc::new(Mutex::new( + std::fs::OpenOptions::new() + .create(true) + .append(true) + .open(&path) + .unwrap(), + )); + FileLog(Arc::clone(&log)).write_all(b"first\n").unwrap(); + // A later writer (another run, another thread) appends, not replaces. + FileLog(log).write_all(b"second\n").unwrap(); + assert_eq!(std::fs::read_to_string(&path).unwrap(), "kept\nfirst\nsecond\n"); + } + + /// An unusable log file path is no error: `init_with` goes on with the + /// console alone. The parent directories are missing, so the open fails + /// without creating anything. + #[test] + fn file_log_unusable() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("no/such/dir/run.log"); + init_with("info", None, Some(path)); + } }