From 964c460fe5b282570bf50c6da462fc1f67f7ad6a Mon Sep 17 00:00:00 2001 From: Marek Sebera Date: Sat, 6 Apr 2024 11:11:46 +0200 Subject: [PATCH] enhance pcaptool to output processed bursts --- .gitignore | 2 ++ okdmr/dmrlib/tools/pcap_tool.py | 2 +- .../transmission/transmission_watcher.py | 4 ++- okdmr/dmrlib/utils/log_color_formatter.py | 27 +++++++++++++++++++ okdmr/tests/dmrlib/storage/test_repeater.py | 2 +- pyproject.toml | 3 ++- 6 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 okdmr/dmrlib/utils/log_color_formatter.py diff --git a/.gitignore b/.gitignore index 26b0561..cf26a94 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ __pycache__ /.coverage /coverage.xml /.pdm.toml +/.idea +/.tool-versions diff --git a/okdmr/dmrlib/tools/pcap_tool.py b/okdmr/dmrlib/tools/pcap_tool.py index b001ef0..7691542 100644 --- a/okdmr/dmrlib/tools/pcap_tool.py +++ b/okdmr/dmrlib/tools/pcap_tool.py @@ -298,7 +298,7 @@ def iter_pcap( if len(ip_whitelist): # if no whitelisted ips, do not filter - if ip_layer.src not in ip_whitelist: + if ip_layer and ip_layer.src not in ip_whitelist: continue if len(ports_whitelist): diff --git a/okdmr/dmrlib/transmission/transmission_watcher.py b/okdmr/dmrlib/transmission/transmission_watcher.py index 44f48fd..bba7782 100644 --- a/okdmr/dmrlib/transmission/transmission_watcher.py +++ b/okdmr/dmrlib/transmission/transmission_watcher.py @@ -36,7 +36,9 @@ def process_packet(self, data: bytes, packet: IP) -> None: data=data, packet=packet, silent=True ) if burst: - self.process_burst(burst) + processed_burst: Burst = self.process_burst(burst) + if processed_burst: + print(processed_burst) if self.debug_voice_bytes and burst.is_vocoder: import logging diff --git a/okdmr/dmrlib/utils/log_color_formatter.py b/okdmr/dmrlib/utils/log_color_formatter.py new file mode 100644 index 0000000..9e5f230 --- /dev/null +++ b/okdmr/dmrlib/utils/log_color_formatter.py @@ -0,0 +1,27 @@ +import logging + + +class LogColorFormatter(logging.Formatter): + # \x1b == \033 + grey = "\x1b[38;20m" + yellow = "\x1b[33;20m" + red = "\x1b[31;20m" + bold_red = "\x1b[31;1m" + reset = "\x1b[0m" + format = ( + "%(asctime)s - %(name)s - %(levelname)s - %(message)s (%(filename)s:%(lineno)d)" + ) + + FORMATS = { + logging.DEBUG: red + format + reset, + logging.INFO: yellow + format + reset, + logging.WARNING: yellow + format + reset, + logging.ERROR: red + format + reset, + logging.CRITICAL: bold_red + format + reset, + } + + def format(self, record): + print(f"OH {record.levelno}") + log_fmt = self.FORMATS.get(record.levelno) + formatter = logging.Formatter(log_fmt) + return formatter.format(record) diff --git a/okdmr/tests/dmrlib/storage/test_repeater.py b/okdmr/tests/dmrlib/storage/test_repeater.py index e0ffa82..fbb4ea5 100644 --- a/okdmr/tests/dmrlib/storage/test_repeater.py +++ b/okdmr/tests/dmrlib/storage/test_repeater.py @@ -25,7 +25,7 @@ def test_repeater(): rpt.attr("custom_attr", 123_456) assert rpt.attr("custom_attr") == 123_456 rpt.delete_attr("custom_attr") - assert rpt.attr("custom_attr") == None + assert rpt.attr("custom_attr") is None rpt.nat_enabled = True assert rpt.repeater_target_address() == ADDRESS_EMPTY diff --git a/pyproject.toml b/pyproject.toml index 592f331..a6b663b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -49,7 +49,8 @@ test = [ "pytest>=7.0.0", "pytest-cov>=4.0.0", "pytest-asyncio>=0.20.0", - "crc>=4.0.0" + "crc>=4.0.0", + "coverage>=7.4.0" ] [project.scripts]