blob: 567335786e7a1f90b326455911988c8052875f22 [file] [log] [blame]
David 'Digit' Turnerebefc482009-05-29 14:45:04 +02001/*
2 * Copyright (C) 2009 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 _ANDROID_LOG_H
18#define _ANDROID_LOG_H
19
20/******************************************************************
21 *
22 * IMPORTANT NOTICE:
23 *
24 * This file is part of Android's set of stable system headers
25 * exposed by the Android NDK (Native Development Kit) since
26 * platform release 1.5
27 *
28 * Third-party source AND binary code relies on the definitions
29 * here to be FROZEN ON ALL UPCOMING PLATFORM RELEASES.
30 *
31 * - DO NOT MODIFY ENUMS (EXCEPT IF YOU ADD NEW 32-BIT VALUES)
32 * - DO NOT MODIFY CONSTANTS OR FUNCTIONAL MACROS
33 * - DO NOT CHANGE THE SIGNATURE OF FUNCTIONS IN ANY WAY
34 * - DO NOT CHANGE THE LAYOUT OR SIZE OF STRUCTURES
35 */
36
37/*
38 * Support routines to send messages to the Android in-kernel log buffer,
39 * which can later be accessed through the 'logcat' utility.
40 *
41 * Each log message must have
42 * - a priority
43 * - a log tag
44 * - some text
45 *
46 * The tag normally corresponds to the component that emits the log message,
47 * and should be reasonably small.
48 *
49 * Log message text may be truncated to less than an implementation-specific
50 * limit (e.g. 1023 characters max).
51 *
52 * Note that a newline character ("\n") will be appended automatically to your
53 * log message, if not already there. It is not possible to send several messages
54 * and have them appear on a single line in logcat.
55 *
56 * PLEASE USE LOGS WITH MODERATION:
57 *
58 * - Sending log messages eats CPU and slow down your application and the
59 * system.
60 *
61 * - The circular log buffer is pretty small (<64KB), sending many messages
62 * might push off other important log messages from the rest of the system.
63 *
64 * - In release builds, only send log messages to account for exceptional
65 * conditions.
66 *
67 * NOTE: These functions MUST be implemented by /system/lib/liblog.so
68 */
69
70#include <stdarg.h>
71
72#ifdef __cplusplus
73extern "C" {
74#endif
75
76/*
Mark Salyzyn749a2982016-10-11 07:34:52 -070077 * This file uses ", ## __VA_ARGS__" zero-argument token pasting to
78 * work around issues with debug-only syntax errors in assertions
79 * that are missing format strings. See commit
80 * 19299904343daf191267564fe32e6cd5c165cd42
81 */
82#if defined(__clang__)
83#pragma clang diagnostic push
84#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
85#endif
86
87#ifndef __predict_false
88#define __predict_false(exp) __builtin_expect((exp) != 0, 0)
89#endif
90
91/*
92 * LOG_TAG is the local tag used for the following simplified
93 * logging macros. You must set this preprocessor definition,
94 * or more tenuously supply a variable definition, before using
95 * the macros.
96 */
97
98/*
99 * Normally we strip the effects of ALOGV (VERBOSE messages),
100 * LOG_FATAL and LOG_FATAL_IF (FATAL assert messages) from the
101 * release builds be defining NDEBUG. You can modify this (for
102 * example with "#define LOG_NDEBUG 0" at the top of your source
103 * file) to change that behavior.
104 */
105
106#ifndef LOG_NDEBUG
107#ifdef NDEBUG
108#define LOG_NDEBUG 1
109#else
110#define LOG_NDEBUG 0
111#endif
112#endif
113
114/*
David 'Digit' Turnerebefc482009-05-29 14:45:04 +0200115 * Android log priority values, in ascending priority order.
116 */
Mark Salyzyn749a2982016-10-11 07:34:52 -0700117#ifndef __android_LogPriority_defined
118#define __android_LogPriority_defined
David 'Digit' Turnerebefc482009-05-29 14:45:04 +0200119typedef enum android_LogPriority {
120 ANDROID_LOG_UNKNOWN = 0,
121 ANDROID_LOG_DEFAULT, /* only for SetMinPriority() */
122 ANDROID_LOG_VERBOSE,
123 ANDROID_LOG_DEBUG,
124 ANDROID_LOG_INFO,
125 ANDROID_LOG_WARN,
126 ANDROID_LOG_ERROR,
127 ANDROID_LOG_FATAL,
128 ANDROID_LOG_SILENT, /* only for SetMinPriority(); must be last */
129} android_LogPriority;
Mark Salyzyn749a2982016-10-11 07:34:52 -0700130#endif
Mark Salyzyndf7a4c62016-08-23 10:23:36 -0700131
132/*
David 'Digit' Turnerebefc482009-05-29 14:45:04 +0200133 * Send a simple string to the log.
134 */
Mark Salyzyn749a2982016-10-11 07:34:52 -0700135int __android_log_write(int prio, const char* tag, const char* text);
136
137#define android_writeLog(prio, tag, text) \
138 __android_log_write(prio, tag, text)
David 'Digit' Turnerebefc482009-05-29 14:45:04 +0200139
140/*
141 * Send a formatted string to the log, used like printf(fmt,...)
142 */
Mark Salyzyn749a2982016-10-11 07:34:52 -0700143int __android_log_print(int prio, const char* tag, const char* fmt, ...)
David 'Digit' Turnerebefc482009-05-29 14:45:04 +0200144#if defined(__GNUC__)
Dan Willemsen0cddcc62014-06-11 15:36:58 -0700145#ifdef __USE_MINGW_ANSI_STDIO
Andrew Hsiehad832852014-05-09 11:50:45 +0800146#if __USE_MINGW_ANSI_STDIO
147 __attribute__ ((format(gnu_printf, 3, 4)))
148#else
David 'Digit' Turnerebefc482009-05-29 14:45:04 +0200149 __attribute__ ((format(printf, 3, 4)))
150#endif
Dan Willemsen0cddcc62014-06-11 15:36:58 -0700151#else
152 __attribute__ ((format(printf, 3, 4)))
153#endif
Andrew Hsiehad832852014-05-09 11:50:45 +0800154#endif
David 'Digit' Turnerebefc482009-05-29 14:45:04 +0200155 ;
156
Mark Salyzyn749a2982016-10-11 07:34:52 -0700157#define android_printLog(prio, tag, ...) \
158 __android_log_print(prio, tag, __VA_ARGS__)
159
160/*
161 * Log macro that allows you to specify a number for the priority.
162 */
163#ifndef LOG_PRI
164#define LOG_PRI(priority, tag, ...) \
165 android_printLog(priority, tag, __VA_ARGS__)
166#endif
167
David 'Digit' Turnerebefc482009-05-29 14:45:04 +0200168/*
169 * A variant of __android_log_print() that takes a va_list to list
170 * additional parameters.
171 */
Mark Salyzyn749a2982016-10-11 07:34:52 -0700172int __android_log_vprint(int prio, const char* tag,
173 const char* fmt, va_list ap)
174#if defined(__GNUC__)
175#ifdef __USE_MINGW_ANSI_STDIO
176#if __USE_MINGW_ANSI_STDIO
177 __attribute__ ((format(gnu_printf, 3, 0)))
178#else
179 __attribute__ ((format(printf, 3, 0)))
180#endif
181#else
182 __attribute__ ((format(printf, 3, 0)))
183#endif
184#endif
185 ;
186
187#define android_vprintLog(prio, cond, tag, ...) \
188 __android_log_vprint(prio, tag, __VA_ARGS__)
189
190/*
191 * Log macro that allows you to pass in a varargs ("args" is a va_list).
192 */
193#ifndef LOG_PRI_VA
194#define LOG_PRI_VA(priority, tag, fmt, args) \
195 android_vprintLog(priority, NULL, tag, fmt, args)
196#endif
David 'Digit' Turnerebefc482009-05-29 14:45:04 +0200197
198/*
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700199 * Log an assertion failure and abort the process to have a chance
200 * to inspect it if a debugger is attached. This uses the FATAL priority.
David 'Digit' Turnerebefc482009-05-29 14:45:04 +0200201 */
Mark Salyzyn749a2982016-10-11 07:34:52 -0700202void __android_log_assert(const char* cond, const char* tag,
203 const char* fmt, ...)
David 'Digit' Turnerebefc482009-05-29 14:45:04 +0200204#if defined(__GNUC__)
Elliott Hughes665051c2016-06-20 17:21:59 -0700205 __attribute__ ((__noreturn__))
Dan Willemsen0cddcc62014-06-11 15:36:58 -0700206#ifdef __USE_MINGW_ANSI_STDIO
Andrew Hsiehad832852014-05-09 11:50:45 +0800207#if __USE_MINGW_ANSI_STDIO
208 __attribute__ ((format(gnu_printf, 3, 4)))
209#else
David 'Digit' Turnerebefc482009-05-29 14:45:04 +0200210 __attribute__ ((format(printf, 3, 4)))
211#endif
Dan Willemsen0cddcc62014-06-11 15:36:58 -0700212#else
213 __attribute__ ((format(printf, 3, 4)))
214#endif
Andrew Hsiehad832852014-05-09 11:50:45 +0800215#endif
David 'Digit' Turnerebefc482009-05-29 14:45:04 +0200216 ;
217
Mark Salyzyn749a2982016-10-11 07:34:52 -0700218/* XXX Macros to work around syntax errors in places where format string
219 * arg is not passed to ALOG_ASSERT, LOG_ALWAYS_FATAL or LOG_ALWAYS_FATAL_IF
220 * (happens only in debug builds).
221 */
Mark Salyzyn6584d0a2016-09-28 13:26:55 -0700222
Mark Salyzyn749a2982016-10-11 07:34:52 -0700223/* Returns 2nd arg. Used to substitute default value if caller's vararg list
224 * is empty.
225 */
226#define __android_second(dummy, second, ...) second
Mark Salyzyn6584d0a2016-09-28 13:26:55 -0700227
Mark Salyzyn749a2982016-10-11 07:34:52 -0700228/* If passed multiple args, returns ',' followed by all but 1st arg, otherwise
229 * returns nothing.
230 */
231#define __android_rest(first, ...) , ## __VA_ARGS__
Mark Salyzyn6584d0a2016-09-28 13:26:55 -0700232
Mark Salyzyn749a2982016-10-11 07:34:52 -0700233#define android_printAssert(cond, tag, ...) \
234 __android_log_assert(cond, tag, \
235 __android_second(0, ## __VA_ARGS__, NULL) __android_rest(__VA_ARGS__))
Mark Salyzyn6584d0a2016-09-28 13:26:55 -0700236
237/*
Mark Salyzyn749a2982016-10-11 07:34:52 -0700238 * Log a fatal error. If the given condition fails, this stops program
239 * execution like a normal assertion, but also generating the given message.
240 * It is NOT stripped from release builds. Note that the condition test
241 * is -inverted- from the normal assert() semantics.
Mark Salyzyn6584d0a2016-09-28 13:26:55 -0700242 */
Mark Salyzyn749a2982016-10-11 07:34:52 -0700243#ifndef LOG_ALWAYS_FATAL_IF
244#define LOG_ALWAYS_FATAL_IF(cond, ...) \
245 ( (__predict_false(cond)) \
246 ? ((void)android_printAssert(#cond, LOG_TAG, ## __VA_ARGS__)) \
247 : (void)0 )
248#endif
249
250#ifndef LOG_ALWAYS_FATAL
251#define LOG_ALWAYS_FATAL(...) \
252 ( ((void)android_printAssert(NULL, LOG_TAG, ## __VA_ARGS__)) )
253#endif
254
255/*
256 * Versions of LOG_ALWAYS_FATAL_IF and LOG_ALWAYS_FATAL that
257 * are stripped out of release builds.
258 */
259
260#if LOG_NDEBUG
261
262#ifndef LOG_FATAL_IF
263#define LOG_FATAL_IF(cond, ...) ((void)0)
264#endif
265#ifndef LOG_FATAL
266#define LOG_FATAL(...) ((void)0)
267#endif
268
Mark Salyzyn6584d0a2016-09-28 13:26:55 -0700269#else
Mark Salyzyn749a2982016-10-11 07:34:52 -0700270
271#ifndef LOG_FATAL_IF
272#define LOG_FATAL_IF(cond, ...) LOG_ALWAYS_FATAL_IF(cond, ## __VA_ARGS__)
Mark Salyzyn6584d0a2016-09-28 13:26:55 -0700273#endif
Mark Salyzyn749a2982016-10-11 07:34:52 -0700274#ifndef LOG_FATAL
275#define LOG_FATAL(...) LOG_ALWAYS_FATAL(__VA_ARGS__)
276#endif
277
Mark Salyzyn6584d0a2016-09-28 13:26:55 -0700278#endif
279
280/*
Mark Salyzyn749a2982016-10-11 07:34:52 -0700281 * Assertion that generates a log message when the assertion fails.
282 * Stripped out of release builds. Uses the current LOG_TAG.
Mark Salyzyn6584d0a2016-09-28 13:26:55 -0700283 */
Mark Salyzyn749a2982016-10-11 07:34:52 -0700284#ifndef ALOG_ASSERT
285#define ALOG_ASSERT(cond, ...) LOG_FATAL_IF(!(cond), ## __VA_ARGS__)
Mark Salyzyn6584d0a2016-09-28 13:26:55 -0700286#endif
287
Mark Salyzyn749a2982016-10-11 07:34:52 -0700288/* --------------------------------------------------------------------- */
Mark Salyzyn6584d0a2016-09-28 13:26:55 -0700289
290/*
Mark Salyzyn749a2982016-10-11 07:34:52 -0700291 * C/C++ logging functions. See the logging documentation for API details.
292 *
293 * We'd like these to be available from C code (in case we import some from
294 * somewhere), so this has a C interface.
295 *
296 * The output will be correct when the log file is shared between multiple
297 * threads and/or multiple processes so long as the operating system
298 * supports O_APPEND. These calls have mutex-protected data structures
299 * and so are NOT reentrant. Do not use LOG in a signal handler.
Mark Salyzyn6584d0a2016-09-28 13:26:55 -0700300 */
Mark Salyzyn749a2982016-10-11 07:34:52 -0700301
302/* --------------------------------------------------------------------- */
Mark Salyzyn6584d0a2016-09-28 13:26:55 -0700303
304/*
305 * Simplified macro to send a verbose log message using the current LOG_TAG.
306 */
307#ifndef ALOGV
308#define __ALOGV(...) ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
309#if LOG_NDEBUG
310#define ALOGV(...) do { if (0) { __ALOGV(__VA_ARGS__); } } while (0)
311#else
312#define ALOGV(...) __ALOGV(__VA_ARGS__)
313#endif
314#endif
315
316#ifndef ALOGV_IF
317#if LOG_NDEBUG
318#define ALOGV_IF(cond, ...) ((void)0)
319#else
320#define ALOGV_IF(cond, ...) \
321 ( (__predict_false(cond)) \
322 ? ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
323 : (void)0 )
324#endif
325#endif
326
327/*
328 * Simplified macro to send a debug log message using the current LOG_TAG.
329 */
330#ifndef ALOGD
331#define ALOGD(...) ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__))
332#endif
333
334#ifndef ALOGD_IF
335#define ALOGD_IF(cond, ...) \
336 ( (__predict_false(cond)) \
337 ? ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
338 : (void)0 )
339#endif
340
341/*
342 * Simplified macro to send an info log message using the current LOG_TAG.
343 */
344#ifndef ALOGI
345#define ALOGI(...) ((void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__))
346#endif
347
348#ifndef ALOGI_IF
349#define ALOGI_IF(cond, ...) \
350 ( (__predict_false(cond)) \
351 ? ((void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__)) \
352 : (void)0 )
353#endif
354
355/*
356 * Simplified macro to send a warning log message using the current LOG_TAG.
357 */
358#ifndef ALOGW
359#define ALOGW(...) ((void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__))
360#endif
361
362#ifndef ALOGW_IF
363#define ALOGW_IF(cond, ...) \
364 ( (__predict_false(cond)) \
365 ? ((void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__)) \
366 : (void)0 )
367#endif
368
369/*
370 * Simplified macro to send an error log message using the current LOG_TAG.
371 */
372#ifndef ALOGE
373#define ALOGE(...) ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__))
374#endif
375
376#ifndef ALOGE_IF
377#define ALOGE_IF(cond, ...) \
378 ( (__predict_false(cond)) \
379 ? ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
380 : (void)0 )
381#endif
382
Mark Salyzyn749a2982016-10-11 07:34:52 -0700383/* --------------------------------------------------------------------- */
Mark Salyzyn6584d0a2016-09-28 13:26:55 -0700384
385/*
386 * Conditional based on whether the current LOG_TAG is enabled at
387 * verbose priority.
388 */
389#ifndef IF_ALOGV
390#if LOG_NDEBUG
391#define IF_ALOGV() if (false)
392#else
393#define IF_ALOGV() IF_ALOG(LOG_VERBOSE, LOG_TAG)
394#endif
395#endif
396
397/*
398 * Conditional based on whether the current LOG_TAG is enabled at
399 * debug priority.
400 */
401#ifndef IF_ALOGD
402#define IF_ALOGD() IF_ALOG(LOG_DEBUG, LOG_TAG)
403#endif
404
405/*
406 * Conditional based on whether the current LOG_TAG is enabled at
407 * info priority.
408 */
409#ifndef IF_ALOGI
410#define IF_ALOGI() IF_ALOG(LOG_INFO, LOG_TAG)
411#endif
412
413/*
414 * Conditional based on whether the current LOG_TAG is enabled at
415 * warn priority.
416 */
417#ifndef IF_ALOGW
418#define IF_ALOGW() IF_ALOG(LOG_WARN, LOG_TAG)
419#endif
420
421/*
422 * Conditional based on whether the current LOG_TAG is enabled at
423 * error priority.
424 */
425#ifndef IF_ALOGE
426#define IF_ALOGE() IF_ALOG(LOG_ERROR, LOG_TAG)
427#endif
428
Mark Salyzyn749a2982016-10-11 07:34:52 -0700429/* --------------------------------------------------------------------- */
Mark Salyzyn6584d0a2016-09-28 13:26:55 -0700430
431/*
432 * Basic log message macro.
433 *
434 * Example:
435 * ALOG(LOG_WARN, NULL, "Failed with error %d", errno);
436 *
437 * The second argument may be NULL or "" to indicate the "global" tag.
438 */
439#ifndef ALOG
440#define ALOG(priority, tag, ...) \
441 LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__)
442#endif
443
444/*
Mark Salyzyn6584d0a2016-09-28 13:26:55 -0700445 * Conditional given a desired logging priority and tag.
446 */
447#ifndef IF_ALOG
448#define IF_ALOG(priority, tag) \
449 if (android_testLog(ANDROID_##priority, tag))
450#endif
451
Mark Salyzyn749a2982016-10-11 07:34:52 -0700452/* --------------------------------------------------------------------- */
Mark Salyzyn6584d0a2016-09-28 13:26:55 -0700453
454/*
455 * IF_ALOG uses android_testLog, but IF_ALOG can be overridden.
456 * android_testLog will remain constant in its purpose as a wrapper
457 * for Android logging filter policy, and can be subject to
458 * change. It can be reused by the developers that override
459 * IF_ALOG as a convenient means to reimplement their policy
460 * over Android.
461 */
Mark Salyzyn749a2982016-10-11 07:34:52 -0700462
463#ifndef __ANDROID_USE_LIBLOG_LOGGABLE_INTERFACE
464#ifndef __ANDROID_API__
465#define __ANDROID_USE_LIBLOG_LOGGABLE_INTERFACE 2
466#elif __ANDROID_API__ > 24 /* > Nougat */
467#define __ANDROID_USE_LIBLOG_LOGGABLE_INTERFACE 2
468#elif __ANDROID_API__ > 22 /* > Lollipop */
469#define __ANDROID_USE_LIBLOG_LOGGABLE_INTERFACE 1
470#else
471#define __ANDROID_USE_LIBLOG_LOGGABLE_INTERFACE 0
472#endif
473#endif
474
475#if __ANDROID_USE_LIBLOG_LOGGABLE_INTERFACE
476
477/*
478 * Use the per-tag properties "log.tag.<tagname>" to generate a runtime
479 * result of non-zero to expose a log. prio is ANDROID_LOG_VERBOSE to
480 * ANDROID_LOG_FATAL. default_prio if no property. Undefined behavior if
481 * any other value.
482 */
483int __android_log_is_loggable(int prio, const char* tag, int default_prio);
484
485#if __ANDROID_USE_LIBLOG_LOGGABLE_INTERFACE > 1
486#include <sys/types.h>
487
488int __android_log_is_loggable_len(int prio, const char* tag, size_t len,
489 int default_prio);
490
Mark Salyzyn6584d0a2016-09-28 13:26:55 -0700491#if LOG_NDEBUG /* Production */
492#define android_testLog(prio, tag) \
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700493 (__android_log_is_loggable_len(prio, tag, (tag && *tag) ? strlen(tag) : 0, \
494 ANDROID_LOG_DEBUG) != 0)
Mark Salyzyn6584d0a2016-09-28 13:26:55 -0700495#else
496#define android_testLog(prio, tag) \
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700497 (__android_log_is_loggable_len(prio, tag, (tag && *tag) ? strlen(tag) : 0, \
498 ANDROID_LOG_VERBOSE) != 0)
Mark Salyzyn6584d0a2016-09-28 13:26:55 -0700499#endif
500
Mark Salyzyn749a2982016-10-11 07:34:52 -0700501#else
Mark Salyzyn6584d0a2016-09-28 13:26:55 -0700502
Mark Salyzyn749a2982016-10-11 07:34:52 -0700503#if LOG_NDEBUG /* Production */
504#define android_testLog(prio, tag) \
505 (__android_log_is_loggable(prio, tag, ANDROID_LOG_DEBUG) != 0)
506#else
507#define android_testLog(prio, tag) \
508 (__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE) != 0)
Mark Salyzyn6584d0a2016-09-28 13:26:55 -0700509#endif
Mark Salyzyn749a2982016-10-11 07:34:52 -0700510
511#endif
512
513#else /* __ANDROID_USE_LIBLOG_LOGGABLE_INTERFACE */
514
515#define android_testLog(prio, tag) (1)
516
517#endif /* !__ANDROID_USE_LIBLOG_LOGGABLE_INTERFACE */
Mark Salyzyn6584d0a2016-09-28 13:26:55 -0700518
519#if defined(__clang__)
520#pragma clang diagnostic pop
521#endif
522
David 'Digit' Turnerebefc482009-05-29 14:45:04 +0200523#ifdef __cplusplus
524}
525#endif
526
527#endif /* _ANDROID_LOG_H */