00001 """
00002 Logging facilities for RPKI libraries.
00003
00004 $Id: log.py 2933 2010-01-07 01:00:17Z 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, sys, os, time
00036 import traceback as tb
00037
00038
00039
00040
00041 enable_trace = False
00042
00043
00044
00045
00046 use_syslog = True
00047
00048 tag = ""
00049 pid = 0
00050
00051 def init(ident = "rpki", flags = syslog.LOG_PID, facility = syslog.LOG_DAEMON):
00052 """
00053 Initialize logging system.
00054 """
00055
00056 if use_syslog:
00057 return syslog.openlog(ident, flags, facility)
00058 else:
00059 global tag, pid
00060 tag = ident
00061 pid = os.getpid()
00062
00063 def set_trace(enable):
00064 """
00065 Enable or disable call tracing.
00066 """
00067
00068 global enable_trace
00069 enable_trace = enable
00070
00071 class logger(object):
00072 """
00073 Closure for logging.
00074 """
00075
00076 def __init__(self, priority):
00077 self.priority = priority
00078
00079 def __call__(self, message):
00080 if use_syslog:
00081 return syslog.syslog(self.priority, message)
00082 else:
00083 sys.stderr.write("%s %s[%d]: %s\n" % (time.strftime("%F %T"), tag, pid, message))
00084
00085 error = logger(syslog.LOG_ERR)
00086 warn = logger(syslog.LOG_WARNING)
00087 note = logger(syslog.LOG_NOTICE)
00088 info = logger(syslog.LOG_INFO)
00089 debug = logger(syslog.LOG_DEBUG)
00090
00091 def trace():
00092 """
00093 Execution trace -- where are we now, and whence came we here?
00094 """
00095
00096 if enable_trace:
00097 bt = tb.extract_stack(limit = 3)
00098 return debug("[%s() at %s:%d from %s:%d]" % (bt[1][2], bt[1][0], bt[1][1], bt[0][0], bt[0][1]))
00099
00100 def traceback():
00101 """
00102 Consolidated backtrace facility with a bit of extra info.
00103 """
00104
00105 assert sys.exc_info() != (None, None, None), "rpki.log.traceback() called without valid trace on stack, this is a programming error"
00106 bt = tb.extract_stack(limit = 3)
00107 error("Exception caught in %s() at %s:%d called from %s:%d" % (bt[1][2], bt[1][0], bt[1][1], bt[0][0], bt[0][1]))
00108 bt = tb.format_exc()
00109 assert bt is not None, "Apparently I'm still not using the right test for null backtrace"
00110 for line in bt.splitlines():
00111 error(line)