0001 """A simple log mechanism styled after PEP 282.""" 0002 0003 # This module should be kept compatible with Python 1.5.2. 0004 0005 # The class here is styled after PEP 282 so that it could later be 0006 # replaced with a standard Python logging implementation. 0007 0008 DEBUG = 1 0009 INFO = 2 0010 WARN = 3 0011 ERROR = 4 0012 FATAL = 5 0013 0014 import sys 0015 0016 class Log: 0017 0018 def __init__(self, threshold=WARN): 0019 self.threshold = threshold 0020 0021 def _log(self, level, msg, args): 0022 if level >= self.threshold: 0023 print msg % args 0024 sys.stdout.flush() 0025 0026 def log(self, level, msg, *args): 0027 self._log(level, msg, args) 0028 0029 def debug(self, msg, *args): 0030 self._log(DEBUG, msg, args) 0031 0032 def info(self, msg, *args): 0033 self._log(INFO, msg, args) 0034 0035 def warn(self, msg, *args): 0036 self._log(WARN, msg, args) 0037 0038 def error(self, msg, *args): 0039 self._log(ERROR, msg, args) 0040 0041 def fatal(self, msg, *args): 0042 self._log(FATAL, msg, args) 0043 0044 _global_log = Log() 0045 log = _global_log.log 0046 debug = _global_log.debug 0047 info = _global_log.info 0048 warn = _global_log.warn 0049 error = _global_log.error 0050 fatal = _global_log.fatal 0051 0052 def set_threshold(level): 0053 # return the old threshold for use from tests 0054 old = _global_log.threshold 0055 _global_log.threshold = level 0056 return old 0057 0058 def set_verbosity(v): 0059 if v <= 0: 0060 set_threshold(WARN) 0061 elif v == 1: 0062 set_threshold(INFO) 0063 elif v >= 2: 0064 set_threshold(DEBUG) 0065
Generated by PyXR 0.9.4