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.
 
 
 

91 lines
2.1 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. class ILogger:
  10. '''Logger interface, by default this class
  11. will be used and logging calls are no-ops.
  12. '''
  13. level = 0
  14. def __init__(self, msg):
  15. return
  16. def warning(self, *args):
  17. return
  18. def debug(self, *args):
  19. return
  20. def error(self, *args):
  21. return
  22. def setLevel(cls, level):
  23. cls.level = level
  24. setLevel = classmethod(setLevel)
  25. _LoggerClass = ILogger
  26. class BasicLogger(ILogger):
  27. def __init__(self, msg, out=sys.stdout):
  28. self.msg, self.out = msg, out
  29. def warning(self, msg, *args):
  30. if self.level < 1: return
  31. print >>self, self.WARN, self.msg,
  32. print >>self, msg %args
  33. WARN = 'WARN'
  34. def debug(self, msg, *args):
  35. if self.level < 2: return
  36. print >>self, self.DEBUG, self.msg,
  37. print >>self, msg %args
  38. DEBUG = 'DEBUG'
  39. def error(self, msg, *args):
  40. print >>self, self.ERROR, self.msg,
  41. print >>self, msg %args
  42. ERROR = 'ERROR'
  43. def write(self, *args):
  44. '''Write convenience function; writes strings.
  45. '''
  46. for s in args: self.out.write(s)
  47. def setBasicLogger():
  48. '''Use Basic Logger.
  49. '''
  50. setLoggerClass(BasicLogger)
  51. BasicLogger.setLevel(0)
  52. def setBasicLoggerWARN():
  53. '''Use Basic Logger.
  54. '''
  55. setLoggerClass(BasicLogger)
  56. BasicLogger.setLevel(1)
  57. def setBasicLoggerDEBUG():
  58. '''Use Basic Logger.
  59. '''
  60. setLoggerClass(BasicLogger)
  61. BasicLogger.setLevel(2)
  62. def setLoggerClass(loggingClass):
  63. '''Set Logging Class.
  64. '''
  65. assert issubclass(loggingClass, ILogger), 'loggingClass must subclass ILogger'
  66. global _LoggerClass
  67. _LoggerClass = loggingClass
  68. def setLevel(level=0):
  69. '''Set Global Logging Level.
  70. '''
  71. ILogger.level = level
  72. def getLogger(msg):
  73. '''Return instance of Logging class.
  74. '''
  75. return _LoggerClass(msg)