Skip to content

Commit

Permalink
Fix logging messages after flush() (#7)
Browse files Browse the repository at this point in the history
Resets the "flushed" flag on each `write()` invocation.
Otherwise we would buffer data and never flush it out as demonstrated in
#4 (comment)

Adds unit test for this corner case.
  • Loading branch information
thomasjfox authored May 4, 2024
1 parent d6e7fea commit 63ea22f
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/syslog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ thread_local! { static BUF: RefCell<Vec<u8>> = RefCell::new(Vec::with_capacity(2
impl io::Write for SyslogWriter {
fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
BUF.with(|buf| buf.borrow_mut().extend(bytes));
self.flushed = false;
Ok(bytes.len())
}

Expand Down Expand Up @@ -477,6 +478,29 @@ mod tests {
}
}

#[test]
fn write_after_flush() {
let _lock = INITIALIZED.lock();

let process = "example-program";
let text = "test message";

let msg = capture_stderr(|| {
use std::io::Write;

let syslog = Syslog::new(IDENTITY, OPTIONS | Options::LOG_PERROR, FACILITY).unwrap();
let mut writer = syslog.make_writer();

writer.write_all(text.as_bytes()).unwrap();
writer.flush().unwrap();

writer.write_all(text.as_bytes()).unwrap();
// writer dropped here -> flush()
});

assert_eq!(msg, format!("{process}: {text}\n{process}: {text}\n"))
}

#[test]
#[should_panic = "interior nul byte"]
fn invalid_chars_panic() {
Expand Down

0 comments on commit 63ea22f

Please sign in to comment.