14adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao"""A simple log mechanism styled after PEP 282."""
24adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
34adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao# The class here is styled after PEP 282 so that it could later be
44adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao# replaced with a standard Python logging implementation.
54adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
64adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoDEBUG = 1
74adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoINFO = 2
84adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoWARN = 3
94adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoERROR = 4
104adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoFATAL = 5
114adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
124adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoimport sys
134adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
144adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoclass Log:
154adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
164adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def __init__(self, threshold=WARN):
174adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.threshold = threshold
184adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
194adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def _log(self, level, msg, args):
204adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        if level not in (DEBUG, INFO, WARN, ERROR, FATAL):
214adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            raise ValueError('%s wrong log level' % str(level))
224adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
234adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        if level >= self.threshold:
244adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            if args:
254adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                msg = msg % args
264adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            if level in (WARN, ERROR, FATAL):
274adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                stream = sys.stderr
284adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            else:
294adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                stream = sys.stdout
304adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            stream.write('%s\n' % msg)
314adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            stream.flush()
324adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
334adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def log(self, level, msg, *args):
344adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self._log(level, msg, args)
354adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
364adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def debug(self, msg, *args):
374adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self._log(DEBUG, msg, args)
384adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
394adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def info(self, msg, *args):
404adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self._log(INFO, msg, args)
414adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
424adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def warn(self, msg, *args):
434adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self._log(WARN, msg, args)
444adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
454adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def error(self, msg, *args):
464adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self._log(ERROR, msg, args)
474adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
484adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def fatal(self, msg, *args):
494adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self._log(FATAL, msg, args)
504adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
514adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao_global_log = Log()
524adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaolog = _global_log.log
534adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaodebug = _global_log.debug
544adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoinfo = _global_log.info
554adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaowarn = _global_log.warn
564adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoerror = _global_log.error
574adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaofatal = _global_log.fatal
584adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
594adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaodef set_threshold(level):
604adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    # return the old threshold for use from tests
614adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    old = _global_log.threshold
624adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    _global_log.threshold = level
634adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    return old
644adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
654adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaodef set_verbosity(v):
664adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    if v <= 0:
674adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        set_threshold(WARN)
684adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    elif v == 1:
694adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        set_threshold(INFO)
704adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    elif v >= 2:
714adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        set_threshold(DEBUG)
72