d.addCallback(_check_addresses)
return d
- def test_log(self):
- basedir = "test_node/test_log"
- fileutil.make_dirs(basedir)
- n = TestNode(basedir)
- n.log("this is a message")
- n.log("with %d %s %s", args=(2, "interpolated", "parameters"))
- n.log("with bogus %d expansion", args=("not an integer",))
-
- def test_logpublisher(self):
- basedir = "test_node/test_logpublisher"
- fileutil.make_dirs(basedir)
- observer = LogObserver()
- n = TestNode(basedir)
- n.setServiceParent(self.parent)
- d = n.when_tub_ready()
- def _ready(res):
- n.log("starting up")
- flogport = open(os.path.join(n.basedir,"logport.furl"), "r").read()
- return n.tub.getReference(flogport.strip())
- d.addCallback(_ready)
- def _got_logport(logport):
- d = logport.callRemote("get_versions")
- def _check(versions):
- self.failUnlessEqual(versions["allmydata"],
- allmydata.__version__)
- d.addCallback(_check)
- d.addCallback(lambda res:
- logport.callRemote("subscribe_to_all", observer))
- def _emit(subscription):
- self._subscription = subscription
- log.msg("message here")
- d.addCallback(_emit)
- d.addCallback(fireEventually)
- d.addCallback(fireEventually)
- def _check_observer(res):
- msgs = observer.messages
- self.failUnlessEqual(len(msgs), 1)
- #print msgs
- self.failUnlessEqual(msgs[0]["message"], ("message here",) )
- d.addCallback(_check_observer)
- def _done(res):
- return logport.callRemote("unsubscribe", self._subscription)
- d.addCallback(_done)
- return d
- d.addCallback(_got_logport)
- return d
-
- def test_log_gatherer(self):
- t = Tub()
- t.setServiceParent(self.parent)
- t.listenOn("tcp:0:interface=127.0.0.1")
- l = t.getListeners()[0]
- portnum = l.getPortnum()
- t.setLocation("127.0.0.1:%d" % portnum)
- gatherer = Gatherer()
- gatherer.d = defer.Deferred()
- gatherer_furl = t.registerReference(gatherer)
-
- basedir = "test_node/test_log_gatherer"
- fileutil.make_dirs(basedir)
- f = open(os.path.join(basedir, "log_gatherer.furl"), "w")
- f.write(gatherer_furl + "\n")
- f.close()
-
- n = TestNode(basedir)
- n.setServiceParent(self.parent)
- d = n.when_tub_ready()
- def _ready(res):
- n.log("starting up")
- # about now, the node will be contacting the Gatherer and
- # offering its logport.
- return gatherer.d
- d.addCallback(_ready)
- return d
-
def test_timestamp(self):
# this modified logger doesn't seem to get used during the tests,
# probably because we don't modify the LogObserver that trial
d = self.pm.poll(check_f=i.next,
pollinterval=0.1)
return self._check(d)
-
-class SampleError(Exception):
- pass
-
-class Log(unittest.TestCase):
- def setUp(self):
- self.catcher = []
- twisted_log.addObserver(self.catcher.append)
- def tearDown(self):
- twisted_log.removeObserver(self.catcher.append)
-
- def test_log(self):
- num = log.msg("this is a message")
- self.failUnless(isinstance(num, int))
- log.msg("sub message", parent=num)
- log.msg("numbered message", number=47)
-
- logs = self.catcher[:]
-
- self.failUnlessEqual(logs[0]['message'], ("this is a message",))
- self.failUnlessEqual(logs[0]['number'], num)
- self.failUnlessEqual(logs[0]['parent'], None)
-
- self.failUnlessEqual(logs[1]['message'], ("sub message",))
- self.failUnlessEqual(logs[1]['number'], num+1)
- self.failUnlessEqual(logs[1]['parent'], num)
-
- self.failUnlessEqual(logs[2]['message'], ("numbered message",))
- self.failUnlessEqual(logs[2]['number'], 47)
- self.failUnlessEqual(logs[2]['parent'], None)
-
- def test_err(self):
- if not hasattr(self, "flushLoggedErrors"):
- raise unittest.SkipTest("need newer Twisted to test log.err")
-
- f = failure.Failure(SampleError())
- num2 = log.err(f)
- log.err(f, parent=num2)
- log.err(f, number=48)
-
- logs = self.catcher[:]
- self.flushLoggedErrors(SampleError)
-
- self.failUnlessEqual(logs[0]['message'], ())
- self.failUnlessEqual(logs[0]['failure'], f)
- self.failUnlessEqual(logs[0]['number'], num2)
- self.failUnlessEqual(logs[0]['parent'], None)
-
- self.failUnlessEqual(logs[1]['message'], ())
- self.failUnlessEqual(logs[1]['failure'], f)
- self.failUnlessEqual(logs[1]['number'], num2+1)
- self.failUnlessEqual(logs[1]['parent'], num2)
-
- self.failUnlessEqual(logs[2]['message'], ())
- self.failUnlessEqual(logs[2]['failure'], f)
- self.failUnlessEqual(logs[2]['number'], 48)
- self.failUnlessEqual(logs[2]['parent'], None)
-
-import itertools
-from twisted.python import log
-
-counter = itertools.count()
-
-def msg(*message, **kw):
- if 'number' not in kw:
- number = counter.next()
- kw['number'] = number
- else:
- number = kw['number']
- if 'parent' not in kw:
- kw['parent'] = None
- log.msg(*message, **kw)
- return number
-
-def err(*args, **kw):
- if 'number' not in kw:
- number = counter.next()
- kw['number'] = number
- else:
- number = kw['number']
- if 'parent' not in kw:
- kw['parent'] = None
- log.err(*args, **kw)
- return number
+from foolscap.logging import log
+from twisted.python import failure
+
+msg = log.msg
+
+def err(f=None, **kwargs):
+ if not f:
+ f = failure.Failure()
+ kwargs['failure'] = f
+ if 'level' not in kwargs:
+ kwargs['level'] = log.UNUSUAL
+ return log.msg("failure", **kwargs)