Skip to content

Commit

Permalink
fix: handle correctly multiple log messages per line
Browse files Browse the repository at this point in the history
  • Loading branch information
pamburus committed Oct 8, 2020
1 parent 9ae2fe5 commit 5651654
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,32 +127,32 @@ impl App {
buf: &mut Vec<u8>,
) {
for data in segment.data().split(|c| *c == b'\n') {
let data = strip(data, b'\r');
let data = trim_right(data, |ch| ch == b'\r');
if data.len() == 0 {
continue;
}
match json::from_slice::<Record>(data) {
Ok(rec) => {
if rec.matches(&self.options.filter) {
formatter.format_record(buf, &rec);
}
}
_ => {
buf.extend_from_slice(data);
buf.push(b'\n');
let mut stream = json::Deserializer::from_slice(data).into_iter::<Record>();
while let Some(Ok(record)) = stream.next() {
if record.matches(&self.options.filter) {
formatter.format_record(buf, &record);
}
}
let remainder = trim_right(&data[stream.byte_offset()..], |ch| match ch {
b'\r' | b'\n' | b' ' | b'\t' => true,
_ => false,
});
if remainder.len() > 0 {
buf.extend_from_slice(remainder);
buf.push(b'\n');
}
}
}
}

fn strip<'a>(slice: &'a [u8], ch: u8) -> &'a [u8] {
let n = slice.len();
if n == 0 {
slice
} else if slice[n - 1] == ch {
&slice[..n - 1]
fn trim_right<'a, F: Fn(u8) -> bool>(slice: &'a [u8], predicate: F) -> &'a [u8] {
if let Some(pos) = slice.iter().rposition(|&ch| !predicate(ch)) {
&slice[..pos + 1]
} else {
slice
&slice[0..0]
}
}

0 comments on commit 5651654

Please sign in to comment.