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