2 * Copyright (C) 2005 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 // C/C++ logging functions. See the logging documentation for API details.
20 // We'd like these to be available from C code (in case we import some from
21 // somewhere), so this has a C interface.
23 // The output will be correct when the log file is shared between multiple
24 // threads and/or multiple processes so long as the operating system
25 // supports O_APPEND. These calls have mutex-protected data structures
26 // and so are NOT reentrant. Do not use LOG in a signal handler.
28 #ifndef _LIBS_LOG_LOG_H
29 #define _LIBS_LOG_LOG_H
33 #include <sys/types.h>
44 #include <android/log.h>
51 // ---------------------------------------------------------------------
54 * Normally we strip ALOGV (VERBOSE messages) from release builds.
55 * You can modify this (for example with "#define LOG_NDEBUG 0"
56 * at the top of your source file) to change that behavior.
67 * This is the local tag used for the following simplified
68 * logging macros. You can change this preprocessor definition
69 * before using the other macros to change the tag.
75 // ---------------------------------------------------------------------
78 * Simplified macro to send a verbose log message using the current LOG_TAG.
82 #define ALOGV(...) ((void)0)
84 #define ALOGV(...) ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
88 #define CONDITION(cond) (__builtin_expect((cond)!=0, 0))
92 #define ALOGV_IF(cond, ...) ((void)0)
94 #define ALOGV_IF(cond, ...) \
96 ? ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
102 * Simplified macro to send a debug log message using the current LOG_TAG.
105 #define ALOGD(...) ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__))
109 #define ALOGD_IF(cond, ...) \
110 ( (CONDITION(cond)) \
111 ? ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
116 * Simplified macro to send an info log message using the current LOG_TAG.
119 #define ALOGI(...) ((void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__))
123 #define ALOGI_IF(cond, ...) \
124 ( (CONDITION(cond)) \
125 ? ((void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__)) \
130 * Simplified macro to send a warning log message using the current LOG_TAG.
133 #define ALOGW(...) ((void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__))
137 #define ALOGW_IF(cond, ...) \
138 ( (CONDITION(cond)) \
139 ? ((void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__)) \
144 * Simplified macro to send an error log message using the current LOG_TAG.
147 #define ALOGE(...) ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__))
151 #define ALOGE_IF(cond, ...) \
152 ( (CONDITION(cond)) \
153 ? ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
157 // ---------------------------------------------------------------------
160 * Conditional based on whether the current LOG_TAG is enabled at
165 #define IF_ALOGV() if (false)
167 #define IF_ALOGV() IF_ALOG(LOG_VERBOSE, LOG_TAG)
172 * Conditional based on whether the current LOG_TAG is enabled at
176 #define IF_ALOGD() IF_ALOG(LOG_DEBUG, LOG_TAG)
180 * Conditional based on whether the current LOG_TAG is enabled at
184 #define IF_ALOGI() IF_ALOG(LOG_INFO, LOG_TAG)
188 * Conditional based on whether the current LOG_TAG is enabled at
192 #define IF_ALOGW() IF_ALOG(LOG_WARN, LOG_TAG)
196 * Conditional based on whether the current LOG_TAG is enabled at
200 #define IF_ALOGE() IF_ALOG(LOG_ERROR, LOG_TAG)
204 // ---------------------------------------------------------------------
207 * Simplified macro to send a verbose system log message using the current LOG_TAG.
211 #define SLOGV(...) ((void)0)
213 #define SLOGV(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
217 #define CONDITION(cond) (__builtin_expect((cond)!=0, 0))
221 #define SLOGV_IF(cond, ...) ((void)0)
223 #define SLOGV_IF(cond, ...) \
224 ( (CONDITION(cond)) \
225 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
231 * Simplified macro to send a debug system log message using the current LOG_TAG.
234 #define SLOGD(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
238 #define SLOGD_IF(cond, ...) \
239 ( (CONDITION(cond)) \
240 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
245 * Simplified macro to send an info system log message using the current LOG_TAG.
248 #define SLOGI(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__))
252 #define SLOGI_IF(cond, ...) \
253 ( (CONDITION(cond)) \
254 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)) \
259 * Simplified macro to send a warning system log message using the current LOG_TAG.
262 #define SLOGW(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__))
266 #define SLOGW_IF(cond, ...) \
267 ( (CONDITION(cond)) \
268 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)) \
273 * Simplified macro to send an error system log message using the current LOG_TAG.
276 #define SLOGE(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
280 #define SLOGE_IF(cond, ...) \
281 ( (CONDITION(cond)) \
282 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
286 // ---------------------------------------------------------------------
289 * Simplified macro to send a verbose radio log message using the current LOG_TAG.
293 #define RLOGV(...) ((void)0)
295 #define RLOGV(...) ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
299 #define CONDITION(cond) (__builtin_expect((cond)!=0, 0))
303 #define RLOGV_IF(cond, ...) ((void)0)
305 #define RLOGV_IF(cond, ...) \
306 ( (CONDITION(cond)) \
307 ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
313 * Simplified macro to send a debug radio log message using the current LOG_TAG.
316 #define RLOGD(...) ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
320 #define RLOGD_IF(cond, ...) \
321 ( (CONDITION(cond)) \
322 ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
327 * Simplified macro to send an info radio log message using the current LOG_TAG.
330 #define RLOGI(...) ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__))
334 #define RLOGI_IF(cond, ...) \
335 ( (CONDITION(cond)) \
336 ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)) \
341 * Simplified macro to send a warning radio log message using the current LOG_TAG.
344 #define RLOGW(...) ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__))
348 #define RLOGW_IF(cond, ...) \
349 ( (CONDITION(cond)) \
350 ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)) \
355 * Simplified macro to send an error radio log message using the current LOG_TAG.
358 #define RLOGE(...) ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
362 #define RLOGE_IF(cond, ...) \
363 ( (CONDITION(cond)) \
364 ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
369 // ---------------------------------------------------------------------
372 * Log a fatal error. If the given condition fails, this stops program
373 * execution like a normal assertion, but also generating the given message.
374 * It is NOT stripped from release builds. Note that the condition test
375 * is -inverted- from the normal assert() semantics.
377 #ifndef LOG_ALWAYS_FATAL_IF
378 #define LOG_ALWAYS_FATAL_IF(cond, ...) \
379 ( (CONDITION(cond)) \
380 ? ((void)android_printAssert(#cond, LOG_TAG, ## __VA_ARGS__)) \
384 #ifndef LOG_ALWAYS_FATAL
385 #define LOG_ALWAYS_FATAL(...) \
386 ( ((void)android_printAssert(NULL, LOG_TAG, ## __VA_ARGS__)) )
390 * Versions of LOG_ALWAYS_FATAL_IF and LOG_ALWAYS_FATAL that
391 * are stripped out of release builds.
396 #define LOG_FATAL_IF(cond, ...) ((void)0)
399 #define LOG_FATAL(...) ((void)0)
405 #define LOG_FATAL_IF(cond, ...) LOG_ALWAYS_FATAL_IF(cond, ## __VA_ARGS__)
408 #define LOG_FATAL(...) LOG_ALWAYS_FATAL(__VA_ARGS__)
414 * Assertion that generates a log message when the assertion fails.
415 * Stripped out of release builds. Uses the current LOG_TAG.
418 #define ALOG_ASSERT(cond, ...) LOG_FATAL_IF(!(cond), ## __VA_ARGS__)
419 //#define ALOG_ASSERT(cond) LOG_FATAL_IF(!(cond), "Assertion failed: " #cond)
422 // ---------------------------------------------------------------------
425 * Basic log message macro.
428 * ALOG(LOG_WARN, NULL, "Failed with error %d", errno);
430 * The second argument may be NULL or "" to indicate the "global" tag.
433 #define ALOG(priority, tag, ...) \
434 LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__)
438 * Log macro that allows you to specify a number for the priority.
441 #define LOG_PRI(priority, tag, ...) \
442 android_printLog(priority, tag, __VA_ARGS__)
446 * Log macro that allows you to pass in a varargs ("args" is a va_list).
449 #define LOG_PRI_VA(priority, tag, fmt, args) \
450 android_vprintLog(priority, NULL, tag, fmt, args)
454 * Conditional given a desired logging priority and tag.
457 #define IF_ALOG(priority, tag) \
458 if (android_testLog(ANDROID_##priority, tag))
461 // ---------------------------------------------------------------------
468 * Event log entry types. These must match up with the declarations in
469 * java/android/android/util/EventLog.java.
474 EVENT_TYPE_STRING
= 2,
476 } AndroidEventLogType
;
479 #ifndef LOG_EVENT_INT
480 #define LOG_EVENT_INT(_tag, _value) { \
481 int intBuf = _value; \
482 (void) android_btWriteLog(_tag, EVENT_TYPE_INT, &intBuf, \
486 #ifndef LOG_EVENT_LONG
487 #define LOG_EVENT_LONG(_tag, _value) { \
488 long long longBuf = _value; \
489 (void) android_btWriteLog(_tag, EVENT_TYPE_LONG, &longBuf, \
493 #ifndef LOG_EVENT_STRING
494 #define LOG_EVENT_STRING(_tag, _value) \
495 ((void) 0) /* not implemented -- must combine len with string */
497 /* TODO: something for LIST */
500 * ===========================================================================
502 * The stuff in the rest of this file should not be used directly.
505 #define android_printLog(prio, tag, fmt...) \
506 __android_log_print(prio, tag, fmt)
508 #define android_vprintLog(prio, cond, tag, fmt...) \
509 __android_log_vprint(prio, tag, fmt)
511 /* XXX Macros to work around syntax errors in places where format string
512 * arg is not passed to ALOG_ASSERT, LOG_ALWAYS_FATAL or LOG_ALWAYS_FATAL_IF
513 * (happens only in debug builds).
516 /* Returns 2nd arg. Used to substitute default value if caller's vararg list
519 #define __android_second(dummy, second, ...) second
521 /* If passed multiple args, returns ',' followed by all but 1st arg, otherwise
524 #define __android_rest(first, ...) , ## __VA_ARGS__
526 #define android_printAssert(cond, tag, fmt...) \
527 __android_log_assert(cond, tag, \
528 __android_second(0, ## fmt, NULL) __android_rest(fmt))
530 #define android_writeLog(prio, tag, text) \
531 __android_log_write(prio, tag, text)
533 #define android_bWriteLog(tag, payload, len) \
534 __android_log_bwrite(tag, payload, len)
535 #define android_btWriteLog(tag, type, payload, len) \
536 __android_log_btwrite(tag, type, payload, len)
538 // TODO: remove these prototypes and their users
539 #define android_testLog(prio, tag) (1)
540 #define android_writevLog(vec,num) do{}while(0)
541 #define android_write1Log(str,len) do{}while (0)
542 #define android_setMinPriority(tag, prio) do{}while(0)
543 //#define android_logToCallback(func) do{}while(0)
544 #define android_logToFile(tag, file) (0)
545 #define android_logToFd(tag, fd) (0)
557 * Send a simple string to the log.
559 int __android_log_buf_write(int bufID
, int prio
, const char *tag
, const char *text
);
560 int __android_log_buf_print(int bufID
, int prio
, const char *tag
, const char *fmt
, ...);
567 #endif // _LIBS_CUTILS_LOG_H