From 13a27e5484e6d655bebe4835e13ec7f7b90df4a2 Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Tue, 5 Mar 2024 12:01:13 +0100 Subject: [PATCH] Initialize logging after native init Make sure to initialize logging after the native code init. This makes sure that the C++ pychip_native_init is called after pychip_CommonStackInit. This is required to make sure that the platform specific initialization in src/platform/Linux/PlatformManagerImpl.cpp is being called before using the SDK. See also: https://github.com/project-chip/connectedhomeip/pull/20234 --- matter_server/server/stack.py | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/matter_server/server/stack.py b/matter_server/server/stack.py index a708ed5e..5a4ebcb9 100644 --- a/matter_server/server/stack.py +++ b/matter_server/server/stack.py @@ -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( @@ -58,15 +60,15 @@ 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") @@ -74,11 +76,8 @@ def init_logging(category: str) -> None: 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: @@ -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,