1 /*
2 * Copyright (C) 2005-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 #pragma once
18
19 #include <stdbool.h>
20 #include <sys/cdefs.h>
21 #include <sys/types.h>
22
23 #include <android/log.h>
24
25 __BEGIN_DECLS
26
27 /*
28 * Normally we strip the effects of ALOGV (VERBOSE messages),
29 * LOG_FATAL and LOG_FATAL_IF (FATAL assert messages) from the
30 * release builds be defining NDEBUG. You can modify this (for
31 * example with "#define LOG_NDEBUG 0" at the top of your source
32 * file) to change that behavior.
33 */
34
35 #ifndef LOG_NDEBUG
36 #ifdef NDEBUG
37 #define LOG_NDEBUG 1
38 #else
39 #define LOG_NDEBUG 0
40 #endif
41 #endif
42
43 /* --------------------------------------------------------------------- */
44
45 /*
46 * This file uses ", ## __VA_ARGS__" zero-argument token pasting to
47 * work around issues with debug-only syntax errors in assertions
48 * that are missing format strings. See commit
49 * 19299904343daf191267564fe32e6cd5c165cd42
50 */
51 #if defined(__clang__)
52 #pragma clang diagnostic push
53 #pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
54 #endif
55
56 /*
57 * Use __VA_ARGS__ if running a static analyzer,
58 * to avoid warnings of unused variables in __VA_ARGS__.
59 * Use contexpr function in C++ mode, so these macros can be used
60 * in other constexpr functions without warning.
61 */
62 #ifdef __clang_analyzer__
63 #ifdef __cplusplus
64 extern "C++" {
65 template <typename... Ts>
__fake_use_va_args(Ts...)66 constexpr int __fake_use_va_args(Ts...) {
67 return 0;
68 }
69 }
70 #else
71 extern int __fake_use_va_args(int, ...);
72 #endif /* __cplusplus */
73 #define __FAKE_USE_VA_ARGS(...) ((void)__fake_use_va_args(0, ##__VA_ARGS__))
74 #else
75 #define __FAKE_USE_VA_ARGS(...) ((void)(0))
76 #endif /* __clang_analyzer__ */
77
78 #ifndef __predict_false
79 #define __predict_false(exp) __builtin_expect((exp) != 0, 0)
80 #endif
81
82 #define android_writeLog(prio, tag, text) __android_log_write(prio, tag, text)
83
84 #define android_printLog(prio, tag, ...) \
85 __android_log_print(prio, tag, __VA_ARGS__)
86
87 #define android_vprintLog(prio, cond, tag, ...) \
88 __android_log_vprint(prio, tag, __VA_ARGS__)
89
90 /*
91 * Log macro that allows you to specify a number for the priority.
92 */
93 #ifndef LOG_PRI
94 #define LOG_PRI(priority, tag, ...) android_printLog(priority, tag, __VA_ARGS__)
95 #endif
96
97 /*
98 * Log macro that allows you to pass in a varargs ("args" is a va_list).
99 */
100 #ifndef LOG_PRI_VA
101 #define LOG_PRI_VA(priority, tag, fmt, args) \
102 android_vprintLog(priority, NULL, tag, fmt, args)
103 #endif
104
105 /* --------------------------------------------------------------------- */
106
107 /* XXX Macros to work around syntax errors in places where format string
108 * arg is not passed to ALOG_ASSERT, LOG_ALWAYS_FATAL or LOG_ALWAYS_FATAL_IF
109 * (happens only in debug builds).
110 */
111
112 /* Returns 2nd arg. Used to substitute default value if caller's vararg list
113 * is empty.
114 */
115 #define __android_second(dummy, second, ...) second
116
117 /* If passed multiple args, returns ',' followed by all but 1st arg, otherwise
118 * returns nothing.
119 */
120 #define __android_rest(first, ...) , ##__VA_ARGS__
121
122 #define android_printAssert(cond, tag, ...) \
123 __android_log_assert(cond, tag, \
124 __android_second(0, ##__VA_ARGS__, NULL) \
125 __android_rest(__VA_ARGS__))
126
127 /*
128 * Log a fatal error. If the given condition fails, this stops program
129 * execution like a normal assertion, but also generating the given message.
130 * It is NOT stripped from release builds. Note that the condition test
131 * is -inverted- from the normal assert() semantics.
132 */
133 #ifndef LOG_ALWAYS_FATAL_IF
134 #define LOG_ALWAYS_FATAL_IF(cond, ...) \
135 ((__predict_false(cond)) \
136 ? ((void)android_printAssert(#cond, LOG_TAG, ##__VA_ARGS__)) \
137 : __FAKE_USE_VA_ARGS(__VA_ARGS__))
138 #endif
139
140 #ifndef LOG_ALWAYS_FATAL
141 #define LOG_ALWAYS_FATAL(...) \
142 (((void)android_printAssert(NULL, LOG_TAG, ##__VA_ARGS__)))
143 #endif
144
145 /*
146 * Versions of LOG_ALWAYS_FATAL_IF and LOG_ALWAYS_FATAL that
147 * are stripped out of release builds.
148 */
149
150 #if LOG_NDEBUG
151
152 #ifndef LOG_FATAL_IF
153 #define LOG_FATAL_IF(cond, ...) __FAKE_USE_VA_ARGS(__VA_ARGS__)
154 #endif
155 #ifndef LOG_FATAL
156 #define LOG_FATAL(...) __FAKE_USE_VA_ARGS(__VA_ARGS__)
157 #endif
158
159 #else
160
161 #ifndef LOG_FATAL_IF
162 #define LOG_FATAL_IF(cond, ...) LOG_ALWAYS_FATAL_IF(cond, ##__VA_ARGS__)
163 #endif
164 #ifndef LOG_FATAL
165 #define LOG_FATAL(...) LOG_ALWAYS_FATAL(__VA_ARGS__)
166 #endif
167
168 #endif
169
170 /*
171 * Assertion that generates a log message when the assertion fails.
172 * Stripped out of release builds. Uses the current LOG_TAG.
173 */
174 #ifndef ALOG_ASSERT
175 #define ALOG_ASSERT(cond, ...) LOG_FATAL_IF(!(cond), ##__VA_ARGS__)
176 #endif
177
178 /* --------------------------------------------------------------------- */
179
180 /*
181 * C/C++ logging functions. See the logging documentation for API details.
182 *
183 * We'd like these to be available from C code (in case we import some from
184 * somewhere), so this has a C interface.
185 *
186 * The output will be correct when the log file is shared between multiple
187 * threads and/or multiple processes so long as the operating system
188 * supports O_APPEND. These calls have mutex-protected data structures
189 * and so are NOT reentrant. Do not use LOG in a signal handler.
190 */
191
192 /* --------------------------------------------------------------------- */
193
194 /*
195 * Simplified macro to send a verbose log message using the current LOG_TAG.
196 */
197 #ifndef ALOGV
198 #define __ALOGV(...) ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
199 #if LOG_NDEBUG
200 #define ALOGV(...) \
201 do { \
202 __FAKE_USE_VA_ARGS(__VA_ARGS__); \
203 if (false) { \
204 __ALOGV(__VA_ARGS__); \
205 } \
206 } while (false)
207 #else
208 #define ALOGV(...) __ALOGV(__VA_ARGS__)
209 #endif
210 #endif
211
212 #ifndef ALOGV_IF
213 #if LOG_NDEBUG
214 #define ALOGV_IF(cond, ...) __FAKE_USE_VA_ARGS(__VA_ARGS__)
215 #else
216 #define ALOGV_IF(cond, ...) \
217 ((__predict_false(cond)) ? ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
218 : __FAKE_USE_VA_ARGS(__VA_ARGS__))
219 #endif
220 #endif
221
222 /*
223 * Simplified macro to send a debug log message using the current LOG_TAG.
224 */
225 #ifndef ALOGD
226 #define ALOGD(...) ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__))
227 #endif
228
229 #ifndef ALOGD_IF
230 #define ALOGD_IF(cond, ...) \
231 ((__predict_false(cond)) ? ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
232 : __FAKE_USE_VA_ARGS(__VA_ARGS__))
233 #endif
234
235 /*
236 * Simplified macro to send an info log message using the current LOG_TAG.
237 */
238 #ifndef ALOGI
239 #define ALOGI(...) ((void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__))
240 #endif
241
242 #ifndef ALOGI_IF
243 #define ALOGI_IF(cond, ...) \
244 ((__predict_false(cond)) ? ((void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__)) \
245 : __FAKE_USE_VA_ARGS(__VA_ARGS__))
246 #endif
247
248 /*
249 * Simplified macro to send a warning log message using the current LOG_TAG.
250 */
251 #ifndef ALOGW
252 #define ALOGW(...) ((void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__))
253 #endif
254
255 #ifndef ALOGW_IF
256 #define ALOGW_IF(cond, ...) \
257 ((__predict_false(cond)) ? ((void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__)) \
258 : __FAKE_USE_VA_ARGS(__VA_ARGS__))
259 #endif
260
261 /*
262 * Simplified macro to send an error log message using the current LOG_TAG.
263 */
264 #ifndef ALOGE
265 #define ALOGE(...) ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__))
266 #endif
267
268 #ifndef ALOGE_IF
269 #define ALOGE_IF(cond, ...) \
270 ((__predict_false(cond)) ? ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
271 : __FAKE_USE_VA_ARGS(__VA_ARGS__))
272 #endif
273
274 /* --------------------------------------------------------------------- */
275
276 /*
277 * Conditional based on whether the current LOG_TAG is enabled at
278 * verbose priority.
279 */
280 #ifndef IF_ALOGV
281 #if LOG_NDEBUG
282 #define IF_ALOGV() if (false)
283 #else
284 #define IF_ALOGV() IF_ALOG(LOG_VERBOSE, LOG_TAG)
285 #endif
286 #endif
287
288 /*
289 * Conditional based on whether the current LOG_TAG is enabled at
290 * debug priority.
291 */
292 #ifndef IF_ALOGD
293 #define IF_ALOGD() IF_ALOG(LOG_DEBUG, LOG_TAG)
294 #endif
295
296 /*
297 * Conditional based on whether the current LOG_TAG is enabled at
298 * info priority.
299 */
300 #ifndef IF_ALOGI
301 #define IF_ALOGI() IF_ALOG(LOG_INFO, LOG_TAG)
302 #endif
303
304 /*
305 * Conditional based on whether the current LOG_TAG is enabled at
306 * warn priority.
307 */
308 #ifndef IF_ALOGW
309 #define IF_ALOGW() IF_ALOG(LOG_WARN, LOG_TAG)
310 #endif
311
312 /*
313 * Conditional based on whether the current LOG_TAG is enabled at
314 * error priority.
315 */
316 #ifndef IF_ALOGE
317 #define IF_ALOGE() IF_ALOG(LOG_ERROR, LOG_TAG)
318 #endif
319
320 /* --------------------------------------------------------------------- */
321
322 /*
323 * Basic log message macro.
324 *
325 * Example:
326 * ALOG(LOG_WARN, NULL, "Failed with error %d", errno);
327 *
328 * The second argument may be NULL or "" to indicate the "global" tag.
329 */
330 #ifndef ALOG
331 #define ALOG(priority, tag, ...) LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__)
332 #endif
333
334 /*
335 * Conditional given a desired logging priority and tag.
336 */
337 #ifndef IF_ALOG
338 #define IF_ALOG(priority, tag) if (android_testLog(ANDROID_##priority, tag))
339 #endif
340
341 /* --------------------------------------------------------------------- */
342
343 /*
344 * IF_ALOG uses android_testLog, but IF_ALOG can be overridden.
345 * android_testLog will remain constant in its purpose as a wrapper
346 * for Android logging filter policy, and can be subject to
347 * change. It can be reused by the developers that override
348 * IF_ALOG as a convenient means to reimplement their policy
349 * over Android.
350 */
351
352 /*
353 * Use the per-tag properties "log.tag.<tagname>" to generate a runtime
354 * result of non-zero to expose a log. prio is ANDROID_LOG_VERBOSE to
355 * ANDROID_LOG_FATAL. default_prio if no property. Undefined behavior if
356 * any other value.
357 */
358 int __android_log_is_loggable(int prio, const char* tag, int default_prio);
359 int __android_log_is_loggable_len(int prio, const char* tag, size_t len, int default_prio);
360
361 #if LOG_NDEBUG /* Production */
362 #define android_testLog(prio, tag) \
363 (__android_log_is_loggable_len(prio, tag, ((tag) && *(tag)) ? strlen(tag) : 0, \
364 ANDROID_LOG_DEBUG) != 0)
365 #else
366 #define android_testLog(prio, tag) \
367 (__android_log_is_loggable_len(prio, tag, ((tag) && *(tag)) ? strlen(tag) : 0, \
368 ANDROID_LOG_VERBOSE) != 0)
369 #endif
370
371 #if defined(__clang__)
372 #pragma clang diagnostic pop
373 #endif
374
375 __END_DECLS
376