blob: a44aba898750193b7b913ab4da57304b818b01bc [file] [log] [blame]
Mark Salyzyn749a2982016-10-11 07:34:52 -07001/*
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#ifndef _LIBS_LOG_LOG_H
18#define _LIBS_LOG_LOG_H
19
20/* Too many in the ecosystem assume these are included */
21#if !defined(_WIN32)
22#include <pthread.h>
23#endif
24#include <stdint.h> /* uint16_t, int32_t */
25#include <stdio.h>
26#include <sys/types.h>
27#include <time.h> /* clock_gettime */
28#include <unistd.h>
29
30#include <log/uio.h> /* helper to define iovec for portability */
31
Mark Salyzynaeaaf812016-09-30 13:30:33 -070032#if (defined(__cplusplus) && defined(_USING_LIBCXX))
33extern "C++" {
34#include <string>
35}
36#endif
37
Mark Salyzyna1667082016-09-28 09:58:56 -070038#include <android/log.h>
Mark Salyzyn749a2982016-10-11 07:34:52 -070039
40#ifdef __cplusplus
41extern "C" {
42#endif
43
44/*
45 * LOG_TAG is the local tag used for the following simplified
46 * logging macros. You can change this preprocessor definition
47 * before using the other macros to change the tag.
48 */
49
50#ifndef LOG_TAG
51#define LOG_TAG NULL
52#endif
53
54/* --------------------------------------------------------------------- */
55
56/*
57 * This file uses ", ## __VA_ARGS__" zero-argument token pasting to
58 * work around issues with debug-only syntax errors in assertions
59 * that are missing format strings. See commit
60 * 19299904343daf191267564fe32e6cd5c165cd42
61 */
62#if defined(__clang__)
63#pragma clang diagnostic push
64#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
65#endif
66
67/*
68 * Send a simple string to the log.
69 */
70int __android_log_buf_write(int bufID, int prio, const char* tag, const char* text);
71int __android_log_buf_print(int bufID, int prio, const char* tag, const char* fmt, ...)
72#if defined(__GNUC__)
73 __attribute__((__format__(printf, 4, 5)))
74#endif
75 ;
76
77/*
78 * Simplified macro to send a verbose system log message using current LOG_TAG.
79 */
80#ifndef SLOGV
81#define __SLOGV(...) \
82 ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
83#if LOG_NDEBUG
84#define SLOGV(...) do { if (0) { __SLOGV(__VA_ARGS__); } } while (0)
85#else
86#define SLOGV(...) __SLOGV(__VA_ARGS__)
87#endif
88#endif
89
90#ifndef SLOGV_IF
91#if LOG_NDEBUG
92#define SLOGV_IF(cond, ...) ((void)0)
93#else
94#define SLOGV_IF(cond, ...) \
95 ( (__predict_false(cond)) \
96 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
97 : (void)0 )
98#endif
99#endif
100
101/*
102 * Simplified macro to send a debug system log message using current LOG_TAG.
103 */
104#ifndef SLOGD
105#define SLOGD(...) \
106 ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
107#endif
108
109#ifndef SLOGD_IF
110#define SLOGD_IF(cond, ...) \
111 ( (__predict_false(cond)) \
112 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
113 : (void)0 )
114#endif
115
116/*
117 * Simplified macro to send an info system log message using current LOG_TAG.
118 */
119#ifndef SLOGI
120#define SLOGI(...) \
121 ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__))
122#endif
123
124#ifndef SLOGI_IF
125#define SLOGI_IF(cond, ...) \
126 ( (__predict_false(cond)) \
127 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)) \
128 : (void)0 )
129#endif
130
131/*
132 * Simplified macro to send a warning system log message using current LOG_TAG.
133 */
134#ifndef SLOGW
135#define SLOGW(...) \
136 ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__))
137#endif
138
139#ifndef SLOGW_IF
140#define SLOGW_IF(cond, ...) \
141 ( (__predict_false(cond)) \
142 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)) \
143 : (void)0 )
144#endif
145
146/*
147 * Simplified macro to send an error system log message using current LOG_TAG.
148 */
149#ifndef SLOGE
150#define SLOGE(...) \
151 ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
152#endif
153
154#ifndef SLOGE_IF
155#define SLOGE_IF(cond, ...) \
156 ( (__predict_false(cond)) \
157 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
158 : (void)0 )
159#endif
160
161/* --------------------------------------------------------------------- */
162
163/*
164 * Simplified macro to send a verbose radio log message using current LOG_TAG.
165 */
166#ifndef RLOGV
167#define __RLOGV(...) \
168 ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
169#if LOG_NDEBUG
170#define RLOGV(...) do { if (0) { __RLOGV(__VA_ARGS__); } } while (0)
171#else
172#define RLOGV(...) __RLOGV(__VA_ARGS__)
173#endif
174#endif
175
176#ifndef RLOGV_IF
177#if LOG_NDEBUG
178#define RLOGV_IF(cond, ...) ((void)0)
179#else
180#define RLOGV_IF(cond, ...) \
181 ( (__predict_false(cond)) \
182 ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
183 : (void)0 )
184#endif
185#endif
186
187/*
188 * Simplified macro to send a debug radio log message using current LOG_TAG.
189 */
190#ifndef RLOGD
191#define RLOGD(...) \
192 ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
193#endif
194
195#ifndef RLOGD_IF
196#define RLOGD_IF(cond, ...) \
197 ( (__predict_false(cond)) \
198 ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
199 : (void)0 )
200#endif
201
202/*
203 * Simplified macro to send an info radio log message using current LOG_TAG.
204 */
205#ifndef RLOGI
206#define RLOGI(...) \
207 ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__))
208#endif
209
210#ifndef RLOGI_IF
211#define RLOGI_IF(cond, ...) \
212 ( (__predict_false(cond)) \
213 ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)) \
214 : (void)0 )
215#endif
216
217/*
218 * Simplified macro to send a warning radio log message using current LOG_TAG.
219 */
220#ifndef RLOGW
221#define RLOGW(...) \
222 ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__))
223#endif
224
225#ifndef RLOGW_IF
226#define RLOGW_IF(cond, ...) \
227 ( (__predict_false(cond)) \
228 ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)) \
229 : (void)0 )
230#endif
231
232/*
233 * Simplified macro to send an error radio log message using current LOG_TAG.
234 */
235#ifndef RLOGE
236#define RLOGE(...) \
237 ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
238#endif
239
240#ifndef RLOGE_IF
241#define RLOGE_IF(cond, ...) \
242 ( (__predict_false(cond)) \
243 ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
244 : (void)0 )
245#endif
246
247/* --------------------------------------------------------------------- */
248
249/*
250 * Event logging.
251 */
252
253/*
254 * The following should not be used directly.
255 */
256
257int __android_log_bwrite(int32_t tag, const void* payload, size_t len);
258int __android_log_btwrite(int32_t tag, char type, const void* payload,
259 size_t len);
260int __android_log_bswrite(int32_t tag, const char* payload);
261
262#define android_bWriteLog(tag, payload, len) \
263 __android_log_bwrite(tag, payload, len)
264#define android_btWriteLog(tag, type, payload, len) \
265 __android_log_btwrite(tag, type, payload, len)
266
267/*
268 * Event log entry types.
269 */
270#ifndef __AndroidEventLogType_defined
271#define __AndroidEventLogType_defined
272typedef enum {
273 /* Special markers for android_log_list_element type */
274 EVENT_TYPE_LIST_STOP = '\n', /* declare end of list */
275 EVENT_TYPE_UNKNOWN = '?', /* protocol error */
276
277 /* must match with declaration in java/android/android/util/EventLog.java */
278 EVENT_TYPE_INT = 0, /* int32_t */
279 EVENT_TYPE_LONG = 1, /* int64_t */
280 EVENT_TYPE_STRING = 2,
281 EVENT_TYPE_LIST = 3,
282 EVENT_TYPE_FLOAT = 4,
283} AndroidEventLogType;
284#endif
285#define sizeof_AndroidEventLogType sizeof(typeof_AndroidEventLogType)
286#define typeof_AndroidEventLogType unsigned char
287
288#ifndef LOG_EVENT_INT
289#define LOG_EVENT_INT(_tag, _value) { \
290 int intBuf = _value; \
291 (void) android_btWriteLog(_tag, EVENT_TYPE_INT, &intBuf, \
292 sizeof(intBuf)); \
293 }
294#endif
295#ifndef LOG_EVENT_LONG
296#define LOG_EVENT_LONG(_tag, _value) { \
297 long long longBuf = _value; \
298 (void) android_btWriteLog(_tag, EVENT_TYPE_LONG, &longBuf, \
299 sizeof(longBuf)); \
300 }
301#endif
302#ifndef LOG_EVENT_FLOAT
303#define LOG_EVENT_FLOAT(_tag, _value) { \
304 float floatBuf = _value; \
305 (void) android_btWriteLog(_tag, EVENT_TYPE_FLOAT, &floatBuf, \
306 sizeof(floatBuf)); \
307 }
308#endif
309#ifndef LOG_EVENT_STRING
310#define LOG_EVENT_STRING(_tag, _value) \
311 (void) __android_log_bswrite(_tag, _value);
312#endif
313
314#ifndef log_id_t_defined
315#define log_id_t_defined
316typedef enum log_id {
317 LOG_ID_MIN = 0,
318
319 LOG_ID_MAIN = 0,
320 LOG_ID_RADIO = 1,
321 LOG_ID_EVENTS = 2,
322 LOG_ID_SYSTEM = 3,
323 LOG_ID_CRASH = 4,
324 LOG_ID_SECURITY = 5,
325 LOG_ID_KERNEL = 6, /* place last, third-parties can not use it */
326
327 LOG_ID_MAX
328} log_id_t;
329#endif
330#define sizeof_log_id_t sizeof(typeof_log_id_t)
331#define typeof_log_id_t unsigned char
332
333/* --------------------------------------------------------------------- */
334
Mark Salyzynaeaaf812016-09-30 13:30:33 -0700335/*
336 * Native log reading interface section. See logcat for sample code.
337 *
338 * The preferred API is an exec of logcat. Likely uses of this interface
339 * are if native code suffers from exec or filtration being too costly,
340 * access to raw information, or parsing is an issue.
341 */
342
343/*
344 * The userspace structure for version 1 of the logger_entry ABI.
345 */
346#ifndef __struct_logger_entry_defined
347#define __struct_logger_entry_defined
348struct logger_entry {
349 uint16_t len; /* length of the payload */
350 uint16_t __pad; /* no matter what, we get 2 bytes of padding */
351 int32_t pid; /* generating process's pid */
352 int32_t tid; /* generating process's tid */
353 int32_t sec; /* seconds since Epoch */
354 int32_t nsec; /* nanoseconds */
355#ifndef __cplusplus
356 char msg[0]; /* the entry's payload */
357#endif
358};
359#endif
360
361/*
362 * The userspace structure for version 2 of the logger_entry ABI.
363 */
364#ifndef __struct_logger_entry_v2_defined
365#define __struct_logger_entry_v2_defined
366struct logger_entry_v2 {
367 uint16_t len; /* length of the payload */
368 uint16_t hdr_size; /* sizeof(struct logger_entry_v2) */
369 int32_t pid; /* generating process's pid */
370 int32_t tid; /* generating process's tid */
371 int32_t sec; /* seconds since Epoch */
372 int32_t nsec; /* nanoseconds */
373 uint32_t euid; /* effective UID of logger */
374#ifndef __cplusplus
375 char msg[0]; /* the entry's payload */
376#endif
377} __attribute__((__packed__));
378#endif
379
380/*
381 * The userspace structure for version 3 of the logger_entry ABI.
382 */
383#ifndef __struct_logger_entry_v3_defined
384#define __struct_logger_entry_v3_defined
385struct logger_entry_v3 {
386 uint16_t len; /* length of the payload */
387 uint16_t hdr_size; /* sizeof(struct logger_entry_v3) */
388 int32_t pid; /* generating process's pid */
389 int32_t tid; /* generating process's tid */
390 int32_t sec; /* seconds since Epoch */
391 int32_t nsec; /* nanoseconds */
392 uint32_t lid; /* log id of the payload */
393#ifndef __cplusplus
394 char msg[0]; /* the entry's payload */
395#endif
396} __attribute__((__packed__));
397#endif
398
399/*
400 * The userspace structure for version 4 of the logger_entry ABI.
401 */
402#ifndef __struct_logger_entry_v4_defined
403#define __struct_logger_entry_v4_defined
404struct logger_entry_v4 {
405 uint16_t len; /* length of the payload */
406 uint16_t hdr_size; /* sizeof(struct logger_entry_v4) */
407 int32_t pid; /* generating process's pid */
408 uint32_t tid; /* generating process's tid */
409 uint32_t sec; /* seconds since Epoch */
410 uint32_t nsec; /* nanoseconds */
411 uint32_t lid; /* log id of the payload, bottom 4 bits currently */
412 uint32_t uid; /* generating process's uid */
413#ifndef __cplusplus
414 char msg[0]; /* the entry's payload */
415#endif
416};
417#endif
418
419/* struct log_time is a wire-format variant of struct timespec */
420#define NS_PER_SEC 1000000000ULL
421
422#ifndef __struct_log_time_defined
423#define __struct_log_time_defined
424#ifdef __cplusplus
425
426/*
427 * NB: we did NOT define a copy constructor. This will result in structure
428 * no longer being compatible with pass-by-value which is desired
429 * efficient behavior. Also, pass-by-reference breaks C/C++ ABI.
430 */
431struct log_time {
432public:
433 uint32_t tv_sec; /* good to Feb 5 2106 */
434 uint32_t tv_nsec;
435
436 static const uint32_t tv_sec_max = 0xFFFFFFFFUL;
437 static const uint32_t tv_nsec_max = 999999999UL;
438
439 log_time(const timespec& T)
440 {
441 tv_sec = static_cast<uint32_t>(T.tv_sec);
442 tv_nsec = static_cast<uint32_t>(T.tv_nsec);
443 }
444 log_time(uint32_t sec, uint32_t nsec)
445 {
446 tv_sec = sec;
447 tv_nsec = nsec;
448 }
449#ifdef _SYSTEM_CORE_INCLUDE_PRIVATE_ANDROID_LOGGER_H_
450#define __struct_log_time_private_defined
451 static const timespec EPOCH;
452#endif
453 log_time()
454 {
455 }
456#ifdef __linux__
457 log_time(clockid_t id)
458 {
459 timespec T;
460 clock_gettime(id, &T);
461 tv_sec = static_cast<uint32_t>(T.tv_sec);
462 tv_nsec = static_cast<uint32_t>(T.tv_nsec);
463 }
464#endif
465 log_time(const char* T)
466 {
467 const uint8_t* c = reinterpret_cast<const uint8_t*>(T);
468 tv_sec = c[0] |
469 (static_cast<uint32_t>(c[1]) << 8) |
470 (static_cast<uint32_t>(c[2]) << 16) |
471 (static_cast<uint32_t>(c[3]) << 24);
472 tv_nsec = c[4] |
473 (static_cast<uint32_t>(c[5]) << 8) |
474 (static_cast<uint32_t>(c[6]) << 16) |
475 (static_cast<uint32_t>(c[7]) << 24);
476 }
477
478 /* timespec */
479 bool operator== (const timespec& T) const
480 {
481 return (tv_sec == static_cast<uint32_t>(T.tv_sec))
482 && (tv_nsec == static_cast<uint32_t>(T.tv_nsec));
483 }
484 bool operator!= (const timespec& T) const
485 {
486 return !(*this == T);
487 }
488 bool operator< (const timespec& T) const
489 {
490 return (tv_sec < static_cast<uint32_t>(T.tv_sec))
491 || ((tv_sec == static_cast<uint32_t>(T.tv_sec))
492 && (tv_nsec < static_cast<uint32_t>(T.tv_nsec)));
493 }
494 bool operator>= (const timespec& T) const
495 {
496 return !(*this < T);
497 }
498 bool operator> (const timespec& T) const
499 {
500 return (tv_sec > static_cast<uint32_t>(T.tv_sec))
501 || ((tv_sec == static_cast<uint32_t>(T.tv_sec))
502 && (tv_nsec > static_cast<uint32_t>(T.tv_nsec)));
503 }
504 bool operator<= (const timespec& T) const
505 {
506 return !(*this > T);
507 }
508
509#ifdef _SYSTEM_CORE_INCLUDE_PRIVATE_ANDROID_LOGGER_H_
510 log_time operator-= (const timespec& T);
511 log_time operator- (const timespec& T) const
512 {
513 log_time local(*this);
514 return local -= T;
515 }
516 log_time operator+= (const timespec& T);
517 log_time operator+ (const timespec& T) const
518 {
519 log_time local(*this);
520 return local += T;
521 }
522#endif
523
524 /* log_time */
525 bool operator== (const log_time& T) const
526 {
527 return (tv_sec == T.tv_sec) && (tv_nsec == T.tv_nsec);
528 }
529 bool operator!= (const log_time& T) const
530 {
531 return !(*this == T);
532 }
533 bool operator< (const log_time& T) const
534 {
535 return (tv_sec < T.tv_sec)
536 || ((tv_sec == T.tv_sec) && (tv_nsec < T.tv_nsec));
537 }
538 bool operator>= (const log_time& T) const
539 {
540 return !(*this < T);
541 }
542 bool operator> (const log_time& T) const
543 {
544 return (tv_sec > T.tv_sec)
545 || ((tv_sec == T.tv_sec) && (tv_nsec > T.tv_nsec));
546 }
547 bool operator<= (const log_time& T) const
548 {
549 return !(*this > T);
550 }
551
552#ifdef _SYSTEM_CORE_INCLUDE_PRIVATE_ANDROID_LOGGER_H_
553 log_time operator-= (const log_time& T);
554 log_time operator- (const log_time& T) const
555 {
556 log_time local(*this);
557 return local -= T;
558 }
559 log_time operator+= (const log_time& T);
560 log_time operator+ (const log_time& T) const
561 {
562 log_time local(*this);
563 return local += T;
564 }
565#endif
566
567 uint64_t nsec() const
568 {
569 return static_cast<uint64_t>(tv_sec) * NS_PER_SEC + tv_nsec;
570 }
571
572#ifdef _SYSTEM_CORE_INCLUDE_PRIVATE_ANDROID_LOGGER_H_
573 static const char default_format[];
574
575 /* Add %#q for the fraction of a second to the standard library functions */
576 char* strptime(const char* s, const char* format = default_format);
577#endif
578} __attribute__((__packed__));
579
580#else
581
582typedef struct log_time {
583 uint32_t tv_sec;
584 uint32_t tv_nsec;
585} __attribute__((__packed__)) log_time;
586
587#endif
588#endif
589
590/*
591 * The maximum size of the log entry payload that can be
592 * written to the logger. An attempt to write more than
593 * this amount will result in a truncated log entry.
594 */
595#define LOGGER_ENTRY_MAX_PAYLOAD 4068
596
597/*
598 * The maximum size of a log entry which can be read from the
599 * kernel logger driver. An attempt to read less than this amount
600 * may result in read() returning EINVAL.
601 */
602#define LOGGER_ENTRY_MAX_LEN (5*1024)
603
604#ifndef __struct_log_msg_defined
605#define __struct_log_msg_defined
606struct log_msg {
607 union {
608 unsigned char buf[LOGGER_ENTRY_MAX_LEN + 1];
609 struct logger_entry_v4 entry;
610 struct logger_entry_v4 entry_v4;
611 struct logger_entry_v3 entry_v3;
612 struct logger_entry_v2 entry_v2;
613 struct logger_entry entry_v1;
614 } __attribute__((aligned(4)));
615#ifdef __cplusplus
616 /* Matching log_time operators */
617 bool operator== (const log_msg& T) const
618 {
619 return (entry.sec == T.entry.sec) && (entry.nsec == T.entry.nsec);
620 }
621 bool operator!= (const log_msg& T) const
622 {
623 return !(*this == T);
624 }
625 bool operator< (const log_msg& T) const
626 {
627 return (entry.sec < T.entry.sec)
628 || ((entry.sec == T.entry.sec)
629 && (entry.nsec < T.entry.nsec));
630 }
631 bool operator>= (const log_msg& T) const
632 {
633 return !(*this < T);
634 }
635 bool operator> (const log_msg& T) const
636 {
637 return (entry.sec > T.entry.sec)
638 || ((entry.sec == T.entry.sec)
639 && (entry.nsec > T.entry.nsec));
640 }
641 bool operator<= (const log_msg& T) const
642 {
643 return !(*this > T);
644 }
645 uint64_t nsec() const
646 {
647 return static_cast<uint64_t>(entry.sec) * NS_PER_SEC + entry.nsec;
648 }
649
650 /* packet methods */
651 log_id_t id()
652 {
653 return static_cast<log_id_t>(entry.lid);
654 }
655 char* msg()
656 {
657 unsigned short hdr_size = entry.hdr_size;
658 if (!hdr_size) {
659 hdr_size = sizeof(entry_v1);
660 }
661 if ((hdr_size < sizeof(entry_v1)) || (hdr_size > sizeof(entry))) {
662 return NULL;
663 }
664 return reinterpret_cast<char*>(buf) + hdr_size;
665 }
666 unsigned int len()
667 {
668 return (entry.hdr_size ?
669 entry.hdr_size :
670 static_cast<uint16_t>(sizeof(entry_v1))) +
671 entry.len;
672 }
673#endif
674};
675#endif
676
677#ifndef __ANDROID_USE_LIBLOG_READER_INTERFACE
678#ifndef __ANDROID_API__
679#define __ANDROID_USE_LIBLOG_READER_INTERFACE 3
680#elif __ANDROID_API__ > 23 /* > Marshmallow */
681#define __ANDROID_USE_LIBLOG_READER_INTERFACE 3
682#elif __ANDROID_API__ > 22 /* > Lollipop */
683#define __ANDROID_USE_LIBLOG_READER_INTERFACE 2
684#elif __ANDROID_API__ > 19 /* > KitKat */
685#define __ANDROID_USE_LIBLOG_READER_INTERFACE 1
686#else
687#define __ANDROID_USE_LIBLOG_READER_INTERFACE 0
688#endif
689#endif
690
691#if __ANDROID_USE_LIBLOG_READER_INTERFACE
692
693struct logger;
694
695log_id_t android_logger_get_id(struct logger* logger);
696
697int android_logger_clear(struct logger* logger);
698long android_logger_get_log_size(struct logger* logger);
699int android_logger_set_log_size(struct logger* logger, unsigned long size);
700long android_logger_get_log_readable_size(struct logger* logger);
701int android_logger_get_log_version(struct logger* logger);
702
703struct logger_list;
704
705#if __ANDROID_USE_LIBLOG_READER_INTERFACE > 1
706ssize_t android_logger_get_statistics(struct logger_list* logger_list,
707 char* buf, size_t len);
708ssize_t android_logger_get_prune_list(struct logger_list* logger_list,
709 char* buf, size_t len);
710int android_logger_set_prune_list(struct logger_list* logger_list,
711 char* buf, size_t len);
712#endif
713
714#define ANDROID_LOG_RDONLY O_RDONLY
715#define ANDROID_LOG_WRONLY O_WRONLY
716#define ANDROID_LOG_RDWR O_RDWR
717#define ANDROID_LOG_ACCMODE O_ACCMODE
718#define ANDROID_LOG_NONBLOCK O_NONBLOCK
719#if __ANDROID_USE_LIBLOG_READER_INTERFACE > 2
720#define ANDROID_LOG_WRAP 0x40000000 /* Block until buffer about to wrap */
721#define ANDROID_LOG_WRAP_DEFAULT_TIMEOUT 7200 /* 2 hour default */
722#endif
723#if __ANDROID_USE_LIBLOG_READER_INTERFACE > 1
724#define ANDROID_LOG_PSTORE 0x80000000
725#endif
726
727struct logger_list* android_logger_list_alloc(int mode,
728 unsigned int tail,
729 pid_t pid);
730struct logger_list* android_logger_list_alloc_time(int mode,
731 log_time start,
732 pid_t pid);
733void android_logger_list_free(struct logger_list* logger_list);
734/* In the purest sense, the following two are orthogonal interfaces */
735int android_logger_list_read(struct logger_list* logger_list,
736 struct log_msg* log_msg);
737
738/* Multiple log_id_t opens */
739struct logger* android_logger_open(struct logger_list* logger_list,
740 log_id_t id);
741#define android_logger_close android_logger_free
742/* Single log_id_t open */
743struct logger_list* android_logger_list_open(log_id_t id,
744 int mode,
745 unsigned int tail,
746 pid_t pid);
747#define android_logger_list_close android_logger_list_free
748
749#endif /* __ANDROID_USE_LIBLOG_READER_INTERFACE */
750
751#ifdef __linux__
752
753#ifndef __ANDROID_USE_LIBLOG_CLOCK_INTERFACE
754#ifndef __ANDROID_API__
755#define __ANDROID_USE_LIBLOG_CLOCK_INTERFACE 1
756#elif __ANDROID_API__ > 22 /* > Lollipop */
757#define __ANDROID_USE_LIBLOG_CLOCK_INTERFACE 1
758#else
759#define __ANDROID_USE_LIBLOG_CLOCK_INTERFACE 0
760#endif
761#endif
762
763#if __ANDROID_USE_LIBLOG_CLOCK_INTERFACE
764clockid_t android_log_clockid();
765#endif
766
767#endif /* __linux__ */
768
769/*
770 * log_id_t helpers
771 */
772log_id_t android_name_to_log_id(const char* logName);
773const char* android_log_id_to_name(log_id_t log_id);
774
775/* --------------------------------------------------------------------- */
776
Mark Salyzyn749a2982016-10-11 07:34:52 -0700777#ifndef __ANDROID_USE_LIBLOG_EVENT_INTERFACE
778#ifndef __ANDROID_API__
779#define __ANDROID_USE_LIBLOG_EVENT_INTERFACE 1
780#elif __ANDROID_API__ > 23 /* > Marshmallow */
781#define __ANDROID_USE_LIBLOG_EVENT_INTERFACE 1
782#else
783#define __ANDROID_USE_LIBLOG_EVENT_INTERFACE 0
784#endif
785#endif
786
787#if __ANDROID_USE_LIBLOG_EVENT_INTERFACE
788
789/* For manipulating lists of events. */
790
791#define ANDROID_MAX_LIST_NEST_DEPTH 8
792
793/*
794 * The opaque context used to manipulate lists of events.
795 */
796#ifndef __android_log_context_defined
797#define __android_log_context_defined
798typedef struct android_log_context_internal* android_log_context;
799#endif
800
801/*
802 * Elements returned when reading a list of events.
803 */
804#ifndef __android_log_list_element_defined
805#define __android_log_list_element_defined
806typedef struct {
807 AndroidEventLogType type;
808 uint16_t complete;
809 uint16_t len;
810 union {
811 int32_t int32;
812 int64_t int64;
813 char* string;
814 float float32;
815 } data;
816} android_log_list_element;
817#endif
818
819/*
820 * Creates a context associated with an event tag to write elements to
821 * the list of events.
822 */
823android_log_context create_android_logger(uint32_t tag);
824
825/* All lists must be braced by a begin and end call */
826/*
827 * NB: If the first level braces are missing when specifying multiple
828 * elements, we will manufacturer a list to embrace it for your API
829 * convenience. For a single element, it will remain solitary.
830 */
831int android_log_write_list_begin(android_log_context ctx);
832int android_log_write_list_end(android_log_context ctx);
833
834int android_log_write_int32(android_log_context ctx, int32_t value);
835int android_log_write_int64(android_log_context ctx, int64_t value);
836int android_log_write_string8(android_log_context ctx, const char* value);
837int android_log_write_string8_len(android_log_context ctx,
838 const char* value, size_t maxlen);
839int android_log_write_float32(android_log_context ctx, float value);
840
841/* Submit the composed list context to the specified logger id */
842/* NB: LOG_ID_EVENTS and LOG_ID_SECURITY only valid binary buffers */
843int android_log_write_list(android_log_context ctx, log_id_t id);
844
845/*
846 * Creates a context from a raw buffer representing a list of events to be read.
847 */
848android_log_context create_android_log_parser(const char* msg, size_t len);
849
850android_log_list_element android_log_read_next(android_log_context ctx);
851android_log_list_element android_log_peek_next(android_log_context ctx);
852
853/* Finished with reader or writer context */
854int android_log_destroy(android_log_context* ctx);
855
Mark Salyzynaeaaf812016-09-30 13:30:33 -0700856#ifdef __cplusplus
857#ifndef __class_android_log_event_context
858#define __class_android_log_event_context
859/* android_log_context C++ helpers */
860extern "C++" {
861class android_log_event_context {
862 android_log_context ctx;
863 int ret;
864
865 android_log_event_context(const android_log_event_context&) = delete;
866 void operator =(const android_log_event_context&) = delete;
867
868public:
869 explicit android_log_event_context(int tag) : ret(0) {
870 ctx = create_android_logger(static_cast<uint32_t>(tag));
871 }
872 explicit android_log_event_context(log_msg& log_msg) : ret(0) {
873 ctx = create_android_log_parser(log_msg.msg() + sizeof(uint32_t),
874 log_msg.entry.len - sizeof(uint32_t));
875 }
876 ~android_log_event_context() { android_log_destroy(&ctx); }
877
878 int close() {
879 int retval = android_log_destroy(&ctx);
880 if (retval < 0) ret = retval;
881 return retval;
882 }
883
884 /* To allow above C calls to use this class as parameter */
885 operator android_log_context() const { return ctx; }
886
887 int status() const { return ret; }
888
889 int begin() {
890 int retval = android_log_write_list_begin(ctx);
891 if (retval < 0) ret = retval;
892 return ret;
893 }
894 int end() {
895 int retval = android_log_write_list_end(ctx);
896 if (retval < 0) ret = retval;
897 return ret;
898 }
899
900 android_log_event_context& operator <<(int32_t value) {
901 int retval = android_log_write_int32(ctx, value);
902 if (retval < 0) ret = retval;
903 return *this;
904 }
905 android_log_event_context& operator <<(uint32_t value) {
906 int retval = android_log_write_int32(ctx, static_cast<int32_t>(value));
907 if (retval < 0) ret = retval;
908 return *this;
909 }
910 android_log_event_context& operator <<(int64_t value) {
911 int retval = android_log_write_int64(ctx, value);
912 if (retval < 0) ret = retval;
913 return *this;
914 }
915 android_log_event_context& operator <<(uint64_t value) {
916 int retval = android_log_write_int64(ctx, static_cast<int64_t>(value));
917 if (retval < 0) ret = retval;
918 return *this;
919 }
920 android_log_event_context& operator <<(const char* value) {
921 int retval = android_log_write_string8(ctx, value);
922 if (retval < 0) ret = retval;
923 return *this;
924 }
925#if defined(_USING_LIBCXX)
926 android_log_event_context& operator <<(const std::string& value) {
927 int retval = android_log_write_string8_len(ctx,
928 value.data(),
929 value.length());
930 if (retval < 0) ret = retval;
931 return *this;
932 }
933#endif
934 android_log_event_context& operator <<(float value) {
935 int retval = android_log_write_float32(ctx, value);
936 if (retval < 0) ret = retval;
937 return *this;
938 }
939
940 int write(log_id_t id = LOG_ID_EVENTS) {
941 int retval = android_log_write_list(ctx, id);
942 if (retval < 0) ret = retval;
943 return ret;
944 }
945
946 int operator <<(log_id_t id) {
947 int retval = android_log_write_list(ctx, id);
948 if (retval < 0) ret = retval;
949 android_log_destroy(&ctx);
950 return ret;
951 }
952
953 /*
954 * Append should be a lesser-used interface, but adds
955 * access to string with length. So we offer all types.
956 */
957 template <typename Tvalue>
958 bool Append(Tvalue value) { *this << value; return ret >= 0; }
959
960 bool Append(const char* value, size_t len) {
961 int retval = android_log_write_string8_len(ctx, value, len);
962 if (retval < 0) ret = retval;
963 return ret >= 0;
964 }
965
966 android_log_list_element read() { return android_log_read_next(ctx); }
967 android_log_list_element peek() { return android_log_peek_next(ctx); }
968
969};
970}
971#endif
972#endif
973
Mark Salyzyn749a2982016-10-11 07:34:52 -0700974#endif /* __ANDROID_USE_LIBLOG_EVENT_INTERFACE */
975
976/* --------------------------------------------------------------------- */
977
978#ifndef _ANDROID_USE_LIBLOG_SAFETYNET_INTERFACE
979#ifndef __ANDROID_API__
980#define __ANDROID_USE_LIBLOG_SAFETYNET_INTERFACE 1
981#elif __ANDROID_API__ > 22 /* > Lollipop */
982#define __ANDROID_USE_LIBLOG_SAFETYNET_INTERFACE 1
983#else
984#define __ANDROID_USE_LIBLOG_SAFETYNET_INTERFACE 0
985#endif
986#endif
987
988#if __ANDROID_USE_LIBLOG_SAFETYNET_INTERFACE
989
990#define android_errorWriteLog(tag, subTag) \
991 __android_log_error_write(tag, subTag, -1, NULL, 0)
992
993#define android_errorWriteWithInfoLog(tag, subTag, uid, data, dataLen) \
994 __android_log_error_write(tag, subTag, uid, data, dataLen)
995
996int __android_log_error_write(int tag, const char* subTag, int32_t uid,
997 const char* data, uint32_t dataLen);
998
999#endif /* __ANDROID_USE_LIBLOG_SAFETYNET_INTERFACE */
1000
1001/* --------------------------------------------------------------------- */
1002
1003#ifndef __ANDROID_USE_LIBLOG_CLOSE_INTERFACE
1004#ifndef __ANDROID_API__
1005#define __ANDROID_USE_LIBLOG_CLOSE_INTERFACE 1
1006#elif __ANDROID_API__ > 18 /* > JellyBean */
1007#define __ANDROID_USE_LIBLOG_CLOSE_INTERFACE 1
1008#else
1009#define __ANDROID_USE_LIBLOG_CLOSE_INTERFACE 0
1010#endif
1011#endif
1012
1013#if __ANDROID_USE_LIBLOG_CLOSE_INTERFACE
1014/*
1015 * Release any logger resources (a new log write will immediately re-acquire)
1016 *
1017 * May be used to clean up File descriptors after a Fork, the resources are
1018 * all O_CLOEXEC so wil self clean on exec().
1019 */
1020void __android_log_close();
1021#endif
1022
1023#if defined(__clang__)
1024#pragma clang diagnostic pop
1025#endif
1026
1027#ifdef __cplusplus
1028}
1029#endif
1030
1031#endif /* _LIBS_LOG_LOG_H */