From d3b8df42be87c6d2d24c2f318325732a888a3230 Mon Sep 17 00:00:00 2001 From: Reto Kaiser Date: Mon, 19 Nov 2018 13:14:26 +0100 Subject: [PATCH] Flush the IO writer after each log message See #13 --- CHANGELOG.md | 5 +++++ lib.rs | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 28ce648..d19cc10 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [Unreleased] +### Added + +* Option `flush` to enable flushing of the `io::Write` after each record. + ## 2.2.0 - 2017-12-10 ### Added diff --git a/lib.rs b/lib.rs index b5cdb2b..233b4b7 100644 --- a/lib.rs +++ b/lib.rs @@ -168,6 +168,7 @@ impl slog::Serializer for SerdeSerializer /// to a given `io` pub struct Json { newlines: bool, + flush: bool, values: Vec, io: RefCell, pretty: bool, @@ -238,6 +239,9 @@ impl slog::Drain for Json if self.newlines { try!(io.write_all("\n".as_bytes())); } + if self.flush { + io.flush()?; + } Ok(()) } } @@ -250,6 +254,7 @@ impl slog::Drain for Json /// Create with `Json::new`. pub struct JsonBuilder { newlines: bool, + flush: bool, values: Vec, io: W, pretty: bool, @@ -261,6 +266,7 @@ impl JsonBuilder fn new(io: W) -> Self { JsonBuilder { newlines: true, + flush: false, values: vec![], io: io, pretty: false, @@ -274,6 +280,7 @@ impl JsonBuilder Json { values: self.values, newlines: self.newlines, + flush: self.flush, io: RefCell::new(self.io), pretty: self.pretty, } @@ -285,6 +292,12 @@ impl JsonBuilder self } + /// Enable flushing of the `io::Write` after every log record + pub fn set_flush(mut self, enabled: bool) -> Self { + self.flush = enabled; + self + } + /// Set whether or not pretty formatted logging should be used pub fn set_pretty(mut self, enabled: bool) -> Self { self.pretty = enabled;