Files
peerspeak/src/lib.rs
T
molluskandClaude Opus 4.8 e3b63c0856 fix: make log_msg writes atomic
Each log_msg opened the file in O_APPEND mode then used writeln!, which
issues a separate write() syscall per formatting fragment. O_APPEND only
guarantees atomicity per write() call, so concurrent log_msg calls from
the app's many threads/tasks could interleave their fragments mid-line.

Format the full line (timestamp + msg + newline) into one String, then
emit it with a single write_all so each log line lands as one atomic
append. No behavior change beyond non-interleaved output.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-01 15:43:08 -04:00

53 lines
1.7 KiB
Rust

pub mod audio;
pub mod codec;
pub mod network;
pub mod core;
pub mod app;
pub mod config;
pub mod notify;
use std::path::PathBuf;
use std::sync::OnceLock;
/// Resolves the log file path once: `$XDG_STATE_HOME/peerspeak/peerspeak.log`
/// (via `dirs::state_dir`), falling back to the system temp dir. Computed lazily
/// so we never hardcode a per-user path.
fn log_path() -> &'static PathBuf {
static LOG_PATH: OnceLock<PathBuf> = OnceLock::new();
LOG_PATH.get_or_init(|| {
let mut dir = dirs::state_dir()
.or_else(dirs::cache_dir)
.unwrap_or_else(std::env::temp_dir);
dir.push("peerspeak");
let _ = std::fs::create_dir_all(&dir);
dir.push("peerspeak.log");
dir
})
}
/// The resolved log file path. Exposed so diagnostic tools (e.g. the audio
/// probe) can tail the same log the app writes to.
pub fn log_file_path() -> PathBuf {
log_path().clone()
}
pub fn log_msg(msg: &str) {
// Format the whole line into one buffer first, then emit it with a single
// `write_all`. The file is opened with `O_APPEND`, so a lone `write()` is
// atomic w.r.t. concurrent writers; building the line up front avoids the
// multi-syscall `writeln!` path that would let threads interleave fragments.
let line = match std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH) {
Ok(time) => format!("[{}.{:03}] {}\n", time.as_secs(), time.subsec_millis(), msg),
Err(_) => format!("{}\n", msg),
};
if let Ok(mut file) = std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(log_path())
{
use std::io::Write;
let _ = file.write_all(line.as_bytes());
}
}