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.

145 lines
3.3 KiB

4 years ago
  1. #
  2. # This file is part of pyasn1 software.
  3. #
  4. # Copyright (c) 2005-2018, Ilya Etingof <etingof@gmail.com>
  5. # License: http://snmplabs.com/pyasn1/license.html
  6. #
  7. import logging
  8. from pyasn1 import __version__
  9. from pyasn1 import error
  10. from pyasn1.compat.octets import octs2ints
  11. __all__ = ['Debug', 'setLogger', 'hexdump']
  12. flagNone = 0x0000
  13. flagEncoder = 0x0001
  14. flagDecoder = 0x0002
  15. flagAll = 0xffff
  16. flagMap = {
  17. 'none': flagNone,
  18. 'encoder': flagEncoder,
  19. 'decoder': flagDecoder,
  20. 'all': flagAll
  21. }
  22. class Printer(object):
  23. # noinspection PyShadowingNames
  24. def __init__(self, logger=None, handler=None, formatter=None):
  25. if logger is None:
  26. logger = logging.getLogger('pyasn1')
  27. logger.setLevel(logging.DEBUG)
  28. if handler is None:
  29. handler = logging.StreamHandler()
  30. if formatter is None:
  31. formatter = logging.Formatter('%(asctime)s %(name)s: %(message)s')
  32. handler.setFormatter(formatter)
  33. handler.setLevel(logging.DEBUG)
  34. logger.addHandler(handler)
  35. self.__logger = logger
  36. def __call__(self, msg):
  37. self.__logger.debug(msg)
  38. def __str__(self):
  39. return '<python logging>'
  40. if hasattr(logging, 'NullHandler'):
  41. NullHandler = logging.NullHandler
  42. else:
  43. # Python 2.6 and older
  44. class NullHandler(logging.Handler):
  45. def emit(self, record):
  46. pass
  47. class Debug(object):
  48. defaultPrinter = Printer()
  49. def __init__(self, *flags, **options):
  50. self._flags = flagNone
  51. if 'loggerName' in options:
  52. # route our logs to parent logger
  53. self._printer = Printer(
  54. logger=logging.getLogger(options['loggerName']),
  55. handler=NullHandler()
  56. )
  57. elif 'printer' in options:
  58. self._printer = options.get('printer')
  59. else:
  60. self._printer = self.defaultPrinter
  61. self._printer('running pyasn1 %s, debug flags %s' % (__version__, ', '.join(flags)))
  62. for flag in flags:
  63. inverse = flag and flag[0] in ('!', '~')
  64. if inverse:
  65. flag = flag[1:]
  66. try:
  67. if inverse:
  68. self._flags &= ~flagMap[flag]
  69. else:
  70. self._flags |= flagMap[flag]
  71. except KeyError:
  72. raise error.PyAsn1Error('bad debug flag %s' % flag)
  73. self._printer("debug category '%s' %s" % (flag, inverse and 'disabled' or 'enabled'))
  74. def __str__(self):
  75. return 'logger %s, flags %x' % (self._printer, self._flags)
  76. def __call__(self, msg):
  77. self._printer(msg)
  78. def __and__(self, flag):
  79. return self._flags & flag
  80. def __rand__(self, flag):
  81. return flag & self._flags
  82. logger = 0
  83. def setLogger(userLogger):
  84. global logger
  85. if userLogger:
  86. logger = userLogger
  87. else:
  88. logger = 0
  89. def hexdump(octets):
  90. return ' '.join(
  91. ['%s%.2X' % (n % 16 == 0 and ('\n%.5d: ' % n) or '', x)
  92. for n, x in zip(range(len(octets)), octs2ints(octets))]
  93. )
  94. class Scope(object):
  95. def __init__(self):
  96. self._list = []
  97. def __str__(self): return '.'.join(self._list)
  98. def push(self, token):
  99. self._list.append(token)
  100. def pop(self):
  101. return self._list.pop()
  102. scope = Scope()