blob: 0208c738960093e721eb7a9618017f2418390a7e [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 Salyzyn154f4602014-02-20 14:59:07 -080016#include <errno.h>
17#include <fcntl.h>
Yabin Cui4a6e5a32015-01-26 19:48:54 -080018#if !defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080019#include <pthread.h>
20#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080021#include <stdarg.h>
Mark Salyzyn154f4602014-02-20 14:59:07 -080022#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
Nick Kralevicha1703222013-03-15 09:45:12 -070025#include <sys/stat.h>
Mark Salyzyn154f4602014-02-20 14:59:07 -080026#include <sys/types.h>
27#if (FAKE_LOG_DEVICE == 0)
28#include <sys/socket.h>
29#include <sys/un.h>
30#endif
31#include <time.h>
32#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033
Dan Albertc68fedc2014-08-18 17:29:34 -070034#ifdef __BIONIC__
35#include <android/set_abort_message.h>
36#endif
37
Colin Cross9227bd32013-07-23 16:59:20 -070038#include <log/logd.h>
Mark Salyzyn154f4602014-02-20 14:59:07 -080039#include <log/logger.h>
40#include <log/log_read.h>
41#include <private/android_filesystem_config.h>
Mark Salyzyn7a809402015-01-16 13:38:07 -080042#include <private/android_logger.h>
Mark Salyzyne9c41962014-01-02 13:52:29 -080043
Mark Salyzyncf4aa032013-11-22 07:54:30 -080044#define LOG_BUF_SIZE 1024
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045
46#if FAKE_LOG_DEVICE
Mark Salyzyn154f4602014-02-20 14:59:07 -080047/* This will be defined when building for the host. */
Kristian Monsenb5a98902014-01-28 11:26:56 -080048#include "fake_log_device.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049#endif
50
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051static int __write_to_log_init(log_id_t, struct iovec *vec, size_t nr);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -080052static int (*write_to_log)(log_id_t, struct iovec *vec, size_t nr) = __write_to_log_init;
Yabin Cui4a6e5a32015-01-26 19:48:54 -080053#if !defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080054static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
55#endif
56
Mark Salyzyna04464a2014-04-30 08:50:53 -070057#ifndef __unused
58#define __unused __attribute__((__unused__))
59#endif
Kristian Monsenb5a98902014-01-28 11:26:56 -080060
Mark Salyzyn154f4602014-02-20 14:59:07 -080061#if FAKE_LOG_DEVICE
Mark Salyzyn99f47a92014-04-07 14:58:08 -070062static int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1, -1, -1 };
Mark Salyzyna04464a2014-04-30 08:50:53 -070063#else
64static int logd_fd = -1;
Mark Salyzynd91ab582014-12-15 10:52:12 -080065static int pstore_fd = -1;
Mark Salyzyn154f4602014-02-20 14:59:07 -080066#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080067
68/*
69 * This is used by the C++ code to decide if it should write logs through
Mark Salyzyn154f4602014-02-20 14:59:07 -080070 * the C code. Basically, if /dev/socket/logd is available, we're running in
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080071 * the simulator rather than a desktop tool and want to use the device.
72 */
73static enum {
Chris Pearson19299902010-06-02 16:25:35 -070074 kLogUninitialized, kLogNotAvailable, kLogAvailable
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080075} g_log_status = kLogUninitialized;
Mark Salyzyn53016d82015-02-04 12:28:47 -080076
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077int __android_log_dev_available(void)
78{
79 if (g_log_status == kLogUninitialized) {
Mark Salyzyn154f4602014-02-20 14:59:07 -080080 if (access("/dev/socket/logdw", W_OK) == 0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080081 g_log_status = kLogAvailable;
82 else
83 g_log_status = kLogNotAvailable;
84 }
85
86 return (g_log_status == kLogAvailable);
87}
88
Mark Salyzyna04464a2014-04-30 08:50:53 -070089#if !FAKE_LOG_DEVICE
Mark Salyzyn8245af12014-03-25 13:45:33 -070090/* give up, resources too limited */
Mark Salyzyna04464a2014-04-30 08:50:53 -070091static int __write_to_log_null(log_id_t log_fd __unused, struct iovec *vec __unused,
92 size_t nr __unused)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080093{
94 return -1;
95}
Mark Salyzyna04464a2014-04-30 08:50:53 -070096#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080097
Mark Salyzyn8245af12014-03-25 13:45:33 -070098/* log_init_lock assumed */
99static int __write_to_log_initialize()
100{
101 int i, ret = 0;
102
103#if FAKE_LOG_DEVICE
104 for (i = 0; i < LOG_ID_MAX; i++) {
105 char buf[sizeof("/dev/log_system")];
106 snprintf(buf, sizeof(buf), "/dev/log_%s", android_log_id_to_name(i));
107 log_fds[i] = fakeLogOpen(buf, O_WRONLY);
108 }
109#else
110 if (logd_fd >= 0) {
111 i = logd_fd;
112 logd_fd = -1;
113 close(i);
114 }
Mark Salyzynd91ab582014-12-15 10:52:12 -0800115 if (pstore_fd >= 0) {
116 i = pstore_fd;
117 pstore_fd = -1;
118 close(i);
119 }
120 pstore_fd = open("/dev/pmsg0", O_WRONLY);
Mark Salyzyn8245af12014-03-25 13:45:33 -0700121
Nick Kralevich118d1b32014-07-02 22:30:39 -0700122 i = socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
Mark Salyzyn8245af12014-03-25 13:45:33 -0700123 if (i < 0) {
124 ret = -errno;
125 write_to_log = __write_to_log_null;
126 } else if (fcntl(i, F_SETFL, O_NONBLOCK) < 0) {
127 ret = -errno;
128 close(i);
129 i = -1;
130 write_to_log = __write_to_log_null;
131 } else {
132 struct sockaddr_un un;
133 memset(&un, 0, sizeof(struct sockaddr_un));
134 un.sun_family = AF_UNIX;
135 strcpy(un.sun_path, "/dev/socket/logdw");
136
137 if (connect(i, (struct sockaddr *)&un, sizeof(struct sockaddr_un)) < 0) {
138 ret = -errno;
139 close(i);
140 i = -1;
141 }
142 }
143 logd_fd = i;
144#endif
145
146 return ret;
147}
148
Mark Salyzyn53016d82015-02-04 12:28:47 -0800149static 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 -0800150{
151 ssize_t ret;
Mark Salyzyn8245af12014-03-25 13:45:33 -0700152#if FAKE_LOG_DEVICE
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800153 int log_fd;
154
155 if (/*(int)log_id >= 0 &&*/ (int)log_id < (int)LOG_ID_MAX) {
156 log_fd = log_fds[(int)log_id];
157 } else {
Mark Salyzyn8245af12014-03-25 13:45:33 -0700158 return -EBADF;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800159 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160 do {
Mark Salyzyn154f4602014-02-20 14:59:07 -0800161 ret = fakeLogWritev(log_fd, vec, nr);
Mark Salyzyn8245af12014-03-25 13:45:33 -0700162 if (ret < 0) {
163 ret = -errno;
164 }
165 } while (ret == -EINTR);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800166#else
Mark Salyzynd91ab582014-12-15 10:52:12 -0800167 static const unsigned header_length = 2;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700168 struct iovec newVec[nr + header_length];
Mark Salyzynd91ab582014-12-15 10:52:12 -0800169 android_log_header_t header;
170 android_pmsg_log_header_t pmsg_header;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700171 struct timespec ts;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700172 size_t i, payload_size;
Mark Salyzyn076ba812014-05-29 17:53:41 -0700173 static uid_t last_uid = AID_ROOT; /* logd *always* starts up as AID_ROOT */
Mark Salyzynd91ab582014-12-15 10:52:12 -0800174 static pid_t last_pid = (pid_t) -1;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700175
Mark Salyzyn076ba812014-05-29 17:53:41 -0700176 if (last_uid == AID_ROOT) { /* have we called to get the UID yet? */
177 last_uid = getuid();
178 }
Mark Salyzynd91ab582014-12-15 10:52:12 -0800179 if (last_pid == (pid_t) -1) {
180 last_pid = getpid();
Mark Salyzyn154f4602014-02-20 14:59:07 -0800181 }
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700182 /*
183 * struct {
Mark Salyzyn53016d82015-02-04 12:28:47 -0800184 * // what we provide to pstore
Mark Salyzynd91ab582014-12-15 10:52:12 -0800185 * android_pmsg_log_header_t pmsg_header;
186 * // what we provide to socket
Mark Salyzyn7a809402015-01-16 13:38:07 -0800187 * android_log_header_t header;
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700188 * // caller provides
189 * union {
190 * struct {
191 * char prio;
192 * char payload[];
193 * } string;
194 * struct {
195 * uint32_t tag
196 * char payload[];
197 * } binary;
198 * };
199 * };
200 */
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700201
Mark Salyzyn076ba812014-05-29 17:53:41 -0700202 clock_gettime(CLOCK_REALTIME, &ts);
Mark Salyzyn076ba812014-05-29 17:53:41 -0700203
Mark Salyzynd91ab582014-12-15 10:52:12 -0800204 pmsg_header.magic = LOGGER_MAGIC;
205 pmsg_header.len = sizeof(pmsg_header) + sizeof(header);
206 pmsg_header.uid = last_uid;
207 pmsg_header.pid = last_pid;
208
Mark Salyzyn7a809402015-01-16 13:38:07 -0800209 header.id = log_id;
210 header.tid = gettid();
211 header.realtime.tv_sec = ts.tv_sec;
212 header.realtime.tv_nsec = ts.tv_nsec;
Mark Salyzyn154f4602014-02-20 14:59:07 -0800213
Mark Salyzynd91ab582014-12-15 10:52:12 -0800214 newVec[0].iov_base = (unsigned char *) &pmsg_header;
215 newVec[0].iov_len = sizeof(pmsg_header);
216 newVec[1].iov_base = (unsigned char *) &header;
217 newVec[1].iov_len = sizeof(header);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800218
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700219 for (payload_size = 0, i = header_length; i < nr + header_length; i++) {
220 newVec[i].iov_base = vec[i - header_length].iov_base;
221 payload_size += newVec[i].iov_len = vec[i - header_length].iov_len;
222
223 if (payload_size > LOGGER_ENTRY_MAX_PAYLOAD) {
224 newVec[i].iov_len -= payload_size - LOGGER_ENTRY_MAX_PAYLOAD;
225 if (newVec[i].iov_len) {
226 ++i;
227 }
Mark Salyzynd91ab582014-12-15 10:52:12 -0800228 payload_size = LOGGER_ENTRY_MAX_PAYLOAD;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700229 break;
230 }
Mark Salyzyn154f4602014-02-20 14:59:07 -0800231 }
Mark Salyzynd91ab582014-12-15 10:52:12 -0800232 pmsg_header.len += payload_size;
233
234 if (pstore_fd >= 0) {
235 TEMP_FAILURE_RETRY(writev(pstore_fd, newVec, i));
236 }
237
238 if (last_uid == AID_LOGD) { /* logd, after initialization and priv drop */
239 /*
240 * ignore log messages we send to ourself (logd).
241 * Such log messages are often generated by libraries we depend on
242 * which use standard Android logging.
243 */
244 return 0;
245 }
246
247 if (logd_fd < 0) {
248 return -EBADF;
249 }
Mark Salyzyn154f4602014-02-20 14:59:07 -0800250
Mark Salyzyn8245af12014-03-25 13:45:33 -0700251 /*
252 * The write below could be lost, but will never block.
253 *
Mark Salyzynd91ab582014-12-15 10:52:12 -0800254 * To logd, we drop the pmsg_header
255 *
Mark Salyzyn8245af12014-03-25 13:45:33 -0700256 * ENOTCONN occurs if logd dies.
257 * EAGAIN occurs if logd is overloaded.
258 */
Mark Salyzynd91ab582014-12-15 10:52:12 -0800259 ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, i - 1));
Mark Salyzyn8245af12014-03-25 13:45:33 -0700260 if (ret < 0) {
261 ret = -errno;
262 if (ret == -ENOTCONN) {
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800263#if !defined(_WIN32)
Mark Salyzyn8245af12014-03-25 13:45:33 -0700264 pthread_mutex_lock(&log_init_lock);
265#endif
266 ret = __write_to_log_initialize();
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800267#if !defined(_WIN32)
Mark Salyzyn8245af12014-03-25 13:45:33 -0700268 pthread_mutex_unlock(&log_init_lock);
269#endif
270
271 if (ret < 0) {
272 return ret;
273 }
274
Mark Salyzynd91ab582014-12-15 10:52:12 -0800275 ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, i - 1));
Mark Salyzyn8245af12014-03-25 13:45:33 -0700276 if (ret < 0) {
277 ret = -errno;
278 }
279 }
280 }
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700281
Mark Salyzyn7a809402015-01-16 13:38:07 -0800282 if (ret > (ssize_t)sizeof(header)) {
283 ret -= sizeof(header);
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700284 }
Mark Salyzyn154f4602014-02-20 14:59:07 -0800285#endif
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700286
287 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800288}
289
Mark Salyzyn154f4602014-02-20 14:59:07 -0800290#if FAKE_LOG_DEVICE
291static const char *LOG_NAME[LOG_ID_MAX] = {
292 [LOG_ID_MAIN] = "main",
293 [LOG_ID_RADIO] = "radio",
294 [LOG_ID_EVENTS] = "events",
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700295 [LOG_ID_SYSTEM] = "system",
296 [LOG_ID_CRASH] = "crash"
Mark Salyzyn154f4602014-02-20 14:59:07 -0800297};
298
Adam Lesinskia1ac84c2014-10-20 12:31:30 -0700299const char *android_log_id_to_name(log_id_t log_id)
Mark Salyzyn154f4602014-02-20 14:59:07 -0800300{
301 if (log_id >= LOG_ID_MAX) {
302 log_id = LOG_ID_MAIN;
303 }
304 return LOG_NAME[log_id];
305}
306#endif
307
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800308static int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr)
309{
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800310#if !defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800311 pthread_mutex_lock(&log_init_lock);
312#endif
313
314 if (write_to_log == __write_to_log_init) {
Mark Salyzyn8245af12014-03-25 13:45:33 -0700315 int ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800316
Mark Salyzyn8245af12014-03-25 13:45:33 -0700317 ret = __write_to_log_initialize();
318 if (ret < 0) {
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800319#if !defined(_WIN32)
Mark Salyzyn8245af12014-03-25 13:45:33 -0700320 pthread_mutex_unlock(&log_init_lock);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800321#endif
Mark Salyzyn8245af12014-03-25 13:45:33 -0700322 return ret;
323 }
324
Mark Salyzyn53016d82015-02-04 12:28:47 -0800325 write_to_log = __write_to_log_daemon;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800326 }
327
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800328#if !defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800329 pthread_mutex_unlock(&log_init_lock);
330#endif
331
332 return write_to_log(log_id, vec, nr);
333}
334
335int __android_log_write(int prio, const char *tag, const char *msg)
336{
337 struct iovec vec[3];
338 log_id_t log_id = LOG_ID_MAIN;
Wink Saville3761e962012-11-28 12:20:19 -0800339 char tmp_tag[32];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800340
341 if (!tag)
342 tag = "";
343
344 /* XXX: This needs to go! */
345 if (!strcmp(tag, "HTC_RIL") ||
John Michelaued7ccae2009-08-19 10:01:55 -0500346 !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
Jeff Sharkey84dcf092012-08-13 11:27:30 -0700347 !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800348 !strcmp(tag, "AT") ||
349 !strcmp(tag, "GSM") ||
Wink Saville89efdc92009-04-02 11:00:57 -0700350 !strcmp(tag, "STK") ||
351 !strcmp(tag, "CDMA") ||
352 !strcmp(tag, "PHONE") ||
Wink Saville3761e962012-11-28 12:20:19 -0800353 !strcmp(tag, "SMS")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800354 log_id = LOG_ID_RADIO;
Mark Salyzyn8245af12014-03-25 13:45:33 -0700355 /* Inform third party apps/ril/radio.. to use Rlog or RLOG */
Wink Saville3761e962012-11-28 12:20:19 -0800356 snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
357 tag = tmp_tag;
358 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800359
Elliott Hughes26864bf2014-05-06 20:40:15 -0700360#if __BIONIC__
361 if (prio == ANDROID_LOG_FATAL) {
Dan Albertc68fedc2014-08-18 17:29:34 -0700362 android_set_abort_message(msg);
Elliott Hughes26864bf2014-05-06 20:40:15 -0700363 }
364#endif
365
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800366 vec[0].iov_base = (unsigned char *) &prio;
367 vec[0].iov_len = 1;
368 vec[1].iov_base = (void *) tag;
369 vec[1].iov_len = strlen(tag) + 1;
370 vec[2].iov_base = (void *) msg;
371 vec[2].iov_len = strlen(msg) + 1;
372
373 return write_to_log(log_id, vec, 3);
374}
375
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800376int __android_log_buf_write(int bufID, int prio, const char *tag, const char *msg)
377{
378 struct iovec vec[3];
Wink Saville3761e962012-11-28 12:20:19 -0800379 char tmp_tag[32];
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800380
381 if (!tag)
382 tag = "";
383
384 /* XXX: This needs to go! */
Wink Saville3761e962012-11-28 12:20:19 -0800385 if ((bufID != LOG_ID_RADIO) &&
386 (!strcmp(tag, "HTC_RIL") ||
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800387 !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
Jeff Sharkey84dcf092012-08-13 11:27:30 -0700388 !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800389 !strcmp(tag, "AT") ||
390 !strcmp(tag, "GSM") ||
391 !strcmp(tag, "STK") ||
392 !strcmp(tag, "CDMA") ||
393 !strcmp(tag, "PHONE") ||
Wink Saville3761e962012-11-28 12:20:19 -0800394 !strcmp(tag, "SMS"))) {
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800395 bufID = LOG_ID_RADIO;
Mark Salyzyn8245af12014-03-25 13:45:33 -0700396 /* Inform third party apps/ril/radio.. to use Rlog or RLOG */
Wink Saville3761e962012-11-28 12:20:19 -0800397 snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
398 tag = tmp_tag;
399 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800400
401 vec[0].iov_base = (unsigned char *) &prio;
402 vec[0].iov_len = 1;
403 vec[1].iov_base = (void *) tag;
404 vec[1].iov_len = strlen(tag) + 1;
405 vec[2].iov_base = (void *) msg;
406 vec[2].iov_len = strlen(msg) + 1;
407
408 return write_to_log(bufID, vec, 3);
409}
410
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800411int __android_log_vprint(int prio, const char *tag, const char *fmt, va_list ap)
412{
Chris Pearson19299902010-06-02 16:25:35 -0700413 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800414
415 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
416
417 return __android_log_write(prio, tag, buf);
418}
419
420int __android_log_print(int prio, const char *tag, const char *fmt, ...)
421{
422 va_list ap;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800423 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800424
425 va_start(ap, fmt);
426 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
427 va_end(ap);
428
429 return __android_log_write(prio, tag, buf);
430}
431
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800432int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...)
433{
434 va_list ap;
435 char buf[LOG_BUF_SIZE];
436
437 va_start(ap, fmt);
438 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
439 va_end(ap);
440
441 return __android_log_buf_write(bufID, prio, tag, buf);
442}
443
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800444void __android_log_assert(const char *cond, const char *tag,
Mark Salyzyncf4aa032013-11-22 07:54:30 -0800445 const char *fmt, ...)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800446{
Chris Pearson19299902010-06-02 16:25:35 -0700447 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800448
Chris Pearson19299902010-06-02 16:25:35 -0700449 if (fmt) {
450 va_list ap;
451 va_start(ap, fmt);
452 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
453 va_end(ap);
454 } else {
455 /* Msg not provided, log condition. N.B. Do not use cond directly as
456 * format string as it could contain spurious '%' syntax (e.g.
457 * "%d" in "blocks%devs == 0").
458 */
459 if (cond)
460 snprintf(buf, LOG_BUF_SIZE, "Assertion failed: %s", cond);
461 else
462 strcpy(buf, "Unspecified assertion failed");
463 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800464
465 __android_log_write(ANDROID_LOG_FATAL, tag, buf);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800466 __builtin_trap(); /* trap so we have a chance to debug the situation */
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700467 /* NOTREACHED */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800468}
469
470int __android_log_bwrite(int32_t tag, const void *payload, size_t len)
471{
472 struct iovec vec[2];
473
474 vec[0].iov_base = &tag;
475 vec[0].iov_len = sizeof(tag);
476 vec[1].iov_base = (void*)payload;
477 vec[1].iov_len = len;
478
479 return write_to_log(LOG_ID_EVENTS, vec, 2);
480}
481
482/*
483 * Like __android_log_bwrite, but takes the type as well. Doesn't work
484 * for the general case where we're generating lists of stuff, but very
485 * handy if we just want to dump an integer into the log.
486 */
487int __android_log_btwrite(int32_t tag, char type, const void *payload,
Mark Salyzyn154f4602014-02-20 14:59:07 -0800488 size_t len)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800489{
490 struct iovec vec[3];
491
492 vec[0].iov_base = &tag;
493 vec[0].iov_len = sizeof(tag);
494 vec[1].iov_base = &type;
495 vec[1].iov_len = sizeof(type);
496 vec[2].iov_base = (void*)payload;
497 vec[2].iov_len = len;
498
499 return write_to_log(LOG_ID_EVENTS, vec, 3);
500}
Nick Kralevich2a4d05a2014-07-01 10:57:16 -0700501
502/*
503 * Like __android_log_bwrite, but used for writing strings to the
504 * event log.
505 */
506int __android_log_bswrite(int32_t tag, const char *payload)
507{
508 struct iovec vec[4];
509 char type = EVENT_TYPE_STRING;
510 uint32_t len = strlen(payload);
511
512 vec[0].iov_base = &tag;
513 vec[0].iov_len = sizeof(tag);
514 vec[1].iov_base = &type;
515 vec[1].iov_len = sizeof(type);
516 vec[2].iov_base = &len;
517 vec[2].iov_len = sizeof(len);
518 vec[3].iov_base = (void*)payload;
519 vec[3].iov_len = len;
520
521 return write_to_log(LOG_ID_EVENTS, vec, 4);
522}