summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMark Salyzyn2017-01-09 14:44:13 -0600
committerMark Salyzyn2017-01-25 17:41:43 -0600
commit5c967da341c9395a4c378c149f326505224ec66e (patch)
tree0e1de2d192d55de6300d99269441882ccbb0b2bd /liblog/include/log/log_main.h
parent01678a0c84e1f94420630709fe7aea7b4357f019 (diff)
downloadplatform-system-core-5c967da341c9395a4c378c149f326505224ec66e.tar.gz
platform-system-core-5c967da341c9395a4c378c149f326505224ec66e.tar.xz
platform-system-core-5c967da341c9395a4c378c149f326505224ec66e.zip
ndk: reverse course on android/log.h
move LOG macros to log/log_main.h move include/android/log.h to liblog/include/android/log.h Test: compile of all components and gTest liblog-unit-tests Bug: 34250038 Bug: 30465923 Change-Id: If182dd9b83689e8b7bc1a44b2f5d913c7ee5eeee
Diffstat (limited to 'liblog/include/log/log_main.h')
-rw-r--r--liblog/include/log/log_main.h395
1 files changed, 395 insertions, 0 deletions
diff --git a/liblog/include/log/log_main.h b/liblog/include/log/log_main.h
new file mode 100644
index 000000000..f45397ab8
--- /dev/null
+++ b/liblog/include/log/log_main.h
@@ -0,0 +1,395 @@
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
22#ifdef __cplusplus
23extern "C" {
24#endif
25
26/*
27 * Normally we strip the effects of ALOGV (VERBOSE messages),
28 * LOG_FATAL and LOG_FATAL_IF (FATAL assert messages) from the
29 * release builds be defining NDEBUG. You can modify this (for
30 * example with "#define LOG_NDEBUG 0" at the top of your source
31 * file) to change that behavior.
32 */
33
34#ifndef LOG_NDEBUG
35#ifdef NDEBUG
36#define LOG_NDEBUG 1
37#else
38#define LOG_NDEBUG 0
39#endif
40#endif
41
42/* --------------------------------------------------------------------- */
43
44/*
45 * This file uses ", ## __VA_ARGS__" zero-argument token pasting to
46 * work around issues with debug-only syntax errors in assertions
47 * that are missing format strings. See commit
48 * 19299904343daf191267564fe32e6cd5c165cd42
49 */
50#if defined(__clang__)
51#pragma clang diagnostic push
52#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
53#endif
54
55#ifndef __predict_false
56#define __predict_false(exp) __builtin_expect((exp) != 0, 0)
57#endif
58
59#define android_writeLog(prio, tag, text) \
60 __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, ...) \
73 android_printLog(priority, tag, __VA_ARGS__)
74#endif
75
76/*
77 * Log macro that allows you to pass in a varargs ("args" is a va_list).
78 */
79#ifndef LOG_PRI_VA
80#define LOG_PRI_VA(priority, tag, fmt, args) \
81 android_vprintLog(priority, NULL, tag, fmt, args)
82#endif
83
84/* --------------------------------------------------------------------- */
85
86/* XXX Macros to work around syntax errors in places where format string
87 * arg is not passed to ALOG_ASSERT, LOG_ALWAYS_FATAL or LOG_ALWAYS_FATAL_IF
88 * (happens only in debug builds).
89 */
90
91/* Returns 2nd arg. Used to substitute default value if caller's vararg list
92 * is empty.
93 */
94#define __android_second(dummy, second, ...) second
95
96/* If passed multiple args, returns ',' followed by all but 1st arg, otherwise
97 * returns nothing.
98 */
99#define __android_rest(first, ...) , ## __VA_ARGS__
100
101#define android_printAssert(cond, tag, ...) \
102 __android_log_assert(cond, tag, \
103 __android_second(0, ## __VA_ARGS__, NULL) __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(...) do { if (0) { __ALOGV(__VA_ARGS__); } } while (0)
179#else
180#define ALOGV(...) __ALOGV(__VA_ARGS__)
181#endif
182#endif
183
184#ifndef ALOGV_IF
185#if LOG_NDEBUG
186#define ALOGV_IF(cond, ...) ((void)0)
187#else
188#define ALOGV_IF(cond, ...) \
189 ( (__predict_false(cond)) \
190 ? ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
191 : (void)0 )
192#endif
193#endif
194
195/*
196 * Simplified macro to send a debug log message using the current LOG_TAG.
197 */
198#ifndef ALOGD
199#define ALOGD(...) ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__))
200#endif
201
202#ifndef ALOGD_IF
203#define ALOGD_IF(cond, ...) \
204 ( (__predict_false(cond)) \
205 ? ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
206 : (void)0 )
207#endif
208
209/*
210 * Simplified macro to send an info log message using the current LOG_TAG.
211 */
212#ifndef ALOGI
213#define ALOGI(...) ((void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__))
214#endif
215
216#ifndef ALOGI_IF
217#define ALOGI_IF(cond, ...) \
218 ( (__predict_false(cond)) \
219 ? ((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)) \
233 ? ((void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__)) \
234 : (void)0 )
235#endif
236
237/*
238 * Simplified macro to send an error log message using the current LOG_TAG.
239 */
240#ifndef ALOGE
241#define ALOGE(...) ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__))
242#endif
243
244#ifndef ALOGE_IF
245#define ALOGE_IF(cond, ...) \
246 ( (__predict_false(cond)) \
247 ? ((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, ...) \
309 LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__)
310#endif
311
312/*
313 * Conditional given a desired logging priority and tag.
314 */
315#ifndef IF_ALOG
316#define IF_ALOG(priority, tag) \
317 if (android_testLog(ANDROID_##priority, tag))
318#endif
319
320/* --------------------------------------------------------------------- */
321
322/*
323 * IF_ALOG uses android_testLog, but IF_ALOG can be overridden.
324 * android_testLog will remain constant in its purpose as a wrapper
325 * for Android logging filter policy, and can be subject to
326 * change. It can be reused by the developers that override
327 * IF_ALOG as a convenient means to reimplement their policy
328 * over Android.
329 */
330
331#ifndef __ANDROID_USE_LIBLOG_LOGGABLE_INTERFACE
332#ifndef __ANDROID_API__
333#define __ANDROID_USE_LIBLOG_LOGGABLE_INTERFACE 2
334#elif __ANDROID_API__ > 24 /* > Nougat */
335#define __ANDROID_USE_LIBLOG_LOGGABLE_INTERFACE 2
336#elif __ANDROID_API__ > 22 /* > Lollipop */
337#define __ANDROID_USE_LIBLOG_LOGGABLE_INTERFACE 1
338#else
339#define __ANDROID_USE_LIBLOG_LOGGABLE_INTERFACE 0
340#endif
341#endif
342
343#if __ANDROID_USE_LIBLOG_LOGGABLE_INTERFACE
344
345/*
346 * Use the per-tag properties "log.tag.<tagname>" to generate a runtime
347 * result of non-zero to expose a log. prio is ANDROID_LOG_VERBOSE to
348 * ANDROID_LOG_FATAL. default_prio if no property. Undefined behavior if
349 * any other value.
350 */
351int __android_log_is_loggable(int prio, const char* tag, int default_prio);
352
353#if __ANDROID_USE_LIBLOG_LOGGABLE_INTERFACE > 1
354#include <sys/types.h>
355
356int __android_log_is_loggable_len(int prio, const char* tag, size_t len,
357 int default_prio);
358
359#if LOG_NDEBUG /* Production */
360#define android_testLog(prio, tag) \
361 (__android_log_is_loggable_len(prio, tag, (tag && *tag) ? strlen(tag) : 0, \
362 ANDROID_LOG_DEBUG) != 0)
363#else
364#define android_testLog(prio, tag) \
365 (__android_log_is_loggable_len(prio, tag, (tag && *tag) ? strlen(tag) : 0, \
366 ANDROID_LOG_VERBOSE) != 0)
367#endif
368
369#else
370
371#if LOG_NDEBUG /* Production */
372#define android_testLog(prio, tag) \
373 (__android_log_is_loggable(prio, tag, ANDROID_LOG_DEBUG) != 0)
374#else
375#define android_testLog(prio, tag) \
376 (__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE) != 0)
377#endif
378
379#endif
380
381#else /* __ANDROID_USE_LIBLOG_LOGGABLE_INTERFACE */
382
383#define android_testLog(prio, tag) (1)
384
385#endif /* !__ANDROID_USE_LIBLOG_LOGGABLE_INTERFACE */
386
387#if defined(__clang__)
388#pragma clang diagnostic pop
389#endif
390
391#ifdef __cplusplus
392}
393#endif
394
395#endif /* _LIBS_LOG_LOG_MAIN_H */