/Lib/distutils/log.py

http://unladen-swallow.googlecode.com/ · Python · 69 lines · 46 code · 16 blank · 7 comment · 6 complexity · 7df4f56bfffda5af33090e250086671f MD5 · raw file

  1. """A simple log mechanism styled after PEP 282."""
  2. # This module should be kept compatible with Python 2.1.
  3. # The class here is styled after PEP 282 so that it could later be
  4. # replaced with a standard Python logging implementation.
  5. DEBUG = 1
  6. INFO = 2
  7. WARN = 3
  8. ERROR = 4
  9. FATAL = 5
  10. import sys
  11. class Log:
  12. def __init__(self, threshold=WARN):
  13. self.threshold = threshold
  14. def _log(self, level, msg, args):
  15. if level >= self.threshold:
  16. if not args:
  17. # msg may contain a '%'. If args is empty,
  18. # don't even try to string-format
  19. print msg
  20. else:
  21. print msg % args
  22. sys.stdout.flush()
  23. def log(self, level, msg, *args):
  24. self._log(level, msg, args)
  25. def debug(self, msg, *args):
  26. self._log(DEBUG, msg, args)
  27. def info(self, msg, *args):
  28. self._log(INFO, msg, args)
  29. def warn(self, msg, *args):
  30. self._log(WARN, msg, args)
  31. def error(self, msg, *args):
  32. self._log(ERROR, msg, args)
  33. def fatal(self, msg, *args):
  34. self._log(FATAL, msg, args)
  35. _global_log = Log()
  36. log = _global_log.log
  37. debug = _global_log.debug
  38. info = _global_log.info
  39. warn = _global_log.warn
  40. error = _global_log.error
  41. fatal = _global_log.fatal
  42. def set_threshold(level):
  43. # return the old threshold for use from tests
  44. old = _global_log.threshold
  45. _global_log.threshold = level
  46. return old
  47. def set_verbosity(v):
  48. if v <= 0:
  49. set_threshold(WARN)
  50. elif v == 1:
  51. set_threshold(INFO)
  52. elif v >= 2:
  53. set_threshold(DEBUG)