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 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 #ifndef __FILENAME__
29 #define __FILENAME__ __FILE__
30 #endif
31 
32 #ifndef PRIuSIZE
33 #define PRIuSIZE "zu"
34 #endif
35 
36 // TODO: Should use PRIu8 etc. from inttypes.h instead of %d, etc. (add -Wall
37 // and -Werror to cflags to catch these)
38 #define CHPP_LINUX_LOG(level, color, fmt, ...)                                 \
39   {                                                                            \
40     char name[16];                                                             \
41     pthread_getname_np(pthread_self(), name, 16);                              \
42     printf("\e[" color "m%s %s:%d\t (%s) " fmt "\e[0m\n", level, __FILENAME__, \
43            __LINE__, name, ##__VA_ARGS__);                                     \
44   }
45 
46 #define CHPP_LOGE(fmt, ...) CHPP_LINUX_LOG("E", "91", fmt, ##__VA_ARGS__)
47 #define CHPP_LOGW(fmt, ...) CHPP_LINUX_LOG("W", "93", fmt, ##__VA_ARGS__)
48 #define CHPP_LOGI(fmt, ...) CHPP_LINUX_LOG("I", "96", fmt, ##__VA_ARGS__)
49 #define CHPP_LOGD(fmt, ...) CHPP_LINUX_LOG("D", "97", fmt, ##__VA_ARGS__)
50 
51 #ifdef __cplusplus
52 }
53 #endif
54 
55 #endif  // CHPP_PLATFORM_LOG_H_
56