forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogging.cpp
70 lines (58 loc) · 1.76 KB
/
Logging.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* See Project CHIP LICENSE file for licensing information. */
#include <platform/logging/LogV.h>
#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(__gnu_linux__)
#include <sys/syscall.h>
#include <unistd.h>
#endif
namespace chip {
namespace Logging {
namespace Platform {
// TODO: investigate whether pw_chrono or pw_log could be used here
void LogV(const char * module, uint8_t category, const char * msg, va_list v)
{
// Stdout needs to be locked, because it's printed in pieces
#if defined(_POSIX_THREAD_SAFE_FUNCTIONS)
flockfile(stdout);
#endif
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;
}
#if defined(__APPLE__) || defined(__gnu_linux__)
timespec ts;
timespec_get(&ts, TIME_UTC);
printf("[%lld.%03ld] ", static_cast<long long>(ts.tv_sec), static_cast<long>(ts.tv_nsec / 1000000));
#endif
#if defined(__APPLE__)
uint64_t ktid;
pthread_threadid_np(nullptr, &ktid);
printf("[%lld:%lld] ", static_cast<long long>(getpid()), static_cast<long long>(ktid));
#elif defined(__gnu_linux__) && !defined(__NuttX__)
// 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("[%s] ", module);
vprintf(msg, v);
printf("\033[0m\n");
#if defined(_POSIX_THREAD_SAFE_FUNCTIONS)
funlockfile(stdout);
#endif
}
} // namespace Platform
} // namespace Logging
} // namespace chip