fix logging cnt didnt reset

This commit is contained in:
Marcel Märtens 2021-06-27 22:51:23 +02:00
parent f6782e21b8
commit dd5c9d04b0

View File

@ -4,7 +4,8 @@ use tracing::Level;
/// used to collect multiple traces and not spam the console /// used to collect multiple traces and not spam the console
pub(crate) struct DeferredTracer<T: Eq + Hash> { pub(crate) struct DeferredTracer<T: Eq + Hash> {
level: Level, _level: Level,
log_enabled: bool, // cache
items: HashMap<T, u64>, items: HashMap<T, u64>,
last: Instant, last: Instant,
last_cnt: u32, last_cnt: u32,
@ -13,7 +14,8 @@ pub(crate) struct DeferredTracer<T: Eq + Hash> {
impl<T: Eq + Hash> DeferredTracer<T> { impl<T: Eq + Hash> DeferredTracer<T> {
pub(crate) fn new(level: Level) -> Self { pub(crate) fn new(level: Level) -> Self {
Self { Self {
level, _level: level,
log_enabled: tracing::level_enabled!(level),
items: HashMap::new(), items: HashMap::new(),
last: Instant::now(), last: Instant::now(),
last_cnt: 0, last_cnt: 0,
@ -21,7 +23,7 @@ impl<T: Eq + Hash> DeferredTracer<T> {
} }
pub(crate) fn log(&mut self, t: T) { pub(crate) fn log(&mut self, t: T) {
if tracing::level_enabled!(self.level) { if self.log_enabled {
*self.items.entry(t).or_default() += 1; *self.items.entry(t).or_default() += 1;
self.last = Instant::now(); self.last = Instant::now();
self.last_cnt += 1; self.last_cnt += 1;
@ -32,12 +34,13 @@ impl<T: Eq + Hash> DeferredTracer<T> {
pub(crate) fn print(&mut self) -> Option<HashMap<T, u64>> { pub(crate) fn print(&mut self) -> Option<HashMap<T, u64>> {
const MAX_LOGS: u32 = 10_000; const MAX_LOGS: u32 = 10_000;
const MAX_SECS: u64 = 1; const MAX_SECS: u64 = 1;
if tracing::level_enabled!(self.level) if self.log_enabled
&& (self.last_cnt > MAX_LOGS || self.last.elapsed().as_secs() >= MAX_SECS) && (self.last_cnt > MAX_LOGS || self.last.elapsed().as_secs() >= MAX_SECS)
{ {
if self.last_cnt > MAX_LOGS { if self.last_cnt > MAX_LOGS {
tracing::debug!("this seems to be logged continuesly"); tracing::debug!("this seems to be logged continuously");
} }
self.last_cnt = 0;
Some(std::mem::take(&mut self.items)) Some(std::mem::take(&mut self.items))
} else { } else {
None None