RPKI Engine 1.0

log.py (3793)

Go to the documentation of this file.
00001 """
00002 Logging facilities for RPKI libraries.
00003 
00004 $Id: log.py 3793 2011-04-27 04:34:52Z sra $
00005 
00006 Copyright (C) 2009--2010  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 ## @var enable_trace
00039 # Whether call tracing is enabled.
00040 
00041 enable_trace = False
00042 
00043 ## @var use_syslog
00044 # Whether to use syslog
00045 
00046 use_syslog = True
00047 
00048 ## @var show_python_ids
00049 # Whether __repr__() methods should show Python id numbers
00050 
00051 show_python_ids = False
00052 
00053 tag = ""
00054 pid = 0
00055 
00056 def init(ident = "rpki", flags = syslog.LOG_PID, facility = syslog.LOG_DAEMON):
00057   """
00058   Initialize logging system.
00059   """
00060 
00061   if use_syslog:
00062     return syslog.openlog(ident, flags, facility)
00063   else:
00064     global tag, pid
00065     tag = ident
00066     pid = os.getpid()
00067 
00068 def set_trace(enable):
00069   """
00070   Enable or disable call tracing.
00071   """
00072 
00073   global enable_trace
00074   enable_trace = enable
00075 
00076 class logger(object):
00077   """
00078   Closure for logging.
00079   """
00080 
00081   def __init__(self, priority):
00082     self.priority = priority
00083 
00084   def __call__(self, message):
00085     if use_syslog:
00086       return syslog.syslog(self.priority, message)
00087     else:
00088       sys.stderr.write("%s %s[%d]: %s\n" % (time.strftime("%F %T"), tag, pid, message))
00089 
00090 error = logger(syslog.LOG_ERR)
00091 warn  = logger(syslog.LOG_WARNING)
00092 note  = logger(syslog.LOG_NOTICE)
00093 info  = logger(syslog.LOG_INFO)
00094 debug = logger(syslog.LOG_DEBUG)
00095 
00096 def trace():
00097   """
00098   Execution trace -- where are we now, and whence came we here?
00099   """
00100 
00101   if enable_trace:
00102     bt = tb.extract_stack(limit = 3)
00103     return debug("[%s() at %s:%d from %s:%d]" % (bt[1][2], bt[1][0], bt[1][1], bt[0][0], bt[0][1]))
00104 
00105 def traceback():
00106   """
00107   Consolidated backtrace facility with a bit of extra info.
00108   """
00109 
00110   assert sys.exc_info() != (None, None, None), "rpki.log.traceback() called without valid trace on stack, this is a programming error"
00111   bt = tb.extract_stack(limit = 3)
00112   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]))
00113   bt = tb.format_exc()
00114   assert bt is not None, "Apparently I'm still not using the right test for null backtrace"
00115   for line in bt.splitlines():
00116     warn(line)
00117 
00118 def log_repr(obj, *tokens):
00119   """
00120   Constructor for __repr__() strings, handles suppression of Python
00121   IDs as needed.
00122   """
00123 
00124   words = ["%s.%s" % (obj.__class__.__module__, obj.__class__.__name__)]
00125   words.extend(str(token) for token in tokens if token is not None and token != "")
00126   if show_python_ids:
00127     words.append(" at %#x" % id(obj))
00128   return "<" + " ".join(words) + ">"
 All Classes Namespaces Files Functions Variables