diff --git a/src/syslog.rs b/src/syslog.rs index 4f902b3..c7eb2ce 100644 --- a/src/syslog.rs +++ b/src/syslog.rs @@ -339,6 +339,7 @@ thread_local! { static BUF: RefCell> = RefCell::new(Vec::with_capacity(2 impl io::Write for SyslogWriter { fn write(&mut self, bytes: &[u8]) -> io::Result { BUF.with(|buf| buf.borrow_mut().extend(bytes)); + self.flushed = false; Ok(bytes.len()) } @@ -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() {