1 #ifndef __CUTILS_LOG_H__
2 #define __CUTILS_LOG_H__
3
4 #ifndef LOG_TAG
5 #define LOG_TAG nullptr
6 #endif
7
8 /*
9 * Normally we strip the effects of ALOGV (VERBOSE messages),
10 * LOG_FATAL and LOG_FATAL_IF (FATAL assert messages) from the
11 * release builds be defining NDEBUG. You can modify this (for
12 * example with "#define LOG_NDEBUG 0" at the top of your source
13 * file) to change that behavior.
14 */
15
16 #ifndef LOG_NDEBUG
17 #ifdef NDEBUG
18 #define LOG_NDEBUG 1
19 #else
20 #define LOG_NDEBUG 0
21 #endif
22 #endif
23
24 /*
25 * Use __VA_ARGS__ if running a static analyzer,
26 * to avoid warnings of unused variables in __VA_ARGS__.
27 * Use constexpr function in C++ mode, so these macros can be used
28 * in other constexpr functions without warning.
29 */
30 #ifdef __clang_analyzer__
31 #ifdef __cplusplus
32 extern "C++" {
33 template <typename... Ts>
__fake_use_va_args(Ts...)34 constexpr int __fake_use_va_args(Ts...) {
35 return 0;
36 }
37 }
38 #else
39 extern int __fake_use_va_args(int, ...);
40 #endif /* __cplusplus */
41 #define __FAKE_USE_VA_ARGS(...) ((void)__fake_use_va_args(0, ##__VA_ARGS__))
42 #else
43 #define __FAKE_USE_VA_ARGS(...) ((void)(0))
44 #endif /* __clang_analyzer__ */
45
46 enum {
47 ANDROID_LOG_UNKNOWN = 0,
48 ANDROID_LOG_DEFAULT,
49 ANDROID_LOG_VERBOSE,
50 ANDROID_LOG_DEBUG,
51 ANDROID_LOG_INFO,
52 ANDROID_LOG_WARN,
53 ANDROID_LOG_ERROR,
54 ANDROID_LOG_FATAL,
55 ANDROID_LOG_SILENT,
56 };
57
58 #define android_printLog(prio, tag, format, ...) \
59 __android_log_print(prio, tag, __FILE__, __LINE__, "[prio %d] " format, prio, ##__VA_ARGS__)
60
61 #define LOG_PRI(priority, tag, ...) android_printLog(priority, tag, __VA_ARGS__)
62 #define ALOG(priority, tag, ...) LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__)
63
64 #define __android_second(dummy, second, ...) second
65 #define __android_rest(first, ...) , ##__VA_ARGS__
66
67 #define android_printAssert(condition, tag, format, ...) \
68 __android_log_assert(condition, tag, __FILE__, __LINE__, "assert: condition: %s " format, \
69 condition, ##__VA_ARGS__)
70
71 #define LOG_ALWAYS_FATAL_IF(condition, ...) \
72 ((condition) \
73 ? ((void)android_printAssert(#condition, LOG_TAG, ##__VA_ARGS__)) \
74 : (void)0)
75
76 #define LOG_ALWAYS_FATAL(...) \
77 (((void)android_printAssert(NULL, LOG_TAG, ##__VA_ARGS__)))
78
79 /*
80 * Simplified macro to send a verbose log message using the current LOG_TAG.
81 */
82 #define __ALOGV(...) ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
83 #if LOG_NDEBUG
84 #define ALOGV(...) \
85 do { \
86 __FAKE_USE_VA_ARGS(__VA_ARGS__); \
87 if (false) { \
88 __ALOGV(__VA_ARGS__); \
89 } \
90 } while (false)
91 #else
92 #define ALOGV(...) __ALOGV(__VA_ARGS__)
93 #endif
94
95 #define ALOGE(...) ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__))
96 #define ALOGW(...) ((void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__))
97 #define ALOGI(...) ((void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__))
98 #define ALOGD(...) ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__))
99
100 #if LOG_NDEBUG
101
102 #define LOG_FATAL_IF(cond, ...) __FAKE_USE_VA_ARGS(__VA_ARGS__)
103
104 #define LOG_FATAL(...) __FAKE_USE_VA_ARGS(__VA_ARGS__)
105
106 #else
107
108 #define LOG_FATAL_IF(cond, ...) LOG_ALWAYS_FATAL_IF(cond, ##__VA_ARGS__)
109
110 #define LOG_FATAL(...) LOG_ALWAYS_FATAL(__VA_ARGS__)
111
112 #endif
113
114 #define ALOG_ASSERT(cond, ...) LOG_FATAL_IF(!(cond), ##__VA_ARGS__)
115
116 extern "C" {
117
118 int __android_log_print(int priority, const char* tag, const char* file, int line,
119 const char* format, ...);
120
121 [[noreturn]] void __android_log_assert(const char* condition, const char* tag, const char* file,
122 int line, const char* format, ...);
123 }
124
125 #endif
126