use std::collections::HashMap;
use std::io;
#[cfg(unix)]
use std::os::fd::{AsRawFd, RawFd};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use shep_core::logstamp::Stamper;
use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt as _, BufWriter};
use tokio::time::Instant;
#[cfg(unix)]
use crate::boot::DIR_MODE;
use crate::runner::{FlushError, LogCtl, ReopenError, check_log_ancestry, open_log_path};
use super::pump::Streams;
#[cfg(unix)]
use super::pump::drain_ready;
use super::{IDLE_FLUSH, LOG_BUFFER};
#[cfg(unix)]
pub(super) fn carried_sink(path: PathBuf, handle: Option<tokio::fs::File>) -> LogSink {
match handle {
Some(file) => LogSink::Carried(path, file),
None => LogSink::Path(path),
}
}
#[derive(Debug)]
pub(super) struct LogFile<W = tokio::fs::File> {
pub(super) path: PathBuf,
pub(super) handle: Option<BufWriter<W>>,
pub(super) record: Arc<tokio::sync::Mutex<()>>,
pub(super) buffered_since: Option<Instant>,
pub(super) stamp: String,
pub(super) stamper: Option<Stamper>,
}
pub(super) enum LogSink {
Path(PathBuf),
#[cfg(unix)]
Carried(PathBuf, tokio::fs::File),
}
impl LogFile<tokio::fs::File> {
pub(super) async fn from_sink(sink: LogSink, stamped: bool) -> Self {
match sink {
LogSink::Path(path) => Self::open(path, stamped).await,
#[cfg(unix)]
LogSink::Carried(path, file) => Self::from_file(path, file, stamped),
}
}
#[cfg(unix)]
pub(super) fn from_file(path: PathBuf, file: tokio::fs::File, stamped: bool) -> Self {
Self {
record: record_lock(&path),
path,
handle: Some(BufWriter::with_capacity(LOG_BUFFER, file)),
buffered_since: None,
stamp: String::new(),
stamper: stamped.then(Stamper::default),
}
}
pub(super) async fn open(path: PathBuf, stamped: bool) -> Self {
let handle = open_append(&path)
.await
.ok()
.map(|file| BufWriter::with_capacity(LOG_BUFFER, file));
Self {
record: record_lock(&path),
path,
handle,
buffered_since: None,
stamp: String::new(),
stamper: stamped.then(Stamper::default),
}
}
async fn reopen(&mut self) -> Result<(), ReopenError> {
self.buffered_since = None;
if let Some(handle) = self.handle.as_mut() {
let _record = self.record.lock().await;
if let Err(error) = handle.flush().await {
tracing::error!(path = ?self.path, %error, "log file flush failed");
}
}
drop(self.handle.take());
match open_append(&self.path).await {
Ok(handle) => {
self.handle = Some(BufWriter::with_capacity(LOG_BUFFER, handle));
Ok(())
}
Err(error) => Err(ReopenError {
message: format!("{}: {error}", self.path.display()),
}),
}
}
#[cfg(unix)]
fn raw_fd(&self) -> Option<RawFd> {
self.handle
.as_ref()
.map(|handle| handle.get_ref().as_raw_fd())
}
}
pub(crate) async fn open_append(path: &Path) -> io::Result<tokio::fs::File> {
check_log_ancestry(path)
.inspect_err(|error| tracing::error!(?path, %error, "log ancestry check failed"))?;
if let Some(parent) = path.parent()
&& !parent.as_os_str().is_empty()
{
let mut builder = tokio::fs::DirBuilder::new();
builder.recursive(true);
#[cfg(unix)]
builder.mode(DIR_MODE);
if let Err(error) = builder.create(parent).await {
tracing::error!(?path, %error, "log directory create failed");
return Err(error);
}
}
let mut options = tokio::fs::OpenOptions::new();
options.create(true).append(true);
open_log_path(&mut options, path)
.await
.inspect_err(|error| tracing::error!(?path, %error, "log file open failed"))
}
pub(crate) fn record_lock(path: &Path) -> Arc<tokio::sync::Mutex<()>> {
static LOCKS: std::sync::OnceLock<
std::sync::Mutex<HashMap<PathBuf, Arc<tokio::sync::Mutex<()>>>>,
> = std::sync::OnceLock::new();
let mut locks = LOCKS
.get_or_init(std::sync::Mutex::default)
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
Arc::clone(locks.entry(path.to_path_buf()).or_default())
}
impl<W: AsyncWrite + Unpin> LogFile<W> {
pub(super) async fn append(&mut self, line: &str) {
let Some(handle) = self.handle.as_mut() else {
return;
};
self.stamp.clear();
if let Some(stamper) = self.stamper.as_mut() {
stamper.stamp_into(&mut self.stamp);
}
self.stamp.push_str(line);
self.stamp.push('\n');
let written = {
let _record = self.record.lock().await;
handle.write_all(self.stamp.as_bytes()).await
};
self.buffered_since.get_or_insert_with(Instant::now);
if let Err(error) = written {
tracing::error!(path = ?self.path, %error, "log file append failed");
}
}
pub(super) async fn flush(&mut self) -> Result<(), FlushError> {
self.buffered_since = None;
let Some(handle) = self.handle.as_mut() else {
return Ok(());
};
let _record = self.record.lock().await;
handle.flush().await.map_err(|error| FlushError {
message: format!("{}: {error}", self.path.display()),
})
}
}
#[cfg(unix)]
#[derive(Debug, Clone, Copy, Default)]
pub(super) struct PipeFds {
pub(super) out: Option<RawFd>,
pub(super) err: Option<RawFd>,
pub(super) stdin: Option<RawFd>,
pub(super) channel: Option<RawFd>,
}
#[cfg(not(unix))]
#[derive(Debug, Clone, Copy, Default)]
pub(super) struct PipeFds;
#[derive(Debug)]
pub(super) struct LogFiles {
pub(super) out: LogFile,
pub(super) err: LogFile,
pub(super) pipes: PipeFds,
#[cfg(unix)]
pub(super) parked: bool,
}
impl LogFiles {
pub(super) fn stream(&mut self, err: bool) -> &mut LogFile {
if err { &mut self.err } else { &mut self.out }
}
pub(super) fn reading(&self) -> bool {
#[cfg(unix)]
{
!self.parked
}
#[cfg(not(unix))]
{
true
}
}
pub(super) fn flush_deadline(&self) -> Option<Instant> {
let oldest = match (self.out.buffered_since, self.err.buffered_since) {
(Some(out), Some(err)) => out.min(err),
(Some(only), None) | (None, Some(only)) => only,
(None, None) => return None,
};
Some(oldest + IDLE_FLUSH)
}
pub(super) async fn flush_idle(&mut self) {
for file in [&mut self.out, &mut self.err] {
if let Err(error) = file.flush().await {
tracing::error!(%error, "log file idle flush failed");
}
}
}
#[cfg_attr(
not(unix),
allow(
unused_variables,
reason = "`streams` is read only by `ReportFds`, and that variant is unix-only"
)
)]
pub(super) async fn serve<O, E>(&mut self, ctl: LogCtl, streams: &mut Streams<O, E>)
where
O: AsyncRead + Unpin,
E: AsyncRead + Unpin,
{
match ctl {
LogCtl::Reopen { done } => {
let mut failures = Vec::new();
if let Err(error) = self.out.reopen().await {
failures.push(error.message);
}
if let Err(error) = self.err.reopen().await {
failures.push(error.message);
}
let result = if failures.is_empty() {
Ok(())
} else {
Err(ReopenError {
message: failures.join(", "),
})
};
let _ = done.send(result);
}
LogCtl::Flush { done } => {
let mut failures = Vec::new();
if let Err(error) = self.out.flush().await {
failures.push(error.message);
}
if let Err(error) = self.err.flush().await {
failures.push(error.message);
}
let result = if failures.is_empty() {
Ok(())
} else {
Err(FlushError {
message: failures.join(", "),
})
};
let _ = done.send(result);
}
#[cfg(unix)]
LogCtl::ReportFds { done } => {
self.parked = true;
drain_ready(&mut streams.out, &mut self.out).await;
drain_ready(&mut streams.err, &mut self.err).await;
for file in [&mut self.out, &mut self.err] {
if let Err(error) = file.flush().await {
tracing::error!(%error, "log flush before a handover report failed");
}
}
let _ = done.send(crate::handover::CarriedFds {
out_pipe: self.pipes.out,
err_pipe: self.pipes.err,
out_log: self.out.raw_fd(),
err_log: self.err.raw_fd(),
stdin: self.pipes.stdin,
channel: self.pipes.channel,
});
}
#[cfg(unix)]
LogCtl::Resume => self.parked = false,
}
}
}