00001 """Logging facilities for RPKI libraries.
00002
00003 $Id: log.py 1873 2008-06-12 02:49:41Z sra $
00004
00005 Copyright (C) 2007--2008 American Registry for Internet Numbers ("ARIN")
00006
00007 Permission to use, copy, modify, and distribute this software for any
00008 purpose with or without fee is hereby granted, provided that the above
00009 copyright notice and this permission notice appear in all copies.
00010
00011 THE SOFTWARE IS PROVIDED "AS IS" AND ARIN DISCLAIMS ALL WARRANTIES WITH
00012 REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00013 AND FITNESS. IN NO EVENT SHALL ARIN BE LIABLE FOR ANY SPECIAL, DIRECT,
00014 INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00015 LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00016 OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00017 PERFORMANCE OF THIS SOFTWARE.
00018 """
00019
00020 import syslog, traceback
00021
00022
00023
00024
00025 enable_trace = False
00026
00027 def init(ident = "rpki", flags = syslog.LOG_PID | syslog.LOG_PERROR, facility = syslog.LOG_DAEMON):
00028 """Initialize logging system."""
00029
00030 return syslog.openlog(ident, flags, facility)
00031
00032 def set_trace(trace):
00033 """Enable or disable call tracing."""
00034
00035 global enable_trace
00036 enable_trace = trace
00037
00038 class logger(object):
00039 """Closure for logging."""
00040
00041 def __init__(self, priority):
00042 self.priority = priority
00043
00044 def __call__(self, message):
00045 return syslog.syslog(self.priority, message)
00046
00047 error = logger(syslog.LOG_ERR)
00048 warn = logger(syslog.LOG_WARNING)
00049 note = logger(syslog.LOG_NOTICE)
00050 info = logger(syslog.LOG_INFO)
00051 debug = logger(syslog.LOG_DEBUG)
00052
00053 def trace():
00054 """Execution trace -- where are we now, and whence came we here?"""
00055
00056 if enable_trace:
00057 bt = traceback.extract_stack(limit = 3)
00058 return debug("[%s() at %s:%d from %s:%d]" % (bt[1][2], bt[1][0], bt[1][1], bt[0][0], bt[0][1]))