Log to a file, and explain a failed card data swap
A Windows release build has no console, so mlog! lines and panics went nowhere. They now also go to mtgc.log in the app data folder, which the Info tab's Show log file button reveals, and each launch logs the version. A card data update logs each stage, and a failed swap logs every attempt plus what's blocking it: both files' details, whether the folder refuses changes in general, and on Windows whether the files can be opened exclusively and which processes the Restart Manager says hold them.
This commit is contained in:
110
src-tauri/src/logfile.rs
Normal file
110
src-tauri/src/logfile.rs
Normal file
@@ -0,0 +1,110 @@
|
||||
//! The log file, so a problem on someone else's machine leaves a trace.
|
||||
//!
|
||||
//! A Windows release build has no console, so stderr goes nowhere there. Every
|
||||
//! [`mlog!`](crate::mlog) line is also appended to `mtgc.log` in the app data
|
||||
//! folder, which is safe to ask a user for: nothing logged here includes a
|
||||
//! sync token.
|
||||
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Mutex;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
/// Past this size the log is set aside at the next launch.
|
||||
const MAX_BYTES: u64 = 1 << 20;
|
||||
|
||||
static FILE: Mutex<Option<File>> = Mutex::new(None);
|
||||
|
||||
pub fn log_path(data_dir: &Path) -> PathBuf {
|
||||
data_dir.join("mtgc.log")
|
||||
}
|
||||
|
||||
/// Start logging to the file. Best-effort: a log that can't be written must
|
||||
/// never stop the app from opening.
|
||||
pub fn init(data_dir: &Path) {
|
||||
let path = log_path(data_dir);
|
||||
rotate(&path);
|
||||
match std::fs::OpenOptions::new().create(true).append(true).open(&path) {
|
||||
Ok(f) => *FILE.lock().unwrap() = Some(f),
|
||||
Err(e) => eprintln!("[mtgc] couldn't open {}: {e}", path.display()),
|
||||
}
|
||||
|
||||
// Release builds abort on a panic, so this is the only record of one.
|
||||
let previous = std::panic::take_hook();
|
||||
std::panic::set_hook(Box::new(move |info| {
|
||||
write(format_args!("panic: {info}"));
|
||||
previous(info);
|
||||
}));
|
||||
}
|
||||
|
||||
/// Keep one previous log, once the current one has grown large enough.
|
||||
fn rotate(path: &Path) {
|
||||
if std::fs::metadata(path).is_ok_and(|m| m.len() > MAX_BYTES) {
|
||||
let _ = std::fs::rename(path, path.with_extension("old.log"));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write(message: std::fmt::Arguments) {
|
||||
eprintln!("[mtgc] {message}");
|
||||
if let Ok(mut file) = FILE.lock() {
|
||||
if let Some(f) = file.as_mut() {
|
||||
let _ = writeln!(f, "{} {message}", timestamp(SystemTime::now()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `2026-10-30T19:59:01Z`.
|
||||
fn timestamp(t: SystemTime) -> String {
|
||||
let secs = t.duration_since(UNIX_EPOCH).map(|d| d.as_secs()).unwrap_or(0) as i64;
|
||||
let (y, m, d) = civil_from_days(secs.div_euclid(86_400));
|
||||
let s = secs.rem_euclid(86_400);
|
||||
format!("{y:04}-{m:02}-{d:02}T{:02}:{:02}:{:02}Z", s / 3600, s / 60 % 60, s % 60)
|
||||
}
|
||||
|
||||
/// Year, month and day of a count of days since 1970-01-01, by Howard
|
||||
/// Hinnant's `civil_from_days`.
|
||||
fn civil_from_days(days: i64) -> (i64, i64, i64) {
|
||||
let z = days + 719_468;
|
||||
let era = z.div_euclid(146_097);
|
||||
let doe = z.rem_euclid(146_097);
|
||||
let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365;
|
||||
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
|
||||
let mp = (5 * doy + 2) / 153;
|
||||
let d = doy - (153 * mp + 2) / 5 + 1;
|
||||
let m = if mp < 10 { mp + 3 } else { mp - 9 };
|
||||
(yoe + era * 400 + i64::from(m <= 2), m, d)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::time::Duration;
|
||||
|
||||
#[test]
|
||||
fn timestamps_read_as_utc_dates() {
|
||||
let at = |secs| timestamp(UNIX_EPOCH + Duration::from_secs(secs));
|
||||
assert_eq!(at(0), "1970-01-01T00:00:00Z");
|
||||
assert_eq!(at(951_782_400), "2000-02-29T00:00:00Z");
|
||||
assert_eq!(at(1_793_390_341), "2026-10-30T19:59:01Z");
|
||||
assert_eq!(at(1_798_761_599), "2026-12-31T23:59:59Z");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_a_large_log_is_set_aside() {
|
||||
let dir = std::env::temp_dir().join(format!("mtgc-log-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = log_path(&dir);
|
||||
let old = dir.join("mtgc.old.log");
|
||||
|
||||
std::fs::write(&path, b"short").unwrap();
|
||||
rotate(&path);
|
||||
assert!(path.exists() && !old.exists());
|
||||
|
||||
std::fs::write(&path, vec![b'x'; MAX_BYTES as usize + 1]).unwrap();
|
||||
rotate(&path);
|
||||
assert!(!path.exists() && old.exists());
|
||||
std::fs::remove_dir_all(&dir).unwrap();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user