Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
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.

debug.py 3.5KB

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