rust 49 lines · 9 steps

A buffered log writer in Rust

How a struct batches log entries in memory and flushes them to a file, including on drop.

Explained by highlit
1use std::fs::{File, OpenOptions};
2use std::io::{self, Write};
3use std::path::Path;
4 
5pub struct LogBuffer {
6 file: File,
7 buffer: Vec<String>,
8 capacity: usize,
9}
10 
11impl LogBuffer {
12 pub fn open(path: impl AsRef<Path>, capacity: usize) -> io::Result<Self> {
13 let file = OpenOptions::new().create(true).append(true).open(path)?;
14 Ok(Self {
15 file,
16 buffer: Vec::with_capacity(capacity),
17 capacity,
18 })
19 }
20 
21 pub fn write(&mut self, entry: impl Into<String>) -> io::Result<()> {
22 self.buffer.push(entry.into());
23 if self.buffer.len() >= self.capacity {
24 self.flush()?;
25 }
26 Ok(())
27 }
28 
29 pub fn flush(&mut self) -> io::Result<()> {
30 if self.buffer.is_empty() {
31 return Ok(());
32 }
33 let mut batch = String::new();
34 for entry in self.buffer.drain(..) {
35 batch.push_str(&entry);
36 batch.push('\n');
37 }
38 self.file.write_all(batch.as_bytes())?;
39 self.file.flush()
40 }
41}
42 
43impl Drop for LogBuffer {
44 fn drop(&mut self) {
45 if let Err(e) = self.flush() {
46 eprintln!("failed to flush log buffer on drop: {e}");
47 }
48 }
49}
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1Batching writes in memory and flushing in bulk trades a little latency for far fewer syscalls.
  2. 2Implementing Drop guarantees buffered data is flushed even when callers forget to do it explicitly.
  3. 3Accepting impl Into<String> and impl AsRef<Path> lets one API take a wide range of caller-friendly argument types.

Related explainers

Share this explainer

Here's the card — post it anywhere.

A buffered log writer in Rust — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code