[Zope-CVS] CVS: Packages/SpreadLog - __init__.py:1.1 log.py:1.1 server.py:1.1
Jeremy Hylton
jeremy@zope.com
Mon, 8 Oct 2001 21:51:28 -0400
Update of /cvs-repository/Packages/SpreadLog
In directory cvs.zope.org:/tmp/cvs-serv22199
Added Files:
__init__.py log.py server.py
Log Message:
Initial checkin of zLOG that uses Spread
=== Added File Packages/SpreadLog/__init__.py ===
from log import * # log is where the action is
=== Added File Packages/SpreadLog/log.py ===
"""Distributed logging facility based on the Spread toolkit."""
import os
import zLOG
import spread
from zLOG import log_time, severity_string
from zLOG.FormatException import format_exception
class SpreadLog:
mbox = None
severity = 0
max_len = 128 * 1024 # XXX how to determine this?
def __init__(self):
self.initialize()
def initialize(self):
if self.mbox is not None:
self.mbox.disconnect()
spread_name = os.environ.get('SPREAD_LOG_NAME')
self.group = os.environ.get('SPREAD_LOG_GROUP', 'zLOG')
if not (spread_name is None or self.group is None):
name = "log:%d" % os.getpid()
self.mbox = spread.connect(spread_name, name, 0, 0)
severity = os.environ.get('SPREAD_LOG_SEVERITY')
if severity is not None:
self.severity = int(severity)
fmt = "------\n%%s pid %5d %%s %%s %%s\n" % os.getpid()
def log(self, subsystem, severity, summary, detail, error):
if self.mbox is None or severity < self.severity:
return
buf = self.fmt % (log_time(), severity_string(severity),
subsystem, summary)
if detail:
buf = buf + detail
if error:
try:
lines = format_exception(error[0], error[1], error[2],
trailer="\n", limit=100)
except Exception, err:
lines = ["%s: %s" % (error[0], error[1])]
buf = buf + "".join(lines)
truncated = 0
if len(buf) > self.max_len:
buf = buf[:self.max_len]
truncated = 1
self.mbox.multicast(spread.FIFO_MESS, self.group, buf)
if truncated:
zLOG.LOG("spreadlog", zLOG.PROBLEM,
"logged message was truncated")
log = SpreadLog()
zLOG.log_write = log.log
=== Added File Packages/SpreadLog/server.py ===
"""Run server process for the SpreadLog.
python server.py spread_name [log file] [group name]
arguments:
spread_name -- the name of the Spread daemon to use
log file -- the path to the log file (default is stderr)
group name -- the Spread group name to use (default is "zLOG")
"""
import select
import sys
import spread
class TestExitHook:
"""A hook for exiting the server during testing.
This can be passed as the text_exit argument to main(). If get()
returns true, the server will exit.
"""
def __init__(self):
self.exit = 0
def set(self):
self.exit = 1
def get(self):
return self.exit
def main(spread_name, log_file=sys.stderr, group="zLOG",
test_exit=None, ready_lock=None):
mbox = spread.connect(spread_name, "server", 0, 0)
mbox.join(group)
# just for testing...
if ready_lock is not None:
ready_lock.release()
while 1:
r = None
while 1:
try:
r, w, x = select.select([mbox.fileno()], [], [], 0.5)
except select.error:
pass
if r:
try:
msg = mbox.receive()
except spread.error, err:
pass
else:
print >> log_file, msg.message
else:
break
if test_exit is not None:
if test_exit.get():
break
mbox.disconnect()
if __name__ == "__main__":
if len(sys.argv) == 1:
print __doc__
sys.exit(-1)
spread_name = sys.argv[1]
if len(sys.argv) == 2:
log_path = sys.argv[2]
log_file = open(log_path, 'a', 1)
main(spread_name, log_file)
elif len(sys.argv) == 3:
log_path = sys.argv[2]
log_file = open(log_path, 'a', 1)
group = sys.argv[3]
main(spread_name, log_file, group)
else:
main(spread_name)