From f7c18f0b98702448ea87147e3aa218943a22dfaa Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 10 Jun 2024 18:07:01 -0500 Subject: [PATCH] feat: guard debug logging that reformats data in the arguments The reformatting runs even if debug logging is disabled --- .coveragerc | 1 + src/uiprotect/api.py | 37 ++++++++++++++++++++----------------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/.coveragerc b/.coveragerc index 4d657328..0abd3c63 100644 --- a/.coveragerc +++ b/.coveragerc @@ -23,3 +23,4 @@ exclude_lines = if TYPE_CHECKING: @overload except ImportError: + if _LOGGER.isEnabledFor(logging.DEBUG): diff --git a/src/uiprotect/api.py b/src/uiprotect/api.py index c02e98cd..d2cbc7f1 100644 --- a/src/uiprotect/api.py +++ b/src/uiprotect/api.py @@ -818,23 +818,26 @@ async def update(self, force: bool = False) -> Bootstrap | None: return self._bootstrap def emit_message(self, msg: WSSubscriptionMessage) -> None: - if msg.new_obj is not None: - _LOGGER.debug( - "emitting message: %s:%s:%s:%s", - msg.action, - msg.new_obj.model, - msg.new_obj.id, - list(msg.changed_data.keys()), - ) - elif msg.old_obj is not None: - _LOGGER.debug( - "emitting message: %s:%s:%s", - msg.action, - msg.old_obj.model, - msg.old_obj.id, - ) - else: - _LOGGER.debug("emitting message: %s", msg.action) + """Emit message to all subscriptions.""" + if _LOGGER.isEnabledFor(logging.DEBUG): + if msg.new_obj is not None: + _LOGGER.debug( + "emitting message: %s:%s:%s:%s", + msg.action, + msg.new_obj.model, + msg.new_obj.id, + list(msg.changed_data.keys()), + ) + elif msg.old_obj is not None: + _LOGGER.debug( + "emitting message: %s:%s:%s", + msg.action, + msg.old_obj.model, + msg.old_obj.id, + ) + else: + _LOGGER.debug("emitting message: %s", msg.action) + for sub in self._ws_subscriptions: try: sub(msg)