Skip to content

Commit 45bbd16

Browse files
ksperling-applebzbarsky-apple
authored andcommitted
Add chip_logging_backend option and 'none' and 'syslog' backends (project-chip#34830)
* Tidy up stdio logging target dependencies Also rename from Logging.cpp to Stdio.cpp and add license header. * Add chip_logging_backend option and 'none' backend chip_logging_backend controls which backend is pulled in by the src/platform/logging:default target. The default is 'platform', retaining the current behavior. On Darwin, remove the no-op LoggingImpl and make stdio the default backend when compiling tools or example apps. Use the new 'none' backend when building Matter.framework, retaining current behavior. Depend on logging:default instead of logging:stdio for linux:app-main examples. * Add a syslog logging backend * Fix STM32 build * Use stdio logging backend for cirque tests The stdio and linux backends use slightly different output formats and the cirque tests currently only parse the stdio format correctly. * Apply suggestions from code review Co-authored-by: Boris Zbarsky <bzbarsky@apple.com> * Fix stray space in GN string comparison --------- Co-authored-by: Boris Zbarsky <bzbarsky@apple.com>
1 parent bf4698b commit 45bbd16

File tree

11 files changed

+148
-22
lines changed

11 files changed

+148
-22
lines changed

examples/platform/linux/BUILD.gn

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ source_set("app-main") {
9393
"${chip_root}/src/controller:controller",
9494
"${chip_root}/src/controller:gen_check_chip_controller_headers",
9595
"${chip_root}/src/lib",
96-
"${chip_root}/src/platform/logging:stdio",
96+
"${chip_root}/src/platform/logging:default",
9797
]
9898
deps = [
9999
":ota-test-event-trigger",

scripts/build/gn_gen_cirque.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ echo "Setup build environment"
3636
source "./scripts/activate.sh"
3737

3838
echo "Build: GN configure"
39-
gn --root="$CHIP_ROOT" gen --check --fail-on-unused-args out/debug --args='target_os="all"'"chip_build_tests=false chip_enable_wifi=false chip_im_force_fabric_quota_check=true enable_default_builds=false enable_host_gcc_build=true enable_standalone_chip_tool_build=true enable_linux_all_clusters_app_build=true enable_linux_lighting_app_build=true enable_linux_lit_icd_app_build=true"
39+
gn --root="$CHIP_ROOT" gen --check --fail-on-unused-args out/debug --args='target_os="all" chip_logging_backend="stdio" chip_build_tests=false chip_enable_wifi=false chip_im_force_fabric_quota_check=true enable_default_builds=false enable_host_gcc_build=true enable_standalone_chip_tool_build=true enable_linux_all_clusters_app_build=true enable_linux_lighting_app_build=true enable_linux_lit_icd_app_build=true'
4040

4141
echo "Build: Ninja build"
4242
time ninja -C out/debug all check

src/darwin/Framework/chip_xcode_build_connector.sh

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ declare -a args=(
103103
'chip_enable_python_modules=false'
104104
'chip_device_config_enable_dynamic_mrp_config=true'
105105
'chip_log_message_max_size=4096' # might as well allow nice long log messages
106+
'chip_logging_backend="none"' # os_log() is integrated via CHIP_SYSTEM_CONFIG_PLATFORM_LOG
106107
'chip_disable_platform_kvs=true'
107108
'enable_fuzz_test_targets=false'
108109
"target_cpu=\"$target_cpu\""

src/lib/core/core.gni

+25
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,23 @@ declare_args() {
5454
# Configure chip logging to output through external logging implementation.
5555
# External code will need to provide implementation for CHIP log output
5656
# function (LogV), which is defined in "src/platform/logging/LogV.h".
57+
# Same as setting chip_logging_backend = "external"
5758
chip_use_external_logging = false
59+
}
60+
61+
declare_args() {
62+
# Logging backend to use for targets that don't link a specific log
63+
# backend (e.g. command line utilites usually use 'stdio'). Options:
64+
# 'platform' - The default log backend of the device platform
65+
# 'external' - External LogV implementation (src/platform/logging/LogV.h)
66+
# 'none' - Discard all log output
67+
# 'stdio' - Print to stdout
68+
# 'syslog' - POSIX syslog()
69+
if (chip_use_external_logging) {
70+
chip_logging_backend = "external"
71+
} else {
72+
chip_logging_backend = "platform"
73+
}
5874

5975
# Enable short error strings.
6076
chip_config_short_error_str = false
@@ -117,6 +133,15 @@ if (chip_target_style == "") {
117133
}
118134
}
119135

136+
assert(
137+
chip_logging_backend == "platform" || chip_logging_backend == "external" ||
138+
chip_logging_backend == "none" || chip_logging_backend == "stdio" ||
139+
chip_logging_backend == "syslog",
140+
"Please select a valid logging backend: platform, external, none, stdio, syslog")
141+
assert(
142+
!chip_use_external_logging || chip_logging_backend == "external",
143+
"Setting chip_use_external_logging = true conflicts with selected chip_logging_backend")
144+
120145
assert(chip_target_style == "unix" || chip_target_style == "embedded",
121146
"Please select a valid target style: unix, embedded")
122147

src/platform/Darwin/BUILD.gn

-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ static_library("logging") {
151151
sources = [
152152
"Logging.h",
153153
"Logging.mm",
154-
"LoggingImpl.cpp",
155154
]
156155

157156
deps = [

src/platform/logging/BUILD.gn

+30-9
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import("${chip_root}/src/lib/core/core.gni")
88
import("${chip_root}/src/lib/shell/shell_device.gni")
99
import("${chip_root}/src/platform/device.gni")
1010

11-
source_set("default") {
12-
if (!chip_use_external_logging) {
11+
group("default") {
12+
if (chip_logging_backend == "platform") {
1313
deps = []
1414

1515
if (chip_use_pw_logging) {
@@ -63,7 +63,7 @@ source_set("default") {
6363
} else if (chip_device_platform == "qpg") {
6464
deps += [ "${chip_root}/src/platform/qpg:logging" ]
6565
} else if (chip_device_platform == "darwin") {
66-
deps += [ "${chip_root}/src/platform/Darwin:logging" ]
66+
deps += [ ":stdio" ] # For tools / examples. The framework uses "none".
6767
} else if (chip_device_platform == "mw320") {
6868
deps += [ "${chip_root}/src/platform/nxp/mw320:logging" ]
6969
} else if (chip_device_platform == "k32w0" ||
@@ -82,22 +82,43 @@ source_set("default") {
8282
assert(chip_device_platform == "fake" ||
8383
chip_device_platform == "external" || chip_device_platform == "none")
8484
}
85+
} else if (chip_logging_backend == "none" ||
86+
chip_logging_backend == "stdio" ||
87+
chip_logging_backend == "syslog") {
88+
deps = [ ":${chip_logging_backend}" ]
89+
} else {
90+
assert(chip_logging_backend == "external")
8591
}
8692
}
8793

8894
source_set("headers") {
8995
public = [ "LogV.h" ]
96+
public_deps = [
97+
"${chip_root}/src/lib/support:attributes",
98+
"${chip_root}/src/lib/support:logging_constants",
99+
]
100+
}
101+
102+
source_set("none") {
103+
sources = [ "impl/None.cpp" ]
104+
deps = [
105+
":headers",
106+
"${chip_root}/src/platform:platform_base",
107+
]
90108
}
91109

92110
source_set("stdio") {
93-
sources = [ "impl/stdio/Logging.cpp" ]
111+
sources = [ "impl/Stdio.cpp" ]
112+
deps = [
113+
":headers",
114+
"${chip_root}/src/platform:platform_base",
115+
]
116+
}
94117

118+
source_set("syslog") {
119+
sources = [ "impl/Syslog.cpp" ]
95120
deps = [
96121
":headers",
97-
"${chip_root}/src/lib/core:chip_config_header",
98-
"${chip_root}/src/lib/support:attributes",
99-
"${chip_root}/src/lib/support:logging_constants",
100-
"${chip_root}/src/platform:platform_config_header",
101-
"${chip_root}/src/platform/logging:headers",
122+
"${chip_root}/src/platform:platform_base",
102123
]
103124
}

src/platform/Darwin/LoggingImpl.cpp src/platform/logging/impl/None.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
*
3-
* Copyright (c) 2021 Project CHIP Authors
3+
* Copyright (c) 2024 Project CHIP Authors
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -21,12 +21,11 @@ namespace chip {
2121
namespace Logging {
2222
namespace Platform {
2323

24-
void LogV(const char * module, uint8_t category, const char * msg, va_list v)
24+
void LogV(const char *, uint8_t, const char *, va_list)
2525
{
26-
// ChipPlatformLog expands to an os_log call directly (see Logging.h), so
27-
// we don't need to do anything further here. However his function and the
28-
// call to it still exist because of scenarios where a different logging
29-
// backend (usually stdio) is swapped in at link time, e.g. for unit tests.
26+
// This backend discards all log messages. This is useful when all log output
27+
// is routed via `SetLogRedirectCallback()` and/or platform logging
28+
// integration at the log macro level (`CHIP_SYSTEM_CONFIG_PLATFORM_LOG`).
3029
}
3130

3231
} // namespace Platform

src/platform/logging/impl/stdio/Logging.cpp src/platform/logging/impl/Stdio.cpp

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
1-
/* See Project CHIP LICENSE file for licensing information. */
1+
/*
2+
*
3+
* Copyright (c) 2021-2024 Project CHIP Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
217

318
#include <platform/logging/LogV.h>
419

src/platform/logging/impl/Syslog.cpp

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
*
3+
* Copyright (c) 2024 Project CHIP Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include <platform/logging/LogV.h>
19+
20+
#include <lib/core/CHIPConfig.h>
21+
#include <lib/support/logging/Constants.h>
22+
23+
#include <mutex>
24+
#include <syslog.h>
25+
26+
namespace chip {
27+
namespace Logging {
28+
namespace Platform {
29+
30+
namespace {
31+
int LogPriority(uint8_t category)
32+
{
33+
switch (category)
34+
{
35+
case kLogCategory_Error:
36+
return LOG_ERR;
37+
case kLogCategory_Progress:
38+
return LOG_NOTICE;
39+
default:
40+
return LOG_DEBUG;
41+
}
42+
}
43+
} // namespace
44+
45+
void LogV(const char * module, uint8_t category, const char * msg, va_list v)
46+
{
47+
static std::mutex sMutex;
48+
static bool sInitialized = false;
49+
static char sBuffer[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE];
50+
std::lock_guard guard(sMutex);
51+
52+
if (!sInitialized)
53+
{
54+
openlog(nullptr, 0, LOG_DAEMON);
55+
sInitialized = true;
56+
}
57+
58+
// Pre-format the message so we can include the module name
59+
vsnprintf(sBuffer, sizeof(sBuffer), msg, v);
60+
syslog(LogPriority(category), "%s: %s", module, sBuffer);
61+
}
62+
63+
} // namespace Platform
64+
} // namespace Logging
65+
} // namespace chip

src/platform/stm32/BUILD.gn

+1-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ static_library("stm32") {
3939
sources = [
4040
"../FreeRTOS/SystemTimeSupport.cpp",
4141
"../SingletonConfigurationManager.cpp",
42-
"../logging/impl/stdio/Logging.cpp",
4342
"BLEManagerImpl.cpp",
4443
"BLEManagerImpl.h",
4544
"BlePlatformConfig.h",
@@ -69,7 +68,7 @@ static_library("stm32") {
6968
"SystemPlatformConfig.h",
7069
]
7170

72-
deps += [ "${chip_root}/src/platform/logging:headers" ]
71+
deps += [ "${chip_root}/src/platform/logging:stdio" ]
7372
}
7473

7574
public = [

src/system/SystemConfig.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,9 @@ struct LwIPEvent;
558558
* @def CHIP_SYSTEM_CONFIG_PLATFORM_LOG
559559
*
560560
* @brief
561-
* Defines whether (1) or not (0) the system uses a platform-specific logging implementation.
561+
* Defines whether (1) or not (0) the system uses a platform-specific implementation of
562+
* ChipLog* macros. Most platforms do not use this option and simply provide a logging
563+
* backend that implements LogV.
562564
*
563565
* See CHIPLogging.h for details.
564566
*/

0 commit comments

Comments
 (0)