use std::{
fs::{self, File, OpenOptions},
io::{self, Write},
path::{Path, PathBuf},
sync::{
Arc, Mutex,
mpsc::{self, Receiver, Sender},
},
thread::{self, JoinHandle},
};
use chrono::{Local, NaiveDate};
use serde::Deserialize;
use crate::core::logging::rotation::{cleanup_rotated_files, compress_file, rotate_files};
use crate::core::{CoreError, CoreResult};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LogWriterConfig {
Stdout,
Stderr,
File(PathBuf),
RollingFile(RollingFileConfig),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum RotationPolicy {
#[default]
Daily,
Size,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RollingFileConfig {
pub path: PathBuf,
pub rotation: RotationPolicy,
pub max_bytes: Option<u64>,
pub max_files: usize,
pub keep_days: Option<u64>,
pub compress: bool,
}
impl RollingFileConfig {
pub fn by_size(path: impl Into<PathBuf>, max_bytes: u64, max_files: usize) -> Self {
Self {
path: path.into(),
rotation: RotationPolicy::Size,
max_bytes: Some(max_bytes),
max_files,
keep_days: None,
compress: false,
}
}
pub fn daily(path: impl Into<PathBuf>, max_files: usize) -> Self {
Self {
path: path.into(),
rotation: RotationPolicy::Daily,
max_bytes: None,
max_files,
keep_days: None,
compress: false,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PreparedLogWriter {
pub path: Option<PathBuf>,
pub rotation_enabled: bool,
}
impl LogWriterConfig {
pub fn file(path: impl Into<PathBuf>) -> Self {
Self::File(path.into())
}
}
pub fn validate_writer(config: &LogWriterConfig) -> CoreResult<PreparedLogWriter> {
match config {
LogWriterConfig::Stdout | LogWriterConfig::Stderr => Ok(PreparedLogWriter {
path: None,
rotation_enabled: false,
}),
LogWriterConfig::File(path) => {
open_append_file(path)?;
Ok(PreparedLogWriter {
path: Some(path.clone()),
rotation_enabled: false,
})
}
LogWriterConfig::RollingFile(rolling) => {
open_append_file(&rolling.path)?;
Ok(PreparedLogWriter {
path: Some(rolling.path.clone()),
rotation_enabled: true,
})
}
}
}
#[derive(Clone)]
pub struct RuntimeRollingFileWriter {
inner: Arc<Mutex<RuntimeRollingFileState>>,
compression_worker: Option<CompressionWorker>,
}
impl RuntimeRollingFileWriter {
pub fn new(config: RollingFileConfig) -> CoreResult<Self> {
if let Some(parent) = config.path.parent() {
fs::create_dir_all(parent).map_err(|error| CoreError::Logging(error.to_string()))?;
}
cleanup_rotated_files(&config).map_err(|error| CoreError::Logging(error.to_string()))?;
let file = open_append_file(&config.path)?;
let bytes_written = file
.metadata()
.map_err(|error| CoreError::Logging(error.to_string()))?
.len();
let compression_worker = config.compress.then(CompressionWorker::spawn);
Ok(Self {
inner: Arc::new(Mutex::new(RuntimeRollingFileState {
current_day: current_day(),
config,
file,
bytes_written,
compression_sender: compression_worker.as_ref().map(CompressionWorker::sender),
})),
compression_worker,
})
}
}
impl Drop for RuntimeRollingFileWriter {
fn drop(&mut self) {
if Arc::strong_count(&self.inner) == 1 {
if let Ok(mut state) = self.inner.lock() {
state.compression_sender = None;
}
self.compression_worker.take();
}
}
}
impl Write for RuntimeRollingFileWriter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let mut state = self
.inner
.lock()
.map_err(|_| io::Error::other("rolling log writer mutex poisoned"))?;
state.write(buf)
}
fn flush(&mut self) -> io::Result<()> {
let mut state = self
.inner
.lock()
.map_err(|_| io::Error::other("rolling log writer mutex poisoned"))?;
state.file.flush()
}
}
pub(crate) fn open_append_file(path: &Path) -> CoreResult<File> {
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).map_err(|error| CoreError::Logging(error.to_string()))?;
}
OpenOptions::new()
.create(true)
.append(true)
.open(path)
.map_err(|error| CoreError::Logging(error.to_string()))
}
struct RuntimeRollingFileState {
config: RollingFileConfig,
file: File,
bytes_written: u64,
current_day: NaiveDate,
compression_sender: Option<Sender<PathBuf>>,
}
impl RuntimeRollingFileState {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.rotate_if_needed(buf.len() as u64)?;
self.file.write_all(buf)?;
self.bytes_written = self.bytes_written.saturating_add(buf.len() as u64);
Ok(buf.len())
}
fn rotate_if_needed(&mut self, incoming_bytes: u64) -> io::Result<()> {
match self.config.rotation {
RotationPolicy::Size => self.rotate_by_size_if_needed(incoming_bytes),
RotationPolicy::Daily => self.rotate_by_day_if_needed(),
}
}
fn rotate_by_size_if_needed(&mut self, incoming_bytes: u64) -> io::Result<()> {
let Some(max_bytes) = self.config.max_bytes else {
return Ok(());
};
if max_bytes == 0 || self.bytes_written.saturating_add(incoming_bytes) <= max_bytes {
return Ok(());
}
self.rotate_active_file()
}
fn rotate_by_day_if_needed(&mut self) -> io::Result<()> {
let today = current_day();
if today <= self.current_day {
return Ok(());
}
self.current_day = today;
self.rotate_active_file()
}
fn rotate_active_file(&mut self) -> io::Result<()> {
self.file.flush()?;
let rotated = rotate_files(&self.config)?;
self.file = OpenOptions::new()
.create(true)
.append(true)
.open(&self.config.path)?;
self.bytes_written = 0;
if let Some(rotated) = rotated {
self.compress_rotated_file(rotated);
}
cleanup_rotated_files(&self.config)
}
fn compress_rotated_file(&mut self, rotated: PathBuf) {
let Some(sender) = &self.compression_sender else {
return;
};
if sender.send(rotated).is_err() {
self.compression_sender = None;
}
}
}
struct CompressionWorker {
inner: Arc<CompressionWorkerInner>,
}
struct CompressionWorkerInner {
sender: Mutex<Option<Sender<PathBuf>>>,
handle: Mutex<Option<JoinHandle<()>>>,
}
impl CompressionWorker {
fn spawn() -> Self {
let (sender, receiver) = mpsc::channel();
let handle = thread::Builder::new()
.name("rs-zero-log-compressor".to_string())
.spawn(move || compress_paths(receiver))
.expect("spawn log compression worker");
Self {
inner: Arc::new(CompressionWorkerInner {
sender: Mutex::new(Some(sender)),
handle: Mutex::new(Some(handle)),
}),
}
}
fn sender(&self) -> Sender<PathBuf> {
self.inner
.sender
.lock()
.expect("compression worker sender mutex")
.as_ref()
.expect("compression worker sender open")
.clone()
}
}
impl Clone for CompressionWorker {
fn clone(&self) -> Self {
Self {
inner: Arc::clone(&self.inner),
}
}
}
impl Drop for CompressionWorkerInner {
fn drop(&mut self) {
self.sender
.lock()
.expect("compression worker sender mutex")
.take();
let handle = self
.handle
.lock()
.expect("compression worker handle mutex")
.take();
if let Some(handle) = handle {
let _ = handle.join();
}
}
}
fn compress_paths(receiver: Receiver<PathBuf>) {
for path in receiver {
let _ = compress_file(&path);
}
}
fn current_day() -> NaiveDate {
#[cfg(test)]
{
Local::now().date_naive()
+ chrono::Duration::days(crate::core::logging::test_hooks::current_day_offset() as i64)
}
#[cfg(not(test))]
{
Local::now().date_naive()
}
}
#[cfg(test)]
mod tests {
use std::{
fs,
io::Write,
thread,
time::{Duration, Instant},
};
use super::{RollingFileConfig, RotationPolicy, RuntimeRollingFileWriter};
use crate::core::logging::test_hooks;
#[test]
fn compresses_rotated_files_asynchronously() {
let temp = tempfile::tempdir().expect("tempdir");
let path = temp.path().join("service.log");
let pause = test_hooks::pause_compression_for_dir(temp.path());
let mut writer = RuntimeRollingFileWriter::new(RollingFileConfig {
path: path.clone(),
rotation: RotationPolicy::Size,
max_bytes: Some(64),
max_files: 2,
keep_days: None,
compress: true,
})
.expect("writer");
writer
.write_all(
b"first rotated payload with enough bytes to cross the configured size limit\n",
)
.expect("first write");
writer
.write_all(b"second payload reopens the active file before gzip can finish\n")
.expect("second write");
assert!(
path.exists(),
"active log file should be reopened before compression finishes"
);
assert!(
has_uncompressed_rotated_file(temp.path()),
"rotated file should remain uncompressed while compression is paused"
);
assert!(
!has_gzip_rotated_file(temp.path()),
"compression should not run synchronously on the logging path"
);
pause.resume();
drop(writer);
wait_until(Duration::from_secs(2), || {
has_gzip_rotated_file(temp.path())
});
}
#[test]
fn rotates_daily_when_day_advances() {
let temp = tempfile::tempdir().expect("tempdir");
let path = temp.path().join("service.log");
let clock = test_hooks::set_current_day_offset(-1);
let mut writer = RuntimeRollingFileWriter::new(RollingFileConfig {
path: path.clone(),
rotation: RotationPolicy::Daily,
max_bytes: None,
max_files: 2,
keep_days: None,
compress: false,
})
.expect("writer");
writer.write_all(b"before-day-advance\n").expect("before");
clock.set_offset_days(0);
writer.write_all(b"after-day-advance\n").expect("after");
drop(writer);
let active = fs::read_to_string(&path).expect("active log");
assert!(active.contains("after-day-advance"));
assert!(!active.contains("before-day-advance"));
let rotated = fs::read_dir(temp.path())
.expect("log dir")
.filter_map(|entry| entry.ok().map(|entry| entry.path()))
.find(|candidate| {
candidate
.file_name()
.and_then(|name| name.to_str())
.is_some_and(|name| name.starts_with("service.log.20"))
})
.expect("dated rotated log");
let rotated_output = fs::read_to_string(rotated).expect("rotated log");
assert!(rotated_output.contains("before-day-advance"));
}
fn wait_until(timeout: Duration, condition: impl Fn() -> bool) {
let start = Instant::now();
while start.elapsed() < timeout {
if condition() {
return;
}
thread::sleep(Duration::from_millis(10));
}
assert!(condition(), "condition was not met before timeout");
}
fn has_gzip_rotated_file(dir: &std::path::Path) -> bool {
fs::read_dir(dir)
.expect("log dir")
.filter_map(|entry| entry.ok().map(|entry| entry.path()))
.any(|path| {
path.file_name()
.and_then(|name| name.to_str())
.is_some_and(|name| name.starts_with("service.log.") && name.ends_with(".gz"))
})
}
fn has_uncompressed_rotated_file(dir: &std::path::Path) -> bool {
fs::read_dir(dir)
.expect("log dir")
.filter_map(|entry| entry.ok().map(|entry| entry.path()))
.any(|path| {
path.file_name()
.and_then(|name| name.to_str())
.is_some_and(|name| name.starts_with("service.log.") && !name.ends_with(".gz"))
})
}
}