mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
fix logging cnt didnt reset
This commit is contained in:
parent
f6782e21b8
commit
dd5c9d04b0
@ -4,7 +4,8 @@ use tracing::Level;
|
||||
|
||||
/// used to collect multiple traces and not spam the console
|
||||
pub(crate) struct DeferredTracer<T: Eq + Hash> {
|
||||
level: Level,
|
||||
_level: Level,
|
||||
log_enabled: bool, // cache
|
||||
items: HashMap<T, u64>,
|
||||
last: Instant,
|
||||
last_cnt: u32,
|
||||
@ -13,7 +14,8 @@ pub(crate) struct DeferredTracer<T: Eq + Hash> {
|
||||
impl<T: Eq + Hash> DeferredTracer<T> {
|
||||
pub(crate) fn new(level: Level) -> Self {
|
||||
Self {
|
||||
level,
|
||||
_level: level,
|
||||
log_enabled: tracing::level_enabled!(level),
|
||||
items: HashMap::new(),
|
||||
last: Instant::now(),
|
||||
last_cnt: 0,
|
||||
@ -21,7 +23,7 @@ impl<T: Eq + Hash> DeferredTracer<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.last = Instant::now();
|
||||
self.last_cnt += 1;
|
||||
@ -32,12 +34,13 @@ impl<T: Eq + Hash> DeferredTracer<T> {
|
||||
pub(crate) fn print(&mut self) -> Option<HashMap<T, u64>> {
|
||||
const MAX_LOGS: u32 = 10_000;
|
||||
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)
|
||||
{
|
||||
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))
|
||||
} else {
|
||||
None
|
||||
|
Loading…
Reference in New Issue
Block a user