use std::fs::File;
use std::io::{Result, Write};
use std::path::Path;
use tracing_appender::{
non_blocking,
non_blocking::{NonBlocking, WorkerGuard},
};
use crate::agent::AgentEvent;
pub(super) struct JsonlWriter {
writer: NonBlocking,
_guard: WorkerGuard,
}
impl JsonlWriter {
pub(super) fn open(file: &Path) -> Result<Self> {
let (writer, _guard) = non_blocking(File::options().append(true).create(true).open(file)?);
Ok(Self { writer, _guard })
}
pub(super) fn write(&mut self, event: &AgentEvent) -> Result<usize> {
let json = serde_json::to_vec(event)?;
let len = json.len() + 1;
self.writer.write_all(&json)?;
self.writer.write_all(b"\n")?;
Ok(len)
}
}