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