#[cfg(unix)]
mod unix_signal {
use crate::REOPEN_FLAG;
use std::sync::atomic::Ordering;
pub(super) extern "C" fn sighup_handler(_: libc::c_int) {
REOPEN_FLAG.store(true, Ordering::Relaxed);
}
pub(super) fn register_sighup() {
unsafe {
let mut sa: libc::sigaction = std::mem::zeroed();
sa.sa_sigaction = sighup_handler as *const () as libc::sighandler_t;
sa.sa_flags = libc::SA_RESTART;
libc::sigemptyset(&mut sa.sa_mask);
if libc::sigaction(libc::SIGHUP, &sa, std::ptr::null_mut()) != 0 {
eprintln!("[minimal_logger] sigaction(SIGHUP) failed — rotation disabled");
}
}
}
}
#[cfg(target_os = "linux")]
pub(crate) mod platform {
use super::unix_signal;
pub fn register_rotation_handler() {
unix_signal::register_sighup();
}
pub fn sync_file(file: &std::fs::File) {
use std::os::unix::io::AsRawFd;
let ret = unsafe { libc::fdatasync(file.as_raw_fd()) };
if ret != 0 {
let err = std::io::Error::last_os_error();
eprintln!("[minimal_logger] fdatasync failed: {err}");
}
}
}
#[cfg(target_os = "macos")]
pub(crate) mod platform {
use super::unix_signal;
pub fn register_rotation_handler() {
unix_signal::register_sighup();
}
pub fn sync_file(file: &std::fs::File) {
use std::os::unix::io::AsRawFd;
let ret = unsafe { libc::fcntl(file.as_raw_fd(), libc::F_FULLFSYNC) };
if ret != 0 {
let err = std::io::Error::last_os_error();
eprintln!("[minimal_logger] F_FULLFSYNC failed: {err}");
}
}
}
#[cfg(windows)]
pub(crate) mod platform {
use crate::REOPEN_FLAG;
use std::sync::atomic::Ordering;
use windows::Win32::Foundation::{HANDLE, INVALID_HANDLE_VALUE};
use windows::Win32::Storage::FileSystem::FlushFileBuffers;
use windows::Win32::System::Threading::{
CreateEventW, INFINITE, ResetEvent, WaitForSingleObject,
};
use windows::core::PCWSTR;
fn to_wide_null(s: &str) -> Vec<u16> {
s.encode_utf16().chain(std::iter::once(0u16)).collect()
}
pub fn register_rotation_handler() {
let name = to_wide_null("Local\\RustLogger_LogRotate");
let _ = std::thread::Builder::new()
.name("logger-rotation-watcher".into())
.stack_size(64 * 1024)
.spawn(move || {
let event =
unsafe { CreateEventW(None, true, false, PCWSTR::from_raw(name.as_ptr())) }
.unwrap_or(INVALID_HANDLE_VALUE);
if event.is_invalid() {
eprintln!("[minimal_logger] CreateEventW failed — rotation disabled");
return;
}
loop {
unsafe { WaitForSingleObject(event, INFINITE) };
REOPEN_FLAG.store(true, Ordering::Relaxed);
let _ = unsafe { ResetEvent(event) };
}
});
}
pub fn sync_file(file: &std::fs::File) {
use std::os::windows::io::AsRawHandle;
if let Err(e) = unsafe { FlushFileBuffers(HANDLE(file.as_raw_handle())) } {
eprintln!("[minimal_logger] FlushFileBuffers failed: {e}");
}
}
}
#[cfg(all(unix, not(target_os = "linux"), not(target_os = "macos")))]
pub(crate) mod platform {
use super::unix_signal;
pub fn register_rotation_handler() {
unix_signal::register_sighup();
}
pub fn sync_file(file: &std::fs::File) {
use std::os::unix::io::AsRawFd;
let ret = unsafe { libc::fsync(file.as_raw_fd()) };
if ret != 0 {
let err = std::io::Error::last_os_error();
eprintln!("[minimal_logger] fsync failed: {err}");
}
}
}