blob: 24c77fcdb5047f8b7e33a78e7e1694d5c11d9482 [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
Colin Cross412ad0d2016-09-15 18:02:46 -070044// This file uses ", ## __VA_ARGS__" zero-argument token pasting to
45// work around issues with debug-only syntax errors in assertions
46// that are missing format strings. See commit
47// 19299904343daf191267564fe32e6cd5c165cd42
48#if defined(__clang__)
49#pragma clang diagnostic push
50#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
51#endif
52
Colin Cross9227bd32013-07-23 16:59:20 -070053// ---------------------------------------------------------------------
54
55/*
56 * Normally we strip ALOGV (VERBOSE messages) from release builds.
57 * You can modify this (for example with "#define LOG_NDEBUG 0"
58 * at the top of your source file) to change that behavior.
59 */
60#ifndef LOG_NDEBUG
61#ifdef NDEBUG
62#define LOG_NDEBUG 1
63#else
64#define LOG_NDEBUG 0
65#endif
66#endif
67
68/*
69 * This is the local tag used for the following simplified
70 * logging macros. You can change this preprocessor definition
71 * before using the other macros to change the tag.
72 */
73#ifndef LOG_TAG
74#define LOG_TAG NULL
75#endif
76
77// ---------------------------------------------------------------------
78
Mark Salyzyn4ff25452015-04-09 16:12:58 -070079#ifndef __predict_false
80#define __predict_false(exp) __builtin_expect((exp) != 0, 0)
81#endif
82
83/*
84 * -DLINT_RLOG in sources that you want to enforce that all logging
85 * goes to the radio log buffer. If any logging goes to any of the other
86 * log buffers, there will be a compile or link error to highlight the
87 * problem. This is not a replacement for a full audit of the code since
88 * this only catches compiled code, not ifdef'd debug code. Options to
89 * defining this, either temporarily to do a spot check, or permanently
90 * to enforce, in all the communications trees; We have hopes to ensure
91 * that by supplying just the radio log buffer that the communications
92 * teams will have their one-stop shop for triaging issues.
93 */
94#ifndef LINT_RLOG
95
Colin Cross9227bd32013-07-23 16:59:20 -070096/*
97 * Simplified macro to send a verbose log message using the current LOG_TAG.
98 */
99#ifndef ALOGV
Colin Cross810d19f2014-02-06 20:07:50 -0800100#define __ALOGV(...) ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
Colin Cross9227bd32013-07-23 16:59:20 -0700101#if LOG_NDEBUG
Colin Cross810d19f2014-02-06 20:07:50 -0800102#define ALOGV(...) do { if (0) { __ALOGV(__VA_ARGS__); } } while (0)
Colin Cross9227bd32013-07-23 16:59:20 -0700103#else
Colin Cross810d19f2014-02-06 20:07:50 -0800104#define ALOGV(...) __ALOGV(__VA_ARGS__)
Colin Cross9227bd32013-07-23 16:59:20 -0700105#endif
106#endif
107
Colin Cross9227bd32013-07-23 16:59:20 -0700108#ifndef ALOGV_IF
109#if LOG_NDEBUG
110#define ALOGV_IF(cond, ...) ((void)0)
111#else
112#define ALOGV_IF(cond, ...) \
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700113 ( (__predict_false(cond)) \
Colin Cross9227bd32013-07-23 16:59:20 -0700114 ? ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
115 : (void)0 )
116#endif
117#endif
118
119/*
120 * Simplified macro to send a debug log message using the current LOG_TAG.
121 */
122#ifndef ALOGD
123#define ALOGD(...) ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__))
124#endif
125
126#ifndef ALOGD_IF
127#define ALOGD_IF(cond, ...) \
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700128 ( (__predict_false(cond)) \
Colin Cross9227bd32013-07-23 16:59:20 -0700129 ? ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
130 : (void)0 )
131#endif
132
133/*
134 * Simplified macro to send an info log message using the current LOG_TAG.
135 */
136#ifndef ALOGI
137#define ALOGI(...) ((void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__))
138#endif
139
140#ifndef ALOGI_IF
141#define ALOGI_IF(cond, ...) \
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700142 ( (__predict_false(cond)) \
Colin Cross9227bd32013-07-23 16:59:20 -0700143 ? ((void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__)) \
144 : (void)0 )
145#endif
146
147/*
148 * Simplified macro to send a warning log message using the current LOG_TAG.
149 */
150#ifndef ALOGW
151#define ALOGW(...) ((void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__))
152#endif
153
154#ifndef ALOGW_IF
155#define ALOGW_IF(cond, ...) \
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700156 ( (__predict_false(cond)) \
Colin Cross9227bd32013-07-23 16:59:20 -0700157 ? ((void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__)) \
158 : (void)0 )
159#endif
160
161/*
162 * Simplified macro to send an error log message using the current LOG_TAG.
163 */
164#ifndef ALOGE
165#define ALOGE(...) ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__))
166#endif
167
168#ifndef ALOGE_IF
169#define ALOGE_IF(cond, ...) \
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700170 ( (__predict_false(cond)) \
Colin Cross9227bd32013-07-23 16:59:20 -0700171 ? ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
172 : (void)0 )
173#endif
174
175// ---------------------------------------------------------------------
176
177/*
178 * Conditional based on whether the current LOG_TAG is enabled at
179 * verbose priority.
180 */
181#ifndef IF_ALOGV
182#if LOG_NDEBUG
183#define IF_ALOGV() if (false)
184#else
185#define IF_ALOGV() IF_ALOG(LOG_VERBOSE, LOG_TAG)
186#endif
187#endif
188
189/*
190 * Conditional based on whether the current LOG_TAG is enabled at
191 * debug priority.
192 */
193#ifndef IF_ALOGD
194#define IF_ALOGD() IF_ALOG(LOG_DEBUG, LOG_TAG)
195#endif
196
197/*
198 * Conditional based on whether the current LOG_TAG is enabled at
199 * info priority.
200 */
201#ifndef IF_ALOGI
202#define IF_ALOGI() IF_ALOG(LOG_INFO, LOG_TAG)
203#endif
204
205/*
206 * Conditional based on whether the current LOG_TAG is enabled at
207 * warn priority.
208 */
209#ifndef IF_ALOGW
210#define IF_ALOGW() IF_ALOG(LOG_WARN, LOG_TAG)
211#endif
212
213/*
214 * Conditional based on whether the current LOG_TAG is enabled at
215 * error priority.
216 */
217#ifndef IF_ALOGE
218#define IF_ALOGE() IF_ALOG(LOG_ERROR, LOG_TAG)
219#endif
220
221
222// ---------------------------------------------------------------------
223
224/*
225 * Simplified macro to send a verbose system log message using the current LOG_TAG.
226 */
227#ifndef SLOGV
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700228#define __SLOGV(...) \
229 ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
Colin Cross9227bd32013-07-23 16:59:20 -0700230#if LOG_NDEBUG
Colin Cross810d19f2014-02-06 20:07:50 -0800231#define SLOGV(...) do { if (0) { __SLOGV(__VA_ARGS__); } } while (0)
Colin Cross9227bd32013-07-23 16:59:20 -0700232#else
Colin Cross810d19f2014-02-06 20:07:50 -0800233#define SLOGV(...) __SLOGV(__VA_ARGS__)
Colin Cross9227bd32013-07-23 16:59:20 -0700234#endif
235#endif
236
Colin Cross9227bd32013-07-23 16:59:20 -0700237#ifndef SLOGV_IF
238#if LOG_NDEBUG
239#define SLOGV_IF(cond, ...) ((void)0)
240#else
241#define SLOGV_IF(cond, ...) \
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700242 ( (__predict_false(cond)) \
Colin Cross9227bd32013-07-23 16:59:20 -0700243 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
244 : (void)0 )
245#endif
246#endif
247
248/*
249 * Simplified macro to send a debug system log message using the current LOG_TAG.
250 */
251#ifndef SLOGD
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700252#define SLOGD(...) \
253 ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
Colin Cross9227bd32013-07-23 16:59:20 -0700254#endif
255
256#ifndef SLOGD_IF
257#define SLOGD_IF(cond, ...) \
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700258 ( (__predict_false(cond)) \
Colin Cross9227bd32013-07-23 16:59:20 -0700259 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
260 : (void)0 )
261#endif
262
263/*
264 * Simplified macro to send an info system log message using the current LOG_TAG.
265 */
266#ifndef SLOGI
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700267#define SLOGI(...) \
268 ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__))
Colin Cross9227bd32013-07-23 16:59:20 -0700269#endif
270
271#ifndef SLOGI_IF
272#define SLOGI_IF(cond, ...) \
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700273 ( (__predict_false(cond)) \
Colin Cross9227bd32013-07-23 16:59:20 -0700274 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)) \
275 : (void)0 )
276#endif
277
278/*
279 * Simplified macro to send a warning system log message using the current LOG_TAG.
280 */
281#ifndef SLOGW
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700282#define SLOGW(...) \
283 ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__))
Colin Cross9227bd32013-07-23 16:59:20 -0700284#endif
285
286#ifndef SLOGW_IF
287#define SLOGW_IF(cond, ...) \
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700288 ( (__predict_false(cond)) \
Colin Cross9227bd32013-07-23 16:59:20 -0700289 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)) \
290 : (void)0 )
291#endif
292
293/*
294 * Simplified macro to send an error system log message using the current LOG_TAG.
295 */
296#ifndef SLOGE
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700297#define SLOGE(...) \
298 ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
Colin Cross9227bd32013-07-23 16:59:20 -0700299#endif
300
301#ifndef SLOGE_IF
302#define SLOGE_IF(cond, ...) \
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700303 ( (__predict_false(cond)) \
Colin Cross9227bd32013-07-23 16:59:20 -0700304 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
305 : (void)0 )
306#endif
307
Mark Salyzyn4ff25452015-04-09 16:12:58 -0700308#endif /* !LINT_RLOG */
309
Colin Cross9227bd32013-07-23 16:59:20 -0700310// ---------------------------------------------------------------------
311
312/*
313 * Simplified macro to send a verbose radio log message using the current LOG_TAG.
314 */
315#ifndef RLOGV
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700316#define __RLOGV(...) \
317 ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
Colin Cross9227bd32013-07-23 16:59:20 -0700318#if LOG_NDEBUG
Colin Cross810d19f2014-02-06 20:07:50 -0800319#define RLOGV(...) do { if (0) { __RLOGV(__VA_ARGS__); } } while (0)
Colin Cross9227bd32013-07-23 16:59:20 -0700320#else
Colin Cross810d19f2014-02-06 20:07:50 -0800321#define RLOGV(...) __RLOGV(__VA_ARGS__)
Colin Cross9227bd32013-07-23 16:59:20 -0700322#endif
323#endif
324
Colin Cross9227bd32013-07-23 16:59:20 -0700325#ifndef RLOGV_IF
326#if LOG_NDEBUG
327#define RLOGV_IF(cond, ...) ((void)0)
328#else
329#define RLOGV_IF(cond, ...) \
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700330 ( (__predict_false(cond)) \
Colin Cross9227bd32013-07-23 16:59:20 -0700331 ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
332 : (void)0 )
333#endif
334#endif
335
336/*
337 * Simplified macro to send a debug radio log message using the current LOG_TAG.
338 */
339#ifndef RLOGD
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700340#define RLOGD(...) \
341 ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
Colin Cross9227bd32013-07-23 16:59:20 -0700342#endif
343
344#ifndef RLOGD_IF
345#define RLOGD_IF(cond, ...) \
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700346 ( (__predict_false(cond)) \
Colin Cross9227bd32013-07-23 16:59:20 -0700347 ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
348 : (void)0 )
349#endif
350
351/*
352 * Simplified macro to send an info radio log message using the current LOG_TAG.
353 */
354#ifndef RLOGI
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700355#define RLOGI(...) \
356 ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__))
Colin Cross9227bd32013-07-23 16:59:20 -0700357#endif
358
359#ifndef RLOGI_IF
360#define RLOGI_IF(cond, ...) \
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700361 ( (__predict_false(cond)) \
Colin Cross9227bd32013-07-23 16:59:20 -0700362 ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)) \
363 : (void)0 )
364#endif
365
366/*
367 * Simplified macro to send a warning radio log message using the current LOG_TAG.
368 */
369#ifndef RLOGW
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700370#define RLOGW(...) \
371 ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__))
Colin Cross9227bd32013-07-23 16:59:20 -0700372#endif
373
374#ifndef RLOGW_IF
375#define RLOGW_IF(cond, ...) \
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700376 ( (__predict_false(cond)) \
Colin Cross9227bd32013-07-23 16:59:20 -0700377 ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)) \
378 : (void)0 )
379#endif
380
381/*
382 * Simplified macro to send an error radio log message using the current LOG_TAG.
383 */
384#ifndef RLOGE
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700385#define RLOGE(...) \
386 ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
Colin Cross9227bd32013-07-23 16:59:20 -0700387#endif
388
389#ifndef RLOGE_IF
390#define RLOGE_IF(cond, ...) \
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700391 ( (__predict_false(cond)) \
Colin Cross9227bd32013-07-23 16:59:20 -0700392 ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
393 : (void)0 )
394#endif
395
396
397// ---------------------------------------------------------------------
398
399/*
400 * Log a fatal error. If the given condition fails, this stops program
401 * execution like a normal assertion, but also generating the given message.
402 * It is NOT stripped from release builds. Note that the condition test
403 * is -inverted- from the normal assert() semantics.
404 */
405#ifndef LOG_ALWAYS_FATAL_IF
406#define LOG_ALWAYS_FATAL_IF(cond, ...) \
Mark Salyzynf5af82e2014-10-08 07:47:08 -0700407 ( (__predict_false(cond)) \
Colin Cross9227bd32013-07-23 16:59:20 -0700408 ? ((void)android_printAssert(#cond, LOG_TAG, ## __VA_ARGS__)) \
409 : (void)0 )
410#endif
411
412#ifndef LOG_ALWAYS_FATAL
413#define LOG_ALWAYS_FATAL(...) \
414 ( ((void)android_printAssert(NULL, LOG_TAG, ## __VA_ARGS__)) )
415#endif
416
417/*
418 * Versions of LOG_ALWAYS_FATAL_IF and LOG_ALWAYS_FATAL that
419 * are stripped out of release builds.
420 */
421#if LOG_NDEBUG
422
423#ifndef LOG_FATAL_IF
424#define LOG_FATAL_IF(cond, ...) ((void)0)
425#endif
426#ifndef LOG_FATAL
427#define LOG_FATAL(...) ((void)0)
428#endif
429
430#else
431
432#ifndef LOG_FATAL_IF
433#define LOG_FATAL_IF(cond, ...) LOG_ALWAYS_FATAL_IF(cond, ## __VA_ARGS__)
434#endif
435#ifndef LOG_FATAL
436#define LOG_FATAL(...) LOG_ALWAYS_FATAL(__VA_ARGS__)
437#endif
438
439#endif
440
441/*
442 * Assertion that generates a log message when the assertion fails.
443 * Stripped out of release builds. Uses the current LOG_TAG.
444 */
445#ifndef ALOG_ASSERT
446#define ALOG_ASSERT(cond, ...) LOG_FATAL_IF(!(cond), ## __VA_ARGS__)
447//#define ALOG_ASSERT(cond) LOG_FATAL_IF(!(cond), "Assertion failed: " #cond)
448#endif
449
450// ---------------------------------------------------------------------
451
452/*
453 * Basic log message macro.
454 *
455 * Example:
456 * ALOG(LOG_WARN, NULL, "Failed with error %d", errno);
457 *
458 * The second argument may be NULL or "" to indicate the "global" tag.
459 */
460#ifndef ALOG
461#define ALOG(priority, tag, ...) \
462 LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__)
463#endif
464
465/*
466 * Log macro that allows you to specify a number for the priority.
467 */
468#ifndef LOG_PRI
469#define LOG_PRI(priority, tag, ...) \
470 android_printLog(priority, tag, __VA_ARGS__)
471#endif
472
473/*
474 * Log macro that allows you to pass in a varargs ("args" is a va_list).
475 */
476#ifndef LOG_PRI_VA
477#define LOG_PRI_VA(priority, tag, fmt, args) \
478 android_vprintLog(priority, NULL, tag, fmt, args)
479#endif
480
481/*
482 * Conditional given a desired logging priority and tag.
483 */
484#ifndef IF_ALOG
485#define IF_ALOG(priority, tag) \
486 if (android_testLog(ANDROID_##priority, tag))
487#endif
488
489// ---------------------------------------------------------------------
490
491/*
492 * Event logging.
493 */
494
495/*
Mark Salyzynbd1ad042016-02-17 16:08:13 -0800496 * Event log entry types.
Colin Cross9227bd32013-07-23 16:59:20 -0700497 */
498typedef enum {
Mark Salyzynbd1ad042016-02-17 16:08:13 -0800499 /* Special markers for android_log_list_element type */
500 EVENT_TYPE_LIST_STOP = '\n', /* declare end of list */
501 EVENT_TYPE_UNKNOWN = '?', /* protocol error */
502
503 /* must match with declaration in java/android/android/util/EventLog.java */
504 EVENT_TYPE_INT = 0, /* uint32_t */
505 EVENT_TYPE_LONG = 1, /* uint64_t */
506 EVENT_TYPE_STRING = 2,
507 EVENT_TYPE_LIST = 3,
508 EVENT_TYPE_FLOAT = 4,
Colin Cross9227bd32013-07-23 16:59:20 -0700509} AndroidEventLogType;
Mark Salyzyn42958412013-11-22 10:50:27 -0800510#define sizeof_AndroidEventLogType sizeof(typeof_AndroidEventLogType)
511#define typeof_AndroidEventLogType unsigned char
Colin Cross9227bd32013-07-23 16:59:20 -0700512
513#ifndef LOG_EVENT_INT
514#define LOG_EVENT_INT(_tag, _value) { \
515 int intBuf = _value; \
516 (void) android_btWriteLog(_tag, EVENT_TYPE_INT, &intBuf, \
517 sizeof(intBuf)); \
518 }
519#endif
520#ifndef LOG_EVENT_LONG
521#define LOG_EVENT_LONG(_tag, _value) { \
522 long long longBuf = _value; \
523 (void) android_btWriteLog(_tag, EVENT_TYPE_LONG, &longBuf, \
524 sizeof(longBuf)); \
525 }
526#endif
Jeff Brown44193d92015-04-28 12:47:02 -0700527#ifndef LOG_EVENT_FLOAT
528#define LOG_EVENT_FLOAT(_tag, _value) { \
529 float floatBuf = _value; \
530 (void) android_btWriteLog(_tag, EVENT_TYPE_FLOAT, &floatBuf, \
531 sizeof(floatBuf)); \
532 }
533#endif
Colin Cross9227bd32013-07-23 16:59:20 -0700534#ifndef LOG_EVENT_STRING
535#define LOG_EVENT_STRING(_tag, _value) \
Nick Kralevich2a4d05a2014-07-01 10:57:16 -0700536 (void) __android_log_bswrite(_tag, _value);
Colin Cross9227bd32013-07-23 16:59:20 -0700537#endif
Mark Salyzynbd1ad042016-02-17 16:08:13 -0800538
539typedef enum log_id {
540 LOG_ID_MIN = 0,
541
542#ifndef LINT_RLOG
543 LOG_ID_MAIN = 0,
544#endif
545 LOG_ID_RADIO = 1,
546#ifndef LINT_RLOG
547 LOG_ID_EVENTS = 2,
548 LOG_ID_SYSTEM = 3,
549 LOG_ID_CRASH = 4,
550 LOG_ID_SECURITY = 5,
551 LOG_ID_KERNEL = 6, /* place last, third-parties can not use it */
552#endif
553
554 LOG_ID_MAX
555} log_id_t;
556#define sizeof_log_id_t sizeof(typeof_log_id_t)
557#define typeof_log_id_t unsigned char
558
559/* For manipulating lists of events. */
560
561#define ANDROID_MAX_LIST_NEST_DEPTH 8
562
563/*
564 * The opaque context used to manipulate lists of events.
565 */
566typedef struct android_log_context_internal *android_log_context;
567
568/*
569 * Elements returned when reading a list of events.
570 */
571typedef struct {
572 AndroidEventLogType type;
573 uint16_t complete;
574 uint16_t len;
575 union {
576 int32_t int32;
577 int64_t int64;
578 char *string;
579 float float32;
580 } data;
581} android_log_list_element;
582
583/*
584 * Creates a context associated with an event tag to write elements to
585 * the list of events.
586 */
587android_log_context create_android_logger(uint32_t tag);
588
589/* All lists must be braced by a begin and end call */
590/*
591 * NB: If the first level braces are missing when specifying multiple
592 * elements, we will manufacturer a list to embrace it for your API
593 * convenience. For a single element, it will remain solitary.
594 */
595int android_log_write_list_begin(android_log_context ctx);
596int android_log_write_list_end(android_log_context ctx);
597
598int android_log_write_int32(android_log_context ctx, int32_t value);
599int android_log_write_int64(android_log_context ctx, int64_t value);
600int android_log_write_string8(android_log_context ctx, const char *value);
Mark Salyzyn67d7eaf2016-02-25 08:41:25 -0800601int android_log_write_string8_len(android_log_context ctx,
602 const char *value, size_t maxlen);
Mark Salyzynbd1ad042016-02-17 16:08:13 -0800603int android_log_write_float32(android_log_context ctx, float value);
604
605/* Submit the composed list context to the specified logger id */
606/* NB: LOG_ID_EVENTS and LOG_ID_SECURITY only valid binary buffers */
607int android_log_write_list(android_log_context ctx, log_id_t id);
608
609/*
610 * Creates a context from a raw buffer representing a list of events to be read.
611 */
612android_log_context create_android_log_parser(const char *msg, size_t len);
613
614android_log_list_element android_log_read_next(android_log_context ctx);
615android_log_list_element android_log_peek_next(android_log_context ctx);
616
617/* Finished with reader or writer context */
618int android_log_destroy(android_log_context *ctx);
Colin Cross9227bd32013-07-23 16:59:20 -0700619
620/*
621 * ===========================================================================
622 *
623 * The stuff in the rest of this file should not be used directly.
624 */
625
Alex Vakulenko3b0d65a2016-04-27 14:37:00 -0700626#define android_printLog(prio, tag, ...) \
627 __android_log_print(prio, tag, __VA_ARGS__)
Colin Cross9227bd32013-07-23 16:59:20 -0700628
Alex Vakulenko3b0d65a2016-04-27 14:37:00 -0700629#define android_vprintLog(prio, cond, tag, ...) \
630 __android_log_vprint(prio, tag, __VA_ARGS__)
Colin Cross9227bd32013-07-23 16:59:20 -0700631
632/* XXX Macros to work around syntax errors in places where format string
633 * arg is not passed to ALOG_ASSERT, LOG_ALWAYS_FATAL or LOG_ALWAYS_FATAL_IF
634 * (happens only in debug builds).
635 */
636
637/* Returns 2nd arg. Used to substitute default value if caller's vararg list
638 * is empty.
639 */
640#define __android_second(dummy, second, ...) second
641
642/* If passed multiple args, returns ',' followed by all but 1st arg, otherwise
643 * returns nothing.
644 */
645#define __android_rest(first, ...) , ## __VA_ARGS__
646
Alex Vakulenko3b0d65a2016-04-27 14:37:00 -0700647#define android_printAssert(cond, tag, ...) \
Colin Cross9227bd32013-07-23 16:59:20 -0700648 __android_log_assert(cond, tag, \
Alex Vakulenko3b0d65a2016-04-27 14:37:00 -0700649 __android_second(0, ## __VA_ARGS__, NULL) __android_rest(__VA_ARGS__))
Colin Cross9227bd32013-07-23 16:59:20 -0700650
651#define android_writeLog(prio, tag, text) \
652 __android_log_write(prio, tag, text)
653
654#define android_bWriteLog(tag, payload, len) \
655 __android_log_bwrite(tag, payload, len)
656#define android_btWriteLog(tag, type, payload, len) \
657 __android_log_btwrite(tag, type, payload, len)
658
William Luh964428c2015-08-13 10:41:58 -0700659#define android_errorWriteLog(tag, subTag) \
660 __android_log_error_write(tag, subTag, -1, NULL, 0)
661
662#define android_errorWriteWithInfoLog(tag, subTag, uid, data, dataLen) \
663 __android_log_error_write(tag, subTag, uid, data, dataLen)
664
Mark Salyzyn1df92e52015-02-09 15:15:56 -0800665/*
666 * IF_ALOG uses android_testLog, but IF_ALOG can be overridden.
667 * android_testLog will remain constant in its purpose as a wrapper
668 * for Android logging filter policy, and can be subject to
669 * change. It can be reused by the developers that override
670 * IF_ALOG as a convenient means to reimplement their policy
671 * over Android.
672 */
Andreas Gampef45bbe42015-02-09 16:13:33 -0800673#if LOG_NDEBUG /* Production */
Mark Salyzyn1df92e52015-02-09 15:15:56 -0800674#define android_testLog(prio, tag) \
675 (__android_log_is_loggable(prio, tag, ANDROID_LOG_DEBUG) != 0)
676#else
677#define android_testLog(prio, tag) \
678 (__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE) != 0)
679#endif
680
Colin Cross9227bd32013-07-23 16:59:20 -0700681/*
Mark Salyzyn95687052014-10-02 11:12:28 -0700682 * Use the per-tag properties "log.tag.<tagname>" to generate a runtime
Mark Salyzyn2d2e0a52015-11-06 12:26:52 -0800683 * result of non-zero to expose a log. prio is ANDROID_LOG_VERBOSE to
684 * ANDROID_LOG_FATAL. default_prio if no property. Undefined behavior if
685 * any other value.
Mark Salyzyn95687052014-10-02 11:12:28 -0700686 */
Mark Salyzyn2d2e0a52015-11-06 12:26:52 -0800687int __android_log_is_loggable(int prio, const char *tag, int default_prio);
Mark Salyzyn95687052014-10-02 11:12:28 -0700688
Mark Salyzynffbd86f2015-12-04 10:59:45 -0800689int __android_log_security(); /* Device Owner is present */
690
William Luh964428c2015-08-13 10:41:58 -0700691int __android_log_error_write(int tag, const char *subTag, int32_t uid, const char *data,
692 uint32_t dataLen);
693
Mark Salyzyn95687052014-10-02 11:12:28 -0700694/*
Colin Cross9227bd32013-07-23 16:59:20 -0700695 * Send a simple string to the log.
696 */
697int __android_log_buf_write(int bufID, int prio, const char *tag, const char *text);
Colin Cross810d19f2014-02-06 20:07:50 -0800698int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...)
699#if defined(__GNUC__)
700 __attribute__((__format__(printf, 4, 5)))
701#endif
702 ;
Colin Cross9227bd32013-07-23 16:59:20 -0700703
Colin Cross412ad0d2016-09-15 18:02:46 -0700704#if defined(__clang__)
705#pragma clang diagnostic pop
706#endif
707
Mark Salyzynf387fa52014-01-03 16:54:28 -0800708#ifdef __cplusplus
709}
710#endif
Colin Cross9227bd32013-07-23 16:59:20 -0700711
Mark Salyzyn819c58a2013-11-22 12:39:43 -0800712#endif /* _LIBS_LOG_LOG_H */