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
ruby
class Order class InvalidTransition < StandardError; end TRANSITIONS = {
A state machine for order transitions in Ruby
state-machine
data-driven
error-handling
Intermediate
8 steps
rust
use std::cmp::Ordering; pub struct PrefixIndex { entries: Vec<String>,
Prefix search with binary partitioning in Rust
binary-search
sorting
case-insensitive
Intermediate
7 steps
rust
use std::cmp::Ordering; use std::str::FromStr; #[derive(Debug, Clone, PartialEq, Eq)]
Parsing and ordering semantic versions in Rust
parsing
trait-implementation
ordering
Intermediate
8 steps
javascript
'use client'; import { useEffect } from 'react'; import * as Sentry from '@sentry/nextjs';
How a Next.js error boundary recovers
error-boundary
error-handling
observability
Intermediate
8 steps
ruby
class Registration < ApplicationRecord belongs_to :event validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP }
Validating registrations in Rails
validations
i18n
error-handling
Intermediate
8 steps
javascript
const MAX_BATCH_SIZE = 20; const FLUSH_INTERVAL = 5000; const ENDPOINT = '/api/analytics';
Batching analytics events in the browser
batching
buffering
sendbeacon
Intermediate
10 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.