1 /*
2  * Copyright (C) 2005-2017 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 #pragma once
18 
19 #include <stdint.h>
20 #include <sys/types.h>
21 
22 #include <android/log.h>
23 #include <log/log_time.h>
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 #define ANDROID_LOG_WRAP_DEFAULT_TIMEOUT 7200 /* 2 hour default */
30 
31 /*
32  * Native log reading interface section. See logcat for sample code.
33  *
34  * The preferred API is an exec of logcat. Likely uses of this interface
35  * are if native code suffers from exec or filtration being too costly,
36  * access to raw information, or parsing is an issue.
37  */
38 
39 struct logger_entry {
40   uint16_t len;      /* length of the payload */
41   uint16_t hdr_size; /* sizeof(struct logger_entry) */
42   int32_t pid;       /* generating process's pid */
43   uint32_t tid;      /* generating process's tid */
44   uint32_t sec;      /* seconds since Epoch */
45   uint32_t nsec;     /* nanoseconds */
46   uint32_t lid;      /* log id of the payload, bottom 4 bits currently */
47   uint32_t uid;      /* generating process's uid */
48 };
49 
50 /*
51  * The maximum size of a log entry which can be read.
52  * An attempt to read less than this amount may result
53  * in read() returning EINVAL.
54  */
55 #define LOGGER_ENTRY_MAX_LEN (5 * 1024)
56 
57 struct log_msg {
58   union {
59     unsigned char buf[LOGGER_ENTRY_MAX_LEN + 1];
60     struct logger_entry entry;
61   } __attribute__((aligned(4)));
62 #ifdef __cplusplus
nseclog_msg63   uint64_t nsec() const {
64     return static_cast<uint64_t>(entry.sec) * NS_PER_SEC + entry.nsec;
65   }
idlog_msg66   log_id_t id() {
67     return static_cast<log_id_t>(entry.lid);
68   }
msglog_msg69   char* msg() {
70     unsigned short hdr_size = entry.hdr_size;
71     if (hdr_size >= sizeof(struct log_msg) - sizeof(entry)) {
72       return nullptr;
73     }
74     return reinterpret_cast<char*>(buf) + hdr_size;
75   }
lenlog_msg76   unsigned int len() { return entry.hdr_size + entry.len; }
77 #endif
78 };
79 
80 struct logger;
81 
82 log_id_t android_logger_get_id(struct logger* logger);
83 
84 int android_logger_clear(struct logger* logger);
85 long android_logger_get_log_size(struct logger* logger);
86 int android_logger_set_log_size(struct logger* logger, unsigned long size);
87 long android_logger_get_log_readable_size(struct logger* logger);
88 int android_logger_get_log_version(struct logger* logger);
89 
90 struct logger_list;
91 
92 ssize_t android_logger_get_statistics(struct logger_list* logger_list,
93                                       char* buf, size_t len);
94 ssize_t android_logger_get_prune_list(struct logger_list* logger_list,
95                                       char* buf, size_t len);
96 int android_logger_set_prune_list(struct logger_list* logger_list, const char* buf, size_t len);
97 
98 /* The below values are used for the `mode` argument of the below functions. */
99 /* Note that 0x00000003 were previously used and should be considered reserved. */
100 #define ANDROID_LOG_NONBLOCK 0x00000800
101 #define ANDROID_LOG_WRAP 0x40000000 /* Block until buffer about to wrap */
102 #define ANDROID_LOG_PSTORE 0x80000000
103 
104 struct logger_list* android_logger_list_alloc(int mode, unsigned int tail,
105                                               pid_t pid);
106 struct logger_list* android_logger_list_alloc_time(int mode, log_time start,
107                                                    pid_t pid);
108 void android_logger_list_free(struct logger_list* logger_list);
109 /* In the purest sense, the following two are orthogonal interfaces */
110 int android_logger_list_read(struct logger_list* logger_list,
111                              struct log_msg* log_msg);
112 
113 /* Multiple log_id_t opens */
114 struct logger* android_logger_open(struct logger_list* logger_list, log_id_t id);
115 /* Single log_id_t open */
116 struct logger_list* android_logger_list_open(log_id_t id, int mode,
117                                              unsigned int tail, pid_t pid);
118 #define android_logger_list_close android_logger_list_free
119 
120 #ifdef __cplusplus
121 }
122 #endif
123