2 # Copyright (c) 2021 Project CHIP Authors
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
17 from chip.logging.library_handle import _GetLoggingLibraryHandle
18 from chip.logging.types import LogRedirectCallback_t
22 # Defines match support/logging/Constants.h (LogCategory enum)
23 ERROR_CATEGORY_NONE = 0
24 ERROR_CATEGORY_ERROR = 1
25 ERROR_CATEGORY_PROGRESS = 2
26 ERROR_CATEGORY_DETAIL = 3
29 @LogRedirectCallback_t
30 def _RedirectToPythonLogging(category, module, message):
32 module = module.decode('utf-8')
33 message = message.decode('utf-8')
35 logger = logging.getLogger('chip.%s' % module)
37 if category == ERROR_CATEGORY_ERROR:
38 logger.error("%s", message)
39 elif category == ERROR_CATEGORY_PROGRESS:
40 logger.info("%s", message)
41 elif category == ERROR_CATEGORY_DETAIL:
42 logger.debug("%s", message)
44 # All logs are expected to have some reasonable category. This treats
45 # unknonw/None as critical.
46 logging.critical("%s", message)
49 def RedirectToPythonLogging():
50 """Redireects CHIP logging to python logging module."""
52 handle = _GetLoggingLibraryHandle()
53 handle.pychip_logging_set_callback(_RedirectToPythonLogging)