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 ms = chppGetCurrentTimeNs() / 1000000;                         \
44     pthread_getname_np(pthread_self(), name, 16);                           \
45     printf("\e[" color "m[%" PRIu64 "] %s %s:%d\t (%s) " fmt "\e[0m\n", ms, \
46            level, __FILENAME__, __LINE__, name, ##__VA_ARGS__);             \
47   }
48 
49 #define CHPP_LOGE(fmt, ...) CHPP_LINUX_LOG("E", "91", fmt, ##__VA_ARGS__)
50 #define CHPP_LOGW(fmt, ...) CHPP_LINUX_LOG("W", "93", fmt, ##__VA_ARGS__)
51 #define CHPP_LOGI(fmt, ...) CHPP_LINUX_LOG("I", "96", fmt, ##__VA_ARGS__)
52 #define CHPP_LOGD(fmt, ...) CHPP_LINUX_LOG("D", "97", fmt, ##__VA_ARGS__)
53 
54 #ifdef __cplusplus
55 }
56 #endif
57 
58 #endif  // CHPP_PLATFORM_LOG_H_
59