Skip to content

Commit d098f11

Browse files
committed
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.
1 parent 686e73b commit d098f11

File tree

3 files changed

+49
-78
lines changed

3 files changed

+49
-78
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

+48-1
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,64 @@
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
{
26+
#if defined(_POSIX_THREAD_SAFE_FUNCTIONS)
27+
flockfile(stdout);
28+
#endif
29+
30+
switch (category)
31+
{
32+
case kLogCategory_Error:
33+
printf("\033[1;31m");
34+
break;
35+
case kLogCategory_Progress:
36+
printf("\033[0;32m");
37+
break;
38+
case kLogCategory_Detail:
39+
printf("\033[0;34m");
40+
break;
41+
}
42+
43+
#if defined(__APPLE__) || defined(__gnu_linux__)
44+
timespec ts;
45+
timespec_get(&ts, TIME_UTC);
46+
printf("[%lld.%06ld] ", static_cast<long long>(ts.tv_sec), static_cast<long>(ts.tv_nsec / 1000));
47+
#endif
48+
49+
#if defined(__APPLE__)
50+
uint64_t ktid;
51+
pthread_threadid_np(nullptr, &ktid);
52+
printf("[%lld:%lld] ", static_cast<long long>(getpid()), static_cast<long long>(ktid));
53+
#elif defined(__gnu_linux__) && !defined(__NuttX__)
54+
// TODO: change to getpid() and gettid() after glib upgrade
55+
printf("[%lld:%lld] ", static_cast<long long>(syscall(SYS_getpid)), static_cast<long long>(syscall(SYS_gettid)));
56+
#endif
57+
1558
printf("CHIP:%s: ", module);
1659
vprintf(msg, v);
17-
printf("\n");
60+
printf("\033[0m\n");
61+
62+
#if defined(_POSIX_THREAD_SAFE_FUNCTIONS)
63+
funlockfile(stdout);
64+
#endif
1865
}
1966

2067
} // namespace Platform

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

-72
This file was deleted.

0 commit comments

Comments
 (0)