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 #ifdef __linux__ 15 #include <time.h> /* clockid_t definition */ 16 #endif 17 18 #include <log/log.h> 19 #include <log/log_read.h> 20 21 #ifdef __cplusplus 22 extern "C" { 23 #endif 24 25 /* 26 * The userspace structure for version 1 of the logger_entry ABI. 27 * This structure is returned to userspace by the kernel logger 28 * driver unless an upgrade to a newer ABI version is requested. 29 */ 30 struct logger_entry { 31 uint16_t len; /* length of the payload */ 32 uint16_t __pad; /* no matter what, we get 2 bytes of padding */ 33 int32_t pid; /* generating process's pid */ 34 int32_t tid; /* generating process's tid */ 35 int32_t sec; /* seconds since Epoch */ 36 int32_t nsec; /* nanoseconds */ 37 char msg[0]; /* the entry's payload */ 38 } __attribute__((__packed__)); 39 40 /* 41 * The userspace structure for version 2 of the logger_entry ABI. 42 * This structure is returned to userspace if ioctl(LOGGER_SET_VERSION) 43 * is called with version==2; or used with the user space log daemon. 44 */ 45 struct logger_entry_v2 { 46 uint16_t len; /* length of the payload */ 47 uint16_t hdr_size; /* sizeof(struct logger_entry_v2) */ 48 int32_t pid; /* generating process's pid */ 49 int32_t tid; /* generating process's tid */ 50 int32_t sec; /* seconds since Epoch */ 51 int32_t nsec; /* nanoseconds */ 52 uint32_t euid; /* effective UID of logger */ 53 char msg[0]; /* the entry's payload */ 54 } __attribute__((__packed__)); 55 56 struct logger_entry_v3 { 57 uint16_t len; /* length of the payload */ 58 uint16_t hdr_size; /* sizeof(struct logger_entry_v3) */ 59 int32_t pid; /* generating process's pid */ 60 int32_t tid; /* generating process's tid */ 61 int32_t sec; /* seconds since Epoch */ 62 int32_t nsec; /* nanoseconds */ 63 uint32_t lid; /* log id of the payload */ 64 char msg[0]; /* the entry's payload */ 65 } __attribute__((__packed__)); 66 67 struct logger_entry_v4 { 68 uint16_t len; /* length of the payload */ 69 uint16_t hdr_size; /* sizeof(struct logger_entry_v4) */ 70 int32_t pid; /* generating process's pid */ 71 uint32_t tid; /* generating process's tid */ 72 uint32_t sec; /* seconds since Epoch */ 73 uint32_t nsec; /* nanoseconds */ 74 uint32_t lid; /* log id of the payload, bottom 4 bits currently */ 75 uint32_t uid; /* generating process's uid */ 76 char msg[0]; /* the entry's payload */ 77 } __attribute__((__packed__)); 78 79 /* 80 * The maximum size of the log entry payload that can be 81 * written to the logger. An attempt to write more than 82 * this amount will result in a truncated log entry. 83 */ 84 #define LOGGER_ENTRY_MAX_PAYLOAD 4068 85 86 /* 87 * The maximum size of a log entry which can be read from the 88 * kernel logger driver. An attempt to read less than this amount 89 * may result in read() returning EINVAL. 90 */ 91 #define LOGGER_ENTRY_MAX_LEN (5*1024) 92 93 #define NS_PER_SEC 1000000000ULL 94 95 struct log_msg { 96 union { 97 unsigned char buf[LOGGER_ENTRY_MAX_LEN + 1]; 98 struct logger_entry_v4 entry; 99 struct logger_entry_v4 entry_v4; 100 struct logger_entry_v3 entry_v3; 101 struct logger_entry_v2 entry_v2; 102 struct logger_entry entry_v1; 103 } __attribute__((aligned(4))); 104 #ifdef __cplusplus 105 /* Matching log_time operators */ 106 bool operator== (const log_msg &T) const 107 { 108 return (entry.sec == T.entry.sec) && (entry.nsec == T.entry.nsec); 109 } 110 bool operator!= (const log_msg &T) const 111 { 112 return !(*this == T); 113 } 114 bool operator< (const log_msg &T) const 115 { 116 return (entry.sec < T.entry.sec) 117 || ((entry.sec == T.entry.sec) 118 && (entry.nsec < T.entry.nsec)); 119 } 120 bool operator>= (const log_msg &T) const 121 { 122 return !(*this < T); 123 } 124 bool operator> (const log_msg &T) const 125 { 126 return (entry.sec > T.entry.sec) 127 || ((entry.sec == T.entry.sec) 128 && (entry.nsec > T.entry.nsec)); 129 } 130 bool operator<= (const log_msg &T) const 131 { 132 return !(*this > T); 133 } nseclog_msg134 uint64_t nsec() const 135 { 136 return static_cast<uint64_t>(entry.sec) * NS_PER_SEC + entry.nsec; 137 } 138 139 /* packet methods */ idlog_msg140 log_id_t id() 141 { 142 return (log_id_t) entry.lid; 143 } msglog_msg144 char *msg() 145 { 146 return entry.hdr_size ? (char *) buf + entry.hdr_size : entry_v1.msg; 147 } lenlog_msg148 unsigned int len() 149 { 150 return (entry.hdr_size ? entry.hdr_size : sizeof(entry_v1)) + entry.len; 151 } 152 #endif 153 }; 154 155 struct logger; 156 157 log_id_t android_logger_get_id(struct logger *logger); 158 159 int android_logger_clear(struct logger *logger); 160 long android_logger_get_log_size(struct logger *logger); 161 int android_logger_set_log_size(struct logger *logger, unsigned long size); 162 long android_logger_get_log_readable_size(struct logger *logger); 163 int android_logger_get_log_version(struct logger *logger); 164 165 struct logger_list; 166 167 ssize_t android_logger_get_statistics(struct logger_list *logger_list, 168 char *buf, size_t len); 169 ssize_t android_logger_get_prune_list(struct logger_list *logger_list, 170 char *buf, size_t len); 171 int android_logger_set_prune_list(struct logger_list *logger_list, 172 char *buf, size_t len); 173 174 #define ANDROID_LOG_RDONLY O_RDONLY 175 #define ANDROID_LOG_WRONLY O_WRONLY 176 #define ANDROID_LOG_RDWR O_RDWR 177 #define ANDROID_LOG_ACCMODE O_ACCMODE 178 #define ANDROID_LOG_NONBLOCK O_NONBLOCK 179 #define ANDROID_LOG_WRAP 0x40000000 /* Block until buffer about to wrap */ 180 #define ANDROID_LOG_WRAP_DEFAULT_TIMEOUT 7200 /* 2 hour default */ 181 #define ANDROID_LOG_PSTORE 0x80000000 182 183 struct logger_list *android_logger_list_alloc(int mode, 184 unsigned int tail, 185 pid_t pid); 186 struct logger_list *android_logger_list_alloc_time(int mode, 187 log_time start, 188 pid_t pid); 189 void android_logger_list_free(struct logger_list *logger_list); 190 /* In the purest sense, the following two are orthogonal interfaces */ 191 int android_logger_list_read(struct logger_list *logger_list, 192 struct log_msg *log_msg); 193 194 /* Multiple log_id_t opens */ 195 struct logger *android_logger_open(struct logger_list *logger_list, 196 log_id_t id); 197 #define android_logger_close android_logger_free 198 /* Single log_id_t open */ 199 struct logger_list *android_logger_list_open(log_id_t id, 200 int mode, 201 unsigned int tail, 202 pid_t pid); 203 #define android_logger_list_close android_logger_list_free 204 205 #ifdef __linux__ 206 clockid_t android_log_clockid(); 207 #endif 208 209 /* 210 * log_id_t helpers 211 */ 212 log_id_t android_name_to_log_id(const char *logName); 213 const char *android_log_id_to_name(log_id_t log_id); 214 215 #ifdef __cplusplus 216 } 217 #endif 218 219 #endif /* _LIBS_LOG_LOGGER_H */ 220