Skip to content

Commit ee49ebd

Browse files
authored
Unify stdio logging (#34070)
* Unify stdio logging Darwin now uses the same stdio logging implementation as other platforms. Logs are colorized and contain timestamps. On linux and darwin, pid and tid are also logged. * Darwin formatting * Fix cirque test driver * Log seconds and milliseconds * Update Logging.cpp
1 parent 2908685 commit ee49ebd

File tree

4 files changed

+52
-80
lines changed

4 files changed

+52
-80
lines changed

src/platform/logging/BUILD.gn

+1-5
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,7 @@ if (current_os == "android") {
109109
stdio_archive = "$root_out_dir/liblogging-stdio.a"
110110

111111
source_set("stdio") {
112-
if (chip_device_platform == "darwin") {
113-
sources = [ "impl/stdio/darwin/Logging.cpp" ]
114-
} else {
115-
sources = [ "impl/stdio/Logging.cpp" ]
116-
}
112+
sources = [ "impl/stdio/Logging.cpp" ]
117113

118114
deps = [
119115
":headers",

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

+50-2
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,65 @@
44

55
#include <lib/support/logging/Constants.h>
66

7+
#include <inttypes.h>
78
#include <stdio.h>
9+
#include <time.h>
10+
11+
#if defined(__APPLE__)
12+
#include <pthread.h>
13+
#include <unistd.h>
14+
#elif defined(__gnu_linux__)
15+
#include <sys/syscall.h>
16+
#include <unistd.h>
17+
#endif
818

919
namespace chip {
1020
namespace Logging {
1121
namespace Platform {
1222

23+
// TODO: investigate whether pw_chrono or pw_log could be used here
1324
void LogV(const char * module, uint8_t category, const char * msg, va_list v)
1425
{
15-
printf("CHIP:%s: ", module);
26+
// Stdout needs to be locked, because it's printed in pieces
27+
#if defined(_POSIX_THREAD_SAFE_FUNCTIONS)
28+
flockfile(stdout);
29+
#endif
30+
31+
switch (category)
32+
{
33+
case kLogCategory_Error:
34+
printf("\033[1;31m");
35+
break;
36+
case kLogCategory_Progress:
37+
printf("\033[0;32m");
38+
break;
39+
case kLogCategory_Detail:
40+
printf("\033[0;34m");
41+
break;
42+
}
43+
44+
#if defined(__APPLE__) || defined(__gnu_linux__)
45+
timespec ts;
46+
timespec_get(&ts, TIME_UTC);
47+
printf("[%lld.%03ld] ", static_cast<long long>(ts.tv_sec), static_cast<long>(ts.tv_nsec / 1000000));
48+
#endif
49+
50+
#if defined(__APPLE__)
51+
uint64_t ktid;
52+
pthread_threadid_np(nullptr, &ktid);
53+
printf("[%lld:%lld] ", static_cast<long long>(getpid()), static_cast<long long>(ktid));
54+
#elif defined(__gnu_linux__) && !defined(__NuttX__)
55+
// TODO: change to getpid() and gettid() after glib upgrade
56+
printf("[%lld:%lld] ", static_cast<long long>(syscall(SYS_getpid)), static_cast<long long>(syscall(SYS_gettid)));
57+
#endif
58+
59+
printf("[%s] ", module);
1660
vprintf(msg, v);
17-
printf("\n");
61+
printf("\033[0m\n");
62+
63+
#if defined(_POSIX_THREAD_SAFE_FUNCTIONS)
64+
funlockfile(stdout);
65+
#endif
1866
}
1967

2068
} // namespace Platform

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

-72
This file was deleted.

src/test_driver/linux-cirque/helper/CHIPTestBase.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def reset_thread_devices(self, devices: Union[List[str], str]):
145145
self.assertTrue(self.wait_for_device_output(
146146
device_id, "Thread Border Router started on AIL", 10))
147147
self.assertTrue(self.wait_for_device_output(
148-
device_id, "CHIP:SVR: Server Listening...", 15))
148+
device_id, "[SVR] Server Listening...", 15))
149149
# Clear default Thread network commissioning data
150150
self.logger.info("Resetting thread network on {}".format(
151151
self.get_device_pretty_id(device_id)))

0 commit comments

Comments
 (0)