blob: 8742b34efa6d52350c37abcf45202bbb10022db9 [file] [log] [blame]
Mark Salyzyn154f4602014-02-20 14:59:07 -08001/*
2 * Copyright (C) 2007-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#include <errno.h>
18#include <fcntl.h>
Mark Salyzyn154f4602014-02-20 14:59:07 -080019#include <pthread.h>
Mark Salyzyn154f4602014-02-20 14:59:07 -080020#include <stdarg.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26#include <time.h>
27#include <unistd.h>
28
Dan Albert40470752014-08-18 17:29:34 -070029#include <android/set_abort_message.h>
Dan Albert40470752014-08-18 17:29:34 -070030
Mark Salyzyn154f4602014-02-20 14:59:07 -080031#include <log/log.h>
32#include <log/logd.h>
33#include <log/logger.h>
34
35#define LOGGER_LOG_MAIN "log/main"
36#define LOGGER_LOG_RADIO "log/radio"
37#define LOGGER_LOG_EVENTS "log/events"
38#define LOGGER_LOG_SYSTEM "log/system"
39
40#define LOG_BUF_SIZE 1024
41
Mark Salyzyn154f4602014-02-20 14:59:07 -080042#define log_open(pathname, flags) open(pathname, (flags) | O_CLOEXEC)
43#define log_writev(filedes, vector, count) writev(filedes, vector, count)
44#define log_close(filedes) close(filedes)
Mark Salyzyn154f4602014-02-20 14:59:07 -080045
46static int __write_to_log_init(log_id_t, struct iovec *vec, size_t nr);
47static int (*write_to_log)(log_id_t, struct iovec *vec, size_t nr) = __write_to_log_init;
Yabin Cui4a6e5a32015-01-26 19:48:54 -080048
Mark Salyzyn154f4602014-02-20 14:59:07 -080049static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
Mark Salyzyn154f4602014-02-20 14:59:07 -080050
Mark Salyzyna04464a2014-04-30 08:50:53 -070051#ifndef __unused
52#define __unused __attribute__((__unused__))
53#endif
Mark Salyzyn154f4602014-02-20 14:59:07 -080054
55static int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1, -1 };
56
57/*
58 * This is used by the C++ code to decide if it should write logs through
59 * the C code. Basically, if /dev/log/... is available, we're running in
60 * the simulator rather than a desktop tool and want to use the device.
61 */
62static enum {
63 kLogUninitialized, kLogNotAvailable, kLogAvailable
64} g_log_status = kLogUninitialized;
65int __android_log_dev_available(void)
66{
67 if (g_log_status == kLogUninitialized) {
68 if (access("/dev/"LOGGER_LOG_MAIN, W_OK) == 0)
69 g_log_status = kLogAvailable;
70 else
71 g_log_status = kLogNotAvailable;
72 }
73
74 return (g_log_status == kLogAvailable);
75}
76
Mark Salyzyna04464a2014-04-30 08:50:53 -070077static int __write_to_log_null(log_id_t log_fd __unused, struct iovec *vec __unused,
78 size_t nr __unused)
Mark Salyzyn154f4602014-02-20 14:59:07 -080079{
80 return -1;
81}
82
83static int __write_to_log_kernel(log_id_t log_id, struct iovec *vec, size_t nr)
84{
85 ssize_t ret;
86 int log_fd;
87
88 if (/*(int)log_id >= 0 &&*/ (int)log_id < (int)LOG_ID_MAX) {
Mark Salyzyn99f47a92014-04-07 14:58:08 -070089 if (log_id == LOG_ID_CRASH) {
90 log_id = LOG_ID_MAIN;
91 }
Mark Salyzyn154f4602014-02-20 14:59:07 -080092 log_fd = log_fds[(int)log_id];
93 } else {
Mark Salyzyn8245af12014-03-25 13:45:33 -070094 return -EBADF;
Mark Salyzyn154f4602014-02-20 14:59:07 -080095 }
96
97 do {
98 ret = log_writev(log_fd, vec, nr);
Mark Salyzyn8245af12014-03-25 13:45:33 -070099 if (ret < 0) {
100 ret = -errno;
101 }
102 } while (ret == -EINTR);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800103
104 return ret;
105}
106
107static int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr)
108{
Mark Salyzyn154f4602014-02-20 14:59:07 -0800109 pthread_mutex_lock(&log_init_lock);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800110
111 if (write_to_log == __write_to_log_init) {
112 log_fds[LOG_ID_MAIN] = log_open("/dev/"LOGGER_LOG_MAIN, O_WRONLY);
113 log_fds[LOG_ID_RADIO] = log_open("/dev/"LOGGER_LOG_RADIO, O_WRONLY);
114 log_fds[LOG_ID_EVENTS] = log_open("/dev/"LOGGER_LOG_EVENTS, O_WRONLY);
115 log_fds[LOG_ID_SYSTEM] = log_open("/dev/"LOGGER_LOG_SYSTEM, O_WRONLY);
116
117 write_to_log = __write_to_log_kernel;
118
119 if (log_fds[LOG_ID_MAIN] < 0 || log_fds[LOG_ID_RADIO] < 0 ||
120 log_fds[LOG_ID_EVENTS] < 0) {
121 log_close(log_fds[LOG_ID_MAIN]);
122 log_close(log_fds[LOG_ID_RADIO]);
123 log_close(log_fds[LOG_ID_EVENTS]);
124 log_fds[LOG_ID_MAIN] = -1;
125 log_fds[LOG_ID_RADIO] = -1;
126 log_fds[LOG_ID_EVENTS] = -1;
127 write_to_log = __write_to_log_null;
128 }
129
130 if (log_fds[LOG_ID_SYSTEM] < 0) {
131 log_fds[LOG_ID_SYSTEM] = log_fds[LOG_ID_MAIN];
132 }
133 }
134
Mark Salyzyn154f4602014-02-20 14:59:07 -0800135 pthread_mutex_unlock(&log_init_lock);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800136
137 return write_to_log(log_id, vec, nr);
138}
139
140int __android_log_write(int prio, const char *tag, const char *msg)
141{
Dan Albertc7aadc42015-04-02 10:32:44 -0700142 return __android_log_buf_write(LOG_ID_MAIN, prio, tag, msg);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800143}
144
145int __android_log_buf_write(int bufID, int prio, const char *tag, const char *msg)
146{
147 struct iovec vec[3];
148 char tmp_tag[32];
149
150 if (!tag)
151 tag = "";
152
153 /* XXX: This needs to go! */
154 if ((bufID != LOG_ID_RADIO) &&
155 (!strcmp(tag, "HTC_RIL") ||
156 !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
157 !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
158 !strcmp(tag, "AT") ||
159 !strcmp(tag, "GSM") ||
160 !strcmp(tag, "STK") ||
161 !strcmp(tag, "CDMA") ||
162 !strcmp(tag, "PHONE") ||
163 !strcmp(tag, "SMS"))) {
164 bufID = LOG_ID_RADIO;
165 /* Inform third party apps/ril/radio.. to use Rlog or RLOG */
166 snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
167 tag = tmp_tag;
168 }
169
Dan Albertc7aadc42015-04-02 10:32:44 -0700170 if (prio == ANDROID_LOG_FATAL) {
171 android_set_abort_message(msg);
172 }
173
Mark Salyzyn154f4602014-02-20 14:59:07 -0800174 vec[0].iov_base = (unsigned char *) &prio;
175 vec[0].iov_len = 1;
176 vec[1].iov_base = (void *) tag;
177 vec[1].iov_len = strlen(tag) + 1;
178 vec[2].iov_base = (void *) msg;
179 vec[2].iov_len = strlen(msg) + 1;
180
181 return write_to_log(bufID, vec, 3);
182}
183
184int __android_log_vprint(int prio, const char *tag, const char *fmt, va_list ap)
185{
186 char buf[LOG_BUF_SIZE];
187
188 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
189
190 return __android_log_write(prio, tag, buf);
191}
192
193int __android_log_print(int prio, const char *tag, const char *fmt, ...)
194{
195 va_list ap;
196 char buf[LOG_BUF_SIZE];
197
198 va_start(ap, fmt);
199 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
200 va_end(ap);
201
202 return __android_log_write(prio, tag, buf);
203}
204
205int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...)
206{
207 va_list ap;
208 char buf[LOG_BUF_SIZE];
209
210 va_start(ap, fmt);
211 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
212 va_end(ap);
213
214 return __android_log_buf_write(bufID, prio, tag, buf);
215}
216
217void __android_log_assert(const char *cond, const char *tag,
218 const char *fmt, ...)
219{
220 char buf[LOG_BUF_SIZE];
221
222 if (fmt) {
223 va_list ap;
224 va_start(ap, fmt);
225 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
226 va_end(ap);
227 } else {
228 /* Msg not provided, log condition. N.B. Do not use cond directly as
229 * format string as it could contain spurious '%' syntax (e.g.
230 * "%d" in "blocks%devs == 0").
231 */
232 if (cond)
233 snprintf(buf, LOG_BUF_SIZE, "Assertion failed: %s", cond);
234 else
235 strcpy(buf, "Unspecified assertion failed");
236 }
237
238 __android_log_write(ANDROID_LOG_FATAL, tag, buf);
Elliott Hughes02ff4b82015-03-07 11:21:37 -0800239 abort(); /* abort so we have a chance to debug the situation */
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700240 /* NOTREACHED */
Mark Salyzyn154f4602014-02-20 14:59:07 -0800241}
242
243int __android_log_bwrite(int32_t tag, const void *payload, size_t len)
244{
245 struct iovec vec[2];
246
247 vec[0].iov_base = &tag;
248 vec[0].iov_len = sizeof(tag);
249 vec[1].iov_base = (void*)payload;
250 vec[1].iov_len = len;
251
252 return write_to_log(LOG_ID_EVENTS, vec, 2);
253}
254
255/*
256 * Like __android_log_bwrite, but takes the type as well. Doesn't work
257 * for the general case where we're generating lists of stuff, but very
258 * handy if we just want to dump an integer into the log.
259 */
260int __android_log_btwrite(int32_t tag, char type, const void *payload,
261 size_t len)
262{
263 struct iovec vec[3];
264
265 vec[0].iov_base = &tag;
266 vec[0].iov_len = sizeof(tag);
267 vec[1].iov_base = &type;
268 vec[1].iov_len = sizeof(type);
269 vec[2].iov_base = (void*)payload;
270 vec[2].iov_len = len;
271
272 return write_to_log(LOG_ID_EVENTS, vec, 3);
273}
Nick Kralevich2a4d05a2014-07-01 10:57:16 -0700274
275/*
276 * Like __android_log_bwrite, but used for writing strings to the
277 * event log.
278 */
279int __android_log_bswrite(int32_t tag, const char *payload)
280{
281 struct iovec vec[4];
282 char type = EVENT_TYPE_STRING;
283 uint32_t len = strlen(payload);
284
285 vec[0].iov_base = &tag;
286 vec[0].iov_len = sizeof(tag);
287 vec[1].iov_base = &type;
288 vec[1].iov_len = sizeof(type);
289 vec[2].iov_base = &len;
290 vec[2].iov_len = sizeof(len);
291 vec[3].iov_base = (void*)payload;
292 vec[3].iov_len = len;
293
294 return write_to_log(LOG_ID_EVENTS, vec, 4);
295}