RPKI Engine
1.0
|
00001 """ 00002 Logging facilities for RPKI libraries. 00003 00004 $Id: log.py 4027 2011-10-07 23:38:53Z sra $ 00005 00006 Copyright (C) 2009--2011 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 ## @var enable_tracebacks 00054 # Whether tracebacks are enabled globally. Individual classes and 00055 # modules may choose to override this. 00056 00057 enable_tracebacks = False 00058 00059 tag = "" 00060 pid = 0 00061 00062 def init(ident = "rpki", flags = syslog.LOG_PID, facility = syslog.LOG_DAEMON): 00063 """ 00064 Initialize logging system. 00065 """ 00066 00067 if use_syslog: 00068 return syslog.openlog(ident, flags, facility) 00069 else: 00070 global tag, pid 00071 tag = ident 00072 pid = os.getpid() 00073 00074 def set_trace(enable): 00075 """ 00076 Enable or disable call tracing. 00077 """ 00078 00079 global enable_trace 00080 enable_trace = enable 00081 00082 class logger(object): 00083 """ 00084 Closure for logging. 00085 """ 00086 00087 def __init__(self, priority): 00088 self.priority = priority 00089 00090 def __call__(self, message): 00091 if use_syslog: 00092 return syslog.syslog(self.priority, message) 00093 else: 00094 sys.stderr.write("%s %s[%d]: %s\n" % (time.strftime("%F %T"), tag, pid, message)) 00095 00096 error = logger(syslog.LOG_ERR) 00097 warn = logger(syslog.LOG_WARNING) 00098 note = logger(syslog.LOG_NOTICE) 00099 info = logger(syslog.LOG_INFO) 00100 debug = logger(syslog.LOG_DEBUG) 00101 00102 def trace(): 00103 """ 00104 Execution trace -- where are we now, and whence came we here? 00105 """ 00106 00107 if enable_trace: 00108 bt = tb.extract_stack(limit = 3) 00109 return debug("[%s() at %s:%d from %s:%d]" % (bt[1][2], bt[1][0], bt[1][1], bt[0][0], bt[0][1])) 00110 00111 def traceback(do_it = None): 00112 """ 00113 Consolidated backtrace facility with a bit of extra info. Argument 00114 specifies whether or not to log the traceback (some modules and 00115 classes have their own controls for this, this lets us provide a 00116 unified interface). If no argument is specified, we use the global 00117 default value rpki.log.enable_tracebacks. 00118 """ 00119 00120 if do_it is None: 00121 do_it = enable_tracebacks 00122 00123 if do_it: 00124 assert sys.exc_info() != (None, None, None), "rpki.log.traceback() called without valid trace on stack, this is a programming error" 00125 bt = tb.extract_stack(limit = 3) 00126 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])) 00127 bt = tb.format_exc() 00128 assert bt is not None, "Apparently I'm still not using the right test for null backtrace" 00129 for line in bt.splitlines(): 00130 warn(line) 00131 00132 def log_repr(obj, *tokens): 00133 """ 00134 Constructor for __repr__() strings, handles suppression of Python 00135 IDs as needed, includes self_handle when available. 00136 """ 00137 00138 words = ["%s.%s" % (obj.__class__.__module__, obj.__class__.__name__)] 00139 try: 00140 words.append("{%s}" % obj.self.self_handle) 00141 except: 00142 pass 00143 words.extend(str(token) for token in tokens if token is not None and token != "") 00144 if show_python_ids: 00145 words.append(" at %#x" % id(obj)) 00146 return "<" + " ".join(words) + ">"