|
4 | 4 |
|
5 | 5 | #include <lib/support/logging/Constants.h>
|
6 | 6 |
|
| 7 | +#include <inttypes.h> |
7 | 8 | #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 |
8 | 18 |
|
9 | 19 | namespace chip {
|
10 | 20 | namespace Logging {
|
11 | 21 | namespace Platform {
|
12 | 22 |
|
| 23 | +// TODO: investigate whether pw_chrono or pw_log could be used here |
13 | 24 | void LogV(const char * module, uint8_t category, const char * msg, va_list v)
|
14 | 25 | {
|
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); |
16 | 60 | vprintf(msg, v);
|
17 |
| - printf("\n"); |
| 61 | + printf("\033[0m\n"); |
| 62 | + |
| 63 | +#if defined(_POSIX_THREAD_SAFE_FUNCTIONS) |
| 64 | + funlockfile(stdout); |
| 65 | +#endif |
18 | 66 | }
|
19 | 67 |
|
20 | 68 | } // namespace Platform
|
|
0 commit comments