Skip to content

Commit

Permalink
Refactor emit.
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeMathWalker committed Jan 8, 2023
1 parent 56aa5b8 commit 710d9bf
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/formatting_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ impl<W: for<'a> MakeWriter<'a> + 'static> BunyanFormattingLayer<W> {
/// If we write directly to the writer returned by self.make_writer in more than one go
/// we can end up with broken/incoherent bits and pieces of those records when
/// running multi-threaded/concurrent programs.
fn emit(&self, mut buffer: Vec<u8>) -> Result<(), std::io::Error> {
fn emit(&self, mut buffer: &mut [u8]) -> Result<(), std::io::Error> {
buffer.write_all(b"\n")?;
self.make_writer.make_writer().write_all(&buffer)
}
Expand Down Expand Up @@ -351,22 +351,22 @@ impl<S, W> Layer<S> for BunyanFormattingLayer<W>
};

let result: std::io::Result<Vec<u8>> = format();
if let Ok(formatted) = result {
let _ = self.emit(formatted);
if let Ok(mut formatted) = result {
let _ = self.emit(&mut formatted);
}
}

fn on_new_span(&self, _attrs: &Attributes, id: &Id, ctx: Context<'_, S>) {
let span = ctx.span(id).expect("Span not found, this is a bug");
if let Ok(serialized) = self.serialize_span(&span, Type::EnterSpan) {
let _ = self.emit(serialized);
if let Ok(mut serialized) = self.serialize_span(&span, Type::EnterSpan) {
let _ = self.emit(&mut serialized);
}
}

fn on_close(&self, id: Id, ctx: Context<'_, S>) {
let span = ctx.span(&id).expect("Span not found, this is a bug");
if let Ok(serialized) = self.serialize_span(&span, Type::ExitSpan) {
let _ = self.emit(serialized);
if let Ok(mut serialized) = self.serialize_span(&span, Type::ExitSpan) {
let _ = self.emit(&mut serialized);
}
}
}

0 comments on commit 710d9bf

Please sign in to comment.