simplify logging

This commit is contained in:
holger krekel
2020-02-22 19:57:24 +01:00
parent 95d45b386f
commit 57311d731e
4 changed files with 23 additions and 26 deletions

View File

@@ -25,7 +25,10 @@ class Account(object):
by the underlying deltachat core library. All public Account methods are
meant to be memory-safe and return memory-safe objects.
"""
def __init__(self, db_path, logid=None, os_name=None, debug=True):
# to prevent garbled logging
_loglock = threading.RLock()
def __init__(self, db_path, logid=None, os_name=None):
""" initialize account object.
:param db_path: a path to the account database. The database
@@ -33,13 +36,12 @@ class Account(object):
:param logid: an optional logging prefix that should be used with
the default internal logging.
:param os_name: this will be put to the X-Mailer header in outgoing messages
:param debug: turn on debug logging for events.
"""
self._dc_context = ffi.gc(
lib.dc_context_new(lib.py_dc_callback, ffi.NULL, as_dc_charpointer(os_name)),
_destroy_dc_context,
)
self._evlogger = EventLogger(self, logid, debug)
self._evlogger = EventLogger(self, logid)
self._threads = IOThreads(self._dc_context, self._evlogger._log_event)
# initialize per-account plugin system
@@ -65,6 +67,15 @@ class Account(object):
# def __del__(self):
# self.shutdown()
def log_line(self, msg):
t = threading.currentThread()
tname = getattr(t, "name", t)
if tname == "MainThread":
tname = "MAIN"
with self._loglock:
print("{:2.2f} [{}-{}] {}".format(time.time() - self._evlogger.init_time,
tname, self._evlogger.logid, msg))
def _check_config_key(self, name):
if name not in self._configkeys:
raise KeyError("{!r} not a valid config key, existing keys: {!r}".format(

View File

@@ -4,11 +4,8 @@ from .hookspec import account_hookimpl
class EventLogger:
_loglock = threading.RLock()
def __init__(self, account, logid=None, debug=True):
def __init__(self, account, logid=None):
self.account = account
self._debug = debug
if logid is None:
logid = str(self.account._dc_context).strip(">").split()[-1]
self.logid = logid
@@ -22,14 +19,5 @@ class EventLogger:
# don't show events that are anyway empty impls now
if evt_name == "DC_EVENT_GET_STRING":
return
if self._debug:
evpart = "{}({!r},{!r})".format(evt_name, data1, data2)
self._log(evpart)
def _log(self, msg):
t = threading.currentThread()
tname = getattr(t, "name", t)
if tname == "MainThread":
tname = "MAIN"
with self._loglock:
print("{:2.2f} [{}-{}] {}".format(time.time() - self.init_time, tname, self.logid, msg))
evpart = "{}({!r},{!r})".format(evt_name, data1, data2)
self.account.log_line(evpart)