Require implementations for warnings.showwarning() support the 'line' argument.
[python.git] / Lib / distutils / log.py
blobfcaa545a79992f22ec3b2f5354aeb5b8f4e77e44
1 """A simple log mechanism styled after PEP 282."""
3 # The class here is styled after PEP 282 so that it could later be
4 # replaced with a standard Python logging implementation.
6 DEBUG = 1
7 INFO = 2
8 WARN = 3
9 ERROR = 4
10 FATAL = 5
12 import sys
14 class Log:
16 def __init__(self, threshold=WARN):
17 self.threshold = threshold
19 def _log(self, level, msg, args):
20 if level >= self.threshold:
21 if not args:
22 # msg may contain a '%'. If args is empty,
23 # don't even try to string-format
24 print msg
25 else:
26 print msg % args
27 sys.stdout.flush()
29 def log(self, level, msg, *args):
30 self._log(level, msg, args)
32 def debug(self, msg, *args):
33 self._log(DEBUG, msg, args)
35 def info(self, msg, *args):
36 self._log(INFO, msg, args)
38 def warn(self, msg, *args):
39 self._log(WARN, msg, args)
41 def error(self, msg, *args):
42 self._log(ERROR, msg, args)
44 def fatal(self, msg, *args):
45 self._log(FATAL, msg, args)
47 _global_log = Log()
48 log = _global_log.log
49 debug = _global_log.debug
50 info = _global_log.info
51 warn = _global_log.warn
52 error = _global_log.error
53 fatal = _global_log.fatal
55 def set_threshold(level):
56 # return the old threshold for use from tests
57 old = _global_log.threshold
58 _global_log.threshold = level
59 return old
61 def set_verbosity(v):
62 if v <= 0:
63 set_threshold(WARN)
64 elif v == 1:
65 set_threshold(INFO)
66 elif v >= 2:
67 set_threshold(DEBUG)