Skip to content

Commit b9779ec

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 b9779ec

File tree

3 files changed

+48
-78
lines changed

3 files changed

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

+47-1
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,63 @@
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

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

2066
} // namespace Platform

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

-72
This file was deleted.

0 commit comments

Comments
 (0)