Skip to content

Commit 43bd3cc

Browse files
committed
avoid debug cost if unset
1 parent c0a33ed commit 43bd3cc

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

pyModbusTCP/client.py

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ def _send_pdu(self, pdu):
797797
# send frame with error check
798798
self._send(tx_frame)
799799
# debug
800-
self._on_tx_rx(frame=tx_frame, tx=True)
800+
self._on_tx_rx(frame=tx_frame, is_tx=True)
801801

802802
def _recv(self, size):
803803
"""Receive data over current socket.
@@ -853,15 +853,15 @@ def _recv_pdu(self, min_len=2):
853853
# checking error status of fields
854854
if f_transaction_err or f_protocol_err or f_length_err or f_unit_id_err:
855855
self.close()
856-
self._on_tx_rx(frame=rx_mbap, tx=False)
856+
self._on_tx_rx(frame=rx_mbap, is_tx=False)
857857
raise ModbusClient._NetworkError(MB_RECV_ERR, 'MBAP checking error')
858858
# recv PDU
859859
rx_pdu = self._recv_all(f_length - 1)
860860
# for auto_close mode, close socket after each request
861861
if self.auto_close:
862862
self.close()
863863
# dump frame
864-
self._on_tx_rx(frame=rx_mbap + rx_pdu, tx=False)
864+
self._on_tx_rx(frame=rx_mbap + rx_pdu, is_tx=False)
865865
# body decode
866866
# check PDU length for global minimal frame (an except frame: func code + exp code)
867867
if len(rx_pdu) < 2:
@@ -931,24 +931,16 @@ def _req_except_handler(self, _except):
931931
def _debug_msg(self, msg: str):
932932
logger.debug(f'({self.host}:{self.port}:{self.unit_id}) {msg}')
933933

934-
def _on_tx_rx(self, frame: bytes, tx: bool):
935-
"""Dump a modbus frame.
936-
937-
modbus/TCP format: [MBAP] PDU
938-
939-
:param frame: modbus frame
940-
:type frame: bytes
941-
:param tx: True on tx, False on rx
942-
:type tx: bool
943-
"""
934+
def _on_tx_rx(self, frame: bytes, is_tx: bool):
944935
# format a log message
945-
type_s = 'Tx' if tx else 'Rx'
946-
mbap_s = hexlify(frame[0:7], sep=' ').upper().decode()
947-
pdu_s = hexlify(frame[7:], sep=' ').upper().decode()
948-
self._debug_msg(f'{type_s} [{mbap_s}] {pdu_s}')
936+
if logger.isEnabledFor(logging.DEBUG):
937+
type_s = 'Tx' if is_tx else 'Rx'
938+
mbap_s = hexlify(frame[0:7], sep=' ').upper().decode()
939+
pdu_s = hexlify(frame[7:], sep=' ').upper().decode()
940+
self._debug_msg(f'{type_s} [{mbap_s}] {pdu_s}')
949941
# notify user
950-
self.on_tx_rx(frame=frame, tx=tx)
942+
self.on_tx_rx(frame=frame, is_tx=is_tx)
951943

952-
def on_tx_rx(self, frame: bytes, tx: bool):
953-
"""Call for each Tx/Rx for user purposes."""
944+
def on_tx_rx(self, frame: bytes, is_tx: bool):
945+
"""Call for each Tx/Rx (for user purposes)."""
954946
pass

0 commit comments

Comments
 (0)