|
| 1 | +"""Pymodbus: Modbus Protocol Implementation. |
| 2 | +
|
| 3 | +Released under the the BSD license |
| 4 | +""" |
| 5 | + |
| 6 | +import logging |
| 7 | +from logging import NullHandler as __null |
| 8 | + |
| 9 | +from pymodbus.utilities import hexlify_packets |
| 10 | + |
| 11 | +# ---------------------------------------------------------------------------# |
| 12 | +# Block unhandled logging |
| 13 | +# ---------------------------------------------------------------------------# |
| 14 | +logging.getLogger(__name__).addHandler(__null()) |
| 15 | + |
| 16 | + |
| 17 | +class Log: |
| 18 | + """Class to hide logging complexity. |
| 19 | +
|
| 20 | + :meta private: |
| 21 | + """ |
| 22 | + |
| 23 | + CRITICAL = logging.CRITICAL |
| 24 | + ERROR = logging.ERROR |
| 25 | + WARNING = logging.WARNING |
| 26 | + DEBUG = logging.DEBUG |
| 27 | + INFO = logging.INFO |
| 28 | + |
| 29 | + LOG_LEVEL = logging.WARNING |
| 30 | + _logger = logging.getLogger(__name__) |
| 31 | + |
| 32 | + @classmethod |
| 33 | + def apply_logging_config(cls, level=logging.WARNING): |
| 34 | + """Apply basic logging configuration""" |
| 35 | + logging.basicConfig( |
| 36 | + format="%(asctime)s %(levelname)-5s %(module)s:%(lineno)s %(message)s", |
| 37 | + datefmt="%H:%M:%S", |
| 38 | + ) |
| 39 | + cls._logger.setLevel(level) |
| 40 | + cls.LOG_LEVEL = level |
| 41 | + |
| 42 | + @classmethod |
| 43 | + def build_msg(cls, txt, *args): |
| 44 | + """Build message.""" |
| 45 | + string_args = [] |
| 46 | + count_args = len(args) -1 |
| 47 | + skip = False |
| 48 | + for i in range(count_args + 1): |
| 49 | + if skip: |
| 50 | + skip = False |
| 51 | + continue |
| 52 | + if i < count_args and isinstance(args[i+1], str) and args[i+1][0] == ':': |
| 53 | + if args[i+1] == ":hex": |
| 54 | + string_args.append(hexlify_packets(args[i])) |
| 55 | + elif args[i+1] == ":str": |
| 56 | + string_args.append(str(args[i])) |
| 57 | + skip = True |
| 58 | + else: |
| 59 | + string_args.append(args[i]) |
| 60 | + return txt.format(*string_args) |
| 61 | + |
| 62 | + @classmethod |
| 63 | + def info(cls, txt, *args): |
| 64 | + """Log info messagees.""" |
| 65 | + if logging.INFO >= cls.LOG_LEVEL: |
| 66 | + cls._logger.info(cls.build_msg(txt, *args)) |
| 67 | + |
| 68 | + @classmethod |
| 69 | + def debug(cls, txt, **args): |
| 70 | + """Log debug messagees.""" |
| 71 | + if logging.DEBUG >= cls.LOG_LEVEL: |
| 72 | + cls._logger.debug(cls.build_msg(txt, *args)) |
| 73 | + |
| 74 | + @classmethod |
| 75 | + def warning(cls, txt, **args): |
| 76 | + """Log warning messagees.""" |
| 77 | + if logging.WARNING >= cls.LOG_LEVEL: |
| 78 | + cls._logger.warning(cls.build_msg(txt, *args)) |
| 79 | + |
| 80 | + @classmethod |
| 81 | + def error(cls, txt, **args): |
| 82 | + """Log error messagees.""" |
| 83 | + if logging.ERROR >= cls.LOG_LEVEL: |
| 84 | + cls._logger.error(cls.build_msg(txt, *args)) |
| 85 | + |
| 86 | + @classmethod |
| 87 | + def critical(cls, txt, **args): |
| 88 | + """Log critical messagees.""" |
| 89 | + if logging.CRITICAL >= cls.LOG_LEVEL: |
| 90 | + cls._logger.critical(cls.build_msg(txt, *args)) |
0 commit comments