From: Zooko O'Whielacronx Date: Mon, 15 Oct 2007 03:43:11 +0000 (-0700) Subject: install our custom timestamp formats in a less disruptive way X-Git-Tag: allmydata-tahoe-0.6.1~18 X-Git-Url: https://git.rkrishnan.org/class-simplejson.JSONEncoder-index.html?a=commitdiff_plain;h=240de64598fdf5ca3ff5dac7423a19d44c8f152d;p=tahoe-lafs%2Ftahoe-lafs.git install our custom timestamp formats in a less disruptive way The unit tests on Windows fail because trial is attempting to remove its own log observer during teardown. This patch customizes the extant log observer object by replacing its formatTime method with our own. I first tried the approach of storing their log observer object and putting it back during teardown, but it didn't work (perhaps because our node object doesn't get a chance to do its deferred stopService behavior in time), and anyway I generally prefer the "fail-safe", or "crash-only" design. --- diff --git a/src/allmydata/node.py b/src/allmydata/node.py index 5e4c5e2e..5b699a91 100644 --- a/src/allmydata/node.py +++ b/src/allmydata/node.py @@ -1,5 +1,5 @@ -import os.path, re, time +import new, os.path, re, time from base64 import b32decode, b32encode import twisted @@ -20,15 +20,14 @@ ADDR_RE=re.compile("^([1-9][0-9]*\.[1-9][0-9]*\.[1-9][0-9]*\.[1-9][0-9]*)(:([1-9 -class MyFileLogObserver(log.FileLogObserver): - def formatTime(self, when): - # we want UTC timestamps that look like: - # 2007-10-12 00:26:28.566Z [Client] rnp752lz: 'client running' - base = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(when)) - # now add the milliseconds - fraction = when - int(when) - suffix = ".%03dZ" % (1000*fraction,) - return base + suffix +def formatTimeTahoeStyle(self, when): + # we want UTC timestamps that look like: + # 2007-10-12 00:26:28.566Z [Client] rnp752lz: 'client running' + base = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(when)) + # now add the milliseconds + fraction = when - int(when) + suffix = ".%03dZ" % (1000*fraction,) + return base + suffix class Node(service.MultiService): @@ -189,14 +188,9 @@ class Node(service.MultiService): if type(o) is type(self.setup_logging): # bound method ob = o.im_self if isinstance(ob, log.FileLogObserver): - log.removeObserver(o) - logdir = os.path.abspath(os.path.join(self.basedir, "logs")) - fileutil.make_dirs(logdir) - lf = logfile.LogFile("twistd.log", logdir) + newmeth = new.instancemethod(formatTimeTahoeStyle, ob, ob.__class__) + ob.formatTime = newmeth # TODO: twisted >2.5.0 offers maxRotatedFiles=50 - ob = MyFileLogObserver(lf) - log.addObserver(ob.emit) - return def log(self, msg, src="", args=()): if src: @@ -208,9 +202,6 @@ class Node(service.MultiService): msg = msg % tuple(map(humanreadable.hr, args)) except TypeError, e: msg = "ERROR: output string '%s' contained invalid %% expansion, error: %s, args: %s\n" % (`msg`, e, `args`) - # TODO: modify the timestamp to include milliseconds - # TODO: modify it to be in UTC instead of localtime - # (see twisted/python/log.py:FileLogObserver.formatTime line 362) log.callWithContext({"system":logsrc}, log.msg,