• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2005-2014 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_LOG_LOG_H
29 #define _LIBS_LOG_LOG_H
30 
31 #include <sys/types.h>
32 #ifdef HAVE_PTHREADS
33 #include <pthread.h>
34 #endif
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <time.h>
38 #include <unistd.h>
39 #include <log/logd.h>
40 #include <log/uio.h>
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 // ---------------------------------------------------------------------
47 
48 /*
49  * Normally we strip ALOGV (VERBOSE messages) from release builds.
50  * You can modify this (for example with "#define LOG_NDEBUG 0"
51  * at the top of your source file) to change that behavior.
52  */
53 #ifndef LOG_NDEBUG
54 #ifdef NDEBUG
55 #define LOG_NDEBUG 1
56 #else
57 #define LOG_NDEBUG 0
58 #endif
59 #endif
60 
61 /*
62  * This is the local tag used for the following simplified
63  * logging macros.  You can change this preprocessor definition
64  * before using the other macros to change the tag.
65  */
66 #ifndef LOG_TAG
67 #define LOG_TAG NULL
68 #endif
69 
70 // ---------------------------------------------------------------------
71 
72 /*
73  * Simplified macro to send a verbose log message using the current LOG_TAG.
74  */
75 #ifndef ALOGV
76 #define __ALOGV(...) ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
77 #if LOG_NDEBUG
78 #define ALOGV(...) do { if (0) { __ALOGV(__VA_ARGS__); } } while (0)
79 #else
80 #define ALOGV(...) __ALOGV(__VA_ARGS__)
81 #endif
82 #endif
83 
84 #define CONDITION(cond)     (__builtin_expect((cond)!=0, 0))
85 
86 #ifndef ALOGV_IF
87 #if LOG_NDEBUG
88 #define ALOGV_IF(cond, ...)   ((void)0)
89 #else
90 #define ALOGV_IF(cond, ...) \
91     ( (CONDITION(cond)) \
92     ? ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
93     : (void)0 )
94 #endif
95 #endif
96 
97 /*
98  * Simplified macro to send a debug log message using the current LOG_TAG.
99  */
100 #ifndef ALOGD
101 #define ALOGD(...) ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__))
102 #endif
103 
104 #ifndef ALOGD_IF
105 #define ALOGD_IF(cond, ...) \
106     ( (CONDITION(cond)) \
107     ? ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
108     : (void)0 )
109 #endif
110 
111 /*
112  * Simplified macro to send an info log message using the current LOG_TAG.
113  */
114 #ifndef ALOGI
115 #define ALOGI(...) ((void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__))
116 #endif
117 
118 #ifndef ALOGI_IF
119 #define ALOGI_IF(cond, ...) \
120     ( (CONDITION(cond)) \
121     ? ((void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__)) \
122     : (void)0 )
123 #endif
124 
125 /*
126  * Simplified macro to send a warning log message using the current LOG_TAG.
127  */
128 #ifndef ALOGW
129 #define ALOGW(...) ((void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__))
130 #endif
131 
132 #ifndef ALOGW_IF
133 #define ALOGW_IF(cond, ...) \
134     ( (CONDITION(cond)) \
135     ? ((void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__)) \
136     : (void)0 )
137 #endif
138 
139 /*
140  * Simplified macro to send an error log message using the current LOG_TAG.
141  */
142 #ifndef ALOGE
143 #define ALOGE(...) ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__))
144 #endif
145 
146 #ifndef ALOGE_IF
147 #define ALOGE_IF(cond, ...) \
148     ( (CONDITION(cond)) \
149     ? ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
150     : (void)0 )
151 #endif
152 
153 // ---------------------------------------------------------------------
154 
155 /*
156  * Conditional based on whether the current LOG_TAG is enabled at
157  * verbose priority.
158  */
159 #ifndef IF_ALOGV
160 #if LOG_NDEBUG
161 #define IF_ALOGV() if (false)
162 #else
163 #define IF_ALOGV() IF_ALOG(LOG_VERBOSE, LOG_TAG)
164 #endif
165 #endif
166 
167 /*
168  * Conditional based on whether the current LOG_TAG is enabled at
169  * debug priority.
170  */
171 #ifndef IF_ALOGD
172 #define IF_ALOGD() IF_ALOG(LOG_DEBUG, LOG_TAG)
173 #endif
174 
175 /*
176  * Conditional based on whether the current LOG_TAG is enabled at
177  * info priority.
178  */
179 #ifndef IF_ALOGI
180 #define IF_ALOGI() IF_ALOG(LOG_INFO, LOG_TAG)
181 #endif
182 
183 /*
184  * Conditional based on whether the current LOG_TAG is enabled at
185  * warn priority.
186  */
187 #ifndef IF_ALOGW
188 #define IF_ALOGW() IF_ALOG(LOG_WARN, LOG_TAG)
189 #endif
190 
191 /*
192  * Conditional based on whether the current LOG_TAG is enabled at
193  * error priority.
194  */
195 #ifndef IF_ALOGE
196 #define IF_ALOGE() IF_ALOG(LOG_ERROR, LOG_TAG)
197 #endif
198 
199 
200 // ---------------------------------------------------------------------
201 
202 /*
203  * Simplified macro to send a verbose system log message using the current LOG_TAG.
204  */
205 #ifndef SLOGV
206 #define __SLOGV(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
207 #if LOG_NDEBUG
208 #define SLOGV(...) do { if (0) { __SLOGV(__VA_ARGS__); } } while (0)
209 #else
210 #define SLOGV(...) __SLOGV(__VA_ARGS__)
211 #endif
212 #endif
213 
214 #define CONDITION(cond)     (__builtin_expect((cond)!=0, 0))
215 
216 #ifndef SLOGV_IF
217 #if LOG_NDEBUG
218 #define SLOGV_IF(cond, ...)   ((void)0)
219 #else
220 #define SLOGV_IF(cond, ...) \
221     ( (CONDITION(cond)) \
222     ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
223     : (void)0 )
224 #endif
225 #endif
226 
227 /*
228  * Simplified macro to send a debug system log message using the current LOG_TAG.
229  */
230 #ifndef SLOGD
231 #define SLOGD(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
232 #endif
233 
234 #ifndef SLOGD_IF
235 #define SLOGD_IF(cond, ...) \
236     ( (CONDITION(cond)) \
237     ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
238     : (void)0 )
239 #endif
240 
241 /*
242  * Simplified macro to send an info system log message using the current LOG_TAG.
243  */
244 #ifndef SLOGI
245 #define SLOGI(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__))
246 #endif
247 
248 #ifndef SLOGI_IF
249 #define SLOGI_IF(cond, ...) \
250     ( (CONDITION(cond)) \
251     ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)) \
252     : (void)0 )
253 #endif
254 
255 /*
256  * Simplified macro to send a warning system log message using the current LOG_TAG.
257  */
258 #ifndef SLOGW
259 #define SLOGW(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__))
260 #endif
261 
262 #ifndef SLOGW_IF
263 #define SLOGW_IF(cond, ...) \
264     ( (CONDITION(cond)) \
265     ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)) \
266     : (void)0 )
267 #endif
268 
269 /*
270  * Simplified macro to send an error system log message using the current LOG_TAG.
271  */
272 #ifndef SLOGE
273 #define SLOGE(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
274 #endif
275 
276 #ifndef SLOGE_IF
277 #define SLOGE_IF(cond, ...) \
278     ( (CONDITION(cond)) \
279     ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
280     : (void)0 )
281 #endif
282 
283 // ---------------------------------------------------------------------
284 
285 /*
286  * Simplified macro to send a verbose radio log message using the current LOG_TAG.
287  */
288 #ifndef RLOGV
289 #define __RLOGV(...) ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
290 #if LOG_NDEBUG
291 #define RLOGV(...) do { if (0) { __RLOGV(__VA_ARGS__); } } while (0)
292 #else
293 #define RLOGV(...) __RLOGV(__VA_ARGS__)
294 #endif
295 #endif
296 
297 #define CONDITION(cond)     (__builtin_expect((cond)!=0, 0))
298 
299 #ifndef RLOGV_IF
300 #if LOG_NDEBUG
301 #define RLOGV_IF(cond, ...)   ((void)0)
302 #else
303 #define RLOGV_IF(cond, ...) \
304     ( (CONDITION(cond)) \
305     ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
306     : (void)0 )
307 #endif
308 #endif
309 
310 /*
311  * Simplified macro to send a debug radio log message using the current LOG_TAG.
312  */
313 #ifndef RLOGD
314 #define RLOGD(...) ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
315 #endif
316 
317 #ifndef RLOGD_IF
318 #define RLOGD_IF(cond, ...) \
319     ( (CONDITION(cond)) \
320     ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
321     : (void)0 )
322 #endif
323 
324 /*
325  * Simplified macro to send an info radio log message using the current LOG_TAG.
326  */
327 #ifndef RLOGI
328 #define RLOGI(...) ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__))
329 #endif
330 
331 #ifndef RLOGI_IF
332 #define RLOGI_IF(cond, ...) \
333     ( (CONDITION(cond)) \
334     ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)) \
335     : (void)0 )
336 #endif
337 
338 /*
339  * Simplified macro to send a warning radio log message using the current LOG_TAG.
340  */
341 #ifndef RLOGW
342 #define RLOGW(...) ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__))
343 #endif
344 
345 #ifndef RLOGW_IF
346 #define RLOGW_IF(cond, ...) \
347     ( (CONDITION(cond)) \
348     ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)) \
349     : (void)0 )
350 #endif
351 
352 /*
353  * Simplified macro to send an error radio log message using the current LOG_TAG.
354  */
355 #ifndef RLOGE
356 #define RLOGE(...) ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
357 #endif
358 
359 #ifndef RLOGE_IF
360 #define RLOGE_IF(cond, ...) \
361     ( (CONDITION(cond)) \
362     ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
363     : (void)0 )
364 #endif
365 
366 
367 // ---------------------------------------------------------------------
368 
369 /*
370  * Log a fatal error.  If the given condition fails, this stops program
371  * execution like a normal assertion, but also generating the given message.
372  * It is NOT stripped from release builds.  Note that the condition test
373  * is -inverted- from the normal assert() semantics.
374  */
375 #ifndef LOG_ALWAYS_FATAL_IF
376 #define LOG_ALWAYS_FATAL_IF(cond, ...) \
377     ( (CONDITION(cond)) \
378     ? ((void)android_printAssert(#cond, LOG_TAG, ## __VA_ARGS__)) \
379     : (void)0 )
380 #endif
381 
382 #ifndef LOG_ALWAYS_FATAL
383 #define LOG_ALWAYS_FATAL(...) \
384     ( ((void)android_printAssert(NULL, LOG_TAG, ## __VA_ARGS__)) )
385 #endif
386 
387 /*
388  * Versions of LOG_ALWAYS_FATAL_IF and LOG_ALWAYS_FATAL that
389  * are stripped out of release builds.
390  */
391 #if LOG_NDEBUG
392 
393 #ifndef LOG_FATAL_IF
394 #define LOG_FATAL_IF(cond, ...) ((void)0)
395 #endif
396 #ifndef LOG_FATAL
397 #define LOG_FATAL(...) ((void)0)
398 #endif
399 
400 #else
401 
402 #ifndef LOG_FATAL_IF
403 #define LOG_FATAL_IF(cond, ...) LOG_ALWAYS_FATAL_IF(cond, ## __VA_ARGS__)
404 #endif
405 #ifndef LOG_FATAL
406 #define LOG_FATAL(...) LOG_ALWAYS_FATAL(__VA_ARGS__)
407 #endif
408 
409 #endif
410 
411 /*
412  * Assertion that generates a log message when the assertion fails.
413  * Stripped out of release builds.  Uses the current LOG_TAG.
414  */
415 #ifndef ALOG_ASSERT
416 #define ALOG_ASSERT(cond, ...) LOG_FATAL_IF(!(cond), ## __VA_ARGS__)
417 //#define ALOG_ASSERT(cond) LOG_FATAL_IF(!(cond), "Assertion failed: " #cond)
418 #endif
419 
420 // ---------------------------------------------------------------------
421 
422 /*
423  * Basic log message macro.
424  *
425  * Example:
426  *  ALOG(LOG_WARN, NULL, "Failed with error %d", errno);
427  *
428  * The second argument may be NULL or "" to indicate the "global" tag.
429  */
430 #ifndef ALOG
431 #define ALOG(priority, tag, ...) \
432     LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__)
433 #endif
434 
435 /*
436  * Log macro that allows you to specify a number for the priority.
437  */
438 #ifndef LOG_PRI
439 #define LOG_PRI(priority, tag, ...) \
440     android_printLog(priority, tag, __VA_ARGS__)
441 #endif
442 
443 /*
444  * Log macro that allows you to pass in a varargs ("args" is a va_list).
445  */
446 #ifndef LOG_PRI_VA
447 #define LOG_PRI_VA(priority, tag, fmt, args) \
448     android_vprintLog(priority, NULL, tag, fmt, args)
449 #endif
450 
451 /*
452  * Conditional given a desired logging priority and tag.
453  */
454 #ifndef IF_ALOG
455 #define IF_ALOG(priority, tag) \
456     if (android_testLog(ANDROID_##priority, tag))
457 #endif
458 
459 // ---------------------------------------------------------------------
460 
461 /*
462  * Event logging.
463  */
464 
465 /*
466  * Event log entry types.  These must match up with the declarations in
467  * java/android/android/util/EventLog.java.
468  */
469 typedef enum {
470     EVENT_TYPE_INT      = 0,
471     EVENT_TYPE_LONG     = 1,
472     EVENT_TYPE_STRING   = 2,
473     EVENT_TYPE_LIST     = 3,
474 } AndroidEventLogType;
475 #define sizeof_AndroidEventLogType sizeof(typeof_AndroidEventLogType)
476 #define typeof_AndroidEventLogType unsigned char
477 
478 #ifndef LOG_EVENT_INT
479 #define LOG_EVENT_INT(_tag, _value) {                                       \
480         int intBuf = _value;                                                \
481         (void) android_btWriteLog(_tag, EVENT_TYPE_INT, &intBuf,            \
482             sizeof(intBuf));                                                \
483     }
484 #endif
485 #ifndef LOG_EVENT_LONG
486 #define LOG_EVENT_LONG(_tag, _value) {                                      \
487         long long longBuf = _value;                                         \
488         (void) android_btWriteLog(_tag, EVENT_TYPE_LONG, &longBuf,          \
489             sizeof(longBuf));                                               \
490     }
491 #endif
492 #ifndef LOG_EVENT_STRING
493 #define LOG_EVENT_STRING(_tag, _value)                                      \
494         (void) __android_log_bswrite(_tag, _value);
495 #endif
496 /* TODO: something for LIST */
497 
498 /*
499  * ===========================================================================
500  *
501  * The stuff in the rest of this file should not be used directly.
502  */
503 
504 #define android_printLog(prio, tag, fmt...) \
505     __android_log_print(prio, tag, fmt)
506 
507 #define android_vprintLog(prio, cond, tag, fmt...) \
508     __android_log_vprint(prio, tag, fmt)
509 
510 /* XXX Macros to work around syntax errors in places where format string
511  * arg is not passed to ALOG_ASSERT, LOG_ALWAYS_FATAL or LOG_ALWAYS_FATAL_IF
512  * (happens only in debug builds).
513  */
514 
515 /* Returns 2nd arg.  Used to substitute default value if caller's vararg list
516  * is empty.
517  */
518 #define __android_second(dummy, second, ...)     second
519 
520 /* If passed multiple args, returns ',' followed by all but 1st arg, otherwise
521  * returns nothing.
522  */
523 #define __android_rest(first, ...)               , ## __VA_ARGS__
524 
525 #define android_printAssert(cond, tag, fmt...) \
526     __android_log_assert(cond, tag, \
527         __android_second(0, ## fmt, NULL) __android_rest(fmt))
528 
529 #define android_writeLog(prio, tag, text) \
530     __android_log_write(prio, tag, text)
531 
532 #define android_bWriteLog(tag, payload, len) \
533     __android_log_bwrite(tag, payload, len)
534 #define android_btWriteLog(tag, type, payload, len) \
535     __android_log_btwrite(tag, type, payload, len)
536 
537 // TODO: remove these prototypes and their users
538 #define android_testLog(prio, tag) (1)
539 #define android_writevLog(vec,num) do{}while(0)
540 #define android_write1Log(str,len) do{}while (0)
541 #define android_setMinPriority(tag, prio) do{}while(0)
542 //#define android_logToCallback(func) do{}while(0)
543 #define android_logToFile(tag, file) (0)
544 #define android_logToFd(tag, fd) (0)
545 
546 typedef enum log_id {
547     LOG_ID_MIN = 0,
548 
549     LOG_ID_MAIN = 0,
550     LOG_ID_RADIO = 1,
551     LOG_ID_EVENTS = 2,
552     LOG_ID_SYSTEM = 3,
553     LOG_ID_CRASH = 4,
554 
555     LOG_ID_MAX
556 } log_id_t;
557 #define sizeof_log_id_t sizeof(typeof_log_id_t)
558 #define typeof_log_id_t unsigned char
559 
560 /*
561  * Send a simple string to the log.
562  */
563 int __android_log_buf_write(int bufID, int prio, const char *tag, const char *text);
564 int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...)
565 #if defined(__GNUC__)
566     __attribute__((__format__(printf, 4, 5)))
567 #endif
568     ;
569 
570 #ifdef __cplusplus
571 }
572 #endif
573 
574 #endif /* _LIBS_LOG_LOG_H */
575