blob: 4e65393e76ce6901fdf639633a38b29eb81b7671 [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 Salyzyna1667082016-09-28 09:58:56 -070032#include <stdint.h>
Mark Salyzyn42958412013-11-22 10:50:27 -080033#include <stdio.h>
Yabin Cui4a6e5a32015-01-26 19:48:54 -080034#include <sys/types.h>
Mark Salyzyn42958412013-11-22 10:50:27 -080035#include <time.h>
36#include <unistd.h>
Yabin Cui4a6e5a32015-01-26 19:48:54 -080037
Mark Salyzyna1667082016-09-28 09:58:56 -070038#include <android/log.h>
Mark Salyzyn42958412013-11-22 10:50:27 -080039#include <log/uio.h>
Colin Cross9227bd32013-07-23 16:59:20 -070040
Mark Salyzynf387fa52014-01-03 16:54:28 -080041#ifdef __cplusplus
42extern "C" {
43#endif
Colin Cross9227bd32013-07-23 16:59:20 -070044
Colin Cross412ad0d2016-09-15 18:02:46 -070045// This file uses ", ## __VA_ARGS__" zero-argument token pasting to
46// work around issues with debug-only syntax errors in assertions
47// that are missing format strings. See commit
48// 19299904343daf191267564fe32e6cd5c165cd42
49#if defined(__clang__)
50#pragma clang diagnostic push
51#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
52#endif
53
Mark Salyzyna1667082016-09-28 09:58:56 -070054int __android_log_bwrite(int32_t tag, const void *payload, size_t len);
55int __android_log_btwrite(int32_t tag, char type, const void *payload,
56 size_t len);
57int __android_log_bswrite(int32_t tag, const char *payload);
58
59int __android_log_security_bwrite(int32_t tag, const void *payload, size_t len);
60int __android_log_security_bswrite(int32_t tag, const char *payload);
61
Colin Cross9227bd32013-07-23 16:59:20 -070062// ---------------------------------------------------------------------
63
64/*
65 * Normally we strip ALOGV (VERBOSE messages) from release builds.
66 * You can modify this (for example with "#define LOG_NDEBUG 0"
67 * at the top of your source file) to change that behavior.
68 */
69#ifndef LOG_NDEBUG
70#ifdef NDEBUG
71#define LOG_NDEBUG 1
72#else
73#define LOG_NDEBUG 0
74#endif
75#endif
76
77/*
78 * This is the local tag used for the following simplified
79 * logging macros. You can change this preprocessor definition
80 * before using the other macros to change the tag.
81 */
82#ifndef LOG_TAG
83#define LOG_TAG NULL
84#endif
85
86// ---------------------------------------------------------------------
87
Mark Salyzyn4ff25452015-04-09 16:12:58 -070088#ifndef __predict_false
89#define __predict_false(exp) __builtin_expect((exp) != 0, 0)
90#endif
91
92/*
93 * -DLINT_RLOG in sources that you want to enforce that all logging
94 * goes to the radio log buffer. If any logging goes to any of the other
95 * log buffers, there will be a compile or link error to highlight the
96 * problem. This is not a replacement for a full audit of the code since
97 * this only catches compiled code, not ifdef'd debug code. Options to
98 * defining this, either temporarily to do a spot check, or permanently
99 * to enforce, in all the communications trees; We have hopes to ensure
100 * that by supplying just the radio log buffer that the communications
101 * teams will have their one-stop shop for triaging issues.
102 */
103#ifndef LINT_RLOG
104
Colin Cross9227bd32013-07-23 16:59:20 -0700105/*
106 * Simplified macro to send a verbose log message using the current LOG_TAG.
107 */
108#ifndef ALOGV
Colin Cross810d19f2014-02-06 20:07:50 -0800109#define __ALOGV(...) ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
Colin Cross9227bd32013-07-23 16:59:20 -0700110#if LOG_NDEBUG
Colin Cross810d19f2014-02-06 20:07:50 -0800111#define ALOGV(...) do { if (0) { __ALOGV(__VA_ARGS__); } } while (0)
Colin Cross9227bd32013-07-23 16:59:20 -0700112#else
Colin Cross810d19f2014-02-06 20:07:50 -0800113#define ALOGV(...) __ALOGV(__VA_ARGS__)
Colin Cross9227bd32013-07-23 16:59:20 -0700114#endif
115#endif
116
Colin Cross9227bd32013-07-23 16:59:20 -0700117#ifndef ALOGV_IF
118#if LOG_NDEBUG
119#define ALOGV_IF(cond, ...) ((void)0)
120#else
121#define ALOGV_IF(cond, ...) \
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700122 ( (__predict_false(cond)) \
Colin Cross9227bd32013-07-23 16:59:20 -0700123 ? ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
124 : (void)0 )
125#endif
126#endif
127
128/*
129 * Simplified macro to send a debug log message using the current LOG_TAG.
130 */
131#ifndef ALOGD
132#define ALOGD(...) ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__))
133#endif
134
135#ifndef ALOGD_IF
136#define ALOGD_IF(cond, ...) \
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700137 ( (__predict_false(cond)) \
Colin Cross9227bd32013-07-23 16:59:20 -0700138 ? ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
139 : (void)0 )
140#endif
141
142/*
143 * Simplified macro to send an info log message using the current LOG_TAG.
144 */
145#ifndef ALOGI
146#define ALOGI(...) ((void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__))
147#endif
148
149#ifndef ALOGI_IF
150#define ALOGI_IF(cond, ...) \
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700151 ( (__predict_false(cond)) \
Colin Cross9227bd32013-07-23 16:59:20 -0700152 ? ((void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__)) \
153 : (void)0 )
154#endif
155
156/*
157 * Simplified macro to send a warning log message using the current LOG_TAG.
158 */
159#ifndef ALOGW
160#define ALOGW(...) ((void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__))
161#endif
162
163#ifndef ALOGW_IF
164#define ALOGW_IF(cond, ...) \
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700165 ( (__predict_false(cond)) \
Colin Cross9227bd32013-07-23 16:59:20 -0700166 ? ((void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__)) \
167 : (void)0 )
168#endif
169
170/*
171 * Simplified macro to send an error log message using the current LOG_TAG.
172 */
173#ifndef ALOGE
174#define ALOGE(...) ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__))
175#endif
176
177#ifndef ALOGE_IF
178#define ALOGE_IF(cond, ...) \
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700179 ( (__predict_false(cond)) \
Colin Cross9227bd32013-07-23 16:59:20 -0700180 ? ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
181 : (void)0 )
182#endif
183
184// ---------------------------------------------------------------------
185
186/*
187 * Conditional based on whether the current LOG_TAG is enabled at
188 * verbose priority.
189 */
190#ifndef IF_ALOGV
191#if LOG_NDEBUG
192#define IF_ALOGV() if (false)
193#else
194#define IF_ALOGV() IF_ALOG(LOG_VERBOSE, LOG_TAG)
195#endif
196#endif
197
198/*
199 * Conditional based on whether the current LOG_TAG is enabled at
200 * debug priority.
201 */
202#ifndef IF_ALOGD
203#define IF_ALOGD() IF_ALOG(LOG_DEBUG, LOG_TAG)
204#endif
205
206/*
207 * Conditional based on whether the current LOG_TAG is enabled at
208 * info priority.
209 */
210#ifndef IF_ALOGI
211#define IF_ALOGI() IF_ALOG(LOG_INFO, LOG_TAG)
212#endif
213
214/*
215 * Conditional based on whether the current LOG_TAG is enabled at
216 * warn priority.
217 */
218#ifndef IF_ALOGW
219#define IF_ALOGW() IF_ALOG(LOG_WARN, LOG_TAG)
220#endif
221
222/*
223 * Conditional based on whether the current LOG_TAG is enabled at
224 * error priority.
225 */
226#ifndef IF_ALOGE
227#define IF_ALOGE() IF_ALOG(LOG_ERROR, LOG_TAG)
228#endif
229
230
231// ---------------------------------------------------------------------
232
233/*
234 * Simplified macro to send a verbose system log message using the current LOG_TAG.
235 */
236#ifndef SLOGV
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700237#define __SLOGV(...) \
238 ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
Colin Cross9227bd32013-07-23 16:59:20 -0700239#if LOG_NDEBUG
Colin Cross810d19f2014-02-06 20:07:50 -0800240#define SLOGV(...) do { if (0) { __SLOGV(__VA_ARGS__); } } while (0)
Colin Cross9227bd32013-07-23 16:59:20 -0700241#else
Colin Cross810d19f2014-02-06 20:07:50 -0800242#define SLOGV(...) __SLOGV(__VA_ARGS__)
Colin Cross9227bd32013-07-23 16:59:20 -0700243#endif
244#endif
245
Colin Cross9227bd32013-07-23 16:59:20 -0700246#ifndef SLOGV_IF
247#if LOG_NDEBUG
248#define SLOGV_IF(cond, ...) ((void)0)
249#else
250#define SLOGV_IF(cond, ...) \
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700251 ( (__predict_false(cond)) \
Colin Cross9227bd32013-07-23 16:59:20 -0700252 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
253 : (void)0 )
254#endif
255#endif
256
257/*
258 * Simplified macro to send a debug system log message using the current LOG_TAG.
259 */
260#ifndef SLOGD
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700261#define SLOGD(...) \
262 ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
Colin Cross9227bd32013-07-23 16:59:20 -0700263#endif
264
265#ifndef SLOGD_IF
266#define SLOGD_IF(cond, ...) \
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700267 ( (__predict_false(cond)) \
Colin Cross9227bd32013-07-23 16:59:20 -0700268 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
269 : (void)0 )
270#endif
271
272/*
273 * Simplified macro to send an info system log message using the current LOG_TAG.
274 */
275#ifndef SLOGI
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700276#define SLOGI(...) \
277 ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__))
Colin Cross9227bd32013-07-23 16:59:20 -0700278#endif
279
280#ifndef SLOGI_IF
281#define SLOGI_IF(cond, ...) \
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700282 ( (__predict_false(cond)) \
Colin Cross9227bd32013-07-23 16:59:20 -0700283 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)) \
284 : (void)0 )
285#endif
286
287/*
288 * Simplified macro to send a warning system log message using the current LOG_TAG.
289 */
290#ifndef SLOGW
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700291#define SLOGW(...) \
292 ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__))
Colin Cross9227bd32013-07-23 16:59:20 -0700293#endif
294
295#ifndef SLOGW_IF
296#define SLOGW_IF(cond, ...) \
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700297 ( (__predict_false(cond)) \
Colin Cross9227bd32013-07-23 16:59:20 -0700298 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)) \
299 : (void)0 )
300#endif
301
302/*
303 * Simplified macro to send an error system log message using the current LOG_TAG.
304 */
305#ifndef SLOGE
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700306#define SLOGE(...) \
307 ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
Colin Cross9227bd32013-07-23 16:59:20 -0700308#endif
309
310#ifndef SLOGE_IF
311#define SLOGE_IF(cond, ...) \
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700312 ( (__predict_false(cond)) \
Colin Cross9227bd32013-07-23 16:59:20 -0700313 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
314 : (void)0 )
315#endif
316
Mark Salyzyn4ff25452015-04-09 16:12:58 -0700317#endif /* !LINT_RLOG */
318
Colin Cross9227bd32013-07-23 16:59:20 -0700319// ---------------------------------------------------------------------
320
321/*
322 * Simplified macro to send a verbose radio log message using the current LOG_TAG.
323 */
324#ifndef RLOGV
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700325#define __RLOGV(...) \
326 ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
Colin Cross9227bd32013-07-23 16:59:20 -0700327#if LOG_NDEBUG
Colin Cross810d19f2014-02-06 20:07:50 -0800328#define RLOGV(...) do { if (0) { __RLOGV(__VA_ARGS__); } } while (0)
Colin Cross9227bd32013-07-23 16:59:20 -0700329#else
Colin Cross810d19f2014-02-06 20:07:50 -0800330#define RLOGV(...) __RLOGV(__VA_ARGS__)
Colin Cross9227bd32013-07-23 16:59:20 -0700331#endif
332#endif
333
Colin Cross9227bd32013-07-23 16:59:20 -0700334#ifndef RLOGV_IF
335#if LOG_NDEBUG
336#define RLOGV_IF(cond, ...) ((void)0)
337#else
338#define RLOGV_IF(cond, ...) \
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700339 ( (__predict_false(cond)) \
Colin Cross9227bd32013-07-23 16:59:20 -0700340 ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
341 : (void)0 )
342#endif
343#endif
344
345/*
346 * Simplified macro to send a debug radio log message using the current LOG_TAG.
347 */
348#ifndef RLOGD
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700349#define RLOGD(...) \
350 ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
Colin Cross9227bd32013-07-23 16:59:20 -0700351#endif
352
353#ifndef RLOGD_IF
354#define RLOGD_IF(cond, ...) \
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700355 ( (__predict_false(cond)) \
Colin Cross9227bd32013-07-23 16:59:20 -0700356 ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
357 : (void)0 )
358#endif
359
360/*
361 * Simplified macro to send an info radio log message using the current LOG_TAG.
362 */
363#ifndef RLOGI
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700364#define RLOGI(...) \
365 ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__))
Colin Cross9227bd32013-07-23 16:59:20 -0700366#endif
367
368#ifndef RLOGI_IF
369#define RLOGI_IF(cond, ...) \
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700370 ( (__predict_false(cond)) \
Colin Cross9227bd32013-07-23 16:59:20 -0700371 ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)) \
372 : (void)0 )
373#endif
374
375/*
376 * Simplified macro to send a warning radio log message using the current LOG_TAG.
377 */
378#ifndef RLOGW
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700379#define RLOGW(...) \
380 ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__))
Colin Cross9227bd32013-07-23 16:59:20 -0700381#endif
382
383#ifndef RLOGW_IF
384#define RLOGW_IF(cond, ...) \
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700385 ( (__predict_false(cond)) \
Colin Cross9227bd32013-07-23 16:59:20 -0700386 ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)) \
387 : (void)0 )
388#endif
389
390/*
391 * Simplified macro to send an error radio log message using the current LOG_TAG.
392 */
393#ifndef RLOGE
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700394#define RLOGE(...) \
395 ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
Colin Cross9227bd32013-07-23 16:59:20 -0700396#endif
397
398#ifndef RLOGE_IF
399#define RLOGE_IF(cond, ...) \
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700400 ( (__predict_false(cond)) \
Colin Cross9227bd32013-07-23 16:59:20 -0700401 ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
402 : (void)0 )
403#endif
404
405
406// ---------------------------------------------------------------------
407
408/*
409 * Log a fatal error. If the given condition fails, this stops program
410 * execution like a normal assertion, but also generating the given message.
411 * It is NOT stripped from release builds. Note that the condition test
412 * is -inverted- from the normal assert() semantics.
413 */
414#ifndef LOG_ALWAYS_FATAL_IF
415#define LOG_ALWAYS_FATAL_IF(cond, ...) \
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700416 ( (__predict_false(cond)) \
Colin Cross9227bd32013-07-23 16:59:20 -0700417 ? ((void)android_printAssert(#cond, LOG_TAG, ## __VA_ARGS__)) \
418 : (void)0 )
419#endif
420
421#ifndef LOG_ALWAYS_FATAL
422#define LOG_ALWAYS_FATAL(...) \
423 ( ((void)android_printAssert(NULL, LOG_TAG, ## __VA_ARGS__)) )
424#endif
425
426/*
427 * Versions of LOG_ALWAYS_FATAL_IF and LOG_ALWAYS_FATAL that
428 * are stripped out of release builds.
429 */
430#if LOG_NDEBUG
431
432#ifndef LOG_FATAL_IF
433#define LOG_FATAL_IF(cond, ...) ((void)0)
434#endif
435#ifndef LOG_FATAL
436#define LOG_FATAL(...) ((void)0)
437#endif
438
439#else
440
441#ifndef LOG_FATAL_IF
442#define LOG_FATAL_IF(cond, ...) LOG_ALWAYS_FATAL_IF(cond, ## __VA_ARGS__)
443#endif
444#ifndef LOG_FATAL
445#define LOG_FATAL(...) LOG_ALWAYS_FATAL(__VA_ARGS__)
446#endif
447
448#endif
449
450/*
451 * Assertion that generates a log message when the assertion fails.
452 * Stripped out of release builds. Uses the current LOG_TAG.
453 */
454#ifndef ALOG_ASSERT
455#define ALOG_ASSERT(cond, ...) LOG_FATAL_IF(!(cond), ## __VA_ARGS__)
456//#define ALOG_ASSERT(cond) LOG_FATAL_IF(!(cond), "Assertion failed: " #cond)
457#endif
458
459// ---------------------------------------------------------------------
460
461/*
462 * Basic log message macro.
463 *
464 * Example:
465 * ALOG(LOG_WARN, NULL, "Failed with error %d", errno);
466 *
467 * The second argument may be NULL or "" to indicate the "global" tag.
468 */
469#ifndef ALOG
470#define ALOG(priority, tag, ...) \
471 LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__)
472#endif
473
474/*
475 * Log macro that allows you to specify a number for the priority.
476 */
477#ifndef LOG_PRI
478#define LOG_PRI(priority, tag, ...) \
479 android_printLog(priority, tag, __VA_ARGS__)
480#endif
481
482/*
483 * Log macro that allows you to pass in a varargs ("args" is a va_list).
484 */
485#ifndef LOG_PRI_VA
486#define LOG_PRI_VA(priority, tag, fmt, args) \
487 android_vprintLog(priority, NULL, tag, fmt, args)
488#endif
489
490/*
491 * Conditional given a desired logging priority and tag.
492 */
493#ifndef IF_ALOG
494#define IF_ALOG(priority, tag) \
495 if (android_testLog(ANDROID_##priority, tag))
496#endif
497
498// ---------------------------------------------------------------------
499
500/*
501 * Event logging.
502 */
503
504/*
Mark Salyzynbd1ad042016-02-17 16:08:13 -0800505 * Event log entry types.
Colin Cross9227bd32013-07-23 16:59:20 -0700506 */
507typedef enum {
Mark Salyzynbd1ad042016-02-17 16:08:13 -0800508 /* Special markers for android_log_list_element type */
509 EVENT_TYPE_LIST_STOP = '\n', /* declare end of list */
510 EVENT_TYPE_UNKNOWN = '?', /* protocol error */
511
512 /* must match with declaration in java/android/android/util/EventLog.java */
513 EVENT_TYPE_INT = 0, /* uint32_t */
514 EVENT_TYPE_LONG = 1, /* uint64_t */
515 EVENT_TYPE_STRING = 2,
516 EVENT_TYPE_LIST = 3,
517 EVENT_TYPE_FLOAT = 4,
Colin Cross9227bd32013-07-23 16:59:20 -0700518} AndroidEventLogType;
Mark Salyzyn42958412013-11-22 10:50:27 -0800519#define sizeof_AndroidEventLogType sizeof(typeof_AndroidEventLogType)
520#define typeof_AndroidEventLogType unsigned char
Colin Cross9227bd32013-07-23 16:59:20 -0700521
522#ifndef LOG_EVENT_INT
523#define LOG_EVENT_INT(_tag, _value) { \
524 int intBuf = _value; \
525 (void) android_btWriteLog(_tag, EVENT_TYPE_INT, &intBuf, \
526 sizeof(intBuf)); \
527 }
528#endif
529#ifndef LOG_EVENT_LONG
530#define LOG_EVENT_LONG(_tag, _value) { \
531 long long longBuf = _value; \
532 (void) android_btWriteLog(_tag, EVENT_TYPE_LONG, &longBuf, \
533 sizeof(longBuf)); \
534 }
535#endif
Jeff Brown44193d92015-04-28 12:47:02 -0700536#ifndef LOG_EVENT_FLOAT
537#define LOG_EVENT_FLOAT(_tag, _value) { \
538 float floatBuf = _value; \
539 (void) android_btWriteLog(_tag, EVENT_TYPE_FLOAT, &floatBuf, \
540 sizeof(floatBuf)); \
541 }
542#endif
Colin Cross9227bd32013-07-23 16:59:20 -0700543#ifndef LOG_EVENT_STRING
544#define LOG_EVENT_STRING(_tag, _value) \
Nick Kralevich2a4d05a2014-07-01 10:57:16 -0700545 (void) __android_log_bswrite(_tag, _value);
Colin Cross9227bd32013-07-23 16:59:20 -0700546#endif
Mark Salyzynbd1ad042016-02-17 16:08:13 -0800547
548typedef enum log_id {
549 LOG_ID_MIN = 0,
550
551#ifndef LINT_RLOG
552 LOG_ID_MAIN = 0,
553#endif
554 LOG_ID_RADIO = 1,
555#ifndef LINT_RLOG
556 LOG_ID_EVENTS = 2,
557 LOG_ID_SYSTEM = 3,
558 LOG_ID_CRASH = 4,
559 LOG_ID_SECURITY = 5,
560 LOG_ID_KERNEL = 6, /* place last, third-parties can not use it */
561#endif
562
563 LOG_ID_MAX
564} log_id_t;
565#define sizeof_log_id_t sizeof(typeof_log_id_t)
566#define typeof_log_id_t unsigned char
567
568/* For manipulating lists of events. */
569
570#define ANDROID_MAX_LIST_NEST_DEPTH 8
571
572/*
573 * The opaque context used to manipulate lists of events.
574 */
575typedef struct android_log_context_internal *android_log_context;
576
577/*
578 * Elements returned when reading a list of events.
579 */
580typedef struct {
581 AndroidEventLogType type;
582 uint16_t complete;
583 uint16_t len;
584 union {
585 int32_t int32;
586 int64_t int64;
587 char *string;
588 float float32;
589 } data;
590} android_log_list_element;
591
592/*
593 * Creates a context associated with an event tag to write elements to
594 * the list of events.
595 */
596android_log_context create_android_logger(uint32_t tag);
597
598/* All lists must be braced by a begin and end call */
599/*
600 * NB: If the first level braces are missing when specifying multiple
601 * elements, we will manufacturer a list to embrace it for your API
602 * convenience. For a single element, it will remain solitary.
603 */
604int android_log_write_list_begin(android_log_context ctx);
605int android_log_write_list_end(android_log_context ctx);
606
607int android_log_write_int32(android_log_context ctx, int32_t value);
608int android_log_write_int64(android_log_context ctx, int64_t value);
609int android_log_write_string8(android_log_context ctx, const char *value);
Mark Salyzyn67d7eaf2016-02-25 08:41:25 -0800610int android_log_write_string8_len(android_log_context ctx,
611 const char *value, size_t maxlen);
Mark Salyzynbd1ad042016-02-17 16:08:13 -0800612int android_log_write_float32(android_log_context ctx, float value);
613
614/* Submit the composed list context to the specified logger id */
615/* NB: LOG_ID_EVENTS and LOG_ID_SECURITY only valid binary buffers */
616int android_log_write_list(android_log_context ctx, log_id_t id);
617
618/*
619 * Creates a context from a raw buffer representing a list of events to be read.
620 */
621android_log_context create_android_log_parser(const char *msg, size_t len);
622
623android_log_list_element android_log_read_next(android_log_context ctx);
624android_log_list_element android_log_peek_next(android_log_context ctx);
625
626/* Finished with reader or writer context */
627int android_log_destroy(android_log_context *ctx);
Colin Cross9227bd32013-07-23 16:59:20 -0700628
629/*
630 * ===========================================================================
631 *
632 * The stuff in the rest of this file should not be used directly.
633 */
634
Alex Vakulenko3b0d65a2016-04-27 14:37:00 -0700635#define android_printLog(prio, tag, ...) \
636 __android_log_print(prio, tag, __VA_ARGS__)
Colin Cross9227bd32013-07-23 16:59:20 -0700637
Alex Vakulenko3b0d65a2016-04-27 14:37:00 -0700638#define android_vprintLog(prio, cond, tag, ...) \
639 __android_log_vprint(prio, tag, __VA_ARGS__)
Colin Cross9227bd32013-07-23 16:59:20 -0700640
641/* XXX Macros to work around syntax errors in places where format string
642 * arg is not passed to ALOG_ASSERT, LOG_ALWAYS_FATAL or LOG_ALWAYS_FATAL_IF
643 * (happens only in debug builds).
644 */
645
646/* Returns 2nd arg. Used to substitute default value if caller's vararg list
647 * is empty.
648 */
649#define __android_second(dummy, second, ...) second
650
651/* If passed multiple args, returns ',' followed by all but 1st arg, otherwise
652 * returns nothing.
653 */
654#define __android_rest(first, ...) , ## __VA_ARGS__
655
Alex Vakulenko3b0d65a2016-04-27 14:37:00 -0700656#define android_printAssert(cond, tag, ...) \
Colin Cross9227bd32013-07-23 16:59:20 -0700657 __android_log_assert(cond, tag, \
Alex Vakulenko3b0d65a2016-04-27 14:37:00 -0700658 __android_second(0, ## __VA_ARGS__, NULL) __android_rest(__VA_ARGS__))
Colin Cross9227bd32013-07-23 16:59:20 -0700659
660#define android_writeLog(prio, tag, text) \
661 __android_log_write(prio, tag, text)
662
663#define android_bWriteLog(tag, payload, len) \
664 __android_log_bwrite(tag, payload, len)
665#define android_btWriteLog(tag, type, payload, len) \
666 __android_log_btwrite(tag, type, payload, len)
667
William Luh964428c2015-08-13 10:41:58 -0700668#define android_errorWriteLog(tag, subTag) \
669 __android_log_error_write(tag, subTag, -1, NULL, 0)
670
671#define android_errorWriteWithInfoLog(tag, subTag, uid, data, dataLen) \
672 __android_log_error_write(tag, subTag, uid, data, dataLen)
673
Mark Salyzyn1df92e52015-02-09 15:15:56 -0800674/*
675 * IF_ALOG uses android_testLog, but IF_ALOG can be overridden.
676 * android_testLog will remain constant in its purpose as a wrapper
677 * for Android logging filter policy, and can be subject to
678 * change. It can be reused by the developers that override
679 * IF_ALOG as a convenient means to reimplement their policy
680 * over Android.
681 */
Andreas Gampef45bbe42015-02-09 16:13:33 -0800682#if LOG_NDEBUG /* Production */
Mark Salyzyn1df92e52015-02-09 15:15:56 -0800683#define android_testLog(prio, tag) \
684 (__android_log_is_loggable(prio, tag, ANDROID_LOG_DEBUG) != 0)
685#else
686#define android_testLog(prio, tag) \
687 (__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE) != 0)
688#endif
689
Colin Cross9227bd32013-07-23 16:59:20 -0700690/*
Mark Salyzyn95687052014-10-02 11:12:28 -0700691 * Use the per-tag properties "log.tag.<tagname>" to generate a runtime
Mark Salyzyn2d2e0a52015-11-06 12:26:52 -0800692 * result of non-zero to expose a log. prio is ANDROID_LOG_VERBOSE to
693 * ANDROID_LOG_FATAL. default_prio if no property. Undefined behavior if
694 * any other value.
Mark Salyzyn95687052014-10-02 11:12:28 -0700695 */
Mark Salyzyn2d2e0a52015-11-06 12:26:52 -0800696int __android_log_is_loggable(int prio, const char *tag, int default_prio);
Mark Salyzyn95687052014-10-02 11:12:28 -0700697
Mark Salyzynffbd86f2015-12-04 10:59:45 -0800698int __android_log_security(); /* Device Owner is present */
699
William Luh964428c2015-08-13 10:41:58 -0700700int __android_log_error_write(int tag, const char *subTag, int32_t uid, const char *data,
701 uint32_t dataLen);
702
Mark Salyzyn95687052014-10-02 11:12:28 -0700703/*
Colin Cross9227bd32013-07-23 16:59:20 -0700704 * Send a simple string to the log.
705 */
706int __android_log_buf_write(int bufID, int prio, const char *tag, const char *text);
Colin Cross810d19f2014-02-06 20:07:50 -0800707int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...)
708#if defined(__GNUC__)
709 __attribute__((__format__(printf, 4, 5)))
710#endif
711 ;
Colin Cross9227bd32013-07-23 16:59:20 -0700712
Colin Cross412ad0d2016-09-15 18:02:46 -0700713#if defined(__clang__)
714#pragma clang diagnostic pop
715#endif
716
Mark Salyzynf387fa52014-01-03 16:54:28 -0800717#ifdef __cplusplus
718}
719#endif
Colin Cross9227bd32013-07-23 16:59:20 -0700720
Mark Salyzyn819c58a2013-11-22 12:39:43 -0800721#endif /* _LIBS_LOG_LOG_H */