xref: /aosp_15_r20/system/chre/host/common/include/chre_host/log.h (revision 84e339476a462649f82315436d70fd732297a399)
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 namespace android::chre {
27 
28 /**
29  * Logs a message to both logcat and stdout/stderr. Don't use this directly;
30  * prefer one of LOGE, LOGW, etc.
31  *
32  * @param level  android log level, e.g., ANDROID_LOG_ERROR
33  * @param stream output stream to print to, e.g., stdout
34  * @param format printf-style format string
35  * @param func   the function name included in the log, e.g., __func__
36  * @param line   line number included in the log
37  */
38 void outputHostLog(int priority, FILE *stream, const char *format,
39                    const char *func, unsigned int line, ...);
40 
41 }  // namespace android::chre
42 
43 #define LOGE(format, ...)                                                     \
44   ::android::chre::outputHostLog(ANDROID_LOG_ERROR, stderr, format, __func__, \
45                                  __LINE__, ##__VA_ARGS__)
46 #define LOGW(format, ...)                                                    \
47   ::android::chre::outputHostLog(ANDROID_LOG_WARN, stdout, format, __func__, \
48                                  __LINE__, ##__VA_ARGS__)
49 #define LOGI(format, ...)                                                    \
50   ::android::chre::outputHostLog(ANDROID_LOG_INFO, stdout, format, __func__, \
51                                  __LINE__, ##__VA_ARGS__)
52 #define LOGD(format, ...)                                                     \
53   ::android::chre::outputHostLog(ANDROID_LOG_DEBUG, stdout, format, __func__, \
54                                  __LINE__, ##__VA_ARGS__)
55 
56 #if LOG_NDEBUG
chreLogNull(const char *,...)57 __attribute__((format(printf, 1, 2))) inline void chreLogNull(
58     const char * /*fmt*/, ...) {}
59 
60 #define LOGV(format, ...) chreLogNull(format, ##__VA_ARGS__)
61 #else
62 #define LOGV(format, ...)                                             \
63   ::android::chre::outputHostLog(ANDROID_LOG_VERBOSE, stdout, format, \
64                                  __func__, __LINE__, ##__VA_ARGS__)
65 #endif
66 
67 /**
68  * Helper to log a library error with a human-readable version of the provided
69  * error code.
70  *
71  * @param message Error message string to log
72  * @param error_code Standard error code number (EINVAL, etc)
73  */
74 #define LOG_ERROR(message, error_code)                          \
75   do {                                                          \
76     char error_string[64];                                      \
77     strerror_r(error_code, error_string, sizeof(error_string)); \
78     LOGE("%s: %s (%d)\n", message, error_string, error_code);   \
79   } while (0)
80 
81 #endif  // CHRE_HOST_LOG_H_
82