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