blob: cd0a2161b39b0fce5f24c936ec48804e1cd5ace1 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
Mark Salyzyne9c41962014-01-02 13:52:29 -08002 * Copyright (C) 2007-2014 The Android Open Source Project
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08003 *
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 */
Mark Salyzyn31dd00f2015-03-04 17:01:30 -080016#if (FAKE_LOG_DEVICE == 0)
Mark Salyzynd45d36e2015-02-12 15:14:26 -080017#include <endian.h>
Mark Salyzyn31dd00f2015-03-04 17:01:30 -080018#endif
Mark Salyzyn154f4602014-02-20 14:59:07 -080019#include <errno.h>
20#include <fcntl.h>
Yabin Cui4a6e5a32015-01-26 19:48:54 -080021#if !defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080022#include <pthread.h>
23#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080024#include <stdarg.h>
Mark Salyzynd45d36e2015-02-12 15:14:26 -080025#include <stdatomic.h>
Mark Salyzyn154f4602014-02-20 14:59:07 -080026#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
Nick Kralevicha1703222013-03-15 09:45:12 -070029#include <sys/stat.h>
Mark Salyzyn154f4602014-02-20 14:59:07 -080030#include <sys/types.h>
31#if (FAKE_LOG_DEVICE == 0)
32#include <sys/socket.h>
33#include <sys/un.h>
34#endif
35#include <time.h>
36#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080037
Dan Albertc68fedc2014-08-18 17:29:34 -070038#ifdef __BIONIC__
39#include <android/set_abort_message.h>
40#endif
41
Colin Cross9227bd32013-07-23 16:59:20 -070042#include <log/logd.h>
Mark Salyzyn154f4602014-02-20 14:59:07 -080043#include <log/logger.h>
44#include <log/log_read.h>
45#include <private/android_filesystem_config.h>
Mark Salyzyn7a809402015-01-16 13:38:07 -080046#include <private/android_logger.h>
Mark Salyzyne9c41962014-01-02 13:52:29 -080047
Mark Salyzyncf4aa032013-11-22 07:54:30 -080048#define LOG_BUF_SIZE 1024
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049
50#if FAKE_LOG_DEVICE
Mark Salyzyn154f4602014-02-20 14:59:07 -080051/* This will be defined when building for the host. */
Kristian Monsenb5a98902014-01-28 11:26:56 -080052#include "fake_log_device.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053#endif
54
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055static int __write_to_log_init(log_id_t, struct iovec *vec, size_t nr);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -080056static int (*write_to_log)(log_id_t, struct iovec *vec, size_t nr) = __write_to_log_init;
Yabin Cui4a6e5a32015-01-26 19:48:54 -080057#if !defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080058static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
59#endif
60
Mark Salyzyna04464a2014-04-30 08:50:53 -070061#ifndef __unused
62#define __unused __attribute__((__unused__))
63#endif
Kristian Monsenb5a98902014-01-28 11:26:56 -080064
Mark Salyzyn154f4602014-02-20 14:59:07 -080065#if FAKE_LOG_DEVICE
Mark Salyzyn99f47a92014-04-07 14:58:08 -070066static int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1, -1, -1 };
Mark Salyzyna04464a2014-04-30 08:50:53 -070067#else
68static int logd_fd = -1;
Mark Salyzynd91ab582014-12-15 10:52:12 -080069static int pstore_fd = -1;
Mark Salyzyn154f4602014-02-20 14:59:07 -080070#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080071
72/*
73 * This is used by the C++ code to decide if it should write logs through
Mark Salyzyn154f4602014-02-20 14:59:07 -080074 * the C code. Basically, if /dev/socket/logd is available, we're running in
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080075 * the simulator rather than a desktop tool and want to use the device.
76 */
77static enum {
Chris Pearson19299902010-06-02 16:25:35 -070078 kLogUninitialized, kLogNotAvailable, kLogAvailable
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080079} g_log_status = kLogUninitialized;
Mark Salyzyn53016d82015-02-04 12:28:47 -080080
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080081int __android_log_dev_available(void)
82{
83 if (g_log_status == kLogUninitialized) {
Mark Salyzyn154f4602014-02-20 14:59:07 -080084 if (access("/dev/socket/logdw", W_OK) == 0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080085 g_log_status = kLogAvailable;
86 else
87 g_log_status = kLogNotAvailable;
88 }
89
90 return (g_log_status == kLogAvailable);
91}
92
Mark Salyzyna04464a2014-04-30 08:50:53 -070093#if !FAKE_LOG_DEVICE
Mark Salyzyn8245af12014-03-25 13:45:33 -070094/* give up, resources too limited */
Mark Salyzyna04464a2014-04-30 08:50:53 -070095static int __write_to_log_null(log_id_t log_fd __unused, struct iovec *vec __unused,
96 size_t nr __unused)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080097{
98 return -1;
99}
Mark Salyzyna04464a2014-04-30 08:50:53 -0700100#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800101
Mark Salyzyn8245af12014-03-25 13:45:33 -0700102/* log_init_lock assumed */
103static int __write_to_log_initialize()
104{
105 int i, ret = 0;
106
107#if FAKE_LOG_DEVICE
108 for (i = 0; i < LOG_ID_MAX; i++) {
109 char buf[sizeof("/dev/log_system")];
110 snprintf(buf, sizeof(buf), "/dev/log_%s", android_log_id_to_name(i));
111 log_fds[i] = fakeLogOpen(buf, O_WRONLY);
112 }
113#else
114 if (logd_fd >= 0) {
115 i = logd_fd;
116 logd_fd = -1;
117 close(i);
118 }
Mark Salyzynd91ab582014-12-15 10:52:12 -0800119 if (pstore_fd >= 0) {
120 i = pstore_fd;
121 pstore_fd = -1;
122 close(i);
123 }
124 pstore_fd = open("/dev/pmsg0", O_WRONLY);
Mark Salyzyn8245af12014-03-25 13:45:33 -0700125
Nick Kralevich118d1b32014-07-02 22:30:39 -0700126 i = socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
Mark Salyzyn8245af12014-03-25 13:45:33 -0700127 if (i < 0) {
128 ret = -errno;
129 write_to_log = __write_to_log_null;
130 } else if (fcntl(i, F_SETFL, O_NONBLOCK) < 0) {
131 ret = -errno;
132 close(i);
133 i = -1;
134 write_to_log = __write_to_log_null;
135 } else {
136 struct sockaddr_un un;
137 memset(&un, 0, sizeof(struct sockaddr_un));
138 un.sun_family = AF_UNIX;
139 strcpy(un.sun_path, "/dev/socket/logdw");
140
141 if (connect(i, (struct sockaddr *)&un, sizeof(struct sockaddr_un)) < 0) {
142 ret = -errno;
143 close(i);
144 i = -1;
145 }
146 }
147 logd_fd = i;
148#endif
149
150 return ret;
151}
152
Mark Salyzyn53016d82015-02-04 12:28:47 -0800153static int __write_to_log_daemon(log_id_t log_id, struct iovec *vec, size_t nr)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800154{
155 ssize_t ret;
Mark Salyzyn8245af12014-03-25 13:45:33 -0700156#if FAKE_LOG_DEVICE
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800157 int log_fd;
158
159 if (/*(int)log_id >= 0 &&*/ (int)log_id < (int)LOG_ID_MAX) {
160 log_fd = log_fds[(int)log_id];
161 } else {
Mark Salyzyn8245af12014-03-25 13:45:33 -0700162 return -EBADF;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800163 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164 do {
Mark Salyzyn154f4602014-02-20 14:59:07 -0800165 ret = fakeLogWritev(log_fd, vec, nr);
Mark Salyzyn8245af12014-03-25 13:45:33 -0700166 if (ret < 0) {
167 ret = -errno;
168 }
169 } while (ret == -EINTR);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800170#else
Mark Salyzynd91ab582014-12-15 10:52:12 -0800171 static const unsigned header_length = 2;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700172 struct iovec newVec[nr + header_length];
Mark Salyzynd91ab582014-12-15 10:52:12 -0800173 android_log_header_t header;
174 android_pmsg_log_header_t pmsg_header;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700175 struct timespec ts;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700176 size_t i, payload_size;
Mark Salyzyn076ba812014-05-29 17:53:41 -0700177 static uid_t last_uid = AID_ROOT; /* logd *always* starts up as AID_ROOT */
Mark Salyzynd91ab582014-12-15 10:52:12 -0800178 static pid_t last_pid = (pid_t) -1;
Mark Salyzynd45d36e2015-02-12 15:14:26 -0800179 static atomic_int_fast32_t dropped;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700180
Mark Salyzyn076ba812014-05-29 17:53:41 -0700181 if (last_uid == AID_ROOT) { /* have we called to get the UID yet? */
182 last_uid = getuid();
183 }
Mark Salyzynd91ab582014-12-15 10:52:12 -0800184 if (last_pid == (pid_t) -1) {
185 last_pid = getpid();
Mark Salyzyn154f4602014-02-20 14:59:07 -0800186 }
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700187 /*
188 * struct {
Mark Salyzyn53016d82015-02-04 12:28:47 -0800189 * // what we provide to pstore
Mark Salyzynd91ab582014-12-15 10:52:12 -0800190 * android_pmsg_log_header_t pmsg_header;
191 * // what we provide to socket
Mark Salyzyn7a809402015-01-16 13:38:07 -0800192 * android_log_header_t header;
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700193 * // caller provides
194 * union {
195 * struct {
196 * char prio;
197 * char payload[];
198 * } string;
199 * struct {
200 * uint32_t tag
201 * char payload[];
202 * } binary;
203 * };
204 * };
205 */
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700206
Mark Salyzyn076ba812014-05-29 17:53:41 -0700207 clock_gettime(CLOCK_REALTIME, &ts);
Mark Salyzyn076ba812014-05-29 17:53:41 -0700208
Mark Salyzynd91ab582014-12-15 10:52:12 -0800209 pmsg_header.magic = LOGGER_MAGIC;
210 pmsg_header.len = sizeof(pmsg_header) + sizeof(header);
211 pmsg_header.uid = last_uid;
212 pmsg_header.pid = last_pid;
213
Mark Salyzyn7a809402015-01-16 13:38:07 -0800214 header.tid = gettid();
215 header.realtime.tv_sec = ts.tv_sec;
216 header.realtime.tv_nsec = ts.tv_nsec;
Mark Salyzyn154f4602014-02-20 14:59:07 -0800217
Mark Salyzynd91ab582014-12-15 10:52:12 -0800218 newVec[0].iov_base = (unsigned char *) &pmsg_header;
219 newVec[0].iov_len = sizeof(pmsg_header);
220 newVec[1].iov_base = (unsigned char *) &header;
221 newVec[1].iov_len = sizeof(header);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800222
Mark Salyzynd45d36e2015-02-12 15:14:26 -0800223 if (logd_fd > 0) {
224 int32_t snapshot = atomic_exchange_explicit(&dropped, 0, memory_order_relaxed);
225 if (snapshot) {
226 android_log_event_int_t buffer;
227
228 header.id = LOG_ID_EVENTS;
229 buffer.header.tag = htole32(LIBLOG_LOG_TAG);
230 buffer.payload.type = EVENT_TYPE_INT;
231 buffer.payload.data = htole32(snapshot);
232
233 newVec[2].iov_base = &buffer;
234 newVec[2].iov_len = sizeof(buffer);
235
236 ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, 2));
237 if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) {
238 atomic_fetch_add_explicit(&dropped, snapshot, memory_order_relaxed);
239 }
240 }
241 }
242
243 header.id = log_id;
244
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700245 for (payload_size = 0, i = header_length; i < nr + header_length; i++) {
246 newVec[i].iov_base = vec[i - header_length].iov_base;
247 payload_size += newVec[i].iov_len = vec[i - header_length].iov_len;
248
249 if (payload_size > LOGGER_ENTRY_MAX_PAYLOAD) {
250 newVec[i].iov_len -= payload_size - LOGGER_ENTRY_MAX_PAYLOAD;
251 if (newVec[i].iov_len) {
252 ++i;
253 }
Mark Salyzynd91ab582014-12-15 10:52:12 -0800254 payload_size = LOGGER_ENTRY_MAX_PAYLOAD;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700255 break;
256 }
Mark Salyzyn154f4602014-02-20 14:59:07 -0800257 }
Mark Salyzynd91ab582014-12-15 10:52:12 -0800258 pmsg_header.len += payload_size;
259
260 if (pstore_fd >= 0) {
261 TEMP_FAILURE_RETRY(writev(pstore_fd, newVec, i));
262 }
263
264 if (last_uid == AID_LOGD) { /* logd, after initialization and priv drop */
265 /*
266 * ignore log messages we send to ourself (logd).
267 * Such log messages are often generated by libraries we depend on
268 * which use standard Android logging.
269 */
270 return 0;
271 }
272
273 if (logd_fd < 0) {
274 return -EBADF;
275 }
Mark Salyzyn154f4602014-02-20 14:59:07 -0800276
Mark Salyzyn8245af12014-03-25 13:45:33 -0700277 /*
278 * The write below could be lost, but will never block.
279 *
Mark Salyzynd91ab582014-12-15 10:52:12 -0800280 * To logd, we drop the pmsg_header
281 *
Mark Salyzyn8245af12014-03-25 13:45:33 -0700282 * ENOTCONN occurs if logd dies.
283 * EAGAIN occurs if logd is overloaded.
284 */
Mark Salyzynd91ab582014-12-15 10:52:12 -0800285 ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, i - 1));
Mark Salyzyn8245af12014-03-25 13:45:33 -0700286 if (ret < 0) {
287 ret = -errno;
288 if (ret == -ENOTCONN) {
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800289#if !defined(_WIN32)
Mark Salyzyn8245af12014-03-25 13:45:33 -0700290 pthread_mutex_lock(&log_init_lock);
291#endif
292 ret = __write_to_log_initialize();
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800293#if !defined(_WIN32)
Mark Salyzyn8245af12014-03-25 13:45:33 -0700294 pthread_mutex_unlock(&log_init_lock);
295#endif
296
297 if (ret < 0) {
298 return ret;
299 }
300
Mark Salyzynd91ab582014-12-15 10:52:12 -0800301 ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, i - 1));
Mark Salyzyn8245af12014-03-25 13:45:33 -0700302 if (ret < 0) {
303 ret = -errno;
304 }
305 }
306 }
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700307
Mark Salyzyn7a809402015-01-16 13:38:07 -0800308 if (ret > (ssize_t)sizeof(header)) {
309 ret -= sizeof(header);
Mark Salyzynd45d36e2015-02-12 15:14:26 -0800310 } else if (ret == -EAGAIN) {
311 atomic_fetch_add_explicit(&dropped, 1, memory_order_relaxed);
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700312 }
Mark Salyzyn154f4602014-02-20 14:59:07 -0800313#endif
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700314
315 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800316}
317
Mark Salyzyn154f4602014-02-20 14:59:07 -0800318#if FAKE_LOG_DEVICE
319static const char *LOG_NAME[LOG_ID_MAX] = {
320 [LOG_ID_MAIN] = "main",
321 [LOG_ID_RADIO] = "radio",
322 [LOG_ID_EVENTS] = "events",
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700323 [LOG_ID_SYSTEM] = "system",
324 [LOG_ID_CRASH] = "crash"
Mark Salyzyn154f4602014-02-20 14:59:07 -0800325};
326
Adam Lesinskia1ac84c2014-10-20 12:31:30 -0700327const char *android_log_id_to_name(log_id_t log_id)
Mark Salyzyn154f4602014-02-20 14:59:07 -0800328{
329 if (log_id >= LOG_ID_MAX) {
330 log_id = LOG_ID_MAIN;
331 }
332 return LOG_NAME[log_id];
333}
334#endif
335
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800336static int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr)
337{
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800338#if !defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800339 pthread_mutex_lock(&log_init_lock);
340#endif
341
342 if (write_to_log == __write_to_log_init) {
Mark Salyzyn8245af12014-03-25 13:45:33 -0700343 int ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800344
Mark Salyzyn8245af12014-03-25 13:45:33 -0700345 ret = __write_to_log_initialize();
346 if (ret < 0) {
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800347#if !defined(_WIN32)
Mark Salyzyn8245af12014-03-25 13:45:33 -0700348 pthread_mutex_unlock(&log_init_lock);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800349#endif
Mark Salyzyn8245af12014-03-25 13:45:33 -0700350 return ret;
351 }
352
Mark Salyzyn53016d82015-02-04 12:28:47 -0800353 write_to_log = __write_to_log_daemon;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800354 }
355
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800356#if !defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800357 pthread_mutex_unlock(&log_init_lock);
358#endif
359
360 return write_to_log(log_id, vec, nr);
361}
362
363int __android_log_write(int prio, const char *tag, const char *msg)
364{
365 struct iovec vec[3];
366 log_id_t log_id = LOG_ID_MAIN;
Wink Saville3761e962012-11-28 12:20:19 -0800367 char tmp_tag[32];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800368
369 if (!tag)
370 tag = "";
371
372 /* XXX: This needs to go! */
373 if (!strcmp(tag, "HTC_RIL") ||
John Michelaued7ccae2009-08-19 10:01:55 -0500374 !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
Jeff Sharkey84dcf092012-08-13 11:27:30 -0700375 !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800376 !strcmp(tag, "AT") ||
377 !strcmp(tag, "GSM") ||
Wink Saville89efdc92009-04-02 11:00:57 -0700378 !strcmp(tag, "STK") ||
379 !strcmp(tag, "CDMA") ||
380 !strcmp(tag, "PHONE") ||
Wink Saville3761e962012-11-28 12:20:19 -0800381 !strcmp(tag, "SMS")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800382 log_id = LOG_ID_RADIO;
Mark Salyzyn8245af12014-03-25 13:45:33 -0700383 /* Inform third party apps/ril/radio.. to use Rlog or RLOG */
Wink Saville3761e962012-11-28 12:20:19 -0800384 snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
385 tag = tmp_tag;
386 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800387
Elliott Hughes26864bf2014-05-06 20:40:15 -0700388#if __BIONIC__
389 if (prio == ANDROID_LOG_FATAL) {
Dan Albertc68fedc2014-08-18 17:29:34 -0700390 android_set_abort_message(msg);
Elliott Hughes26864bf2014-05-06 20:40:15 -0700391 }
392#endif
393
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800394 vec[0].iov_base = (unsigned char *) &prio;
395 vec[0].iov_len = 1;
396 vec[1].iov_base = (void *) tag;
397 vec[1].iov_len = strlen(tag) + 1;
398 vec[2].iov_base = (void *) msg;
399 vec[2].iov_len = strlen(msg) + 1;
400
401 return write_to_log(log_id, vec, 3);
402}
403
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800404int __android_log_buf_write(int bufID, int prio, const char *tag, const char *msg)
405{
406 struct iovec vec[3];
Wink Saville3761e962012-11-28 12:20:19 -0800407 char tmp_tag[32];
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800408
409 if (!tag)
410 tag = "";
411
412 /* XXX: This needs to go! */
Wink Saville3761e962012-11-28 12:20:19 -0800413 if ((bufID != LOG_ID_RADIO) &&
414 (!strcmp(tag, "HTC_RIL") ||
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800415 !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
Jeff Sharkey84dcf092012-08-13 11:27:30 -0700416 !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800417 !strcmp(tag, "AT") ||
418 !strcmp(tag, "GSM") ||
419 !strcmp(tag, "STK") ||
420 !strcmp(tag, "CDMA") ||
421 !strcmp(tag, "PHONE") ||
Wink Saville3761e962012-11-28 12:20:19 -0800422 !strcmp(tag, "SMS"))) {
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800423 bufID = LOG_ID_RADIO;
Mark Salyzyn8245af12014-03-25 13:45:33 -0700424 /* Inform third party apps/ril/radio.. to use Rlog or RLOG */
Wink Saville3761e962012-11-28 12:20:19 -0800425 snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
426 tag = tmp_tag;
427 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800428
429 vec[0].iov_base = (unsigned char *) &prio;
430 vec[0].iov_len = 1;
431 vec[1].iov_base = (void *) tag;
432 vec[1].iov_len = strlen(tag) + 1;
433 vec[2].iov_base = (void *) msg;
434 vec[2].iov_len = strlen(msg) + 1;
435
436 return write_to_log(bufID, vec, 3);
437}
438
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800439int __android_log_vprint(int prio, const char *tag, const char *fmt, va_list ap)
440{
Chris Pearson19299902010-06-02 16:25:35 -0700441 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800442
443 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
444
445 return __android_log_write(prio, tag, buf);
446}
447
448int __android_log_print(int prio, const char *tag, const char *fmt, ...)
449{
450 va_list ap;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800451 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800452
453 va_start(ap, fmt);
454 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
455 va_end(ap);
456
457 return __android_log_write(prio, tag, buf);
458}
459
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800460int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...)
461{
462 va_list ap;
463 char buf[LOG_BUF_SIZE];
464
465 va_start(ap, fmt);
466 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
467 va_end(ap);
468
469 return __android_log_buf_write(bufID, prio, tag, buf);
470}
471
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800472void __android_log_assert(const char *cond, const char *tag,
Mark Salyzyncf4aa032013-11-22 07:54:30 -0800473 const char *fmt, ...)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800474{
Chris Pearson19299902010-06-02 16:25:35 -0700475 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800476
Chris Pearson19299902010-06-02 16:25:35 -0700477 if (fmt) {
478 va_list ap;
479 va_start(ap, fmt);
480 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
481 va_end(ap);
482 } else {
483 /* Msg not provided, log condition. N.B. Do not use cond directly as
484 * format string as it could contain spurious '%' syntax (e.g.
485 * "%d" in "blocks%devs == 0").
486 */
487 if (cond)
488 snprintf(buf, LOG_BUF_SIZE, "Assertion failed: %s", cond);
489 else
490 strcpy(buf, "Unspecified assertion failed");
491 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800492
493 __android_log_write(ANDROID_LOG_FATAL, tag, buf);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800494 __builtin_trap(); /* trap so we have a chance to debug the situation */
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700495 /* NOTREACHED */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800496}
497
498int __android_log_bwrite(int32_t tag, const void *payload, size_t len)
499{
500 struct iovec vec[2];
501
502 vec[0].iov_base = &tag;
503 vec[0].iov_len = sizeof(tag);
504 vec[1].iov_base = (void*)payload;
505 vec[1].iov_len = len;
506
507 return write_to_log(LOG_ID_EVENTS, vec, 2);
508}
509
510/*
511 * Like __android_log_bwrite, but takes the type as well. Doesn't work
512 * for the general case where we're generating lists of stuff, but very
513 * handy if we just want to dump an integer into the log.
514 */
515int __android_log_btwrite(int32_t tag, char type, const void *payload,
Mark Salyzyn154f4602014-02-20 14:59:07 -0800516 size_t len)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800517{
518 struct iovec vec[3];
519
520 vec[0].iov_base = &tag;
521 vec[0].iov_len = sizeof(tag);
522 vec[1].iov_base = &type;
523 vec[1].iov_len = sizeof(type);
524 vec[2].iov_base = (void*)payload;
525 vec[2].iov_len = len;
526
527 return write_to_log(LOG_ID_EVENTS, vec, 3);
528}
Nick Kralevich2a4d05a2014-07-01 10:57:16 -0700529
530/*
531 * Like __android_log_bwrite, but used for writing strings to the
532 * event log.
533 */
534int __android_log_bswrite(int32_t tag, const char *payload)
535{
536 struct iovec vec[4];
537 char type = EVENT_TYPE_STRING;
538 uint32_t len = strlen(payload);
539
540 vec[0].iov_base = &tag;
541 vec[0].iov_len = sizeof(tag);
542 vec[1].iov_base = &type;
543 vec[1].iov_len = sizeof(type);
544 vec[2].iov_base = &len;
545 vec[2].iov_len = sizeof(len);
546 vec[3].iov_base = (void*)payload;
547 vec[3].iov_len = len;
548
549 return write_to_log(LOG_ID_EVENTS, vec, 4);
550}