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 by the underlying deltachat core library. All public Account methods are
meant to be memory-safe and return memory-safe objects. 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. """ initialize account object.
:param db_path: a path to the account database. The database :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 :param logid: an optional logging prefix that should be used with
the default internal logging. the default internal logging.
:param os_name: this will be put to the X-Mailer header in outgoing messages :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( self._dc_context = ffi.gc(
lib.dc_context_new(lib.py_dc_callback, ffi.NULL, as_dc_charpointer(os_name)), lib.dc_context_new(lib.py_dc_callback, ffi.NULL, as_dc_charpointer(os_name)),
_destroy_dc_context, _destroy_dc_context,
) )
self._evlogger = EventLogger(self, logid, debug) self._evlogger = EventLogger(self, logid)
self._threads = IOThreads(self._dc_context, self._evlogger._log_event) self._threads = IOThreads(self._dc_context, self._evlogger._log_event)
# initialize per-account plugin system # initialize per-account plugin system
@@ -65,6 +67,15 @@ class Account(object):
# def __del__(self): # def __del__(self):
# self.shutdown() # 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): def _check_config_key(self, name):
if name not in self._configkeys: if name not in self._configkeys:
raise KeyError("{!r} not a valid config key, existing keys: {!r}".format( 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: class EventLogger:
_loglock = threading.RLock() def __init__(self, account, logid=None):
def __init__(self, account, logid=None, debug=True):
self.account = account self.account = account
self._debug = debug
if logid is None: if logid is None:
logid = str(self.account._dc_context).strip(">").split()[-1] logid = str(self.account._dc_context).strip(">").split()[-1]
self.logid = logid self.logid = logid
@@ -22,14 +19,5 @@ class EventLogger:
# don't show events that are anyway empty impls now # don't show events that are anyway empty impls now
if evt_name == "DC_EVENT_GET_STRING": if evt_name == "DC_EVENT_GET_STRING":
return return
if self._debug: evpart = "{}({!r},{!r})".format(evt_name, data1, data2)
evpart = "{}({!r},{!r})".format(evt_name, data1, data2) self.account.log_line(evpart)
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))

View File

@@ -39,7 +39,7 @@ class FFIEventTracker:
assert not rex.match(ev[0]), "event found {}".format(ev) assert not rex.match(ev[0]), "event found {}".format(ev)
def get_matching(self, event_name_regex, check_error=True, timeout=None): def get_matching(self, event_name_regex, check_error=True, timeout=None):
self.account._evlogger._log("-- waiting for event with regex: {} --".format(event_name_regex)) self.account.log_line("-- waiting for event with regex: {} --".format(event_name_regex))
rex = re.compile("(?:{}).*".format(event_name_regex)) rex = re.compile("(?:{}).*".format(event_name_regex))
while 1: while 1:
ev = self.get(timeout=timeout, check_error=check_error) ev = self.get(timeout=timeout, check_error=check_error)

View File

@@ -16,14 +16,12 @@ def test_callback_None2int():
clear_context_callback(ctx) clear_context_callback(ctx)
def test_dc_close_events(tmpdir): def test_dc_close_events(tmpdir, acfactory):
from deltachat.account import Account ac1 = acfactory.get_unconfigured_account()
p = tmpdir.join("hello.db")
ac1 = Account(p.strpath)
ac1.shutdown() ac1.shutdown()
def find(info_string): def find(info_string):
evlog = ac1._evlogger evlog = ac1._evtracker
while 1: while 1:
ev = evlog.get_matching("DC_EVENT_INFO", check_error=False) ev = evlog.get_matching("DC_EVENT_INFO", check_error=False)
data2 = ev[2] data2 = ev[2]
@@ -80,10 +78,10 @@ def test_markseen_invalid_message_ids(acfactory):
contact1 = ac1.create_contact(email="some1@example.com", name="some1") contact1 = ac1.create_contact(email="some1@example.com", name="some1")
chat = ac1.create_chat_by_contact(contact1) chat = ac1.create_chat_by_contact(contact1)
chat.send_text("one messae") chat.send_text("one messae")
ac1._evlogger.get_matching("DC_EVENT_MSGS_CHANGED") ac1._evtracker.get_matching("DC_EVENT_MSGS_CHANGED")
msg_ids = [9] msg_ids = [9]
lib.dc_markseen_msgs(ac1._dc_context, msg_ids, len(msg_ids)) lib.dc_markseen_msgs(ac1._dc_context, msg_ids, len(msg_ids))
ac1._evlogger.ensure_event_not_queued("DC_EVENT_WARNING|DC_EVENT_ERROR") ac1._evtracker.ensure_event_not_queued("DC_EVENT_WARNING|DC_EVENT_ERROR")
def test_get_special_message_id_returns_empty_message(acfactory): def test_get_special_message_id_returns_empty_message(acfactory):