Skip to content

Commit

Permalink
Unify stdio logging
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mbknust committed Jun 25, 2024
1 parent 686e73b commit 3b114a5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 78 deletions.
6 changes: 1 addition & 5 deletions src/platform/logging/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,7 @@ if (current_os == "android") {
stdio_archive = "$root_out_dir/liblogging-stdio.a"

source_set("stdio") {
if (chip_device_platform == "darwin") {
sources = [ "impl/stdio/darwin/Logging.cpp" ]
} else {
sources = [ "impl/stdio/Logging.cpp" ]
}
sources = [ "impl/stdio/Logging.cpp" ]

deps = [
":headers",
Expand Down
42 changes: 41 additions & 1 deletion src/platform/logging/impl/stdio/Logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,57 @@

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

#include <inttypes.h>
#include <stdio.h>
#include <time.h>

#if defined(__APPLE__)
#include <pthread.h>
#include <unistd.h>
#elif defined(__GLIBC__)
#include <sys/syscall.h>
#include <unistd.h>
#endif

namespace chip {
namespace Logging {
namespace Platform {

void LogV(const char * module, uint8_t category, const char * msg, va_list v)
{
flockfile(stdout);

switch (category)
{
case kLogCategory_Error:
printf("\033[1;31m");
break;
case kLogCategory_Progress:
printf("\033[0;32m");
break;
case kLogCategory_Detail:
printf("\033[0;34m");
break;
}

timespec ts;
timespec_get(&ts, TIME_UTC);
printf("[%" PRIu64 ".%06" PRIu64 "] ", static_cast<uint64_t>(ts.tv_sec), static_cast<uint64_t>(ts.tv_nsec / 1000));

#if defined(__APPLE__) && defined(__MACH__)
uint64_t ktid;
pthread_threadid_np(nullptr, &ktid);
printf("[%lld:%lld] ", static_cast<long long>(getpid()), static_cast<long long>(ktid));
#elif defined(__GLIBC__)
// TODO: change to getpid() and gettid() after glib upgrade
printf("[%lld:%lld] ", static_cast<long long>(syscall(SYS_getpid)), static_cast<long long>(syscall(SYS_gettid)));
#endif

printf("CHIP:%s: ", module);
vprintf(msg, v);
printf("\n");
printf("\033[0m\n");

funlockfile(stdout);
}

} // namespace Platform
Expand Down
72 changes: 0 additions & 72 deletions src/platform/logging/impl/stdio/darwin/Logging.cpp

This file was deleted.

0 comments on commit 3b114a5

Please sign in to comment.