You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

101 lines
2.3 KiB

  1. # Copyright (c) 2003, The Regents of the University of California,
  2. # through Lawrence Berkeley National Laboratory (subject to receipt of
  3. # any required approvals from the U.S. Dept. of Energy). All rights
  4. # reserved.
  5. #
  6. """Logging"""
  7. ident = "$Id$"
  8. import sys
  9. WARN = 1
  10. DEBUG = 2
  11. class ILogger:
  12. '''Logger interface, by default this class
  13. will be used and logging calls are no-ops.
  14. '''
  15. level = 0
  16. def __init__(self, msg):
  17. return
  18. def warning(self, *args):
  19. return
  20. def debug(self, *args):
  21. return
  22. def error(self, *args):
  23. return
  24. def setLevel(cls, level):
  25. cls.level = level
  26. setLevel = classmethod(setLevel)
  27. debugOn = lambda self: self.level >= DEBUG
  28. warnOn = lambda self: self.level >= WARN
  29. _LoggerClass = ILogger
  30. class BasicLogger(ILogger):
  31. def __init__(self, msg, out=sys.stdout):
  32. self.msg, self.out = msg, out
  33. def warning(self, msg, *args):
  34. if self.level < 1: return
  35. print >>self, BasicLogger.WARN, self.msg,
  36. print >>self, msg %args
  37. WARN = 'WARN'
  38. def debug(self, msg, *args):
  39. if self.level < 2: return
  40. print >>self, BasicLogger.DEBUG, self.msg,
  41. print >>self, msg %args
  42. DEBUG = 'DEBUG'
  43. def error(self, msg, *args):
  44. print >>self, BasicLogger.ERROR, self.msg,
  45. print >>self, msg %args
  46. ERROR = 'ERROR'
  47. def write(self, *args):
  48. '''Write convenience function; writes strings.
  49. '''
  50. for s in args: self.out.write(s)
  51. def setBasicLogger():
  52. '''Use Basic Logger.
  53. '''
  54. setLoggerClass(BasicLogger)
  55. BasicLogger.setLevel(0)
  56. def setBasicLoggerWARN():
  57. '''Use Basic Logger.
  58. '''
  59. setLoggerClass(BasicLogger)
  60. BasicLogger.setLevel(WARN)
  61. def setBasicLoggerDEBUG():
  62. '''Use Basic Logger.
  63. '''
  64. setLoggerClass(BasicLogger)
  65. BasicLogger.setLevel(DEBUG)
  66. def setLoggerClass(loggingClass):
  67. '''Set Logging Class.
  68. '''
  69. assert issubclass(loggingClass, ILogger), 'loggingClass must subclass ILogger'
  70. global _LoggerClass
  71. _LoggerClass = loggingClass
  72. def setLevel(level=0):
  73. '''Set Global Logging Level.
  74. '''
  75. ILogger.level = level
  76. def getLevel():
  77. return ILogger.level
  78. def getLogger(msg):
  79. '''Return instance of Logging class.
  80. '''
  81. return _LoggerClass(msg)