Skip to content

Commit

Permalink
Do not track dirty state in logger
Browse files Browse the repository at this point in the history
We don't regularly drop the inner logger, so one additional flush doesn't
justify the added complexity.

Signed-off-by: Moritz Hoffmann <antiguru@gmail.com>
  • Loading branch information
antiguru committed Jan 15, 2025
1 parent 9e693fe commit b128b93
Showing 1 changed file with 1 addition and 12 deletions.
13 changes: 1 addition & 12 deletions logging/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,6 @@ struct LoggerInner<CB: ContainerBuilder, A: ?Sized + FnMut(&Duration, &mut Optio
offset: Duration,
/// container builder to produce buffers of accumulated log events
builder: CB,
/// True if we logged an event since the last flush.
/// Used to avoid flushing buffers on drop.
dirty: bool,
/// action to take on full log buffers, or on flush.
action: A,
}
Expand All @@ -131,7 +128,6 @@ impl<CB: ContainerBuilder> Logger<CB> {
offset,
action,
builder: CB::default(),
dirty: false,
};
let inner = Rc::new(RefCell::new(inner));
Logger { inner }
Expand Down Expand Up @@ -243,7 +239,6 @@ impl<CB: ContainerBuilder, A: ?Sized + FnMut(&Duration, &mut Option<CB::Containe
{
let elapsed = self.time.elapsed() + self.offset;
for event in events {
self.dirty = true;
self.builder.push_into((elapsed, event.into()));
while let Some(container) = self.builder.extract() {
let mut c = Some(std::mem::take(container));
Expand All @@ -270,18 +265,13 @@ impl<CB: ContainerBuilder, A: ?Sized + FnMut(&Duration, &mut Option<CB::Containe

// Send no container to indicate flush.
(self.action)(&elapsed, &mut None);

self.dirty = false;
}
}

/// Flush on the *last* drop of a logger.
impl<CB: ContainerBuilder, A: ?Sized + FnMut(&Duration, &mut Option<CB::Container>)> Drop for LoggerInner<CB, A> {
fn drop(&mut self) {
// Avoid sending out empty buffers just because of drops.
if self.dirty {
self.flush();
}
self.flush();
}
}

Expand All @@ -293,7 +283,6 @@ where
f.debug_struct("LoggerInner")
.field("time", &self.time)
.field("offset", &self.offset)
.field("dirty", &self.dirty)
.field("action", &"FnMut")
.field("builder", &self.builder)
.finish()
Expand Down

0 comments on commit b128b93

Please sign in to comment.