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.
 
 
 

111 lines
2.7 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. class BasicLogger(ILogger):
  30. last = ''
  31. def __init__(self, msg, out=sys.stdout):
  32. self.msg, self.out = msg, out
  33. def warning(self, msg, *args):
  34. if self.warnOn() is False: return
  35. if BasicLogger.last != self.msg:
  36. BasicLogger.last = self.msg
  37. print >>self, "---- ", self.msg, " ----"
  38. print >>self, " %s " %BasicLogger.WARN,
  39. print >>self, msg %args
  40. WARN = '[WARN]'
  41. def debug(self, msg, *args):
  42. if self.debugOn() is False: return
  43. if BasicLogger.last != self.msg:
  44. BasicLogger.last = self.msg
  45. print >>self, "---- ", self.msg, " ----"
  46. print >>self, " %s " %BasicLogger.DEBUG,
  47. print >>self, msg %args
  48. DEBUG = '[DEBUG]'
  49. def error(self, msg, *args):
  50. if BasicLogger.last != self.msg:
  51. BasicLogger.last = self.msg
  52. print >>self, "---- ", self.msg, " ----"
  53. print >>self, " %s " %BasicLogger.ERROR,
  54. print >>self, msg %args
  55. ERROR = '[ERROR]'
  56. def write(self, *args):
  57. '''Write convenience function; writes strings.
  58. '''
  59. for s in args: self.out.write(s)
  60. _LoggerClass = BasicLogger
  61. def setBasicLogger():
  62. '''Use Basic Logger.
  63. '''
  64. setLoggerClass(BasicLogger)
  65. BasicLogger.setLevel(0)
  66. def setBasicLoggerWARN():
  67. '''Use Basic Logger.
  68. '''
  69. setLoggerClass(BasicLogger)
  70. BasicLogger.setLevel(WARN)
  71. def setBasicLoggerDEBUG():
  72. '''Use Basic Logger.
  73. '''
  74. setLoggerClass(BasicLogger)
  75. BasicLogger.setLevel(DEBUG)
  76. def setLoggerClass(loggingClass):
  77. '''Set Logging Class.
  78. '''
  79. assert issubclass(loggingClass, ILogger), 'loggingClass must subclass ILogger'
  80. global _LoggerClass
  81. _LoggerClass = loggingClass
  82. def setLevel(level=0):
  83. '''Set Global Logging Level.
  84. '''
  85. ILogger.level = level
  86. def getLevel():
  87. return ILogger.level
  88. def getLogger(msg):
  89. '''Return instance of Logging class.
  90. '''
  91. return _LoggerClass(msg)