blob: 51239019210a0323e68b61e59e4de987ff6721f3 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2005 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//
18// C/C++ logging functions. See the logging documentation for API details.
19//
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.
22//
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.
27//
28#ifndef _LIBS_CUTILS_LOG_H
29#define _LIBS_CUTILS_LOG_H
30
31#include <stdio.h>
32#include <time.h>
33#include <sys/types.h>
34#include <unistd.h>
35#ifdef HAVE_PTHREADS
36#include <pthread.h>
37#endif
38#include <stdarg.h>
39
40#include <cutils/uio.h>
41#include <cutils/logd.h>
42
43#ifdef __cplusplus
44extern "C" {
45#endif
46
47// ---------------------------------------------------------------------
48
49/*
Steve Block69f4cd72011-10-20 11:54:09 +010050 * Normally we strip ALOGV (VERBOSE messages) from release builds.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051 * You can modify this (for example with "#define LOG_NDEBUG 0"
52 * at the top of your source file) to change that behavior.
53 */
54#ifndef LOG_NDEBUG
55#ifdef NDEBUG
56#define LOG_NDEBUG 1
57#else
58#define LOG_NDEBUG 0
59#endif
60#endif
61
62/*
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.
66 */
67#ifndef LOG_TAG
68#define LOG_TAG NULL
69#endif
70
71// ---------------------------------------------------------------------
72
73/*
74 * Simplified macro to send a verbose log message using the current LOG_TAG.
75 */
Steve Block69f4cd72011-10-20 11:54:09 +010076#ifndef ALOGV
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077#if LOG_NDEBUG
Steve Block69f4cd72011-10-20 11:54:09 +010078#define ALOGV(...) ((void)0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080079#else
Steve Block69f4cd72011-10-20 11:54:09 +010080#define ALOGV(...) ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080081#endif
82#endif
83
84#define CONDITION(cond) (__builtin_expect((cond)!=0, 0))
85
Steve Block69f4cd72011-10-20 11:54:09 +010086#ifndef ALOGV_IF
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087#if LOG_NDEBUG
Steve Block69f4cd72011-10-20 11:54:09 +010088#define ALOGV_IF(cond, ...) ((void)0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080089#else
Steve Block69f4cd72011-10-20 11:54:09 +010090#define ALOGV_IF(cond, ...) \
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080091 ( (CONDITION(cond)) \
Steve Block5fb44952011-10-12 17:22:43 +010092 ? ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080093 : (void)0 )
94#endif
95#endif
96
97/*
98 * Simplified macro to send a debug log message using the current LOG_TAG.
99 */
Steve Block8d66c492011-12-20 16:07:45 +0000100#ifndef ALOGD
101#define ALOGD(...) ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800102#endif
103
Steve Block8d66c492011-12-20 16:07:45 +0000104#ifndef ALOGD_IF
105#define ALOGD_IF(cond, ...) \
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800106 ( (CONDITION(cond)) \
Steve Block5fb44952011-10-12 17:22:43 +0100107 ? ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800108 : (void)0 )
109#endif
110
111/*
112 * Simplified macro to send an info log message using the current LOG_TAG.
113 */
Steve Blockfe71a612012-01-04 19:19:03 +0000114#ifndef ALOGI
115#define ALOGI(...) ((void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800116#endif
117
Steve Blockfe71a612012-01-04 19:19:03 +0000118#ifndef ALOGI_IF
119#define ALOGI_IF(cond, ...) \
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800120 ( (CONDITION(cond)) \
Steve Block5fb44952011-10-12 17:22:43 +0100121 ? ((void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__)) \
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800122 : (void)0 )
123#endif
124
125/*
126 * Simplified macro to send a warning log message using the current LOG_TAG.
127 */
Steve Blockae8b56c2012-01-05 22:25:38 +0000128#ifndef ALOGW
129#define ALOGW(...) ((void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130#endif
131
Steve Blockae8b56c2012-01-05 22:25:38 +0000132#ifndef ALOGW_IF
133#define ALOGW_IF(cond, ...) \
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800134 ( (CONDITION(cond)) \
Steve Block5fb44952011-10-12 17:22:43 +0100135 ? ((void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__)) \
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136 : (void)0 )
137#endif
138
139/*
140 * Simplified macro to send an error log message using the current LOG_TAG.
141 */
Steve Block01dda202012-01-06 14:13:42 +0000142#ifndef ALOGE
143#define ALOGE(...) ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800144#endif
145
Steve Block01dda202012-01-06 14:13:42 +0000146#ifndef ALOGE_IF
147#define ALOGE_IF(cond, ...) \
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800148 ( (CONDITION(cond)) \
Steve Block5fb44952011-10-12 17:22:43 +0100149 ? ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800150 : (void)0 )
151#endif
152
153// ---------------------------------------------------------------------
154
155/*
156 * Conditional based on whether the current LOG_TAG is enabled at
157 * verbose priority.
158 */
Steve Block69f4cd72011-10-20 11:54:09 +0100159#ifndef IF_ALOGV
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160#if LOG_NDEBUG
Steve Block69f4cd72011-10-20 11:54:09 +0100161#define IF_ALOGV() if (false)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800162#else
Steve Block69f4cd72011-10-20 11:54:09 +0100163#define IF_ALOGV() IF_ALOG(LOG_VERBOSE, LOG_TAG)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164#endif
165#endif
166
167/*
168 * Conditional based on whether the current LOG_TAG is enabled at
169 * debug priority.
170 */
Steve Block8d66c492011-12-20 16:07:45 +0000171#ifndef IF_ALOGD
172#define IF_ALOGD() IF_ALOG(LOG_DEBUG, LOG_TAG)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173#endif
174
175/*
176 * Conditional based on whether the current LOG_TAG is enabled at
177 * info priority.
178 */
Steve Blockfe71a612012-01-04 19:19:03 +0000179#ifndef IF_ALOGI
180#define IF_ALOGI() IF_ALOG(LOG_INFO, LOG_TAG)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800181#endif
182
183/*
184 * Conditional based on whether the current LOG_TAG is enabled at
185 * warn priority.
186 */
Steve Blockae8b56c2012-01-05 22:25:38 +0000187#ifndef IF_ALOGW
188#define IF_ALOGW() IF_ALOG(LOG_WARN, LOG_TAG)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189#endif
190
191/*
192 * Conditional based on whether the current LOG_TAG is enabled at
193 * error priority.
194 */
Steve Block01dda202012-01-06 14:13:42 +0000195#ifndef IF_ALOGE
196#define IF_ALOGE() IF_ALOG(LOG_ERROR, LOG_TAG)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800197#endif
198
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800199
200// ---------------------------------------------------------------------
201
202/*
203 * Simplified macro to send a verbose system log message using the current LOG_TAG.
204 */
205#ifndef SLOGV
206#if LOG_NDEBUG
207#define SLOGV(...) ((void)0)
208#else
209#define SLOGV(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
210#endif
211#endif
212
213#define CONDITION(cond) (__builtin_expect((cond)!=0, 0))
214
215#ifndef SLOGV_IF
216#if LOG_NDEBUG
217#define SLOGV_IF(cond, ...) ((void)0)
218#else
219#define SLOGV_IF(cond, ...) \
220 ( (CONDITION(cond)) \
221 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
222 : (void)0 )
223#endif
224#endif
225
226/*
227 * Simplified macro to send a debug system log message using the current LOG_TAG.
228 */
229#ifndef SLOGD
230#define SLOGD(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
231#endif
232
233#ifndef SLOGD_IF
234#define SLOGD_IF(cond, ...) \
235 ( (CONDITION(cond)) \
236 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
237 : (void)0 )
238#endif
239
240/*
241 * Simplified macro to send an info system log message using the current LOG_TAG.
242 */
243#ifndef SLOGI
244#define SLOGI(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__))
245#endif
246
247#ifndef SLOGI_IF
248#define SLOGI_IF(cond, ...) \
249 ( (CONDITION(cond)) \
250 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)) \
251 : (void)0 )
252#endif
253
254/*
255 * Simplified macro to send a warning system log message using the current LOG_TAG.
256 */
257#ifndef SLOGW
258#define SLOGW(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__))
259#endif
260
261#ifndef SLOGW_IF
262#define SLOGW_IF(cond, ...) \
263 ( (CONDITION(cond)) \
264 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)) \
265 : (void)0 )
266#endif
267
268/*
269 * Simplified macro to send an error system log message using the current LOG_TAG.
270 */
271#ifndef SLOGE
272#define SLOGE(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
273#endif
274
275#ifndef SLOGE_IF
276#define SLOGE_IF(cond, ...) \
277 ( (CONDITION(cond)) \
278 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
279 : (void)0 )
280#endif
281
282
283
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800284// ---------------------------------------------------------------------
285
286/*
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.
291 */
Alexandre Elias412514e2011-03-29 16:24:45 -0700292#ifndef LOG_ALWAYS_FATAL_IF
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800293#define LOG_ALWAYS_FATAL_IF(cond, ...) \
294 ( (CONDITION(cond)) \
Chris Pearson19299902010-06-02 16:25:35 -0700295 ? ((void)android_printAssert(#cond, LOG_TAG, ## __VA_ARGS__)) \
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800296 : (void)0 )
Alexandre Elias412514e2011-03-29 16:24:45 -0700297#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800298
Alexandre Elias412514e2011-03-29 16:24:45 -0700299#ifndef LOG_ALWAYS_FATAL
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800300#define LOG_ALWAYS_FATAL(...) \
Chris Pearson19299902010-06-02 16:25:35 -0700301 ( ((void)android_printAssert(NULL, LOG_TAG, ## __VA_ARGS__)) )
Alexandre Elias412514e2011-03-29 16:24:45 -0700302#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800303
304/*
305 * Versions of LOG_ALWAYS_FATAL_IF and LOG_ALWAYS_FATAL that
306 * are stripped out of release builds.
307 */
308#if LOG_NDEBUG
309
Alexandre Elias412514e2011-03-29 16:24:45 -0700310#ifndef LOG_FATAL_IF
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800311#define LOG_FATAL_IF(cond, ...) ((void)0)
Alexandre Elias412514e2011-03-29 16:24:45 -0700312#endif
313#ifndef LOG_FATAL
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800314#define LOG_FATAL(...) ((void)0)
Alexandre Elias412514e2011-03-29 16:24:45 -0700315#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800316
317#else
318
Alexandre Elias412514e2011-03-29 16:24:45 -0700319#ifndef LOG_FATAL_IF
Chris Pearson19299902010-06-02 16:25:35 -0700320#define LOG_FATAL_IF(cond, ...) LOG_ALWAYS_FATAL_IF(cond, ## __VA_ARGS__)
Alexandre Elias412514e2011-03-29 16:24:45 -0700321#endif
322#ifndef LOG_FATAL
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800323#define LOG_FATAL(...) LOG_ALWAYS_FATAL(__VA_ARGS__)
Alexandre Elias412514e2011-03-29 16:24:45 -0700324#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800325
326#endif
327
328/*
329 * Assertion that generates a log message when the assertion fails.
330 * Stripped out of release builds. Uses the current LOG_TAG.
331 */
Alexandre Elias412514e2011-03-29 16:24:45 -0700332#ifndef LOG_ASSERT
Chris Pearson19299902010-06-02 16:25:35 -0700333#define LOG_ASSERT(cond, ...) LOG_FATAL_IF(!(cond), ## __VA_ARGS__)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800334//#define LOG_ASSERT(cond) LOG_FATAL_IF(!(cond), "Assertion failed: " #cond)
Steve Blocka9b84a72012-01-09 22:50:36 +0000335#define ALOG_ASSERT LOG_ASSERT
Alexandre Elias412514e2011-03-29 16:24:45 -0700336#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800337
338// ---------------------------------------------------------------------
339
340/*
341 * Basic log message macro.
342 *
343 * Example:
Steve Block5fb44952011-10-12 17:22:43 +0100344 * ALOG(LOG_WARN, NULL, "Failed with error %d", errno);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800345 *
346 * The second argument may be NULL or "" to indicate the "global" tag.
347 */
Steve Block5fb44952011-10-12 17:22:43 +0100348#ifndef ALOG
349#define ALOG(priority, tag, ...) \
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800350 LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__)
351#endif
352
353/*
354 * Log macro that allows you to specify a number for the priority.
355 */
356#ifndef LOG_PRI
357#define LOG_PRI(priority, tag, ...) \
358 android_printLog(priority, tag, __VA_ARGS__)
359#endif
360
361/*
362 * Log macro that allows you to pass in a varargs ("args" is a va_list).
363 */
364#ifndef LOG_PRI_VA
365#define LOG_PRI_VA(priority, tag, fmt, args) \
366 android_vprintLog(priority, NULL, tag, fmt, args)
367#endif
368
369/*
370 * Conditional given a desired logging priority and tag.
371 */
Steve Block5fb44952011-10-12 17:22:43 +0100372#ifndef IF_ALOG
373#define IF_ALOG(priority, tag) \
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800374 if (android_testLog(ANDROID_##priority, tag))
375#endif
376
377// ---------------------------------------------------------------------
378
379/*
380 * Event logging.
381 */
382
383/*
384 * Event log entry types. These must match up with the declarations in
385 * java/android/android/util/EventLog.java.
386 */
387typedef enum {
388 EVENT_TYPE_INT = 0,
389 EVENT_TYPE_LONG = 1,
390 EVENT_TYPE_STRING = 2,
391 EVENT_TYPE_LIST = 3,
392} AndroidEventLogType;
393
394
Alexandre Elias412514e2011-03-29 16:24:45 -0700395#ifndef LOG_EVENT_INT
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800396#define LOG_EVENT_INT(_tag, _value) { \
397 int intBuf = _value; \
398 (void) android_btWriteLog(_tag, EVENT_TYPE_INT, &intBuf, \
399 sizeof(intBuf)); \
400 }
Alexandre Elias412514e2011-03-29 16:24:45 -0700401#endif
402#ifndef LOG_EVENT_LONG
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800403#define LOG_EVENT_LONG(_tag, _value) { \
404 long long longBuf = _value; \
405 (void) android_btWriteLog(_tag, EVENT_TYPE_LONG, &longBuf, \
406 sizeof(longBuf)); \
407 }
Alexandre Elias412514e2011-03-29 16:24:45 -0700408#endif
409#ifndef LOG_EVENT_STRING
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800410#define LOG_EVENT_STRING(_tag, _value) \
411 ((void) 0) /* not implemented -- must combine len with string */
Alexandre Elias412514e2011-03-29 16:24:45 -0700412#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800413/* TODO: something for LIST */
414
415/*
416 * ===========================================================================
417 *
418 * The stuff in the rest of this file should not be used directly.
419 */
420
421#define android_printLog(prio, tag, fmt...) \
422 __android_log_print(prio, tag, fmt)
423
424#define android_vprintLog(prio, cond, tag, fmt...) \
425 __android_log_vprint(prio, tag, fmt)
426
Chris Pearson19299902010-06-02 16:25:35 -0700427/* XXX Macros to work around syntax errors in places where format string
428 * arg is not passed to LOG_ASSERT, LOG_ALWAYS_FATAL or LOG_ALWAYS_FATAL_IF
429 * (happens only in debug builds).
430 */
431
432/* Returns 2nd arg. Used to substitute default value if caller's vararg list
433 * is empty.
434 */
435#define __android_second(dummy, second, ...) second
436
437/* If passed multiple args, returns ',' followed by all but 1st arg, otherwise
438 * returns nothing.
439 */
440#define __android_rest(first, ...) , ## __VA_ARGS__
441
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800442#define android_printAssert(cond, tag, fmt...) \
Chris Pearson19299902010-06-02 16:25:35 -0700443 __android_log_assert(cond, tag, \
444 __android_second(0, ## fmt, NULL) __android_rest(fmt))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800445
446#define android_writeLog(prio, tag, text) \
447 __android_log_write(prio, tag, text)
448
449#define android_bWriteLog(tag, payload, len) \
450 __android_log_bwrite(tag, payload, len)
451#define android_btWriteLog(tag, type, payload, len) \
452 __android_log_btwrite(tag, type, payload, len)
Chris Pearson19299902010-06-02 16:25:35 -0700453
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800454// TODO: remove these prototypes and their users
455#define android_testLog(prio, tag) (1)
456#define android_writevLog(vec,num) do{}while(0)
457#define android_write1Log(str,len) do{}while (0)
458#define android_setMinPriority(tag, prio) do{}while(0)
459//#define android_logToCallback(func) do{}while(0)
460#define android_logToFile(tag, file) (0)
461#define android_logToFd(tag, fd) (0)
462
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800463typedef enum {
464 LOG_ID_MAIN = 0,
465 LOG_ID_RADIO = 1,
466 LOG_ID_EVENTS = 2,
467 LOG_ID_SYSTEM = 3,
468
469 LOG_ID_MAX
470} log_id_t;
471
472/*
473 * Send a simple string to the log.
474 */
475int __android_log_buf_write(int bufID, int prio, const char *tag, const char *text);
476int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...);
477
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800478
479#ifdef __cplusplus
480}
481#endif
482
483#endif // _LIBS_CUTILS_LOG_H