Skip to content

Commit

Permalink
pass note callback through to actor, also test context noting (though…
Browse files Browse the repository at this point in the history
… not testing actor noting yet)
  • Loading branch information
mahmoud committed Mar 10, 2016
1 parent 08f4fba commit 992fe57
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
18 changes: 8 additions & 10 deletions lithoxyl/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,13 @@ def note(self, name, message, *a, **kw):
"""
if not self.note_handlers:
return
# call_level = kw.pop('call_level', None)
callpoint = None
# if call_level:
# callpoint = None # TODO: extract callpoint info
try:
message = message % a
except Exception:
pass
if a:
try:
message = message % a
except Exception:
pass
for nh in self.note_handlers:
nh(name, message, callpoint=callpoint)
nh(name, message)
return

def enable_async(self, **kwargs):
Expand All @@ -70,7 +67,8 @@ def enable_async(self, **kwargs):
'interval': kwargs.pop('interval', None),
'max_interval': kwargs.pop('max_interval', None),
# be very careful when not daemonizing thread
'daemonize_thread': kwargs.pop('daemonize_thread', True)}
'daemonize_thread': kwargs.pop('daemonize_thread', True),
'note': self.note}
if kwargs:
raise TypeError('unexpected keyword arguments: %r' % kwargs.keys())

Expand Down
25 changes: 24 additions & 1 deletion lithoxyl/tests/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import time

from lithoxyl.context import get_context
from lithoxyl.logger import Logger
from lithoxyl.context import get_context, LithoxylContext


def test_async_on_off():
Expand All @@ -19,3 +20,25 @@ def test_async_on_off():
assert not ctx.async_actor.is_alive()

return


def test_preflush_catching_and_noting():
ctx = LithoxylContext()

def raiser(log):
raise RuntimeError('never gonna catch me')

log = Logger('test_logger', context=ctx)
log.preflush_hooks.append(raiser)

notes = []

def add_note(name, message):
notes.append((name, message))

ctx.note_handlers.append(add_note)

ctx.enable_async()
time.sleep(0.3)

assert notes # should have at least one note in 300ms

0 comments on commit 992fe57

Please sign in to comment.