1 /*
2 **
3 ** Copyright 2007-2014, The Android Open Source Project
4 **
5 ** This file is dual licensed.  It may be redistributed and/or modified
6 ** under the terms of the Apache 2.0 License OR version 2 of the GNU
7 ** General Public License.
8 */
9 
10 #ifndef _LIBS_LOG_LOGGER_H
11 #define _LIBS_LOG_LOGGER_H
12 
13 #include <stdint.h>
14 #include <log/log.h>
15 #include <log/log_read.h>
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 /*
22  * The userspace structure for version 1 of the logger_entry ABI.
23  * This structure is returned to userspace by the kernel logger
24  * driver unless an upgrade to a newer ABI version is requested.
25  */
26 struct logger_entry {
27     uint16_t    len;    /* length of the payload */
28     uint16_t    __pad;  /* no matter what, we get 2 bytes of padding */
29     int32_t     pid;    /* generating process's pid */
30     int32_t     tid;    /* generating process's tid */
31     int32_t     sec;    /* seconds since Epoch */
32     int32_t     nsec;   /* nanoseconds */
33     char        msg[0]; /* the entry's payload */
34 } __attribute__((__packed__));
35 
36 /*
37  * The userspace structure for version 2 of the logger_entry ABI.
38  * This structure is returned to userspace if ioctl(LOGGER_SET_VERSION)
39  * is called with version==2; or used with the user space log daemon.
40  */
41 struct logger_entry_v2 {
42     uint16_t    len;       /* length of the payload */
43     uint16_t    hdr_size;  /* sizeof(struct logger_entry_v2) */
44     int32_t     pid;       /* generating process's pid */
45     int32_t     tid;       /* generating process's tid */
46     int32_t     sec;       /* seconds since Epoch */
47     int32_t     nsec;      /* nanoseconds */
48     uint32_t    euid;      /* effective UID of logger */
49     char        msg[0];    /* the entry's payload */
50 } __attribute__((__packed__));
51 
52 struct logger_entry_v3 {
53     uint16_t    len;       /* length of the payload */
54     uint16_t    hdr_size;  /* sizeof(struct logger_entry_v3) */
55     int32_t     pid;       /* generating process's pid */
56     int32_t     tid;       /* generating process's tid */
57     int32_t     sec;       /* seconds since Epoch */
58     int32_t     nsec;      /* nanoseconds */
59     uint32_t    lid;       /* log id of the payload */
60     char        msg[0];    /* the entry's payload */
61 } __attribute__((__packed__));
62 
63 /*
64  * The maximum size of the log entry payload that can be
65  * written to the logger. An attempt to write more than
66  * this amount will result in a truncated log entry.
67  */
68 #define LOGGER_ENTRY_MAX_PAYLOAD	4076
69 
70 /*
71  * The maximum size of a log entry which can be read from the
72  * kernel logger driver. An attempt to read less than this amount
73  * may result in read() returning EINVAL.
74  */
75 #define LOGGER_ENTRY_MAX_LEN		(5*1024)
76 
77 #define NS_PER_SEC 1000000000ULL
78 
79 struct log_msg {
80     union {
81         unsigned char buf[LOGGER_ENTRY_MAX_LEN + 1];
82         struct logger_entry_v3 entry;
83         struct logger_entry_v3 entry_v3;
84         struct logger_entry_v2 entry_v2;
85         struct logger_entry    entry_v1;
86     } __attribute__((aligned(4)));
87 #ifdef __cplusplus
88     /* Matching log_time operators */
89     bool operator== (const log_msg &T) const
90     {
91         return (entry.sec == T.entry.sec) && (entry.nsec == T.entry.nsec);
92     }
93     bool operator!= (const log_msg &T) const
94     {
95         return !(*this == T);
96     }
97     bool operator< (const log_msg &T) const
98     {
99         return (entry.sec < T.entry.sec)
100             || ((entry.sec == T.entry.sec)
101              && (entry.nsec < T.entry.nsec));
102     }
103     bool operator>= (const log_msg &T) const
104     {
105         return !(*this < T);
106     }
107     bool operator> (const log_msg &T) const
108     {
109         return (entry.sec > T.entry.sec)
110             || ((entry.sec == T.entry.sec)
111              && (entry.nsec > T.entry.nsec));
112     }
113     bool operator<= (const log_msg &T) const
114     {
115         return !(*this > T);
116     }
nseclog_msg117     uint64_t nsec() const
118     {
119         return static_cast<uint64_t>(entry.sec) * NS_PER_SEC + entry.nsec;
120     }
121 
122     /* packet methods */
idlog_msg123     log_id_t id()
124     {
125         return (log_id_t) entry.lid;
126     }
msglog_msg127     char *msg()
128     {
129         return entry.hdr_size ? (char *) buf + entry.hdr_size : entry_v1.msg;
130     }
lenlog_msg131     unsigned int len()
132     {
133         return (entry.hdr_size ? entry.hdr_size : sizeof(entry_v1)) + entry.len;
134     }
135 #endif
136 };
137 
138 struct logger;
139 
140 log_id_t android_logger_get_id(struct logger *logger);
141 
142 int android_logger_clear(struct logger *logger);
143 long android_logger_get_log_size(struct logger *logger);
144 int android_logger_set_log_size(struct logger *logger, unsigned long size);
145 long android_logger_get_log_readable_size(struct logger *logger);
146 int android_logger_get_log_version(struct logger *logger);
147 
148 struct logger_list;
149 
150 ssize_t android_logger_get_statistics(struct logger_list *logger_list,
151                                       char *buf, size_t len);
152 ssize_t android_logger_get_prune_list(struct logger_list *logger_list,
153                                       char *buf, size_t len);
154 int android_logger_set_prune_list(struct logger_list *logger_list,
155                                   char *buf, size_t len);
156 
157 #define ANDROID_LOG_RDONLY   O_RDONLY
158 #define ANDROID_LOG_WRONLY   O_WRONLY
159 #define ANDROID_LOG_RDWR     O_RDWR
160 #define ANDROID_LOG_ACCMODE  O_ACCMODE
161 #define ANDROID_LOG_NONBLOCK O_NONBLOCK
162 #define ANDROID_LOG_PSTORE   0x80000000
163 
164 struct logger_list *android_logger_list_alloc(int mode,
165                                               unsigned int tail,
166                                               pid_t pid);
167 struct logger_list *android_logger_list_alloc_time(int mode,
168                                                    log_time start,
169                                                    pid_t pid);
170 void android_logger_list_free(struct logger_list *logger_list);
171 /* In the purest sense, the following two are orthogonal interfaces */
172 int android_logger_list_read(struct logger_list *logger_list,
173                              struct log_msg *log_msg);
174 
175 /* Multiple log_id_t opens */
176 struct logger *android_logger_open(struct logger_list *logger_list,
177                                    log_id_t id);
178 #define android_logger_close android_logger_free
179 /* Single log_id_t open */
180 struct logger_list *android_logger_list_open(log_id_t id,
181                                              int mode,
182                                              unsigned int tail,
183                                              pid_t pid);
184 #define android_logger_list_close android_logger_list_free
185 
186 /*
187  * log_id_t helpers
188  */
189 log_id_t android_name_to_log_id(const char *logName);
190 const char *android_log_id_to_name(log_id_t log_id);
191 
192 #ifdef __cplusplus
193 }
194 #endif
195 
196 #endif /* _LIBS_LOG_LOGGER_H */
197