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