Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/websocket/_logging.py: 53%

38 statements  

« prev     ^ index     » next       coverage.py v7.3.1, created at 2023-09-25 06:34 +0000

1import logging 

2 

3""" 

4_logging.py 

5websocket - WebSocket client library for Python 

6 

7Copyright 2023 engn33r 

8 

9Licensed under the Apache License, Version 2.0 (the "License"); 

10you may not use this file except in compliance with the License. 

11You may obtain a copy of the License at 

12 

13 http://www.apache.org/licenses/LICENSE-2.0 

14 

15Unless required by applicable law or agreed to in writing, software 

16distributed under the License is distributed on an "AS IS" BASIS, 

17WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

18See the License for the specific language governing permissions and 

19limitations under the License. 

20""" 

21 

22_logger = logging.getLogger('websocket') 

23try: 

24 from logging import NullHandler 

25except ImportError: 

26 class NullHandler(logging.Handler): 

27 def emit(self, record) -> None: 

28 pass 

29 

30_logger.addHandler(NullHandler()) 

31 

32_traceEnabled = False 

33 

34__all__ = ["enableTrace", "dump", "error", "warning", "debug", "trace", 

35 "isEnabledForError", "isEnabledForDebug", "isEnabledForTrace"] 

36 

37 

38def enableTrace(traceable: bool, 

39 handler: logging.StreamHandler = logging.StreamHandler(), 

40 level: str = "DEBUG") -> None: 

41 """ 

42 Turn on/off the traceability. 

43 

44 Parameters 

45 ---------- 

46 traceable: bool 

47 If set to True, traceability is enabled. 

48 """ 

49 global _traceEnabled 

50 _traceEnabled = traceable 

51 if traceable: 

52 _logger.addHandler(handler) 

53 _logger.setLevel(getattr(logging, level)) 

54 

55 

56def dump(title: str, message: str) -> None: 

57 if _traceEnabled: 

58 _logger.debug("--- " + title + " ---") 

59 _logger.debug(message) 

60 _logger.debug("-----------------------") 

61 

62 

63def error(msg: str) -> None: 

64 _logger.error(msg) 

65 

66 

67def warning(msg: str) -> None: 

68 _logger.warning(msg) 

69 

70 

71def debug(msg: str) -> None: 

72 _logger.debug(msg) 

73 

74 

75def info(msg: str) -> None: 

76 _logger.info(msg) 

77 

78 

79def trace(msg: str) -> None: 

80 if _traceEnabled: 

81 _logger.debug(msg) 

82 

83 

84def isEnabledForError() -> bool: 

85 return _logger.isEnabledFor(logging.ERROR) 

86 

87 

88def isEnabledForDebug() -> bool: 

89 return _logger.isEnabledFor(logging.DEBUG) 

90 

91 

92def isEnabledForTrace() -> bool: 

93 return _traceEnabled