Merge branch 'xMAC94x/logging_fix' into 'master'

fix logging cnt didnt reset

See merge request veloren/veloren!2518
This commit is contained in:
Marcel 2021-06-27 21:54:35 +00:00
commit 11d16f34bb

View File

@ -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