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.

_logging.py 2.2KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import logging
  2. """
  3. _logging.py
  4. websocket - WebSocket client library for Python
  5. Copyright 2023 engn33r
  6. Licensed under the Apache License, Version 2.0 (the "License");
  7. you may not use this file except in compliance with the License.
  8. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. """
  16. _logger = logging.getLogger('websocket')
  17. try:
  18. from logging import NullHandler
  19. except ImportError:
  20. class NullHandler(logging.Handler):
  21. def emit(self, record) -> None:
  22. pass
  23. _logger.addHandler(NullHandler())
  24. _traceEnabled = False
  25. __all__ = ["enableTrace", "dump", "error", "warning", "debug", "trace",
  26. "isEnabledForError", "isEnabledForDebug", "isEnabledForTrace"]
  27. def enableTrace(traceable: bool,
  28. handler: logging.StreamHandler = logging.StreamHandler(),
  29. level: str = "DEBUG") -> None:
  30. """
  31. Turn on/off the traceability.
  32. Parameters
  33. ----------
  34. traceable: bool
  35. If set to True, traceability is enabled.
  36. """
  37. global _traceEnabled
  38. _traceEnabled = traceable
  39. if traceable:
  40. _logger.addHandler(handler)
  41. _logger.setLevel(getattr(logging, level))
  42. def dump(title: str, message: str) -> None:
  43. if _traceEnabled:
  44. _logger.debug("--- " + title + " ---")
  45. _logger.debug(message)
  46. _logger.debug("-----------------------")
  47. def error(msg: str) -> None:
  48. _logger.error(msg)
  49. def warning(msg: str) -> None:
  50. _logger.warning(msg)
  51. def debug(msg: str) -> None:
  52. _logger.debug(msg)
  53. def info(msg: str) -> None:
  54. _logger.info(msg)
  55. def trace(msg: str) -> None:
  56. if _traceEnabled:
  57. _logger.debug(msg)
  58. def isEnabledForError() -> bool:
  59. return _logger.isEnabledFor(logging.ERROR)
  60. def isEnabledForDebug() -> bool:
  61. return _logger.isEnabledFor(logging.DEBUG)
  62. def isEnabledForTrace() -> bool:
  63. return _traceEnabled