From c0833d4bb103ac208d100d75068b093e6324bc94 Mon Sep 17 00:00:00 2001 From: objz Date: Mon, 24 Aug 2026 19:14:40 +0200 Subject: [PATCH] fix(instance): make launch log filenames collision-safe Use millisecond-precision timestamps so a same-second relaunch no longer truncates the previous log file. --- src/instance/logs/files.rs | 7 ++++++- src/instance/tests/logs/files.rs | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/instance/logs/files.rs b/src/instance/logs/files.rs index dd24ad2..cef626c 100644 --- a/src/instance/logs/files.rs +++ b/src/instance/logs/files.rs @@ -1,3 +1,6 @@ +// SPDX-FileCopyrightText: 2026 Constantin Bauer +// SPDX-License-Identifier: GPL-3.0-only + // per-launch log files stored under minecraft/logs/launches/ // each launch gets its own timestamped file so you can go back and see what // crashed last tuesday at 3am @@ -21,7 +24,9 @@ pub fn log_dir(instances_dir: &Path, instance_name: &str) -> PathBuf { pub fn create_log_file(instances_dir: &Path, instance_name: &str) -> Option { let dir = log_dir(instances_dir, instance_name); std::fs::create_dir_all(&dir).ok()?; - let now = chrono::Local::now().format("%Y-%m-%d_%H-%M-%S"); + // millisecond precision: a same-second relaunch (crash loop, quit+relaunch) + // would otherwise collide on one file and File::create truncates the old log. + let now = chrono::Local::now().format("%Y-%m-%d_%H-%M-%S%3f"); Some(dir.join(format!("{now}.log"))) } diff --git a/src/instance/tests/logs/files.rs b/src/instance/tests/logs/files.rs index 22518ac..c4df251 100644 --- a/src/instance/tests/logs/files.rs +++ b/src/instance/tests/logs/files.rs @@ -1,5 +1,28 @@ +// SPDX-FileCopyrightText: 2026 Constantin Bauer +// SPDX-License-Identifier: GPL-3.0-only + use super::*; +#[test] +fn create_log_file_name_has_millisecond_precision() { + let tmp = tempfile::tempdir().unwrap(); + let path = create_log_file(tmp.path(), "world").unwrap(); + let name = path.file_name().unwrap().to_str().unwrap(); + assert!(name.ends_with(".log"), "name={name}"); + // stem is 22 chars: %Y-%m-%d_%H-%M-%S%3f + let stem = name.strip_suffix(".log").unwrap(); + assert_eq!(stem.len(), 22, "name={name}"); + assert!(stem[stem.len() - 3..].chars().all(|c| c.is_ascii_digit()), "name={name}"); + + // pin the pattern: %3f must be zero-padded so names stay sortable + let dt = chrono::NaiveDateTime::parse_from_str( + "2025-01-02T15:04:05.005", + "%Y-%m-%dT%H:%M:%S%.3f", + ) + .unwrap(); + assert_eq!(dt.format("%Y-%m-%d_%H-%M-%S%3f").to_string(), "2025-01-02_15-04-05005"); +} + #[test] fn log_dir_builds_correct_path() { let p = log_dir(Path::new("/instances"), "my-world");