Keep reconnect health breadcrumbs
ci / test (push) Waiting to run
ci / fuzz-smoke (push) Waiting to run
ci / windows-client (push) Waiting to run
ci / package-release (linux-x86_64, ubuntu-latest) (push) Waiting to run
ci / package-release (macos-aarch64, macos-14) (push) Waiting to run
ci / package-release (macos-x86_64, macos-13) (push) Waiting to run
ci / package-release (windows-x86_64, windows-latest) (push) Waiting to run
ci / publish-gitea-release (push) Blocked by required conditions
ci / remote-bench (push) Waiting to run
ci / test (push) Waiting to run
ci / fuzz-smoke (push) Waiting to run
ci / windows-client (push) Waiting to run
ci / package-release (linux-x86_64, ubuntu-latest) (push) Waiting to run
ci / package-release (macos-aarch64, macos-14) (push) Waiting to run
ci / package-release (macos-x86_64, macos-13) (push) Waiting to run
ci / package-release (windows-x86_64, windows-latest) (push) Waiting to run
ci / publish-gitea-release (push) Blocked by required conditions
ci / remote-bench (push) Waiting to run
This commit is contained in:
+68
-6
@@ -1,11 +1,13 @@
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::{Mutex, OnceLock};
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
static TRACE_FILE: OnceLock<Option<Mutex<File>>> = OnceLock::new();
|
||||
static HEALTH_FILE: OnceLock<Option<Mutex<File>>> = OnceLock::new();
|
||||
static TRACE_BYTES: OnceLock<bool> = OnceLock::new();
|
||||
const HEALTH_LOG_MAX_BYTES: u64 = 1024 * 1024;
|
||||
|
||||
pub fn enabled() -> bool {
|
||||
TRACE_FILE.get_or_init(open_trace_file).is_some()
|
||||
@@ -19,6 +21,21 @@ pub fn event(name: &str, fields: &[(&str, String)]) {
|
||||
let Some(file) = TRACE_FILE.get_or_init(open_trace_file) else {
|
||||
return;
|
||||
};
|
||||
write_event(file, name, fields);
|
||||
}
|
||||
|
||||
pub fn health_event(name: &str, fields: &[(&str, String)]) {
|
||||
let Some(file) = HEALTH_FILE.get_or_init(open_health_file) else {
|
||||
return;
|
||||
};
|
||||
write_event(file, name, fields);
|
||||
}
|
||||
|
||||
pub fn default_health_log_path() -> PathBuf {
|
||||
trace_root().join("health.log")
|
||||
}
|
||||
|
||||
fn write_event(file: &Mutex<File>, name: &str, fields: &[(&str, String)]) {
|
||||
let now = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.map(|duration| duration.as_millis())
|
||||
@@ -81,11 +98,49 @@ fn open_trace_file() -> Option<Mutex<File>> {
|
||||
.map(Mutex::new)
|
||||
}
|
||||
|
||||
fn open_health_file() -> Option<Mutex<File>> {
|
||||
let path = health_log_path_from_env()?;
|
||||
rotate_health_log_if_needed(&path);
|
||||
if let Some(parent) = path.parent() {
|
||||
let _ = std::fs::create_dir_all(parent);
|
||||
}
|
||||
OpenOptions::new()
|
||||
.create(true)
|
||||
.append(true)
|
||||
.open(&path)
|
||||
.ok()
|
||||
.map(Mutex::new)
|
||||
}
|
||||
|
||||
fn health_log_path_from_env() -> Option<PathBuf> {
|
||||
match std::env::var_os("DOSH_HEALTH_LOG") {
|
||||
Some(raw) => {
|
||||
let normalized = raw.to_string_lossy().to_ascii_lowercase();
|
||||
if normalized.is_empty() || matches!(normalized.as_str(), "0" | "false" | "off") {
|
||||
None
|
||||
} else if matches!(normalized.as_str(), "1" | "true" | "on") {
|
||||
Some(default_health_log_path())
|
||||
} else {
|
||||
Some(PathBuf::from(raw))
|
||||
}
|
||||
}
|
||||
None => Some(default_health_log_path()),
|
||||
}
|
||||
}
|
||||
|
||||
fn rotate_health_log_if_needed(path: &Path) {
|
||||
let Ok(metadata) = std::fs::metadata(path) else {
|
||||
return;
|
||||
};
|
||||
if metadata.len() <= HEALTH_LOG_MAX_BYTES {
|
||||
return;
|
||||
}
|
||||
let rotated = path.with_extension("log.1");
|
||||
let _ = std::fs::remove_file(&rotated);
|
||||
let _ = std::fs::rename(path, rotated);
|
||||
}
|
||||
|
||||
fn default_trace_path() -> PathBuf {
|
||||
let root = dirs::cache_dir()
|
||||
.unwrap_or_else(std::env::temp_dir)
|
||||
.join("dosh")
|
||||
.join("trace");
|
||||
let exe = std::env::current_exe()
|
||||
.ok()
|
||||
.and_then(|path| {
|
||||
@@ -93,7 +148,14 @@ fn default_trace_path() -> PathBuf {
|
||||
.map(|stem| stem.to_string_lossy().to_string())
|
||||
})
|
||||
.unwrap_or_else(|| "dosh".to_string());
|
||||
root.join(format!("{}-{}.log", exe, std::process::id()))
|
||||
trace_root().join(format!("{}-{}.log", exe, std::process::id()))
|
||||
}
|
||||
|
||||
fn trace_root() -> PathBuf {
|
||||
dirs::cache_dir()
|
||||
.unwrap_or_else(std::env::temp_dir)
|
||||
.join("dosh")
|
||||
.join("trace")
|
||||
}
|
||||
|
||||
fn truthy_env(name: &str) -> bool {
|
||||
|
||||
Reference in New Issue
Block a user