|
| 1 | +"""A simple log mechanism styled after PEP 282.""" |
| 2 | + |
| 3 | +# The class here is styled after PEP 282 so that it could later be |
| 4 | +# replaced with a standard Python logging implementation. |
| 5 | + |
| 6 | +DEBUG = 1 |
| 7 | +INFO = 2 |
| 8 | +WARN = 3 |
| 9 | +ERROR = 4 |
| 10 | +FATAL = 5 |
| 11 | + |
| 12 | +class Log: |
| 13 | + |
| 14 | + def __init__(self, threshold=WARN): |
| 15 | + self.threshold = threshold |
| 16 | + |
| 17 | + def _log(self, level, msg, args): |
| 18 | + if level >= self.threshold: |
| 19 | + print msg % args |
| 20 | + |
| 21 | + def log(self, level, msg, *args): |
| 22 | + self._log(level, msg, args) |
| 23 | + |
| 24 | + def debug(self, msg, *args): |
| 25 | + self._log(DEBUG, msg, args) |
| 26 | + |
| 27 | + def info(self, msg, *args): |
| 28 | + self._log(INFO, msg, args) |
| 29 | + |
| 30 | + def warn(self, msg, *args): |
| 31 | + self._log(WARN, msg, args) |
| 32 | + |
| 33 | + def error(self, msg, *args): |
| 34 | + self._log(ERROR, msg, args) |
| 35 | + |
| 36 | + def fatal(self, msg, *args): |
| 37 | + self._log(FATAL, msg, args) |
| 38 | + |
| 39 | +_global_log = Log() |
| 40 | +log = _global_log.log |
| 41 | +debug = _global_log.debug |
| 42 | +info = _global_log.info |
| 43 | +warn = _global_log.warn |
| 44 | +error = _global_log.error |
| 45 | +fatal = _global_log.fatal |
| 46 | + |
| 47 | +def set_threshold(level): |
| 48 | + _global_log.threshold = level |
| 49 | + |
| 50 | +def set_verbosity(v): |
| 51 | + if v == 0: |
| 52 | + set_threshold(WARN) |
| 53 | + if v == 1: |
| 54 | + set_threshold(INFO) |
| 55 | + if v == 2: |
| 56 | + set_threshold(DEBUG) |
0 commit comments