blob: 8f8cc3fd5bebb75ade2f3c1890b01f6e58c1c8a8 [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 Salyzyn31f7df52015-03-13 15:57:11 -0700181 if (!nr) {
182 return -EINVAL;
183 }
184
Mark Salyzyn076ba812014-05-29 17:53:41 -0700185 if (last_uid == AID_ROOT) { /* have we called to get the UID yet? */
186 last_uid = getuid();
187 }
Mark Salyzynd91ab582014-12-15 10:52:12 -0800188 if (last_pid == (pid_t) -1) {
189 last_pid = getpid();
Mark Salyzyn154f4602014-02-20 14:59:07 -0800190 }
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700191 /*
192 * struct {
Mark Salyzyn53016d82015-02-04 12:28:47 -0800193 * // what we provide to pstore
Mark Salyzynd91ab582014-12-15 10:52:12 -0800194 * android_pmsg_log_header_t pmsg_header;
195 * // what we provide to socket
Mark Salyzyn7a809402015-01-16 13:38:07 -0800196 * android_log_header_t header;
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700197 * // caller provides
198 * union {
199 * struct {
200 * char prio;
201 * char payload[];
202 * } string;
203 * struct {
204 * uint32_t tag
205 * char payload[];
206 * } binary;
207 * };
208 * };
209 */
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700210
Mark Salyzyn076ba812014-05-29 17:53:41 -0700211 clock_gettime(CLOCK_REALTIME, &ts);
Mark Salyzyn076ba812014-05-29 17:53:41 -0700212
Mark Salyzynd91ab582014-12-15 10:52:12 -0800213 pmsg_header.magic = LOGGER_MAGIC;
214 pmsg_header.len = sizeof(pmsg_header) + sizeof(header);
215 pmsg_header.uid = last_uid;
216 pmsg_header.pid = last_pid;
217
Mark Salyzyn7a809402015-01-16 13:38:07 -0800218 header.tid = gettid();
219 header.realtime.tv_sec = ts.tv_sec;
220 header.realtime.tv_nsec = ts.tv_nsec;
Mark Salyzyn154f4602014-02-20 14:59:07 -0800221
Mark Salyzynd91ab582014-12-15 10:52:12 -0800222 newVec[0].iov_base = (unsigned char *) &pmsg_header;
223 newVec[0].iov_len = sizeof(pmsg_header);
224 newVec[1].iov_base = (unsigned char *) &header;
225 newVec[1].iov_len = sizeof(header);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800226
Mark Salyzynd45d36e2015-02-12 15:14:26 -0800227 if (logd_fd > 0) {
228 int32_t snapshot = atomic_exchange_explicit(&dropped, 0, memory_order_relaxed);
229 if (snapshot) {
230 android_log_event_int_t buffer;
231
232 header.id = LOG_ID_EVENTS;
233 buffer.header.tag = htole32(LIBLOG_LOG_TAG);
234 buffer.payload.type = EVENT_TYPE_INT;
235 buffer.payload.data = htole32(snapshot);
236
237 newVec[2].iov_base = &buffer;
238 newVec[2].iov_len = sizeof(buffer);
239
240 ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, 2));
241 if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) {
242 atomic_fetch_add_explicit(&dropped, snapshot, memory_order_relaxed);
243 }
244 }
245 }
246
247 header.id = log_id;
248
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700249 for (payload_size = 0, i = header_length; i < nr + header_length; i++) {
250 newVec[i].iov_base = vec[i - header_length].iov_base;
251 payload_size += newVec[i].iov_len = vec[i - header_length].iov_len;
252
253 if (payload_size > LOGGER_ENTRY_MAX_PAYLOAD) {
254 newVec[i].iov_len -= payload_size - LOGGER_ENTRY_MAX_PAYLOAD;
255 if (newVec[i].iov_len) {
256 ++i;
257 }
Mark Salyzynd91ab582014-12-15 10:52:12 -0800258 payload_size = LOGGER_ENTRY_MAX_PAYLOAD;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700259 break;
260 }
Mark Salyzyn154f4602014-02-20 14:59:07 -0800261 }
Mark Salyzynd91ab582014-12-15 10:52:12 -0800262 pmsg_header.len += payload_size;
263
264 if (pstore_fd >= 0) {
265 TEMP_FAILURE_RETRY(writev(pstore_fd, newVec, i));
266 }
267
268 if (last_uid == AID_LOGD) { /* logd, after initialization and priv drop */
269 /*
270 * ignore log messages we send to ourself (logd).
271 * Such log messages are often generated by libraries we depend on
272 * which use standard Android logging.
273 */
274 return 0;
275 }
276
277 if (logd_fd < 0) {
278 return -EBADF;
279 }
Mark Salyzyn154f4602014-02-20 14:59:07 -0800280
Mark Salyzyn8245af12014-03-25 13:45:33 -0700281 /*
282 * The write below could be lost, but will never block.
283 *
Mark Salyzynd91ab582014-12-15 10:52:12 -0800284 * To logd, we drop the pmsg_header
285 *
Mark Salyzyn8245af12014-03-25 13:45:33 -0700286 * ENOTCONN occurs if logd dies.
287 * EAGAIN occurs if logd is overloaded.
288 */
Mark Salyzynd91ab582014-12-15 10:52:12 -0800289 ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, i - 1));
Mark Salyzyn8245af12014-03-25 13:45:33 -0700290 if (ret < 0) {
291 ret = -errno;
292 if (ret == -ENOTCONN) {
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800293#if !defined(_WIN32)
Mark Salyzyn8245af12014-03-25 13:45:33 -0700294 pthread_mutex_lock(&log_init_lock);
295#endif
296 ret = __write_to_log_initialize();
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800297#if !defined(_WIN32)
Mark Salyzyn8245af12014-03-25 13:45:33 -0700298 pthread_mutex_unlock(&log_init_lock);
299#endif
300
301 if (ret < 0) {
302 return ret;
303 }
304
Mark Salyzynd91ab582014-12-15 10:52:12 -0800305 ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, i - 1));
Mark Salyzyn8245af12014-03-25 13:45:33 -0700306 if (ret < 0) {
307 ret = -errno;
308 }
309 }
310 }
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700311
Mark Salyzyn7a809402015-01-16 13:38:07 -0800312 if (ret > (ssize_t)sizeof(header)) {
313 ret -= sizeof(header);
Mark Salyzynd45d36e2015-02-12 15:14:26 -0800314 } else if (ret == -EAGAIN) {
315 atomic_fetch_add_explicit(&dropped, 1, memory_order_relaxed);
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700316 }
Mark Salyzyn154f4602014-02-20 14:59:07 -0800317#endif
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700318
319 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800320}
321
Mark Salyzyn154f4602014-02-20 14:59:07 -0800322#if FAKE_LOG_DEVICE
323static const char *LOG_NAME[LOG_ID_MAX] = {
324 [LOG_ID_MAIN] = "main",
325 [LOG_ID_RADIO] = "radio",
326 [LOG_ID_EVENTS] = "events",
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700327 [LOG_ID_SYSTEM] = "system",
328 [LOG_ID_CRASH] = "crash"
Mark Salyzyn154f4602014-02-20 14:59:07 -0800329};
330
Adam Lesinskia1ac84c2014-10-20 12:31:30 -0700331const char *android_log_id_to_name(log_id_t log_id)
Mark Salyzyn154f4602014-02-20 14:59:07 -0800332{
333 if (log_id >= LOG_ID_MAX) {
334 log_id = LOG_ID_MAIN;
335 }
336 return LOG_NAME[log_id];
337}
338#endif
339
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800340static int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr)
341{
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800342#if !defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800343 pthread_mutex_lock(&log_init_lock);
344#endif
345
346 if (write_to_log == __write_to_log_init) {
Mark Salyzyn8245af12014-03-25 13:45:33 -0700347 int ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800348
Mark Salyzyn8245af12014-03-25 13:45:33 -0700349 ret = __write_to_log_initialize();
350 if (ret < 0) {
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800351#if !defined(_WIN32)
Mark Salyzyn8245af12014-03-25 13:45:33 -0700352 pthread_mutex_unlock(&log_init_lock);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800353#endif
Mark Salyzyn8245af12014-03-25 13:45:33 -0700354 return ret;
355 }
356
Mark Salyzyn53016d82015-02-04 12:28:47 -0800357 write_to_log = __write_to_log_daemon;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800358 }
359
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800360#if !defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800361 pthread_mutex_unlock(&log_init_lock);
362#endif
363
364 return write_to_log(log_id, vec, nr);
365}
366
367int __android_log_write(int prio, const char *tag, const char *msg)
368{
369 struct iovec vec[3];
370 log_id_t log_id = LOG_ID_MAIN;
Wink Saville3761e962012-11-28 12:20:19 -0800371 char tmp_tag[32];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800372
373 if (!tag)
374 tag = "";
375
376 /* XXX: This needs to go! */
377 if (!strcmp(tag, "HTC_RIL") ||
John Michelaued7ccae2009-08-19 10:01:55 -0500378 !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
Jeff Sharkey84dcf092012-08-13 11:27:30 -0700379 !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800380 !strcmp(tag, "AT") ||
381 !strcmp(tag, "GSM") ||
Wink Saville89efdc92009-04-02 11:00:57 -0700382 !strcmp(tag, "STK") ||
383 !strcmp(tag, "CDMA") ||
384 !strcmp(tag, "PHONE") ||
Wink Saville3761e962012-11-28 12:20:19 -0800385 !strcmp(tag, "SMS")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800386 log_id = LOG_ID_RADIO;
Mark Salyzyn8245af12014-03-25 13:45:33 -0700387 /* Inform third party apps/ril/radio.. to use Rlog or RLOG */
Wink Saville3761e962012-11-28 12:20:19 -0800388 snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
389 tag = tmp_tag;
390 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800391
Elliott Hughes26864bf2014-05-06 20:40:15 -0700392#if __BIONIC__
393 if (prio == ANDROID_LOG_FATAL) {
Dan Albertc68fedc2014-08-18 17:29:34 -0700394 android_set_abort_message(msg);
Elliott Hughes26864bf2014-05-06 20:40:15 -0700395 }
396#endif
397
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800398 vec[0].iov_base = (unsigned char *) &prio;
399 vec[0].iov_len = 1;
400 vec[1].iov_base = (void *) tag;
401 vec[1].iov_len = strlen(tag) + 1;
402 vec[2].iov_base = (void *) msg;
403 vec[2].iov_len = strlen(msg) + 1;
404
405 return write_to_log(log_id, vec, 3);
406}
407
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800408int __android_log_buf_write(int bufID, int prio, const char *tag, const char *msg)
409{
410 struct iovec vec[3];
Wink Saville3761e962012-11-28 12:20:19 -0800411 char tmp_tag[32];
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800412
413 if (!tag)
414 tag = "";
415
416 /* XXX: This needs to go! */
Wink Saville3761e962012-11-28 12:20:19 -0800417 if ((bufID != LOG_ID_RADIO) &&
418 (!strcmp(tag, "HTC_RIL") ||
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800419 !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
Jeff Sharkey84dcf092012-08-13 11:27:30 -0700420 !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800421 !strcmp(tag, "AT") ||
422 !strcmp(tag, "GSM") ||
423 !strcmp(tag, "STK") ||
424 !strcmp(tag, "CDMA") ||
425 !strcmp(tag, "PHONE") ||
Wink Saville3761e962012-11-28 12:20:19 -0800426 !strcmp(tag, "SMS"))) {
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800427 bufID = LOG_ID_RADIO;
Mark Salyzyn8245af12014-03-25 13:45:33 -0700428 /* Inform third party apps/ril/radio.. to use Rlog or RLOG */
Wink Saville3761e962012-11-28 12:20:19 -0800429 snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
430 tag = tmp_tag;
431 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800432
433 vec[0].iov_base = (unsigned char *) &prio;
434 vec[0].iov_len = 1;
435 vec[1].iov_base = (void *) tag;
436 vec[1].iov_len = strlen(tag) + 1;
437 vec[2].iov_base = (void *) msg;
438 vec[2].iov_len = strlen(msg) + 1;
439
440 return write_to_log(bufID, vec, 3);
441}
442
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800443int __android_log_vprint(int prio, const char *tag, const char *fmt, va_list ap)
444{
Chris Pearson19299902010-06-02 16:25:35 -0700445 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800446
447 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
448
449 return __android_log_write(prio, tag, buf);
450}
451
452int __android_log_print(int prio, const char *tag, const char *fmt, ...)
453{
454 va_list ap;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800455 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800456
457 va_start(ap, fmt);
458 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
459 va_end(ap);
460
461 return __android_log_write(prio, tag, buf);
462}
463
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800464int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...)
465{
466 va_list ap;
467 char buf[LOG_BUF_SIZE];
468
469 va_start(ap, fmt);
470 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
471 va_end(ap);
472
473 return __android_log_buf_write(bufID, prio, tag, buf);
474}
475
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800476void __android_log_assert(const char *cond, const char *tag,
Mark Salyzyncf4aa032013-11-22 07:54:30 -0800477 const char *fmt, ...)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800478{
Chris Pearson19299902010-06-02 16:25:35 -0700479 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800480
Chris Pearson19299902010-06-02 16:25:35 -0700481 if (fmt) {
482 va_list ap;
483 va_start(ap, fmt);
484 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
485 va_end(ap);
486 } else {
487 /* Msg not provided, log condition. N.B. Do not use cond directly as
488 * format string as it could contain spurious '%' syntax (e.g.
489 * "%d" in "blocks%devs == 0").
490 */
491 if (cond)
492 snprintf(buf, LOG_BUF_SIZE, "Assertion failed: %s", cond);
493 else
494 strcpy(buf, "Unspecified assertion failed");
495 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800496
497 __android_log_write(ANDROID_LOG_FATAL, tag, buf);
Elliott Hughes02ff4b82015-03-07 11:21:37 -0800498 abort(); /* abort so we have a chance to debug the situation */
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700499 /* NOTREACHED */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800500}
501
502int __android_log_bwrite(int32_t tag, const void *payload, size_t len)
503{
504 struct iovec vec[2];
505
506 vec[0].iov_base = &tag;
507 vec[0].iov_len = sizeof(tag);
508 vec[1].iov_base = (void*)payload;
509 vec[1].iov_len = len;
510
511 return write_to_log(LOG_ID_EVENTS, vec, 2);
512}
513
514/*
515 * Like __android_log_bwrite, but takes the type as well. Doesn't work
516 * for the general case where we're generating lists of stuff, but very
517 * handy if we just want to dump an integer into the log.
518 */
519int __android_log_btwrite(int32_t tag, char type, const void *payload,
Mark Salyzyn154f4602014-02-20 14:59:07 -0800520 size_t len)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800521{
522 struct iovec vec[3];
523
524 vec[0].iov_base = &tag;
525 vec[0].iov_len = sizeof(tag);
526 vec[1].iov_base = &type;
527 vec[1].iov_len = sizeof(type);
528 vec[2].iov_base = (void*)payload;
529 vec[2].iov_len = len;
530
531 return write_to_log(LOG_ID_EVENTS, vec, 3);
532}
Nick Kralevich2a4d05a2014-07-01 10:57:16 -0700533
534/*
535 * Like __android_log_bwrite, but used for writing strings to the
536 * event log.
537 */
538int __android_log_bswrite(int32_t tag, const char *payload)
539{
540 struct iovec vec[4];
541 char type = EVENT_TYPE_STRING;
542 uint32_t len = strlen(payload);
543
544 vec[0].iov_base = &tag;
545 vec[0].iov_len = sizeof(tag);
546 vec[1].iov_base = &type;
547 vec[1].iov_len = sizeof(type);
548 vec[2].iov_base = &len;
549 vec[2].iov_len = sizeof(len);
550 vec[3].iov_base = (void*)payload;
551 vec[3].iov_len = len;
552
553 return write_to_log(LOG_ID_EVENTS, vec, 4);
554}