Mercurial > cpython-lru_cache-weakref
annotate Lib/distutils/log.py @ 58925:eecf1bef052d 2.6
Merged revisions 78207 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line
#7930: fix stripid
........
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Tue, 16 Feb 2010 23:29:44 +0000 |
| parents | 8d0579d2b662 |
| children | a5229237eb50 4cdf21a0dbbc |
| rev | line source |
|---|---|
|
23667
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
1 """A simple log mechanism styled after PEP 282.""" |
|
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
2 |
|
33965
893d46576737
Update compatibility comments to 2.1, corresponding to PEP 291 1.13.
Martin v. Löwis <martin@v.loewis.de>
parents:
32943
diff
changeset
|
3 # This module should be kept compatible with Python 2.1. |
|
26285
42bfc98d295b
Add comment to Distutil files about requiring 1.5.2 compatibility, as
Andrew M. Kuchling <amk@amk.ca>
parents:
26014
diff
changeset
|
4 |
|
23667
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
5 # The class here is styled after PEP 282 so that it could later be |
|
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
6 # replaced with a standard Python logging implementation. |
|
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
7 |
|
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
8 DEBUG = 1 |
|
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
9 INFO = 2 |
|
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
10 WARN = 3 |
|
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
11 ERROR = 4 |
|
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
12 FATAL = 5 |
|
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
13 |
|
26014
d424558d24f9
[Bug #620630] Flush stdout after logging every message. Without it,
Andrew M. Kuchling <amk@amk.ca>
parents:
23667
diff
changeset
|
14 import sys |
|
d424558d24f9
[Bug #620630] Flush stdout after logging every message. Without it,
Andrew M. Kuchling <amk@amk.ca>
parents:
23667
diff
changeset
|
15 |
|
23667
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
16 class Log: |
|
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
17 |
|
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
18 def __init__(self, threshold=WARN): |
|
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
19 self.threshold = threshold |
|
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
20 |
|
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
21 def _log(self, level, msg, args): |
|
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
22 if level >= self.threshold: |
|
37377
8d0579d2b662
Bug #1458017: make distutils.Log._log more forgiving when passing in
Georg Brandl <georg@python.org>
parents:
33965
diff
changeset
|
23 if not args: |
|
8d0579d2b662
Bug #1458017: make distutils.Log._log more forgiving when passing in
Georg Brandl <georg@python.org>
parents:
33965
diff
changeset
|
24 # msg may contain a '%'. If args is empty, |
|
8d0579d2b662
Bug #1458017: make distutils.Log._log more forgiving when passing in
Georg Brandl <georg@python.org>
parents:
33965
diff
changeset
|
25 # don't even try to string-format |
|
8d0579d2b662
Bug #1458017: make distutils.Log._log more forgiving when passing in
Georg Brandl <georg@python.org>
parents:
33965
diff
changeset
|
26 print msg |
|
8d0579d2b662
Bug #1458017: make distutils.Log._log more forgiving when passing in
Georg Brandl <georg@python.org>
parents:
33965
diff
changeset
|
27 else: |
|
8d0579d2b662
Bug #1458017: make distutils.Log._log more forgiving when passing in
Georg Brandl <georg@python.org>
parents:
33965
diff
changeset
|
28 print msg % args |
|
26014
d424558d24f9
[Bug #620630] Flush stdout after logging every message. Without it,
Andrew M. Kuchling <amk@amk.ca>
parents:
23667
diff
changeset
|
29 sys.stdout.flush() |
|
23667
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
30 |
|
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
31 def log(self, level, msg, *args): |
|
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
32 self._log(level, msg, args) |
|
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
33 |
|
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
34 def debug(self, msg, *args): |
|
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
35 self._log(DEBUG, msg, args) |
|
32736
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
27987
diff
changeset
|
36 |
|
23667
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
37 def info(self, msg, *args): |
|
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
38 self._log(INFO, msg, args) |
|
32736
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
27987
diff
changeset
|
39 |
|
23667
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
40 def warn(self, msg, *args): |
|
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
41 self._log(WARN, msg, args) |
|
32736
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
27987
diff
changeset
|
42 |
|
23667
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
43 def error(self, msg, *args): |
|
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
44 self._log(ERROR, msg, args) |
|
32736
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
27987
diff
changeset
|
45 |
|
23667
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
46 def fatal(self, msg, *args): |
|
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
47 self._log(FATAL, msg, args) |
|
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
48 |
|
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
49 _global_log = Log() |
|
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
50 log = _global_log.log |
|
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
51 debug = _global_log.debug |
|
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
52 info = _global_log.info |
|
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
53 warn = _global_log.warn |
|
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
54 error = _global_log.error |
|
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
55 fatal = _global_log.fatal |
|
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
56 |
|
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
57 def set_threshold(level): |
|
32943
9fb5138f58dd
make sure distutils logging is shut off in tests to avoid spurious output
Fred Drake <fdrake@acm.org>
parents:
32736
diff
changeset
|
58 # return the old threshold for use from tests |
|
9fb5138f58dd
make sure distutils logging is shut off in tests to avoid spurious output
Fred Drake <fdrake@acm.org>
parents:
32736
diff
changeset
|
59 old = _global_log.threshold |
|
23667
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
60 _global_log.threshold = level |
|
32943
9fb5138f58dd
make sure distutils logging is shut off in tests to avoid spurious output
Fred Drake <fdrake@acm.org>
parents:
32736
diff
changeset
|
61 return old |
|
23667
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
62 |
|
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
63 def set_verbosity(v): |
|
27987
2c751936f697
set_verbosity(): do something reasonable for out-of-range verbosity
Guido van Rossum <guido@python.org>
parents:
26285
diff
changeset
|
64 if v <= 0: |
|
23667
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
65 set_threshold(WARN) |
|
27987
2c751936f697
set_verbosity(): do something reasonable for out-of-range verbosity
Guido van Rossum <guido@python.org>
parents:
26285
diff
changeset
|
66 elif v == 1: |
|
23667
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
67 set_threshold(INFO) |
|
27987
2c751936f697
set_verbosity(): do something reasonable for out-of-range verbosity
Guido van Rossum <guido@python.org>
parents:
26285
diff
changeset
|
68 elif v >= 2: |
|
23667
dc7d5c2d5845
A simple log mechanism styled after the proposed std library module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
69 set_threshold(DEBUG) |
