|
| 1 | +From bf8ce26830a60e0d247f37f3b26d2f2244cb986d Mon Sep 17 00:00:00 2001 |
| 2 | +From: Stefan Agner <stefan@agner.ch> |
| 3 | +Date: Wed, 14 Feb 2024 15:11:44 +0100 |
| 4 | +Subject: [PATCH] [Python] Make Logging Filtering global (#31944) |
| 5 | + |
| 6 | +Log filtering is a global affair, so move the filtering functions to a |
| 7 | +global context too. This allows to setup logging before creating any |
| 8 | +device controller. It aligns with how the SDK treats logging. |
| 9 | + |
| 10 | +Also enable log filtering for the Linux platform to make the |
| 11 | +functionality useful on Linux. |
| 12 | +--- |
| 13 | + src/controller/python/BUILD.gn | 1 + |
| 14 | + .../ChipDeviceController-ScriptBinding.cpp | 20 ---------- |
| 15 | + src/controller/python/chip/ChipDeviceCtrl.py | 17 --------- |
| 16 | + .../python/chip/logging/LoggingFilter.cpp | 37 +++++++++++++++++++ |
| 17 | + .../python/chip/logging/__init__.py | 13 +++++++ |
| 18 | + src/platform/Linux/CHIPPlatformConfig.h | 2 +- |
| 19 | + 6 files changed, 52 insertions(+), 38 deletions(-) |
| 20 | + create mode 100644 src/controller/python/chip/logging/LoggingFilter.cpp |
| 21 | + |
| 22 | +diff --git a/src/controller/python/BUILD.gn b/src/controller/python/BUILD.gn |
| 23 | +index 140ef1e08d..5fc2212098 100644 |
| 24 | +--- a/src/controller/python/BUILD.gn |
| 25 | ++++ b/src/controller/python/BUILD.gn |
| 26 | +@@ -81,6 +81,7 @@ shared_library("ChipDeviceCtrl") { |
| 27 | + "chip/internal/ChipThreadWork.cpp", |
| 28 | + "chip/internal/ChipThreadWork.h", |
| 29 | + "chip/internal/CommissionerImpl.cpp", |
| 30 | ++ "chip/logging/LoggingFilter.cpp", |
| 31 | + "chip/logging/LoggingRedirect.cpp", |
| 32 | + "chip/native/ChipMainLoopWork.h", |
| 33 | + "chip/native/PyChipError.cpp", |
| 34 | +diff --git a/src/controller/python/ChipDeviceController-ScriptBinding.cpp b/src/controller/python/ChipDeviceController-ScriptBinding.cpp |
| 35 | +index 2576a36030..b4f2edb295 100644 |
| 36 | +--- a/src/controller/python/ChipDeviceController-ScriptBinding.cpp |
| 37 | ++++ b/src/controller/python/ChipDeviceController-ScriptBinding.cpp |
| 38 | +@@ -71,7 +71,6 @@ |
| 39 | + #include <lib/support/CodeUtils.h> |
| 40 | + #include <lib/support/DLLUtil.h> |
| 41 | + #include <lib/support/ScopedBuffer.h> |
| 42 | +-#include <lib/support/logging/CHIPLogging.h> |
| 43 | + #include <platform/CHIPDeviceLayer.h> |
| 44 | + #include <setup_payload/QRCodeSetupPayloadParser.h> |
| 45 | + #include <system/SystemClock.h> |
| 46 | +@@ -200,9 +199,6 @@ PyChipError pychip_ScriptDevicePairingDelegate_SetOpenWindowCompleteCallback( |
| 47 | + // BLE |
| 48 | + PyChipError pychip_DeviceCommissioner_CloseBleConnection(chip::Controller::DeviceCommissioner * devCtrl); |
| 49 | + |
| 50 | +-uint8_t pychip_DeviceController_GetLogFilter(); |
| 51 | +-void pychip_DeviceController_SetLogFilter(uint8_t category); |
| 52 | +- |
| 53 | + const char * pychip_Stack_ErrorToString(ChipError::StorageType err); |
| 54 | + const char * pychip_Stack_StatusReportToString(uint32_t profileId, uint16_t statusCode); |
| 55 | + void pychip_Stack_SetLogFunct(LogMessageFunct logFunct); |
| 56 | +@@ -353,22 +349,6 @@ const char * pychip_DeviceController_StatusReportToString(uint32_t profileId, ui |
| 57 | + return nullptr; |
| 58 | + } |
| 59 | + |
| 60 | +-uint8_t pychip_DeviceController_GetLogFilter() |
| 61 | +-{ |
| 62 | +-#if _CHIP_USE_LOGGING |
| 63 | +- return chip::Logging::GetLogFilter(); |
| 64 | +-#else |
| 65 | +- return chip::Logging::kLogCategory_None; |
| 66 | +-#endif |
| 67 | +-} |
| 68 | +- |
| 69 | +-void pychip_DeviceController_SetLogFilter(uint8_t category) |
| 70 | +-{ |
| 71 | +-#if _CHIP_USE_LOGGING |
| 72 | +- chip::Logging::SetLogFilter(category); |
| 73 | +-#endif |
| 74 | +-} |
| 75 | +- |
| 76 | + PyChipError pychip_DeviceController_ConnectBLE(chip::Controller::DeviceCommissioner * devCtrl, uint16_t discriminator, |
| 77 | + uint32_t setupPINCode, chip::NodeId nodeid) |
| 78 | + { |
| 79 | +diff --git a/src/controller/python/chip/ChipDeviceCtrl.py b/src/controller/python/chip/ChipDeviceCtrl.py |
| 80 | +index 2297dfff30..2400d543f6 100644 |
| 81 | +--- a/src/controller/python/chip/ChipDeviceCtrl.py |
| 82 | ++++ b/src/controller/python/chip/ChipDeviceCtrl.py |
| 83 | +@@ -1480,23 +1480,6 @@ class ChipDeviceControllerBase(): |
| 84 | + |
| 85 | + return self._Cluster.ListClusterAttributes() |
| 86 | + |
| 87 | +- def SetLogFilter(self, category): |
| 88 | +- self.CheckIsActive() |
| 89 | +- |
| 90 | +- if category < 0 or category > pow(2, 8): |
| 91 | +- raise ValueError("category must be an unsigned 8-bit integer") |
| 92 | +- |
| 93 | +- self._ChipStack.Call( |
| 94 | +- lambda: self._dmLib.pychip_DeviceController_SetLogFilter(category) |
| 95 | +- ) |
| 96 | +- |
| 97 | +- def GetLogFilter(self): |
| 98 | +- self.CheckIsActive() |
| 99 | +- |
| 100 | +- self._ChipStack.Call( |
| 101 | +- lambda: self._dmLib.pychip_DeviceController_GetLogFilter() |
| 102 | +- ) |
| 103 | +- |
| 104 | + def SetBlockingCB(self, blockingCB): |
| 105 | + self.CheckIsActive() |
| 106 | + |
| 107 | +diff --git a/src/controller/python/chip/logging/LoggingFilter.cpp b/src/controller/python/chip/logging/LoggingFilter.cpp |
| 108 | +new file mode 100644 |
| 109 | +index 0000000000..52271dc3ae |
| 110 | +--- /dev/null |
| 111 | ++++ b/src/controller/python/chip/logging/LoggingFilter.cpp |
| 112 | +@@ -0,0 +1,37 @@ |
| 113 | ++/* |
| 114 | ++ * |
| 115 | ++ * Copyright (c) 2024 Project CHIP Authors |
| 116 | ++ * |
| 117 | ++ * Licensed under the Apache License, Version 2.0 (the "License"); |
| 118 | ++ * you may not use this file except in compliance with the License. |
| 119 | ++ * You may obtain a copy of the License at |
| 120 | ++ * |
| 121 | ++ * http://www.apache.org/licenses/LICENSE-2.0 |
| 122 | ++ * |
| 123 | ++ * Unless required by applicable law or agreed to in writing, software |
| 124 | ++ * distributed under the License is distributed on an "AS IS" BASIS, |
| 125 | ++ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 126 | ++ * See the License for the specific language governing permissions and |
| 127 | ++ * limitations under the License. |
| 128 | ++ */ |
| 129 | ++ |
| 130 | ++#include <lib/support/logging/CHIPLogging.h> |
| 131 | ++ |
| 132 | ++extern "C" { |
| 133 | ++ |
| 134 | ++uint8_t pychip_logging_GetLogFilter() |
| 135 | ++{ |
| 136 | ++#if _CHIP_USE_LOGGING |
| 137 | ++ return chip::Logging::GetLogFilter(); |
| 138 | ++#else |
| 139 | ++ return chip::Logging::kLogCategory_None; |
| 140 | ++#endif |
| 141 | ++} |
| 142 | ++ |
| 143 | ++void pychip_logging_SetLogFilter(uint8_t category) |
| 144 | ++{ |
| 145 | ++#if _CHIP_USE_LOGGING |
| 146 | ++ chip::Logging::SetLogFilter(category); |
| 147 | ++#endif |
| 148 | ++} |
| 149 | ++} |
| 150 | +diff --git a/src/controller/python/chip/logging/__init__.py b/src/controller/python/chip/logging/__init__.py |
| 151 | +index 6916c8b272..047d3f4f8e 100644 |
| 152 | +--- a/src/controller/python/chip/logging/__init__.py |
| 153 | ++++ b/src/controller/python/chip/logging/__init__.py |
| 154 | +@@ -51,3 +51,16 @@ def RedirectToPythonLogging(): |
| 155 | + |
| 156 | + handle = _GetLoggingLibraryHandle() |
| 157 | + handle.pychip_logging_set_callback(_RedirectToPythonLogging) |
| 158 | ++ |
| 159 | ++ |
| 160 | ++def SetLogFilter(category): |
| 161 | ++ if category < 0 or category > pow(2, 8): |
| 162 | ++ raise ValueError("category must be an unsigned 8-bit integer") |
| 163 | ++ |
| 164 | ++ handle = _GetLoggingLibraryHandle() |
| 165 | ++ handle.pychip_logging_SetLogFilter(category) |
| 166 | ++ |
| 167 | ++ |
| 168 | ++def GetLogFilter(): |
| 169 | ++ handle = _GetLoggingLibraryHandle() |
| 170 | ++ return handle.pychip_logging_GetLogFilter() |
| 171 | +diff --git a/src/platform/Linux/CHIPPlatformConfig.h b/src/platform/Linux/CHIPPlatformConfig.h |
| 172 | +index 788fe9b80c..9e2832307f 100644 |
| 173 | +--- a/src/platform/Linux/CHIPPlatformConfig.h |
| 174 | ++++ b/src/platform/Linux/CHIPPlatformConfig.h |
| 175 | +@@ -57,7 +57,7 @@ using CHIP_CONFIG_PERSISTED_STORAGE_KEY_TYPE = const char *; |
| 176 | + #endif // CHIP_CONFIG_MAX_EXCHANGE_CONTEXTS |
| 177 | + |
| 178 | + #ifndef CHIP_LOG_FILTERING |
| 179 | +-#define CHIP_LOG_FILTERING 0 |
| 180 | ++#define CHIP_LOG_FILTERING 1 |
| 181 | + #endif // CHIP_LOG_FILTERING |
| 182 | + |
| 183 | + #ifndef CHIP_CONFIG_BDX_MAX_NUM_TRANSFERS |
| 184 | +-- |
| 185 | +2.43.0 |
| 186 | + |
0 commit comments