xref: /aosp_15_r20/system/chre/chpp/platform/linux/include/chpp/platform/platform_log.h (revision 84e339476a462649f82315436d70fd732297a399)
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef CHPP_PLATFORM_LOG_H_
18 #define CHPP_PLATFORM_LOG_H_
19 
20 #include <inttypes.h>
21 #include <pthread.h>
22 #include <stdio.h>
23 
24 #include "chpp/platform/platform_time.h"
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 #ifndef __FILENAME__
31 #define __FILENAME__ __FILE__
32 #endif
33 
34 #ifndef PRIuSIZE
35 #define PRIuSIZE "zu"
36 #endif
37 
38 // TODO: Should use PRIu8 etc. from inttypes.h instead of %d, etc. (add -Wall
39 // and -Werror to cflags to catch these)
40 #define CHPP_LINUX_LOG(level, color, fmt, ...)                             \
41   {                                                                        \
42     char name[16];                                                         \
43     uint64_t currentTimeMs = chppGetCurrentTimeNs() / 1000000;             \
44     uint64_t sec = currentTimeMs / 1000;                                   \
45     uint64_t msec = currentTimeMs % 1000;                                  \
46     pthread_getname_np(pthread_self(), name, 16);                          \
47     printf("\e[" color "m[%" PRIu64 ".%03" PRIu64 "] %s %s:%d\t (%s) " fmt \
48            "\e[0m\n",                                                      \
49            sec, msec, level, __FILENAME__, __LINE__, name, ##__VA_ARGS__); \
50   }
51 
52 #define CHPP_LOGE(fmt, ...) CHPP_LINUX_LOG("E", "91", fmt, ##__VA_ARGS__)
53 #define CHPP_LOGW(fmt, ...) CHPP_LINUX_LOG("W", "93", fmt, ##__VA_ARGS__)
54 #define CHPP_LOGI(fmt, ...) CHPP_LINUX_LOG("I", "96", fmt, ##__VA_ARGS__)
55 #define CHPP_LOGD(fmt, ...) CHPP_LINUX_LOG("D", "97", fmt, ##__VA_ARGS__)
56 
57 #ifdef __cplusplus
58 }
59 #endif
60 
61 #endif  // CHPP_PLATFORM_LOG_H_
62