/src/connectedhomeip/src/platform/logging/impl/Stdio.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * |
3 | | * Copyright (c) 2021-2024 Project CHIP Authors |
4 | | * |
5 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
6 | | * you may not use this file except in compliance with the License. |
7 | | * You may obtain a copy of the License at |
8 | | * |
9 | | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | | * |
11 | | * Unless required by applicable law or agreed to in writing, software |
12 | | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | | * See the License for the specific language governing permissions and |
15 | | * limitations under the License. |
16 | | */ |
17 | | |
18 | | #include <platform/logging/LogV.h> |
19 | | |
20 | | #include <lib/support/logging/Constants.h> |
21 | | |
22 | | #include <inttypes.h> |
23 | | #include <stdio.h> |
24 | | #include <time.h> |
25 | | |
26 | | #if defined(__APPLE__) |
27 | | #include <platform/Darwin/DispatchQueueNames.h> |
28 | | #include <pthread.h> |
29 | | #include <unistd.h> |
30 | | #elif defined(__gnu_linux__) |
31 | | #include <sys/syscall.h> |
32 | | #include <unistd.h> |
33 | | #endif |
34 | | |
35 | | namespace chip { |
36 | | namespace Logging { |
37 | | namespace Platform { |
38 | | |
39 | | // TODO: investigate whether pw_chrono or pw_log could be used here |
40 | | void LogV(const char * module, uint8_t category, const char * msg, va_list v) |
41 | 22.7k | { |
42 | | // Stdout needs to be locked, because it's printed in pieces |
43 | 22.7k | #if defined(_POSIX_THREAD_SAFE_FUNCTIONS) |
44 | 22.7k | flockfile(stdout); |
45 | 22.7k | #endif |
46 | | |
47 | 22.7k | switch (category) |
48 | 22.7k | { |
49 | 7.57k | case kLogCategory_Error: |
50 | 7.57k | printf("\033[1;31m"); |
51 | 7.57k | break; |
52 | 6.52k | case kLogCategory_Progress: |
53 | 6.52k | printf("\033[0;32m"); |
54 | 6.52k | break; |
55 | 8.64k | case kLogCategory_Detail: |
56 | 8.64k | printf("\033[0;34m"); |
57 | 8.64k | break; |
58 | 22.7k | } |
59 | | |
60 | 22.7k | #if defined(__APPLE__) || defined(__gnu_linux__) |
61 | 22.7k | timespec ts; |
62 | 22.7k | timespec_get(&ts, TIME_UTC); |
63 | 22.7k | printf("[%lld.%03ld] ", static_cast<long long>(ts.tv_sec), static_cast<long>(ts.tv_nsec / 1000000)); |
64 | 22.7k | #endif |
65 | | |
66 | | #if defined(__APPLE__) |
67 | | uint64_t ktid; |
68 | | pthread_threadid_np(nullptr, &ktid); |
69 | | const char * label = darwin::queues::CurrentLabel(); |
70 | | printf("[%lld:%lld:%s] ", static_cast<long long>(getpid()), static_cast<long long>(ktid), label); |
71 | | #elif defined(__gnu_linux__) && !defined(__NuttX__) |
72 | | // TODO: change to getpid() and gettid() after glib upgrade |
73 | 22.7k | printf("[%lld:%lld] ", static_cast<long long>(syscall(SYS_getpid)), static_cast<long long>(syscall(SYS_gettid))); |
74 | 22.7k | #endif |
75 | | |
76 | 22.7k | printf("[%s] ", module); |
77 | 22.7k | vprintf(msg, v); |
78 | 22.7k | printf("\033[0m\n"); |
79 | 22.7k | fflush(stdout); |
80 | | |
81 | 22.7k | #if defined(_POSIX_THREAD_SAFE_FUNCTIONS) |
82 | 22.7k | funlockfile(stdout); |
83 | 22.7k | #endif |
84 | 22.7k | } |
85 | | |
86 | | } // namespace Platform |
87 | | } // namespace Logging |
88 | | } // namespace chip |