diff --git a/d3d12/apps/leonida/src/leonida.rs b/d3d12/apps/leonida/src/leonida.rs index 855fced67b..70b7cc3888 100644 --- a/d3d12/apps/leonida/src/leonida.rs +++ b/d3d12/apps/leonida/src/leonida.rs @@ -29,13 +29,55 @@ where /// The prefix the older scripts use (assembled so the rebrand script does not rewrite it). const OLD_PREFIX: &str = concat!("VIC", "TORY_"); -/// Applies [`settings_names`] to this process. Called first thing in `main`, before any thread starts. -pub fn apply_settings_names() { +/// Applies [`settings_names`] to this process, and returns the `LEONIDA_` names it set. Called first thing in +/// `main`, before any thread starts. +pub fn apply_settings_names() -> Vec { + let mut set = Vec::new(); for (k, v) in settings_names(std::env::vars()) { // SAFETY: called at the start of `main`, before the runtime starts any thread, so nothing reads the // environment concurrently. - unsafe { std::env::set_var(k, v) }; + unsafe { std::env::set_var(&k, v) }; + set.push(k); } + set +} + +/// The most of a value [`settings_line`] shows. +const SHOWN: usize = 120; + +/// The line `run` logs at start: every `LEONIDA_` setting of `vars`, sorted, as `NAME=value`, so that a run's log +/// says what it ran with. A value with any character other than letters, digits, space and `._,:+=*-` (a folder, +/// which names the user, a file, a command line) shows only its length; one longer than [`SHOWN`] is cut. The +/// names in `renamed` came from an older name ([`apply_settings_names`]) and say so. +pub fn settings_line(vars: I, renamed: &[String]) -> String +where + I: IntoIterator, +{ + let mut set: Vec<(String, String)> = vars.into_iter().filter(|(k, _)| k.starts_with("LEONIDA_")).collect(); + if set.is_empty() { + return "no LEONIDA_ settings".to_owned(); + } + set.sort(); + let shown: Vec = set + .iter() + .map(|(k, v)| { + let n = v.chars().count(); + let plain = v.chars().all(|c| c.is_ascii_alphanumeric() || " ._,:+=*-".contains(c)); + let value = if !plain { + format!("") + } else if n > SHOWN { + format!("{}... ({n} chars)", v.chars().take(SHOWN).collect::()) + } else { + v.clone() + }; + let from = match k.strip_prefix("LEONIDA_") { + Some(rest) if renamed.contains(k) => format!(" (from {OLD_PREFIX}{rest})"), + _ => String::new(), + }; + format!("{k}={value}{from}") + }) + .collect(); + format!("{} LEONIDA_ settings: {}", set.len(), shown.join(" ")) } #[cfg(test)] @@ -66,4 +108,46 @@ mod tests { fn bare_prefix_is_ignored() { assert!(settings_names(vec![(old(""), "1".into())]).is_empty()); } + + #[test] + fn the_settings_line_lists_leonida_settings_in_order() { + let line = settings_line( + vars(&[("PATH", "x"), ("LEONIDA_RELAY", "2"), ("LEONIDA_AB", "4,LEONIDA_X"), ("RUST_LOG", "info")]), + &[], + ); + assert_eq!(line, "2 LEONIDA_ settings: LEONIDA_AB=4,LEONIDA_X LEONIDA_RELAY=2"); + assert_eq!(settings_line(vars(&[("PATH", "x")]), &[]), "no LEONIDA_ settings"); + } + + #[test] + fn the_settings_line_hides_folders_and_files() { + let line = settings_line( + vars(&[ + ("LEONIDA_GAME_DIR", r"C:\Users\someone\game"), + ("LEONIDA_DLSS_DIR", r"\\server\share\dlss"), + ("LEONIDA_CAPTURE", "/home/someone/capture"), + ("LEONIDA_KEYS", "5:Enter,10-20:W"), + ]), + &[], + ); + assert_eq!( + line, + "4 LEONIDA_ settings: LEONIDA_CAPTURE= LEONIDA_DLSS_DIR= \ + LEONIDA_GAME_DIR= LEONIDA_KEYS=5:Enter,10-20:W" + ); + assert!(!line.contains("someone")); + } + + #[test] + fn the_settings_line_cuts_long_values_and_names_older_names() { + let long = "1,".repeat(100); + let line = settings_line( + vec![("LEONIDA_LONG".to_owned(), long.clone()), ("LEONIDA_RELAY".to_owned(), "1".to_owned())], + &["LEONIDA_RELAY".to_owned()], + ); + assert_eq!( + line, + format!("2 LEONIDA_ settings: LEONIDA_LONG={}... (200 chars) LEONIDA_RELAY=1 (from {})", &long[..120], old("RELAY")) + ); + } } diff --git a/d3d12/apps/leonida/src/main.rs b/d3d12/apps/leonida/src/main.rs index f65e9027da..b7690e5c2d 100644 --- a/d3d12/apps/leonida/src/main.rs +++ b/d3d12/apps/leonida/src/main.rs @@ -185,7 +185,7 @@ fn main() -> Result<()> { #[cfg(windows)] agility::match_d3d12_core(); // Leonida's settings names (LEONIDA_X for LEONIDA_X), before any thread starts. - leonida::apply_settings_names(); + let renamed = leonida::apply_settings_names(); leonida_core::logging::init("info"); // The audio recorder `run` starts for `LEONIDA_AUDIO_CAPTURE`. #[cfg(all(windows, feature = "diagnostics"))] @@ -243,6 +243,8 @@ fn main() -> Result<()> { title_args, } => { leonida_hle::runtime::set_title_args(title_args); + // What this run was started with, in its log. + leonida_hle::runtime::log_settings(&leonida::settings_line(std::env::vars(), &renamed)); run::run(&exe, unimplemented, strict, execute, console) } #[cfg(all(windows, feature = "diagnostics"))] diff --git a/d3d12/apps/leonida/tests/settings_line.rs b/d3d12/apps/leonida/tests/settings_line.rs new file mode 100644 index 0000000000..7fd1d6f0ae --- /dev/null +++ b/d3d12/apps/leonida/tests/settings_line.rs @@ -0,0 +1,29 @@ +use std::path::Path; +use std::process::Command; + +/// `leonida run` says in its log what it was started with, folders shown only by length. +#[test] +fn a_run_logs_the_settings_it_starts_with() { + let missing = Path::new(env!("CARGO_MANIFEST_DIR")).join("no-such-title.exe"); + assert!(!missing.exists()); + let mut command = Command::new(env!("CARGO_BIN_EXE_leonida")); + // Only the settings this test sets. + for (name, _) in std::env::vars() { + if name.starts_with("LEONIDA_") || name.starts_with(concat!("VIC", "TORY_")) { + command.env_remove(name); + } + } + let output = command + .env("NO_COLOR", "1") + .env("LEONIDA_TEST_KEYS", "5:Enter,10-20:W") + .env("LEONIDA_TEST_FOLDER", r"C:\Users\someone\folder") + .arg("run") + .arg(&missing) + .output() + .unwrap(); + let stderr = String::from_utf8_lossy(&output.stderr); + let line = stderr.lines().find(|l| l.contains("LEONIDA_ settings")).unwrap_or_else(|| panic!("no settings line:\n{stderr}")); + assert!(line.contains("LEONIDA_TEST_KEYS=5:Enter,10-20:W"), "{line}"); + assert!(line.contains("LEONIDA_TEST_FOLDER="), "{line}"); + assert!(!stderr.contains("someone"), "a folder reached the log:\n{stderr}"); +} diff --git a/d3d12/crates/leonida-hle/src/runtime.rs b/d3d12/crates/leonida-hle/src/runtime.rs index 28f73fa95c..d235cffc6d 100644 --- a/d3d12/crates/leonida-hle/src/runtime.rs +++ b/d3d12/crates/leonida-hle/src/runtime.rs @@ -68,6 +68,12 @@ pub fn title_args() -> &'static [String] { TITLE_ARGS.get().map_or(&[], Vec::as_slice) } +/// Logs the settings a run starts with (`line`), at info under the `settings` target, which a +/// filter can keep on its own (`LEONIDA_LOG=warn,settings=info`). +pub fn log_settings(line: &str) { + tracing::info!(target: "settings", "{line}"); +} + /// Maps a title DLL the title loads itself (`LoadLibrary`) and returns its /// base; `Ok(None)` if `file` is not one of the title's DLLs. pub type TitleDllLoader = Box Result, String> + Send + Sync>;