00001 """
00002 Logging facilities for RPKI libraries.
00003
00004 $Id: log.py 2452 2009-05-27 02:54:24Z sra $
00005
00006 Copyright (C) 2009 Internet Systems Consortium ("ISC")
00007
00008 Permission to use, copy, modify, and distribute this software for any
00009 purpose with or without fee is hereby granted, provided that the above
00010 copyright notice and this permission notice appear in all copies.
00011
00012 THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00013 REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00014 AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00015 INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00016 LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00017 OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00018 PERFORMANCE OF THIS SOFTWARE.
00019
00020 Portions copyright (C) 2007--2008 American Registry for Internet Numbers ("ARIN")
00021
00022 Permission to use, copy, modify, and distribute this software for any
00023 purpose with or without fee is hereby granted, provided that the above
00024 copyright notice and this permission notice appear in all copies.
00025
00026 THE SOFTWARE IS PROVIDED "AS IS" AND ARIN DISCLAIMS ALL WARRANTIES WITH
00027 REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00028 AND FITNESS. IN NO EVENT SHALL ARIN BE LIABLE FOR ANY SPECIAL, DIRECT,
00029 INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00030 LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00031 OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00032 PERFORMANCE OF THIS SOFTWARE.
00033 """
00034
00035 import syslog, traceback, sys, os, time
00036
00037
00038
00039
00040 enable_trace = False
00041
00042
00043
00044
00045 use_syslog = False
00046
00047 tag = ""
00048 pid = 0
00049
00050 def init(ident = "rpki", flags = syslog.LOG_PID | syslog.LOG_PERROR, facility = syslog.LOG_DAEMON):
00051 """
00052 Initialize logging system.
00053 """
00054
00055 if use_syslog:
00056 return syslog.openlog(ident, flags, facility)
00057 else:
00058 global tag, pid
00059 tag = ident
00060 pid = os.getpid()
00061
00062 def set_trace(enable):
00063 """
00064 Enable or disable call tracing.
00065 """
00066
00067 global enable_trace
00068 enable_trace = enable
00069
00070 class logger(object):
00071 """
00072 Closure for logging.
00073 """
00074
00075 def __init__(self, priority):
00076 self.priority = priority
00077
00078 def __call__(self, message):
00079 if use_syslog:
00080 return syslog.syslog(self.priority, message)
00081 else:
00082 sys.stderr.write("%s %s[%d]: %s\n" % (time.strftime("%T"), tag, pid, message))
00083
00084 error = logger(syslog.LOG_ERR)
00085 warn = logger(syslog.LOG_WARNING)
00086 note = logger(syslog.LOG_NOTICE)
00087 info = logger(syslog.LOG_INFO)
00088 debug = logger(syslog.LOG_DEBUG)
00089
00090 def trace():
00091 """
00092 Execution trace -- where are we now, and whence came we here?
00093 """
00094
00095 if enable_trace:
00096 bt = traceback.extract_stack(limit = 3)
00097 return debug("[%s() at %s:%d from %s:%d]" % (bt[1][2], bt[1][0], bt[1][1], bt[0][0], bt[0][1]))