Browse Source

support for limiting log messages

devel
Stefan Holst 1 year ago
parent
commit
f7ef78e58d
  1. 23
      src/kyupy/__init__.py

23
src/kyupy/__init__.py

@ -124,6 +124,25 @@ class Log: @@ -124,6 +124,25 @@ class Log:
After each write, ``flush()`` is called as well.
"""
self.indent = 0
self._limit = -1
self.filtered = 0
def limit(self, log_limit):
class Limiter:
def __init__(self, l): self.l = l
def __enter__(self): self.l.start_limit(log_limit); return self
def __exit__(self, *args): self.l.stop_limit()
return Limiter(self)
def start_limit(self, limit):
self.filtered = 0
self._limit = limit
def stop_limit(self):
if self.filtered > 0:
log.info(f'{self.filtered} more messages (filtered).')
self.filtered = 0
self._limit = -1
def __getstate__(self):
return {'elapsed': time.perf_counter() - self.start}
@ -146,9 +165,13 @@ class Log: @@ -146,9 +165,13 @@ class Log:
def ie(self, n=1): self.indent -= n
def log(self, level, message):
if self._limit == 0:
self.filtered += 1
return
t = time.perf_counter() - self.start
self.logfile.write(f'# {t:011.3f} {level} {message}\n')
self.logfile.flush()
self._limit -= 1
def info(self, message):
"""Log an informational message."""

Loading…
Cancel
Save