log.h revision e7e7fac974617a5376f05d7a638d2a53094d3ac5
1/*
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/*
50 * Normally we strip LOGV (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.
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 */
76#ifndef LOGV
77#if LOG_NDEBUG
78#define LOGV(...)   ((void)0)
79#else
80#define LOGV(...) ((void)LOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
81#endif
82#define ALOGV LOGV
83#endif
84
85#define CONDITION(cond)     (__builtin_expect((cond)!=0, 0))
86
87#ifndef LOGV_IF
88#if LOG_NDEBUG
89#define LOGV_IF(cond, ...)   ((void)0)
90#else
91#define LOGV_IF(cond, ...) \
92    ( (CONDITION(cond)) \
93    ? ((void)LOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
94    : (void)0 )
95#endif
96#define ALOGV_IF LOGV_IF
97#endif
98
99/*
100 * Simplified macro to send a debug log message using the current LOG_TAG.
101 */
102#ifndef LOGD
103#define LOGD(...) ((void)LOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__))
104#define ALOGD LOGD
105#endif
106
107#ifndef LOGD_IF
108#define LOGD_IF(cond, ...) \
109    ( (CONDITION(cond)) \
110    ? ((void)LOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
111    : (void)0 )
112#define ALOGD_IF LOGD_IF
113#endif
114
115/*
116 * Simplified macro to send an info log message using the current LOG_TAG.
117 */
118#ifndef LOGI
119#define LOGI(...) ((void)LOG(LOG_INFO, LOG_TAG, __VA_ARGS__))
120#define ALOGI LOGI
121#endif
122
123#ifndef LOGI_IF
124#define LOGI_IF(cond, ...) \
125    ( (CONDITION(cond)) \
126    ? ((void)LOG(LOG_INFO, LOG_TAG, __VA_ARGS__)) \
127    : (void)0 )
128#define ALOGI_IF LOGI_IF
129#endif
130
131/*
132 * Simplified macro to send a warning log message using the current LOG_TAG.
133 */
134#ifndef LOGW
135#define LOGW(...) ((void)LOG(LOG_WARN, LOG_TAG, __VA_ARGS__))
136#define ALOGW LOGW
137#endif
138
139#ifndef LOGW_IF
140#define LOGW_IF(cond, ...) \
141    ( (CONDITION(cond)) \
142    ? ((void)LOG(LOG_WARN, LOG_TAG, __VA_ARGS__)) \
143    : (void)0 )
144#define ALOGW_IF LOGW_IF
145#endif
146
147/*
148 * Simplified macro to send an error log message using the current LOG_TAG.
149 */
150#ifndef LOGE
151#define LOGE(...) ((void)LOG(LOG_ERROR, LOG_TAG, __VA_ARGS__))
152#define ALOGE LOGE
153#endif
154
155#ifndef LOGE_IF
156#define LOGE_IF(cond, ...) \
157    ( (CONDITION(cond)) \
158    ? ((void)LOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
159    : (void)0 )
160#define ALOGE_IF LOGE_IF
161#endif
162
163// ---------------------------------------------------------------------
164
165/*
166 * Conditional based on whether the current LOG_TAG is enabled at
167 * verbose priority.
168 */
169#ifndef IF_LOGV
170#if LOG_NDEBUG
171#define IF_LOGV() if (false)
172#else
173#define IF_LOGV() IF_LOG(LOG_VERBOSE, LOG_TAG)
174#endif
175#define IF_ALOGV IF_LOGV
176#endif
177
178/*
179 * Conditional based on whether the current LOG_TAG is enabled at
180 * debug priority.
181 */
182#ifndef IF_LOGD
183#define IF_LOGD() IF_LOG(LOG_DEBUG, LOG_TAG)
184#define IF_ALOGD IF_LOGD
185#endif
186
187/*
188 * Conditional based on whether the current LOG_TAG is enabled at
189 * info priority.
190 */
191#ifndef IF_LOGI
192#define IF_LOGI() IF_LOG(LOG_INFO, LOG_TAG)
193#define IF_ALOGI IF_LOGI
194#endif
195
196/*
197 * Conditional based on whether the current LOG_TAG is enabled at
198 * warn priority.
199 */
200#ifndef IF_LOGW
201#define IF_LOGW() IF_LOG(LOG_WARN, LOG_TAG)
202#define IF_ALOGW IF_LOGW
203#endif
204
205/*
206 * Conditional based on whether the current LOG_TAG is enabled at
207 * error priority.
208 */
209#ifndef IF_LOGE
210#define IF_LOGE() IF_LOG(LOG_ERROR, LOG_TAG)
211#define IF_ALOGE IF_LOGE
212#endif
213
214
215// ---------------------------------------------------------------------
216
217/*
218 * Simplified macro to send a verbose system log message using the current LOG_TAG.
219 */
220#ifndef SLOGV
221#if LOG_NDEBUG
222#define SLOGV(...)   ((void)0)
223#else
224#define SLOGV(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
225#endif
226#endif
227
228#define CONDITION(cond)     (__builtin_expect((cond)!=0, 0))
229
230#ifndef SLOGV_IF
231#if LOG_NDEBUG
232#define SLOGV_IF(cond, ...)   ((void)0)
233#else
234#define SLOGV_IF(cond, ...) \
235    ( (CONDITION(cond)) \
236    ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
237    : (void)0 )
238#endif
239#endif
240
241/*
242 * Simplified macro to send a debug system log message using the current LOG_TAG.
243 */
244#ifndef SLOGD
245#define SLOGD(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
246#endif
247
248#ifndef SLOGD_IF
249#define SLOGD_IF(cond, ...) \
250    ( (CONDITION(cond)) \
251    ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
252    : (void)0 )
253#endif
254
255/*
256 * Simplified macro to send an info system log message using the current LOG_TAG.
257 */
258#ifndef SLOGI
259#define SLOGI(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__))
260#endif
261
262#ifndef SLOGI_IF
263#define SLOGI_IF(cond, ...) \
264    ( (CONDITION(cond)) \
265    ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)) \
266    : (void)0 )
267#endif
268
269/*
270 * Simplified macro to send a warning system log message using the current LOG_TAG.
271 */
272#ifndef SLOGW
273#define SLOGW(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__))
274#endif
275
276#ifndef SLOGW_IF
277#define SLOGW_IF(cond, ...) \
278    ( (CONDITION(cond)) \
279    ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)) \
280    : (void)0 )
281#endif
282
283/*
284 * Simplified macro to send an error system log message using the current LOG_TAG.
285 */
286#ifndef SLOGE
287#define SLOGE(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
288#endif
289
290#ifndef SLOGE_IF
291#define SLOGE_IF(cond, ...) \
292    ( (CONDITION(cond)) \
293    ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
294    : (void)0 )
295#endif
296
297
298
299// ---------------------------------------------------------------------
300
301/*
302 * Log a fatal error.  If the given condition fails, this stops program
303 * execution like a normal assertion, but also generating the given message.
304 * It is NOT stripped from release builds.  Note that the condition test
305 * is -inverted- from the normal assert() semantics.
306 */
307#ifndef LOG_ALWAYS_FATAL_IF
308#define LOG_ALWAYS_FATAL_IF(cond, ...) \
309    ( (CONDITION(cond)) \
310    ? ((void)android_printAssert(#cond, LOG_TAG, ## __VA_ARGS__)) \
311    : (void)0 )
312#endif
313
314#ifndef LOG_ALWAYS_FATAL
315#define LOG_ALWAYS_FATAL(...) \
316    ( ((void)android_printAssert(NULL, LOG_TAG, ## __VA_ARGS__)) )
317#endif
318
319/*
320 * Versions of LOG_ALWAYS_FATAL_IF and LOG_ALWAYS_FATAL that
321 * are stripped out of release builds.
322 */
323#if LOG_NDEBUG
324
325#ifndef LOG_FATAL_IF
326#define LOG_FATAL_IF(cond, ...) ((void)0)
327#endif
328#ifndef LOG_FATAL
329#define LOG_FATAL(...) ((void)0)
330#endif
331
332#else
333
334#ifndef LOG_FATAL_IF
335#define LOG_FATAL_IF(cond, ...) LOG_ALWAYS_FATAL_IF(cond, ## __VA_ARGS__)
336#endif
337#ifndef LOG_FATAL
338#define LOG_FATAL(...) LOG_ALWAYS_FATAL(__VA_ARGS__)
339#endif
340
341#endif
342
343/*
344 * Assertion that generates a log message when the assertion fails.
345 * Stripped out of release builds.  Uses the current LOG_TAG.
346 */
347#ifndef LOG_ASSERT
348#define LOG_ASSERT(cond, ...) LOG_FATAL_IF(!(cond), ## __VA_ARGS__)
349//#define LOG_ASSERT(cond) LOG_FATAL_IF(!(cond), "Assertion failed: " #cond)
350#endif
351
352// ---------------------------------------------------------------------
353
354/*
355 * Basic log message macro.
356 *
357 * Example:
358 *  LOG(LOG_WARN, NULL, "Failed with error %d", errno);
359 *
360 * The second argument may be NULL or "" to indicate the "global" tag.
361 */
362#ifndef LOG
363#define LOG(priority, tag, ...) \
364    LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__)
365#endif
366
367/*
368 * Log macro that allows you to specify a number for the priority.
369 */
370#ifndef LOG_PRI
371#define LOG_PRI(priority, tag, ...) \
372    android_printLog(priority, tag, __VA_ARGS__)
373#endif
374
375/*
376 * Log macro that allows you to pass in a varargs ("args" is a va_list).
377 */
378#ifndef LOG_PRI_VA
379#define LOG_PRI_VA(priority, tag, fmt, args) \
380    android_vprintLog(priority, NULL, tag, fmt, args)
381#endif
382
383/*
384 * Conditional given a desired logging priority and tag.
385 */
386#ifndef IF_LOG
387#define IF_LOG(priority, tag) \
388    if (android_testLog(ANDROID_##priority, tag))
389#endif
390
391// ---------------------------------------------------------------------
392
393/*
394 * Event logging.
395 */
396
397/*
398 * Event log entry types.  These must match up with the declarations in
399 * java/android/android/util/EventLog.java.
400 */
401typedef enum {
402    EVENT_TYPE_INT      = 0,
403    EVENT_TYPE_LONG     = 1,
404    EVENT_TYPE_STRING   = 2,
405    EVENT_TYPE_LIST     = 3,
406} AndroidEventLogType;
407
408
409#ifndef LOG_EVENT_INT
410#define LOG_EVENT_INT(_tag, _value) {                                       \
411        int intBuf = _value;                                                \
412        (void) android_btWriteLog(_tag, EVENT_TYPE_INT, &intBuf,            \
413            sizeof(intBuf));                                                \
414    }
415#endif
416#ifndef LOG_EVENT_LONG
417#define LOG_EVENT_LONG(_tag, _value) {                                      \
418        long long longBuf = _value;                                         \
419        (void) android_btWriteLog(_tag, EVENT_TYPE_LONG, &longBuf,          \
420            sizeof(longBuf));                                               \
421    }
422#endif
423#ifndef LOG_EVENT_STRING
424#define LOG_EVENT_STRING(_tag, _value)                                      \
425    ((void) 0)  /* not implemented -- must combine len with string */
426#endif
427/* TODO: something for LIST */
428
429/*
430 * ===========================================================================
431 *
432 * The stuff in the rest of this file should not be used directly.
433 */
434
435#define android_printLog(prio, tag, fmt...) \
436    __android_log_print(prio, tag, fmt)
437
438#define android_vprintLog(prio, cond, tag, fmt...) \
439    __android_log_vprint(prio, tag, fmt)
440
441/* XXX Macros to work around syntax errors in places where format string
442 * arg is not passed to LOG_ASSERT, LOG_ALWAYS_FATAL or LOG_ALWAYS_FATAL_IF
443 * (happens only in debug builds).
444 */
445
446/* Returns 2nd arg.  Used to substitute default value if caller's vararg list
447 * is empty.
448 */
449#define __android_second(dummy, second, ...)     second
450
451/* If passed multiple args, returns ',' followed by all but 1st arg, otherwise
452 * returns nothing.
453 */
454#define __android_rest(first, ...)               , ## __VA_ARGS__
455
456#define android_printAssert(cond, tag, fmt...) \
457    __android_log_assert(cond, tag, \
458        __android_second(0, ## fmt, NULL) __android_rest(fmt))
459
460#define android_writeLog(prio, tag, text) \
461    __android_log_write(prio, tag, text)
462
463#define android_bWriteLog(tag, payload, len) \
464    __android_log_bwrite(tag, payload, len)
465#define android_btWriteLog(tag, type, payload, len) \
466    __android_log_btwrite(tag, type, payload, len)
467
468// TODO: remove these prototypes and their users
469#define android_testLog(prio, tag) (1)
470#define android_writevLog(vec,num) do{}while(0)
471#define android_write1Log(str,len) do{}while (0)
472#define android_setMinPriority(tag, prio) do{}while(0)
473//#define android_logToCallback(func) do{}while(0)
474#define android_logToFile(tag, file) (0)
475#define android_logToFd(tag, fd) (0)
476
477typedef enum {
478    LOG_ID_MAIN = 0,
479    LOG_ID_RADIO = 1,
480    LOG_ID_EVENTS = 2,
481    LOG_ID_SYSTEM = 3,
482
483    LOG_ID_MAX
484} log_id_t;
485
486/*
487 * Send a simple string to the log.
488 */
489int __android_log_buf_write(int bufID, int prio, const char *tag, const char *text);
490int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...);
491
492
493#ifdef __cplusplus
494}
495#endif
496
497#endif // _LIBS_CUTILS_LOG_H
498