Skip to content

Commit 1fe5137

Browse files
authored
Merge pull request #14 from njam/write-flush
Flush the IO writer after each log message
2 parents bd53c2c + d3b8df4 commit 1fe5137

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## [Unreleased]
8+
### Added
9+
10+
* Option `flush` to enable flushing of the `io::Write` after each record.
11+
712
## 2.2.0 - 2017-12-10
813
### Added
914

lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ impl<S> slog::Serializer for SerdeSerializer<S>
168168
/// to a given `io`
169169
pub struct Json<W: io::Write> {
170170
newlines: bool,
171+
flush: bool,
171172
values: Vec<OwnedKVList>,
172173
io: RefCell<W>,
173174
pretty: bool,
@@ -238,6 +239,9 @@ impl<W> slog::Drain for Json<W>
238239
if self.newlines {
239240
try!(io.write_all("\n".as_bytes()));
240241
}
242+
if self.flush {
243+
io.flush()?;
244+
}
241245
Ok(())
242246
}
243247
}
@@ -250,6 +254,7 @@ impl<W> slog::Drain for Json<W>
250254
/// Create with `Json::new`.
251255
pub struct JsonBuilder<W: io::Write> {
252256
newlines: bool,
257+
flush: bool,
253258
values: Vec<OwnedKVList>,
254259
io: W,
255260
pretty: bool,
@@ -261,6 +266,7 @@ impl<W> JsonBuilder<W>
261266
fn new(io: W) -> Self {
262267
JsonBuilder {
263268
newlines: true,
269+
flush: false,
264270
values: vec![],
265271
io: io,
266272
pretty: false,
@@ -274,6 +280,7 @@ impl<W> JsonBuilder<W>
274280
Json {
275281
values: self.values,
276282
newlines: self.newlines,
283+
flush: self.flush,
277284
io: RefCell::new(self.io),
278285
pretty: self.pretty,
279286
}
@@ -285,6 +292,12 @@ impl<W> JsonBuilder<W>
285292
self
286293
}
287294

295+
/// Enable flushing of the `io::Write` after every log record
296+
pub fn set_flush(mut self, enabled: bool) -> Self {
297+
self.flush = enabled;
298+
self
299+
}
300+
288301
/// Set whether or not pretty formatted logging should be used
289302
pub fn set_pretty(mut self, enabled: bool) -> Self {
290303
self.pretty = enabled;

0 commit comments

Comments
 (0)