-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace bespoke logging facility with logging module, available since…
… Python 2.3.
- Loading branch information
Showing
15 changed files
with
124 additions
and
242 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,37 @@ | ||
"""A simple log mechanism styled after PEP 282.""" | ||
""" | ||
A simple log mechanism styled after PEP 282. | ||
# The class here is styled after PEP 282 so that it could later be | ||
# replaced with a standard Python logging implementation. | ||
Retained for compatibility and should not be used. | ||
""" | ||
|
||
import sys | ||
import logging | ||
|
||
DEBUG = 1 | ||
INFO = 2 | ||
WARN = 3 | ||
ERROR = 4 | ||
FATAL = 5 | ||
|
||
DEBUG = logging.DEBUG | ||
INFO = logging.INFO | ||
WARN = logging.WARN | ||
ERROR = logging.ERROR | ||
FATAL = logging.FATAL | ||
|
||
class Log: | ||
def __init__(self, threshold=WARN): | ||
self.threshold = threshold | ||
|
||
def _log(self, level, msg, args): | ||
if level not in (DEBUG, INFO, WARN, ERROR, FATAL): | ||
raise ValueError('%s wrong log level' % str(level)) | ||
|
||
if level >= self.threshold: | ||
if args: | ||
msg = msg % args | ||
if level in (WARN, ERROR, FATAL): | ||
stream = sys.stderr | ||
else: | ||
stream = sys.stdout | ||
try: | ||
stream.write('%s\n' % msg) | ||
except UnicodeEncodeError: | ||
# emulate backslashreplace error handler | ||
encoding = stream.encoding | ||
msg = msg.encode(encoding, "backslashreplace").decode(encoding) | ||
stream.write('%s\n' % msg) | ||
stream.flush() | ||
|
||
def log(self, level, msg, *args): | ||
self._log(level, msg, args) | ||
|
||
def debug(self, msg, *args): | ||
self._log(DEBUG, msg, args) | ||
|
||
def info(self, msg, *args): | ||
self._log(INFO, msg, args) | ||
|
||
def warn(self, msg, *args): | ||
self._log(WARN, msg, args) | ||
|
||
def error(self, msg, *args): | ||
self._log(ERROR, msg, args) | ||
|
||
def fatal(self, msg, *args): | ||
self._log(FATAL, msg, args) | ||
|
||
|
||
_global_log = Log() | ||
_global_log = logging.getLogger('distutils') | ||
log = _global_log.log | ||
debug = _global_log.debug | ||
info = _global_log.info | ||
warn = _global_log.warn | ||
warn = _global_log.warning | ||
error = _global_log.error | ||
fatal = _global_log.fatal | ||
|
||
|
||
def set_threshold(level): | ||
# return the old threshold for use from tests | ||
old = _global_log.threshold | ||
_global_log.threshold = level | ||
return old | ||
orig = _global_log.level | ||
_global_log.setLevel(level) | ||
return orig | ||
|
||
|
||
def set_verbosity(v): | ||
if v <= 0: | ||
set_threshold(WARN) | ||
set_threshold(logging.WARN) | ||
elif v == 1: | ||
set_threshold(INFO) | ||
set_threshold(logging.INFO) | ||
elif v >= 2: | ||
set_threshold(DEBUG) | ||
set_threshold(logging.DEBUG) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.