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