Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / controller / python / chip / logging / __init__.py
1 #
2 #    Copyright (c) 2021 Project CHIP Authors
3 #
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
7 #
8 #        http://www.apache.org/licenses/LICENSE-2.0
9 #
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.
15 #
16
17 from chip.logging.library_handle import _GetLoggingLibraryHandle
18 from chip.logging.types import LogRedirectCallback_t
19 import logging
20
21
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
27
28
29 @LogRedirectCallback_t
30 def _RedirectToPythonLogging(category, module, message):
31
32     module = module.decode('utf-8')
33     message = message.decode('utf-8')
34
35     logger = logging.getLogger('chip.%s' % module)
36
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)
43     else:
44         # All logs are expected to have some reasonable category. This treats
45         # unknonw/None as critical.
46         logging.critical("%s", message)
47
48
49 def RedirectToPythonLogging():
50     """Redireects CHIP logging to python logging module."""
51
52     handle = _GetLoggingLibraryHandle()
53     handle.pychip_logging_set_callback(_RedirectToPythonLogging)