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