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
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Batching writes in memory and flushing in bulk trades a little latency for far fewer syscalls.
- 2Implementing Drop guarantees buffered data is flushed even when callers forget to do it explicitly.
- 3Accepting impl Into<String> and impl AsRef<Path> lets one API take a wide range of caller-friendly argument types.
Related explainers
rust
use serde::Deserialize; #[derive(Debug, Deserialize)] #[serde(untagged)]
Parsing flexible JSON shapes with serde
deserialization
enums
json
Intermediate
6 steps
ruby
require "shellwords" require "open3" module Backup
Building safe shell commands in Ruby
shell-out
subprocess
command-injection
Intermediate
7 steps
rust
use std::cmp::{Ordering, Reverse}; use std::collections::BinaryHeap; use std::fs::File; use std::io::{self, BufRead, BufReader, BufWriter, Lines, Write};
K-way merge of sorted logs in Rust
binary-heap
k-way-merge
streaming-io
Intermediate
8 steps
rust
use axum::{ extract::{Path, State}, response::sse::{Event, KeepAlive, Sse}, };
Streaming import progress with SSE in Axum
server-sent-events
streams
watch-channel
Advanced
7 steps
rust
use std::f64::consts::PI; #[derive(Debug, Clone, Copy)] pub struct LatLng {
Building geographic bounding boxes in Rust
geospatial
value-types
option
Intermediate
7 steps
rust
use chrono::{Duration, NaiveDate}; #[derive(Debug)] pub struct DateRange {
Parsing and iterating date ranges in Rust
error-handling
iterators
parsing
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/a-buffered-log-writer-in-rust-explained-rust-d919/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.