Skip to content

Commit

Permalink
Initialize logging after native init (#616)
Browse files Browse the repository at this point in the history
  • Loading branch information
agners committed Mar 5, 2024
1 parent ff06588 commit 0621241
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions matter_server/server/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
CHIP_DETAIL = logging.DEBUG - 1
CHIP_AUTOMATION = logging.DEBUG - 2

_category_num: int = 4


@LogRedirectCallback_t # type: ignore[misc]
def _redirect_to_python_logging(
Expand Down Expand Up @@ -58,27 +60,24 @@ def init_logging(category: str) -> None:
"""Initialize Matter SDK logging. Filter by category."""

_LOGGER.info("Initializing CHIP/Matter Logging...")
category_num = ERROR_CATEGORY_NONE
_category_num = ERROR_CATEGORY_NONE
if category == "ERROR":
category_num = ERROR_CATEGORY_ERROR
_category_num = ERROR_CATEGORY_ERROR
elif category == "PROGRESS":
category_num = ERROR_CATEGORY_PROGRESS
_category_num = ERROR_CATEGORY_PROGRESS
elif category == "DETAIL":
category_num = ERROR_CATEGORY_DETAIL
_category_num = ERROR_CATEGORY_DETAIL
elif category == "AUTOMATION":
category_num = 4
_category_num = 4

logging.addLevelName(CHIP_ERROR, "CHIP_ERROR")
logging.addLevelName(CHIP_PROGRESS, "CHIP_PROGRESS")
logging.addLevelName(CHIP_DETAIL, "CHIP_DETAIL")
logging.addLevelName(CHIP_AUTOMATION, "CHIP_AUTOMATION")
logging.getLogger("chip.native").setLevel(CHIP_AUTOMATION)

handle = _GetLoggingLibraryHandle()
handle.pychip_logging_set_callback(_redirect_to_python_logging)

# Handle log level selection on SDK level
chip.logging.SetLogFilter(category_num)
# We can't setup logging here yet as the stack needs to be
# initialized first!


class MatterStack:
Expand All @@ -95,6 +94,14 @@ def __init__(
self.logger.debug("Using storage file: %s", storage_file)
chip.native.Init()

# Initialize logging after stack init!
# See: https://github.com/project-chip/connectedhomeip/issues/20233
handle = _GetLoggingLibraryHandle()
handle.pychip_logging_set_callback(_redirect_to_python_logging)

# Handle log level selection on SDK level
chip.logging.SetLogFilter(_category_num)

self._chip_stack = ChipStack(
persistentStoragePath=storage_file,
installDefaultLogHandler=False,
Expand Down

0 comments on commit 0621241

Please sign in to comment.