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_CUTILS_LOG_H
29 #define _LIBS_CUTILS_LOG_H
33 #include <sys/types.h>
40 #include <cutils/uio.h>
41 #include <cutils/logd.h>
47 // ---------------------------------------------------------------------
50 * Normally we strip ALOGV (VERBOSE messages) from release builds.
51 * You can modify this (for example with "#define LOG_NDEBUG 0"
52 * at the top of your source file) to change that behavior.
63 * This is the local tag used for the following simplified
64 * logging macros. You can change this preprocessor definition
65 * before using the other macros to change the tag.
71 // ---------------------------------------------------------------------
74 * Simplified macro to send a verbose log message using the current LOG_TAG.
78 #define ALOGV(...) ((void)0)
80 #define ALOGV(...) ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
84 #define CONDITION(cond) (__builtin_expect((cond)!=0, 0))
88 #define ALOGV_IF(cond, ...) ((void)0)
90 #define ALOGV_IF(cond, ...) \
92 ? ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
98 * Simplified macro to send a debug log message using the current LOG_TAG.
101 #define ALOGD(...) ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__))
105 #define ALOGD_IF(cond, ...) \
106 ( (CONDITION(cond)) \
107 ? ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
112 * Simplified macro to send an info log message using the current LOG_TAG.
115 #define ALOGI(...) ((void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__))
119 #define ALOGI_IF(cond, ...) \
120 ( (CONDITION(cond)) \
121 ? ((void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__)) \
126 * Simplified macro to send a warning log message using the current LOG_TAG.
129 #define ALOGW(...) ((void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__))
133 #define ALOGW_IF(cond, ...) \
134 ( (CONDITION(cond)) \
135 ? ((void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__)) \
140 * Simplified macro to send an error log message using the current LOG_TAG.
143 #define ALOGE(...) ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__))
147 #define ALOGE_IF(cond, ...) \
148 ( (CONDITION(cond)) \
149 ? ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
153 // ---------------------------------------------------------------------
156 * Conditional based on whether the current LOG_TAG is enabled at
161 #define IF_ALOGV() if (false)
163 #define IF_ALOGV() IF_ALOG(LOG_VERBOSE, LOG_TAG)
168 * Conditional based on whether the current LOG_TAG is enabled at
172 #define IF_ALOGD() IF_ALOG(LOG_DEBUG, LOG_TAG)
176 * Conditional based on whether the current LOG_TAG is enabled at
180 #define IF_ALOGI() IF_ALOG(LOG_INFO, LOG_TAG)
184 * Conditional based on whether the current LOG_TAG is enabled at
188 #define IF_ALOGW() IF_ALOG(LOG_WARN, LOG_TAG)
192 * Conditional based on whether the current LOG_TAG is enabled at
196 #define IF_ALOGE() IF_ALOG(LOG_ERROR, LOG_TAG)
200 // ---------------------------------------------------------------------
203 * Simplified macro to send a verbose system log message using the current LOG_TAG.
207 #define SLOGV(...) ((void)0)
209 #define SLOGV(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
213 #define CONDITION(cond) (__builtin_expect((cond)!=0, 0))
217 #define SLOGV_IF(cond, ...) ((void)0)
219 #define SLOGV_IF(cond, ...) \
220 ( (CONDITION(cond)) \
221 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
227 * Simplified macro to send a debug system log message using the current LOG_TAG.
230 #define SLOGD(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
234 #define SLOGD_IF(cond, ...) \
235 ( (CONDITION(cond)) \
236 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
241 * Simplified macro to send an info system log message using the current LOG_TAG.
244 #define SLOGI(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__))
248 #define SLOGI_IF(cond, ...) \
249 ( (CONDITION(cond)) \
250 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)) \
255 * Simplified macro to send a warning system log message using the current LOG_TAG.
258 #define SLOGW(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__))
262 #define SLOGW_IF(cond, ...) \
263 ( (CONDITION(cond)) \
264 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)) \
269 * Simplified macro to send an error system log message using the current LOG_TAG.
272 #define SLOGE(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
276 #define SLOGE_IF(cond, ...) \
277 ( (CONDITION(cond)) \
278 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
284 // ---------------------------------------------------------------------
287 * Log a fatal error. If the given condition fails, this stops program
288 * execution like a normal assertion, but also generating the given message.
289 * It is NOT stripped from release builds. Note that the condition test
290 * is -inverted- from the normal assert() semantics.
292 #ifndef LOG_ALWAYS_FATAL_IF
293 #define LOG_ALWAYS_FATAL_IF(cond, ...) \
294 ( (CONDITION(cond)) \
295 ? ((void)android_printAssert(#cond, LOG_TAG, ## __VA_ARGS__)) \
299 #ifndef LOG_ALWAYS_FATAL
300 #define LOG_ALWAYS_FATAL(...) \
301 ( ((void)android_printAssert(NULL, LOG_TAG, ## __VA_ARGS__)) )
305 * Versions of LOG_ALWAYS_FATAL_IF and LOG_ALWAYS_FATAL that
306 * are stripped out of release builds.
311 #define LOG_FATAL_IF(cond, ...) ((void)0)
314 #define LOG_FATAL(...) ((void)0)
320 #define LOG_FATAL_IF(cond, ...) LOG_ALWAYS_FATAL_IF(cond, ## __VA_ARGS__)
323 #define LOG_FATAL(...) LOG_ALWAYS_FATAL(__VA_ARGS__)
329 * Assertion that generates a log message when the assertion fails.
330 * Stripped out of release builds. Uses the current LOG_TAG.
333 #define ALOG_ASSERT(cond, ...) LOG_FATAL_IF(!(cond), ## __VA_ARGS__)
334 //#define ALOG_ASSERT(cond) LOG_FATAL_IF(!(cond), "Assertion failed: " #cond)
337 // ---------------------------------------------------------------------
340 * Basic log message macro.
343 * ALOG(LOG_WARN, NULL, "Failed with error %d", errno);
345 * The second argument may be NULL or "" to indicate the "global" tag.
348 #define ALOG(priority, tag, ...) \
349 LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__)
353 * Log macro that allows you to specify a number for the priority.
356 #define LOG_PRI(priority, tag, ...) \
357 android_printLog(priority, tag, __VA_ARGS__)
361 * Log macro that allows you to pass in a varargs ("args" is a va_list).
364 #define LOG_PRI_VA(priority, tag, fmt, args) \
365 android_vprintLog(priority, NULL, tag, fmt, args)
369 * Conditional given a desired logging priority and tag.
372 #define IF_ALOG(priority, tag) \
373 if (android_testLog(ANDROID_##priority, tag))
376 // ---------------------------------------------------------------------
383 * Event log entry types. These must match up with the declarations in
384 * java/android/android/util/EventLog.java.
389 EVENT_TYPE_STRING
= 2,
391 } AndroidEventLogType
;
394 #ifndef LOG_EVENT_INT
395 #define LOG_EVENT_INT(_tag, _value) { \
396 int intBuf = _value; \
397 (void) android_btWriteLog(_tag, EVENT_TYPE_INT, &intBuf, \
401 #ifndef LOG_EVENT_LONG
402 #define LOG_EVENT_LONG(_tag, _value) { \
403 long long longBuf = _value; \
404 (void) android_btWriteLog(_tag, EVENT_TYPE_LONG, &longBuf, \
408 #ifndef LOG_EVENT_STRING
409 #define LOG_EVENT_STRING(_tag, _value) \
410 ((void) 0) /* not implemented -- must combine len with string */
412 /* TODO: something for LIST */
415 * ===========================================================================
417 * The stuff in the rest of this file should not be used directly.
420 #define android_printLog(prio, tag, fmt...) \
421 __android_log_print(prio, tag, fmt)
423 #define android_vprintLog(prio, cond, tag, fmt...) \
424 __android_log_vprint(prio, tag, fmt)
426 /* XXX Macros to work around syntax errors in places where format string
427 * arg is not passed to ALOG_ASSERT, LOG_ALWAYS_FATAL or LOG_ALWAYS_FATAL_IF
428 * (happens only in debug builds).
431 /* Returns 2nd arg. Used to substitute default value if caller's vararg list
434 #define __android_second(dummy, second, ...) second
436 /* If passed multiple args, returns ',' followed by all but 1st arg, otherwise
439 #define __android_rest(first, ...) , ## __VA_ARGS__
441 #define android_printAssert(cond, tag, fmt...) \
442 __android_log_assert(cond, tag, \
443 __android_second(0, ## fmt, NULL) __android_rest(fmt))
445 #define android_writeLog(prio, tag, text) \
446 __android_log_write(prio, tag, text)
448 #define android_bWriteLog(tag, payload, len) \
449 __android_log_bwrite(tag, payload, len)
450 #define android_btWriteLog(tag, type, payload, len) \
451 __android_log_btwrite(tag, type, payload, len)
453 // TODO: remove these prototypes and their users
454 #define android_testLog(prio, tag) (1)
455 #define android_writevLog(vec,num) do{}while(0)
456 #define android_write1Log(str,len) do{}while (0)
457 #define android_setMinPriority(tag, prio) do{}while(0)
458 //#define android_logToCallback(func) do{}while(0)
459 #define android_logToFile(tag, file) (0)
460 #define android_logToFd(tag, fd) (0)
472 * Send a simple string to the log.
474 int __android_log_buf_write(int bufID
, int prio
, const char *tag
, const char *text
);
475 int __android_log_buf_print(int bufID
, int prio
, const char *tag
, const char *fmt
, ...);
482 #endif // _LIBS_CUTILS_LOG_H