Skip to content

Commit 1031488

Browse files
rgoliverpull[bot]
rgoliver
authored andcommitted
RPC: Support pw logging for linux rpc examples (#20057)
Add a config option CHIP_USE_PW_LOGGING, which uses pw log output instead of printf on linux. This allows the logs to correctly get HDLC encoded and be piped over the socket for linux RPC builds.
1 parent e084f4c commit 1031488

File tree

7 files changed

+52
-2
lines changed

7 files changed

+52
-2
lines changed

examples/chef/linux/with_pw_rpc.gni

+1
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,4 @@ pw_build_LINK_DEPS = [
4040

4141
chip_enable_pw_rpc = true
4242
chip_build_pw_trace_lib = true
43+
chip_use_pw_logging = true

examples/common/pigweed/rpc_console/py/chip_rpc/console.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,8 @@ def write_to_output(data: bytes,
253253
"E": logging.ERROR, "F": logging.FATAL, "V": logging.DEBUG, "D": logging.DEBUG,
254254
"<inf>": logging.INFO, "<dbg>": logging.DEBUG, "<err>": logging.ERROR,
255255
"<info >": logging.INFO, "<warn >": logging.WARNING,
256-
"<error >": logging.ERROR, "<detail>": logging.DEBUG}
256+
"<error >": logging.ERROR, "<detail>": logging.DEBUG,
257+
"ERR": logging.ERROR, "DBG": logging.DEBUG, "INF": logging.INFO}
257258

258259
ESP_CHIP_REGEX = r"(?P<level>[IWEFV]) \((?P<time>\d+)\) (?P<mod>chip\[[a-zA-Z]+\]):\s(?P<msg>.*)"
259260
ESP_APP_REGEX = r"(?P<level>[IWEFVD]) \((?P<time>\d+)\) (?P<mod>[a-z\-_A-Z]+):\s(?P<msg>.*)"
@@ -267,14 +268,17 @@ def write_to_output(data: bytes,
267268
NXP_CHIP_REGEX = r"\[(?P<time>\d+)\]\[(?P<level>[EPDF])\]\[(?P<mod>[a-z\-A-Z]+)\](?P<msg>.*)"
268269
NXP_APP_REGEX = r"\[(?P<time>\d+)\]\[(?P<mod>[a-z\-A-Z]+)\](?P<msg>.*)"
269270

271+
LINUX_REGEX = r".*(?P<level>INF|DBG|ERR).*\s+\[(?P<time>[0-9]+\.?[0-9]*)\]\[(?P<pid>\d+)\:(?P<tid>\d+)\] CHIP:(?P<mod>[a-z\-A-Z]+)\: (?P<msg>.*)"
272+
270273
LogRegexes = [RegexStruct("ESP", "CHIP", re.compile(ESP_CHIP_REGEX), 4),
271274
RegexStruct("ESP", "APP", re.compile(ESP_APP_REGEX), 4),
272275
RegexStruct("EFR", "CHIP", re.compile(EFR_CHIP_REGEX), 3),
273276
RegexStruct("EFR", "APP", re.compile(EFR_APP_REGEX), 1),
274277
RegexStruct("NRF", "CHIP", re.compile(NRF_CHIP_REGEX), 4),
275278
RegexStruct("NRF", "APP", re.compile(NRF_APP_REGEX), 3),
276279
RegexStruct("NXP", "CHIP", re.compile(NXP_CHIP_REGEX), 4),
277-
RegexStruct("NXP", "APP", re.compile(NXP_APP_REGEX), 3)
280+
RegexStruct("NXP", "APP", re.compile(NXP_APP_REGEX), 3),
281+
RegexStruct("LINUX", "CHIP", re.compile(LINUX_REGEX), 6)
278282
]
279283
for line in log_line.decode(errors="surrogateescape").splitlines():
280284
fields = {'level': logging.INFO, "time": "",

examples/lighting-app/linux/with_pw_rpc.gni

+1
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,4 @@ pw_build_LINK_DEPS = [
4040

4141
chip_enable_pw_rpc = true
4242
chip_build_pw_trace_lib = true
43+
chip_use_pw_logging = true

src/lib/core/BUILD.gn

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ buildconfig_header("chip_buildconfig") {
4747
"CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE=${chip_log_message_max_size}",
4848
"CHIP_AUTOMATION_LOGGING=${chip_automation_logging}",
4949
"CHIP_PW_TOKENIZER_LOGGING=${chip_pw_tokenizer_logging}",
50+
"CHIP_USE_PW_LOGGING=${chip_use_pw_logging}",
5051
"CHIP_CONFIG_SHORT_ERROR_STR=${chip_config_short_error_str}",
5152
"CHIP_CONFIG_ENABLE_ARG_PARSER=${chip_config_enable_arg_parser}",
5253
"CHIP_TARGET_STYLE_UNIX=${chip_target_style_unix}",

src/lib/core/core.gni

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ declare_args() {
4444
# Enable pigweed tokenizer logging.
4545
chip_pw_tokenizer_logging = false
4646

47+
# Configure chip logging to output through pigweed logging.
48+
chip_use_pw_logging = false
49+
4750
# Enable short error strings.
4851
chip_config_short_error_str = false
4952

src/platform/Linux/BUILD.gn

+9
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@ import("//build_overrides/chip.gni")
1717

1818
import("${build_root}/config/linux/pkg_config.gni")
1919

20+
import("${chip_root}/src/lib/core/core.gni")
2021
import("${chip_root}/src/platform/device.gni")
2122

2223
assert(chip_device_platform == "linux")
2324

25+
if (chip_use_pw_logging) {
26+
import("//build_overrides/pigweed.gni")
27+
}
28+
2429
if (chip_enable_openthread) {
2530
import("//build_overrides/openthread.gni")
2631
import("//build_overrides/ot_br_posix.gni")
@@ -125,6 +130,10 @@ static_library("Linux") {
125130
public_deps += [ "dbus/openthread" ]
126131
}
127132

133+
if (chip_use_pw_logging) {
134+
deps += [ "$dir_pw_log" ]
135+
}
136+
128137
if (chip_enable_wifi) {
129138
sources += [ "GlibTypeDeleter.h" ]
130139

src/platform/Linux/Logging.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
/* See Project CHIP LICENSE file for licensing information. */
22

3+
#include <lib/core/CHIPConfig.h>
34
#include <lib/support/EnforceFormat.h>
45
#include <lib/support/logging/Constants.h>
56
#include <platform/logging/LogV.h>
67

78
#include <cinttypes>
89
#include <cstdio>
10+
#include <cstring>
911
#include <sys/syscall.h>
1012
#include <sys/time.h>
1113
#include <unistd.h>
1214

15+
#if CHIP_USE_PW_LOGGING
16+
#include <pw_log/log.h>
17+
#endif // CHIP_USE_PW_LOGGING
18+
1319
namespace chip {
1420
namespace DeviceLayer {
1521

@@ -37,6 +43,7 @@ void ENFORCE_FORMAT(3, 0) LogV(const char * module, uint8_t category, const char
3743
// indicate the error occurred during getting time.
3844
gettimeofday(&tv, nullptr);
3945

46+
#if !CHIP_USE_PW_LOGGING
4047
// Lock standard output, so a single log line will not be corrupted in case
4148
// where multiple threads are using logging subsystem at the same time.
4249
flockfile(stdout);
@@ -48,6 +55,30 @@ void ENFORCE_FORMAT(3, 0) LogV(const char * module, uint8_t category, const char
4855
fflush(stdout);
4956

5057
funlockfile(stdout);
58+
#else // !CHIP_USE_PW_LOGGING
59+
char formattedMsg[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE];
60+
snprintf(formattedMsg, sizeof(formattedMsg),
61+
"[%" PRIu64 ".%06" PRIu64 "][%lld:%lld] CHIP:%s: ", static_cast<uint64_t>(tv.tv_sec),
62+
static_cast<uint64_t>(tv.tv_usec), static_cast<long long>(syscall(SYS_getpid)),
63+
static_cast<long long>(syscall(SYS_gettid)), module);
64+
size_t len = strnlen(formattedMsg, sizeof(formattedMsg));
65+
vsnprintf(formattedMsg + len, sizeof(formattedMsg) - len, msg, v);
66+
67+
switch (static_cast<LogCategory>(category))
68+
{
69+
case kLogCategory_Error:
70+
PW_LOG_ERROR("%s", formattedMsg);
71+
break;
72+
case kLogCategory_Progress:
73+
PW_LOG_INFO("%s", formattedMsg);
74+
break;
75+
case kLogCategory_Detail:
76+
case kLogCategory_None:
77+
case kLogCategory_Automation:
78+
PW_LOG_DEBUG("%s", formattedMsg);
79+
break;
80+
}
81+
#endif // !CHIP_USE_PW_LOGGING
5182

5283
// Let the application know that a log message has been emitted.
5384
DeviceLayer::OnLogOutput();

0 commit comments

Comments
 (0)