Skip to content

Commit 3b114a5

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 3b114a5

File tree

3 files changed

+42
-78
lines changed

3 files changed

+42
-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

+41-1
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,57 @@
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(__GLIBC__)
15+
#include <sys/syscall.h>
16+
#include <unistd.h>
17+
#endif
818

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

1323
void LogV(const char * module, uint8_t category, const char * msg, va_list v)
1424
{
25+
flockfile(stdout);
26+
27+
switch (category)
28+
{
29+
case kLogCategory_Error:
30+
printf("\033[1;31m");
31+
break;
32+
case kLogCategory_Progress:
33+
printf("\033[0;32m");
34+
break;
35+
case kLogCategory_Detail:
36+
printf("\033[0;34m");
37+
break;
38+
}
39+
40+
timespec ts;
41+
timespec_get(&ts, TIME_UTC);
42+
printf("[%" PRIu64 ".%06" PRIu64 "] ", static_cast<uint64_t>(ts.tv_sec), static_cast<uint64_t>(ts.tv_nsec / 1000));
43+
44+
#if defined(__APPLE__) && defined(__MACH__)
45+
uint64_t ktid;
46+
pthread_threadid_np(nullptr, &ktid);
47+
printf("[%lld:%lld] ", static_cast<long long>(getpid()), static_cast<long long>(ktid));
48+
#elif defined(__GLIBC__)
49+
// TODO: change to getpid() and gettid() after glib upgrade
50+
printf("[%lld:%lld] ", static_cast<long long>(syscall(SYS_getpid)), static_cast<long long>(syscall(SYS_gettid)));
51+
#endif
52+
1553
printf("CHIP:%s: ", module);
1654
vprintf(msg, v);
17-
printf("\n");
55+
printf("\033[0m\n");
56+
57+
funlockfile(stdout);
1858
}
1959

2060
} // namespace Platform

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

-72
This file was deleted.

0 commit comments

Comments
 (0)