blob: c62a246aff92dc28e79646ebe3afdf70bf76c348 [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 Salyzyn8245af12014-03-25 13:45:33 -070093/* log_init_lock assumed */
94static int __write_to_log_initialize()
95{
96 int i, ret = 0;
97
98#if FAKE_LOG_DEVICE
99 for (i = 0; i < LOG_ID_MAX; i++) {
100 char buf[sizeof("/dev/log_system")];
101 snprintf(buf, sizeof(buf), "/dev/log_%s", android_log_id_to_name(i));
102 log_fds[i] = fakeLogOpen(buf, O_WRONLY);
103 }
104#else
Mark Salyzyn0d00a442015-03-13 12:15:57 -0700105 if (pstore_fd < 0) {
106 pstore_fd = TEMP_FAILURE_RETRY(open("/dev/pmsg0", O_WRONLY));
Mark Salyzyn8245af12014-03-25 13:45:33 -0700107 }
108
Mark Salyzyn0d00a442015-03-13 12:15:57 -0700109 if (logd_fd < 0) {
110 i = TEMP_FAILURE_RETRY(socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0));
111 if (i < 0) {
112 ret = -errno;
113 } else if (TEMP_FAILURE_RETRY(fcntl(i, F_SETFL, O_NONBLOCK)) < 0) {
Mark Salyzyn8245af12014-03-25 13:45:33 -0700114 ret = -errno;
115 close(i);
Mark Salyzyn0d00a442015-03-13 12:15:57 -0700116 } else {
117 struct sockaddr_un un;
118 memset(&un, 0, sizeof(struct sockaddr_un));
119 un.sun_family = AF_UNIX;
120 strcpy(un.sun_path, "/dev/socket/logdw");
121
122 if (TEMP_FAILURE_RETRY(connect(i, (struct sockaddr *)&un,
123 sizeof(struct sockaddr_un))) < 0) {
124 ret = -errno;
125 close(i);
126 } else {
127 logd_fd = i;
128 }
Mark Salyzyn8245af12014-03-25 13:45:33 -0700129 }
130 }
Mark Salyzyn8245af12014-03-25 13:45:33 -0700131#endif
132
133 return ret;
134}
135
Mark Salyzyn53016d82015-02-04 12:28:47 -0800136static 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 -0800137{
138 ssize_t ret;
Mark Salyzyn8245af12014-03-25 13:45:33 -0700139#if FAKE_LOG_DEVICE
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800140 int log_fd;
141
142 if (/*(int)log_id >= 0 &&*/ (int)log_id < (int)LOG_ID_MAX) {
143 log_fd = log_fds[(int)log_id];
144 } else {
Mark Salyzyn8245af12014-03-25 13:45:33 -0700145 return -EBADF;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800146 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800147 do {
Mark Salyzyn154f4602014-02-20 14:59:07 -0800148 ret = fakeLogWritev(log_fd, vec, nr);
Mark Salyzyn8245af12014-03-25 13:45:33 -0700149 if (ret < 0) {
150 ret = -errno;
151 }
152 } while (ret == -EINTR);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800153#else
Mark Salyzynd91ab582014-12-15 10:52:12 -0800154 static const unsigned header_length = 2;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700155 struct iovec newVec[nr + header_length];
Mark Salyzynd91ab582014-12-15 10:52:12 -0800156 android_log_header_t header;
157 android_pmsg_log_header_t pmsg_header;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700158 struct timespec ts;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700159 size_t i, payload_size;
Mark Salyzyn076ba812014-05-29 17:53:41 -0700160 static uid_t last_uid = AID_ROOT; /* logd *always* starts up as AID_ROOT */
Mark Salyzynd91ab582014-12-15 10:52:12 -0800161 static pid_t last_pid = (pid_t) -1;
Mark Salyzynd45d36e2015-02-12 15:14:26 -0800162 static atomic_int_fast32_t dropped;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700163
Mark Salyzyn31f7df52015-03-13 15:57:11 -0700164 if (!nr) {
165 return -EINVAL;
166 }
167
Mark Salyzyn076ba812014-05-29 17:53:41 -0700168 if (last_uid == AID_ROOT) { /* have we called to get the UID yet? */
169 last_uid = getuid();
170 }
Mark Salyzynd91ab582014-12-15 10:52:12 -0800171 if (last_pid == (pid_t) -1) {
172 last_pid = getpid();
Mark Salyzyn154f4602014-02-20 14:59:07 -0800173 }
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700174 /*
175 * struct {
Mark Salyzyn53016d82015-02-04 12:28:47 -0800176 * // what we provide to pstore
Mark Salyzynd91ab582014-12-15 10:52:12 -0800177 * android_pmsg_log_header_t pmsg_header;
178 * // what we provide to socket
Mark Salyzyn7a809402015-01-16 13:38:07 -0800179 * android_log_header_t header;
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700180 * // caller provides
181 * union {
182 * struct {
183 * char prio;
184 * char payload[];
185 * } string;
186 * struct {
187 * uint32_t tag
188 * char payload[];
189 * } binary;
190 * };
191 * };
192 */
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700193
Mark Salyzyn076ba812014-05-29 17:53:41 -0700194 clock_gettime(CLOCK_REALTIME, &ts);
Mark Salyzyn076ba812014-05-29 17:53:41 -0700195
Mark Salyzynd91ab582014-12-15 10:52:12 -0800196 pmsg_header.magic = LOGGER_MAGIC;
197 pmsg_header.len = sizeof(pmsg_header) + sizeof(header);
198 pmsg_header.uid = last_uid;
199 pmsg_header.pid = last_pid;
200
Mark Salyzyn7a809402015-01-16 13:38:07 -0800201 header.tid = gettid();
202 header.realtime.tv_sec = ts.tv_sec;
203 header.realtime.tv_nsec = ts.tv_nsec;
Mark Salyzyn154f4602014-02-20 14:59:07 -0800204
Mark Salyzynd91ab582014-12-15 10:52:12 -0800205 newVec[0].iov_base = (unsigned char *) &pmsg_header;
206 newVec[0].iov_len = sizeof(pmsg_header);
207 newVec[1].iov_base = (unsigned char *) &header;
208 newVec[1].iov_len = sizeof(header);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800209
Mark Salyzynd45d36e2015-02-12 15:14:26 -0800210 if (logd_fd > 0) {
211 int32_t snapshot = atomic_exchange_explicit(&dropped, 0, memory_order_relaxed);
212 if (snapshot) {
213 android_log_event_int_t buffer;
214
215 header.id = LOG_ID_EVENTS;
216 buffer.header.tag = htole32(LIBLOG_LOG_TAG);
217 buffer.payload.type = EVENT_TYPE_INT;
218 buffer.payload.data = htole32(snapshot);
219
220 newVec[2].iov_base = &buffer;
221 newVec[2].iov_len = sizeof(buffer);
222
223 ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, 2));
224 if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) {
225 atomic_fetch_add_explicit(&dropped, snapshot, memory_order_relaxed);
226 }
227 }
228 }
229
230 header.id = log_id;
231
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700232 for (payload_size = 0, i = header_length; i < nr + header_length; i++) {
233 newVec[i].iov_base = vec[i - header_length].iov_base;
234 payload_size += newVec[i].iov_len = vec[i - header_length].iov_len;
235
236 if (payload_size > LOGGER_ENTRY_MAX_PAYLOAD) {
237 newVec[i].iov_len -= payload_size - LOGGER_ENTRY_MAX_PAYLOAD;
238 if (newVec[i].iov_len) {
239 ++i;
240 }
Mark Salyzynd91ab582014-12-15 10:52:12 -0800241 payload_size = LOGGER_ENTRY_MAX_PAYLOAD;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700242 break;
243 }
Mark Salyzyn154f4602014-02-20 14:59:07 -0800244 }
Mark Salyzynd91ab582014-12-15 10:52:12 -0800245 pmsg_header.len += payload_size;
246
247 if (pstore_fd >= 0) {
248 TEMP_FAILURE_RETRY(writev(pstore_fd, newVec, i));
249 }
250
251 if (last_uid == AID_LOGD) { /* logd, after initialization and priv drop */
252 /*
253 * ignore log messages we send to ourself (logd).
254 * Such log messages are often generated by libraries we depend on
255 * which use standard Android logging.
256 */
257 return 0;
258 }
259
260 if (logd_fd < 0) {
261 return -EBADF;
262 }
Mark Salyzyn154f4602014-02-20 14:59:07 -0800263
Mark Salyzyn8245af12014-03-25 13:45:33 -0700264 /*
265 * The write below could be lost, but will never block.
266 *
Mark Salyzynd91ab582014-12-15 10:52:12 -0800267 * To logd, we drop the pmsg_header
268 *
Mark Salyzyn8245af12014-03-25 13:45:33 -0700269 * ENOTCONN occurs if logd dies.
270 * EAGAIN occurs if logd is overloaded.
271 */
Mark Salyzynd91ab582014-12-15 10:52:12 -0800272 ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, i - 1));
Mark Salyzyn8245af12014-03-25 13:45:33 -0700273 if (ret < 0) {
274 ret = -errno;
275 if (ret == -ENOTCONN) {
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800276#if !defined(_WIN32)
Mark Salyzyn8245af12014-03-25 13:45:33 -0700277 pthread_mutex_lock(&log_init_lock);
278#endif
Mark Salyzyn0d00a442015-03-13 12:15:57 -0700279 close(logd_fd);
280 logd_fd = -1;
Mark Salyzyn8245af12014-03-25 13:45:33 -0700281 ret = __write_to_log_initialize();
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800282#if !defined(_WIN32)
Mark Salyzyn8245af12014-03-25 13:45:33 -0700283 pthread_mutex_unlock(&log_init_lock);
284#endif
285
286 if (ret < 0) {
287 return ret;
288 }
289
Mark Salyzynd91ab582014-12-15 10:52:12 -0800290 ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, i - 1));
Mark Salyzyn8245af12014-03-25 13:45:33 -0700291 if (ret < 0) {
292 ret = -errno;
293 }
294 }
295 }
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700296
Mark Salyzyn7a809402015-01-16 13:38:07 -0800297 if (ret > (ssize_t)sizeof(header)) {
298 ret -= sizeof(header);
Mark Salyzynd45d36e2015-02-12 15:14:26 -0800299 } else if (ret == -EAGAIN) {
300 atomic_fetch_add_explicit(&dropped, 1, memory_order_relaxed);
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700301 }
Mark Salyzyn154f4602014-02-20 14:59:07 -0800302#endif
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700303
304 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800305}
306
Mark Salyzyn154f4602014-02-20 14:59:07 -0800307#if FAKE_LOG_DEVICE
308static const char *LOG_NAME[LOG_ID_MAX] = {
309 [LOG_ID_MAIN] = "main",
310 [LOG_ID_RADIO] = "radio",
311 [LOG_ID_EVENTS] = "events",
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700312 [LOG_ID_SYSTEM] = "system",
313 [LOG_ID_CRASH] = "crash"
Mark Salyzyn154f4602014-02-20 14:59:07 -0800314};
315
Adam Lesinskia1ac84c2014-10-20 12:31:30 -0700316const char *android_log_id_to_name(log_id_t log_id)
Mark Salyzyn154f4602014-02-20 14:59:07 -0800317{
318 if (log_id >= LOG_ID_MAX) {
319 log_id = LOG_ID_MAIN;
320 }
321 return LOG_NAME[log_id];
322}
323#endif
324
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800325static int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr)
326{
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800327#if !defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800328 pthread_mutex_lock(&log_init_lock);
329#endif
330
331 if (write_to_log == __write_to_log_init) {
Mark Salyzyn8245af12014-03-25 13:45:33 -0700332 int ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800333
Mark Salyzyn8245af12014-03-25 13:45:33 -0700334 ret = __write_to_log_initialize();
335 if (ret < 0) {
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800336#if !defined(_WIN32)
Mark Salyzyn8245af12014-03-25 13:45:33 -0700337 pthread_mutex_unlock(&log_init_lock);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800338#endif
Mark Salyzyn0d00a442015-03-13 12:15:57 -0700339#if (FAKE_LOG_DEVICE == 0)
340 if (pstore_fd >= 0) {
341 __write_to_log_daemon(log_id, vec, nr);
342 }
343#endif
Mark Salyzyn8245af12014-03-25 13:45:33 -0700344 return ret;
345 }
346
Mark Salyzyn53016d82015-02-04 12:28:47 -0800347 write_to_log = __write_to_log_daemon;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800348 }
349
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800350#if !defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800351 pthread_mutex_unlock(&log_init_lock);
352#endif
353
354 return write_to_log(log_id, vec, nr);
355}
356
357int __android_log_write(int prio, const char *tag, const char *msg)
358{
Dan Albertc7aadc42015-04-02 10:32:44 -0700359 return __android_log_buf_write(LOG_ID_MAIN, prio, tag, msg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800360}
361
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800362int __android_log_buf_write(int bufID, int prio, const char *tag, const char *msg)
363{
364 struct iovec vec[3];
Wink Saville3761e962012-11-28 12:20:19 -0800365 char tmp_tag[32];
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800366
367 if (!tag)
368 tag = "";
369
370 /* XXX: This needs to go! */
Wink Saville3761e962012-11-28 12:20:19 -0800371 if ((bufID != LOG_ID_RADIO) &&
372 (!strcmp(tag, "HTC_RIL") ||
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800373 !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
Jeff Sharkey84dcf092012-08-13 11:27:30 -0700374 !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800375 !strcmp(tag, "AT") ||
376 !strcmp(tag, "GSM") ||
377 !strcmp(tag, "STK") ||
378 !strcmp(tag, "CDMA") ||
379 !strcmp(tag, "PHONE") ||
Wink Saville3761e962012-11-28 12:20:19 -0800380 !strcmp(tag, "SMS"))) {
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800381 bufID = LOG_ID_RADIO;
Mark Salyzyn8245af12014-03-25 13:45:33 -0700382 /* Inform third party apps/ril/radio.. to use Rlog or RLOG */
Wink Saville3761e962012-11-28 12:20:19 -0800383 snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
384 tag = tmp_tag;
385 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800386
Dan Albertc7aadc42015-04-02 10:32:44 -0700387#if __BIONIC__
388 if (prio == ANDROID_LOG_FATAL) {
389 android_set_abort_message(msg);
390 }
391#endif
392
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800393 vec[0].iov_base = (unsigned char *) &prio;
394 vec[0].iov_len = 1;
395 vec[1].iov_base = (void *) tag;
396 vec[1].iov_len = strlen(tag) + 1;
397 vec[2].iov_base = (void *) msg;
398 vec[2].iov_len = strlen(msg) + 1;
399
400 return write_to_log(bufID, vec, 3);
401}
402
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800403int __android_log_vprint(int prio, const char *tag, const char *fmt, va_list ap)
404{
Chris Pearson19299902010-06-02 16:25:35 -0700405 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800406
407 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
408
409 return __android_log_write(prio, tag, buf);
410}
411
412int __android_log_print(int prio, const char *tag, const char *fmt, ...)
413{
414 va_list ap;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800415 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800416
417 va_start(ap, fmt);
418 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
419 va_end(ap);
420
421 return __android_log_write(prio, tag, buf);
422}
423
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800424int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...)
425{
426 va_list ap;
427 char buf[LOG_BUF_SIZE];
428
429 va_start(ap, fmt);
430 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
431 va_end(ap);
432
433 return __android_log_buf_write(bufID, prio, tag, buf);
434}
435
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800436void __android_log_assert(const char *cond, const char *tag,
Mark Salyzyncf4aa032013-11-22 07:54:30 -0800437 const char *fmt, ...)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800438{
Chris Pearson19299902010-06-02 16:25:35 -0700439 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800440
Chris Pearson19299902010-06-02 16:25:35 -0700441 if (fmt) {
442 va_list ap;
443 va_start(ap, fmt);
444 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
445 va_end(ap);
446 } else {
447 /* Msg not provided, log condition. N.B. Do not use cond directly as
448 * format string as it could contain spurious '%' syntax (e.g.
449 * "%d" in "blocks%devs == 0").
450 */
451 if (cond)
452 snprintf(buf, LOG_BUF_SIZE, "Assertion failed: %s", cond);
453 else
454 strcpy(buf, "Unspecified assertion failed");
455 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800456
457 __android_log_write(ANDROID_LOG_FATAL, tag, buf);
Elliott Hughes02ff4b82015-03-07 11:21:37 -0800458 abort(); /* abort so we have a chance to debug the situation */
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700459 /* NOTREACHED */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800460}
461
462int __android_log_bwrite(int32_t tag, const void *payload, size_t len)
463{
464 struct iovec vec[2];
465
466 vec[0].iov_base = &tag;
467 vec[0].iov_len = sizeof(tag);
468 vec[1].iov_base = (void*)payload;
469 vec[1].iov_len = len;
470
471 return write_to_log(LOG_ID_EVENTS, vec, 2);
472}
473
474/*
475 * Like __android_log_bwrite, but takes the type as well. Doesn't work
476 * for the general case where we're generating lists of stuff, but very
477 * handy if we just want to dump an integer into the log.
478 */
479int __android_log_btwrite(int32_t tag, char type, const void *payload,
Mark Salyzyn154f4602014-02-20 14:59:07 -0800480 size_t len)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800481{
482 struct iovec vec[3];
483
484 vec[0].iov_base = &tag;
485 vec[0].iov_len = sizeof(tag);
486 vec[1].iov_base = &type;
487 vec[1].iov_len = sizeof(type);
488 vec[2].iov_base = (void*)payload;
489 vec[2].iov_len = len;
490
491 return write_to_log(LOG_ID_EVENTS, vec, 3);
492}
Nick Kralevich2a4d05a2014-07-01 10:57:16 -0700493
494/*
495 * Like __android_log_bwrite, but used for writing strings to the
496 * event log.
497 */
498int __android_log_bswrite(int32_t tag, const char *payload)
499{
500 struct iovec vec[4];
501 char type = EVENT_TYPE_STRING;
502 uint32_t len = strlen(payload);
503
504 vec[0].iov_base = &tag;
505 vec[0].iov_len = sizeof(tag);
506 vec[1].iov_base = &type;
507 vec[1].iov_len = sizeof(type);
508 vec[2].iov_base = &len;
509 vec[2].iov_len = sizeof(len);
510 vec[3].iov_base = (void*)payload;
511 vec[3].iov_len = len;
512
513 return write_to_log(LOG_ID_EVENTS, vec, 4);
514}