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