1 /*
2  * Copyright (C) 2017 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 CHRE_HOST_LOG_H_
18 #define CHRE_HOST_LOG_H_
19 
20 #ifndef LOG_TAG
21 #define LOG_TAG "CHRE"
22 #endif
23 
24 #include "chre/util/log_common.h"
25 
26 #include <log/log.h>
27 
28 /**
29  * Logs a message to both logcat and stdout. Don't use this directly; prefer one
30  * of LOGE, LOGW, etc. to populate the level.
31  *
32  * @param level log level to pass to ALOG (LOG_ERROR, LOG_WARN, etc.)
33  * @param format printf-style format string
34  */
35 #define CHRE_LOG(level, format, ...)                 \
36     do {                                             \
37         ALOG(level, LOG_TAG, format, ##__VA_ARGS__); \
38         printf("%s:%d : " format "\n", __func__, __LINE__, ##__VA_ARGS__); \
39     } while (0)
40 
41 #define LOGE(format, ...) CHRE_LOG(LOG_ERROR,   format, ##__VA_ARGS__)
42 #define LOGW(format, ...) CHRE_LOG(LOG_WARN,    format, ##__VA_ARGS__)
43 #define LOGI(format, ...) CHRE_LOG(LOG_INFO,    format, ##__VA_ARGS__)
44 #define LOGD(format, ...) CHRE_LOG(LOG_DEBUG,   format, ##__VA_ARGS__)
45 
46 #if LOG_NDEBUG
47 #define LOGV(format, ...) chreLogNull(format, ##__VA_ARGS__)
48 #else
49 #define LOGV(format, ...) CHRE_LOG(LOG_VERBOSE, format, ##__VA_ARGS__)
50 #endif
51 
52 /**
53  * Helper to log a library error with a human-readable version of the provided
54  * error code.
55  *
56  * @param message Error message string to log
57  * @param error_code Standard error code number (EINVAL, etc)
58  */
59 #define LOG_ERROR(message, error_code)                              \
60     do {                                                            \
61         char error_string[64];                                      \
62         strerror_r(error_code, error_string, sizeof(error_string)); \
63         LOGE("%s: %s (%d)\n", message, error_string, error_code);   \
64     } while (0)
65 
66 #endif  // CHRE_HOST_LOG_H_
67