diag: keep the previous run's log as diag.prev.log

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
CRTkafa 2026-10-02 01:12:33 +07:00
parent 82d333e350
commit 64a37cf366

View file

@ -4,7 +4,7 @@
//! overlays and `shell_raise` calls with their results, native focus calls, and the shell document's
//! focus, blur and visibility changes. A line is an event name and a few words: never a URL, a
//! query, page text or anything about an account ([`clean`] keeps plain printable ASCII only and
//! caps the length). The file is truncated at each launch with diagnostics on.
//! caps the length). Each launch with diagnostics on starts a new file; the one before it is kept as `diag.prev.log`.
use std::io::Write;
use std::path::PathBuf;
@ -15,6 +15,7 @@ use std::time::Instant;
pub const ARG: &str = "--diag";
pub const ENV: &str = "WAYBETTER_DIAG";
pub const FILE: &str = "diag.log";
pub const PREVIOUS: &str = "diag.prev.log";
const MAX_LINE: usize = 300;
static ON: AtomicBool = AtomicBool::new(false);
@ -34,10 +35,13 @@ pub fn clean(line: &str) -> String {
line.chars().take(MAX_LINE).map(|ch| if (' '..='~').contains(&ch) { ch } else { '?' }).collect()
}
/// Starts writing to `dir/diag.log` (created, truncated). Nothing happens without a folder.
/// Starts writing to `dir/diag.log` (created, truncated); the previous run's log is kept as `diag.prev.log`.
/// Nothing happens without a folder.
pub fn start(dir: Option<PathBuf>) {
let Some(dir) = dir else { return };
let _ = std::fs::create_dir_all(&dir);
// A run that went wrong is usually relaunched at once: keep its log.
let _ = std::fs::rename(dir.join(FILE), dir.join(PREVIOUS));
if let Ok(file) = std::fs::File::create(dir.join(FILE)) {
if let Ok(mut sink) = SINK.lock() {
*sink = Some((file, Instant::now()));