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