blob: 11c6d9cc8a482bea2d6e4e104ea56529c7b7d040 [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;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057
Mark Salyzyna04464a2014-04-30 08:50:53 -070058#ifndef __unused
59#define __unused __attribute__((__unused__))
60#endif
Kristian Monsenb5a98902014-01-28 11:26:56 -080061
Mark Salyzyn2d2e0a52015-11-06 12:26:52 -080062#if !defined(_WIN32)
63static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
64
65static void lock()
66{
67 /*
68 * If we trigger a signal handler in the middle of locked activity and the
69 * signal handler logs a message, we could get into a deadlock state.
70 */
71 pthread_mutex_lock(&log_init_lock);
72}
73
74static void unlock()
75{
76 pthread_mutex_unlock(&log_init_lock);
77}
78
79#else /* !defined(_WIN32) */
80
81#define lock() ((void)0)
82#define unlock() ((void)0)
83
84#endif /* !defined(_WIN32) */
85
Mark Salyzyn154f4602014-02-20 14:59:07 -080086#if FAKE_LOG_DEVICE
Mark Salyzyn99f47a92014-04-07 14:58:08 -070087static int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1, -1, -1 };
Mark Salyzyna04464a2014-04-30 08:50:53 -070088#else
89static int logd_fd = -1;
Mark Salyzynd91ab582014-12-15 10:52:12 -080090static int pstore_fd = -1;
Mark Salyzyn154f4602014-02-20 14:59:07 -080091#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080092
93/*
94 * This is used by the C++ code to decide if it should write logs through
Mark Salyzyn154f4602014-02-20 14:59:07 -080095 * the C code. Basically, if /dev/socket/logd is available, we're running in
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080096 * the simulator rather than a desktop tool and want to use the device.
97 */
98static enum {
Chris Pearson19299902010-06-02 16:25:35 -070099 kLogUninitialized, kLogNotAvailable, kLogAvailable
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800100} g_log_status = kLogUninitialized;
Mark Salyzyn53016d82015-02-04 12:28:47 -0800101
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800102int __android_log_dev_available(void)
103{
104 if (g_log_status == kLogUninitialized) {
Mark Salyzyn154f4602014-02-20 14:59:07 -0800105 if (access("/dev/socket/logdw", W_OK) == 0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800106 g_log_status = kLogAvailable;
107 else
108 g_log_status = kLogNotAvailable;
109 }
110
111 return (g_log_status == kLogAvailable);
112}
113
Mark Salyzyn8245af12014-03-25 13:45:33 -0700114/* log_init_lock assumed */
115static int __write_to_log_initialize()
116{
117 int i, ret = 0;
118
119#if FAKE_LOG_DEVICE
120 for (i = 0; i < LOG_ID_MAX; i++) {
121 char buf[sizeof("/dev/log_system")];
122 snprintf(buf, sizeof(buf), "/dev/log_%s", android_log_id_to_name(i));
123 log_fds[i] = fakeLogOpen(buf, O_WRONLY);
124 }
125#else
Mark Salyzyn0d00a442015-03-13 12:15:57 -0700126 if (pstore_fd < 0) {
127 pstore_fd = TEMP_FAILURE_RETRY(open("/dev/pmsg0", O_WRONLY));
Mark Salyzyn8245af12014-03-25 13:45:33 -0700128 }
129
Mark Salyzyn0d00a442015-03-13 12:15:57 -0700130 if (logd_fd < 0) {
131 i = TEMP_FAILURE_RETRY(socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0));
132 if (i < 0) {
133 ret = -errno;
134 } else if (TEMP_FAILURE_RETRY(fcntl(i, F_SETFL, O_NONBLOCK)) < 0) {
Mark Salyzyn8245af12014-03-25 13:45:33 -0700135 ret = -errno;
136 close(i);
Mark Salyzyn0d00a442015-03-13 12:15:57 -0700137 } else {
138 struct sockaddr_un un;
139 memset(&un, 0, sizeof(struct sockaddr_un));
140 un.sun_family = AF_UNIX;
141 strcpy(un.sun_path, "/dev/socket/logdw");
142
143 if (TEMP_FAILURE_RETRY(connect(i, (struct sockaddr *)&un,
144 sizeof(struct sockaddr_un))) < 0) {
145 ret = -errno;
146 close(i);
147 } else {
148 logd_fd = i;
149 }
Mark Salyzyn8245af12014-03-25 13:45:33 -0700150 }
151 }
Mark Salyzyn8245af12014-03-25 13:45:33 -0700152#endif
153
154 return ret;
155}
156
Mark Salyzyn53016d82015-02-04 12:28:47 -0800157static 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 -0800158{
159 ssize_t ret;
Mark Salyzyn8245af12014-03-25 13:45:33 -0700160#if FAKE_LOG_DEVICE
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800161 int log_fd;
162
163 if (/*(int)log_id >= 0 &&*/ (int)log_id < (int)LOG_ID_MAX) {
164 log_fd = log_fds[(int)log_id];
165 } else {
Mark Salyzyn8245af12014-03-25 13:45:33 -0700166 return -EBADF;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168 do {
Mark Salyzyn154f4602014-02-20 14:59:07 -0800169 ret = fakeLogWritev(log_fd, vec, nr);
Mark Salyzyn8245af12014-03-25 13:45:33 -0700170 if (ret < 0) {
171 ret = -errno;
172 }
173 } while (ret == -EINTR);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800174#else
Mark Salyzynd91ab582014-12-15 10:52:12 -0800175 static const unsigned header_length = 2;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700176 struct iovec newVec[nr + header_length];
Mark Salyzynd91ab582014-12-15 10:52:12 -0800177 android_log_header_t header;
178 android_pmsg_log_header_t pmsg_header;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700179 struct timespec ts;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700180 size_t i, payload_size;
Mark Salyzyn076ba812014-05-29 17:53:41 -0700181 static uid_t last_uid = AID_ROOT; /* logd *always* starts up as AID_ROOT */
Mark Salyzynd91ab582014-12-15 10:52:12 -0800182 static pid_t last_pid = (pid_t) -1;
Mark Salyzynd45d36e2015-02-12 15:14:26 -0800183 static atomic_int_fast32_t dropped;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700184
Mark Salyzyn31f7df52015-03-13 15:57:11 -0700185 if (!nr) {
186 return -EINVAL;
187 }
188
Mark Salyzyn076ba812014-05-29 17:53:41 -0700189 if (last_uid == AID_ROOT) { /* have we called to get the UID yet? */
190 last_uid = getuid();
191 }
Mark Salyzynd91ab582014-12-15 10:52:12 -0800192 if (last_pid == (pid_t) -1) {
193 last_pid = getpid();
Mark Salyzyn154f4602014-02-20 14:59:07 -0800194 }
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700195 /*
196 * struct {
Mark Salyzyn53016d82015-02-04 12:28:47 -0800197 * // what we provide to pstore
Mark Salyzynd91ab582014-12-15 10:52:12 -0800198 * android_pmsg_log_header_t pmsg_header;
199 * // what we provide to socket
Mark Salyzyn7a809402015-01-16 13:38:07 -0800200 * android_log_header_t header;
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700201 * // caller provides
202 * union {
203 * struct {
204 * char prio;
205 * char payload[];
206 * } string;
207 * struct {
208 * uint32_t tag
209 * char payload[];
210 * } binary;
211 * };
212 * };
213 */
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700214
Mark Salyzynba7a9a02015-12-01 15:57:25 -0800215 clock_gettime(android_log_clockid(), &ts);
Mark Salyzyn076ba812014-05-29 17:53:41 -0700216
Mark Salyzynd91ab582014-12-15 10:52:12 -0800217 pmsg_header.magic = LOGGER_MAGIC;
218 pmsg_header.len = sizeof(pmsg_header) + sizeof(header);
219 pmsg_header.uid = last_uid;
220 pmsg_header.pid = last_pid;
221
Mark Salyzyn7a809402015-01-16 13:38:07 -0800222 header.tid = gettid();
223 header.realtime.tv_sec = ts.tv_sec;
224 header.realtime.tv_nsec = ts.tv_nsec;
Mark Salyzyn154f4602014-02-20 14:59:07 -0800225
Mark Salyzynd91ab582014-12-15 10:52:12 -0800226 newVec[0].iov_base = (unsigned char *) &pmsg_header;
227 newVec[0].iov_len = sizeof(pmsg_header);
228 newVec[1].iov_base = (unsigned char *) &header;
229 newVec[1].iov_len = sizeof(header);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800230
Mark Salyzynd45d36e2015-02-12 15:14:26 -0800231 if (logd_fd > 0) {
232 int32_t snapshot = atomic_exchange_explicit(&dropped, 0, memory_order_relaxed);
233 if (snapshot) {
234 android_log_event_int_t buffer;
235
236 header.id = LOG_ID_EVENTS;
237 buffer.header.tag = htole32(LIBLOG_LOG_TAG);
238 buffer.payload.type = EVENT_TYPE_INT;
239 buffer.payload.data = htole32(snapshot);
240
241 newVec[2].iov_base = &buffer;
242 newVec[2].iov_len = sizeof(buffer);
243
244 ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, 2));
245 if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) {
246 atomic_fetch_add_explicit(&dropped, snapshot, memory_order_relaxed);
247 }
248 }
249 }
250
251 header.id = log_id;
252
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700253 for (payload_size = 0, i = header_length; i < nr + header_length; i++) {
254 newVec[i].iov_base = vec[i - header_length].iov_base;
255 payload_size += newVec[i].iov_len = vec[i - header_length].iov_len;
256
257 if (payload_size > LOGGER_ENTRY_MAX_PAYLOAD) {
258 newVec[i].iov_len -= payload_size - LOGGER_ENTRY_MAX_PAYLOAD;
259 if (newVec[i].iov_len) {
260 ++i;
261 }
Mark Salyzynd91ab582014-12-15 10:52:12 -0800262 payload_size = LOGGER_ENTRY_MAX_PAYLOAD;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700263 break;
264 }
Mark Salyzyn154f4602014-02-20 14:59:07 -0800265 }
Mark Salyzynd91ab582014-12-15 10:52:12 -0800266 pmsg_header.len += payload_size;
267
268 if (pstore_fd >= 0) {
269 TEMP_FAILURE_RETRY(writev(pstore_fd, newVec, i));
270 }
271
272 if (last_uid == AID_LOGD) { /* logd, after initialization and priv drop */
273 /*
274 * ignore log messages we send to ourself (logd).
275 * Such log messages are often generated by libraries we depend on
276 * which use standard Android logging.
277 */
278 return 0;
279 }
280
281 if (logd_fd < 0) {
282 return -EBADF;
283 }
Mark Salyzyn154f4602014-02-20 14:59:07 -0800284
Mark Salyzyn8245af12014-03-25 13:45:33 -0700285 /*
286 * The write below could be lost, but will never block.
287 *
Mark Salyzynd91ab582014-12-15 10:52:12 -0800288 * To logd, we drop the pmsg_header
289 *
Mark Salyzyn8245af12014-03-25 13:45:33 -0700290 * ENOTCONN occurs if logd dies.
291 * EAGAIN occurs if logd is overloaded.
292 */
Mark Salyzynd91ab582014-12-15 10:52:12 -0800293 ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, i - 1));
Mark Salyzyn8245af12014-03-25 13:45:33 -0700294 if (ret < 0) {
295 ret = -errno;
296 if (ret == -ENOTCONN) {
Mark Salyzyn2d2e0a52015-11-06 12:26:52 -0800297 lock();
Mark Salyzyn0d00a442015-03-13 12:15:57 -0700298 close(logd_fd);
299 logd_fd = -1;
Mark Salyzyn8245af12014-03-25 13:45:33 -0700300 ret = __write_to_log_initialize();
Mark Salyzyn2d2e0a52015-11-06 12:26:52 -0800301 unlock();
Mark Salyzyn8245af12014-03-25 13:45:33 -0700302
303 if (ret < 0) {
304 return ret;
305 }
306
Mark Salyzynd91ab582014-12-15 10:52:12 -0800307 ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, i - 1));
Mark Salyzyn8245af12014-03-25 13:45:33 -0700308 if (ret < 0) {
309 ret = -errno;
310 }
311 }
312 }
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700313
Mark Salyzyn7a809402015-01-16 13:38:07 -0800314 if (ret > (ssize_t)sizeof(header)) {
315 ret -= sizeof(header);
Mark Salyzynd45d36e2015-02-12 15:14:26 -0800316 } else if (ret == -EAGAIN) {
317 atomic_fetch_add_explicit(&dropped, 1, memory_order_relaxed);
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700318 }
Mark Salyzyn154f4602014-02-20 14:59:07 -0800319#endif
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700320
321 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800322}
323
Mark Salyzyn154f4602014-02-20 14:59:07 -0800324#if FAKE_LOG_DEVICE
325static const char *LOG_NAME[LOG_ID_MAX] = {
326 [LOG_ID_MAIN] = "main",
327 [LOG_ID_RADIO] = "radio",
328 [LOG_ID_EVENTS] = "events",
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700329 [LOG_ID_SYSTEM] = "system",
Mark Salyzyn440e1092014-10-10 15:13:15 -0700330 [LOG_ID_CRASH] = "crash",
331 [LOG_ID_KERNEL] = "kernel",
Mark Salyzyn154f4602014-02-20 14:59:07 -0800332};
333
Adam Lesinskia1ac84c2014-10-20 12:31:30 -0700334const char *android_log_id_to_name(log_id_t log_id)
Mark Salyzyn154f4602014-02-20 14:59:07 -0800335{
336 if (log_id >= LOG_ID_MAX) {
337 log_id = LOG_ID_MAIN;
338 }
339 return LOG_NAME[log_id];
340}
341#endif
342
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800343static int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr)
344{
Mark Salyzyn2d2e0a52015-11-06 12:26:52 -0800345 lock();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800346
347 if (write_to_log == __write_to_log_init) {
Mark Salyzyn8245af12014-03-25 13:45:33 -0700348 int ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800349
Mark Salyzyn8245af12014-03-25 13:45:33 -0700350 ret = __write_to_log_initialize();
351 if (ret < 0) {
Mark Salyzyn2d2e0a52015-11-06 12:26:52 -0800352 unlock();
Mark Salyzyn0d00a442015-03-13 12:15:57 -0700353#if (FAKE_LOG_DEVICE == 0)
354 if (pstore_fd >= 0) {
355 __write_to_log_daemon(log_id, vec, nr);
356 }
357#endif
Mark Salyzyn8245af12014-03-25 13:45:33 -0700358 return ret;
359 }
360
Mark Salyzyn53016d82015-02-04 12:28:47 -0800361 write_to_log = __write_to_log_daemon;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800362 }
363
Mark Salyzyn2d2e0a52015-11-06 12:26:52 -0800364 unlock();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800365
366 return write_to_log(log_id, vec, nr);
367}
368
369int __android_log_write(int prio, const char *tag, const char *msg)
370{
Dan Albertc7aadc42015-04-02 10:32:44 -0700371 return __android_log_buf_write(LOG_ID_MAIN, prio, tag, msg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800372}
373
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800374int __android_log_buf_write(int bufID, int prio, const char *tag, const char *msg)
375{
376 struct iovec vec[3];
Wink Saville3761e962012-11-28 12:20:19 -0800377 char tmp_tag[32];
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800378
379 if (!tag)
380 tag = "";
381
382 /* XXX: This needs to go! */
Wink Saville3761e962012-11-28 12:20:19 -0800383 if ((bufID != LOG_ID_RADIO) &&
384 (!strcmp(tag, "HTC_RIL") ||
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800385 !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
Jeff Sharkey84dcf092012-08-13 11:27:30 -0700386 !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800387 !strcmp(tag, "AT") ||
388 !strcmp(tag, "GSM") ||
389 !strcmp(tag, "STK") ||
390 !strcmp(tag, "CDMA") ||
391 !strcmp(tag, "PHONE") ||
Wink Saville3761e962012-11-28 12:20:19 -0800392 !strcmp(tag, "SMS"))) {
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800393 bufID = LOG_ID_RADIO;
Mark Salyzyn8245af12014-03-25 13:45:33 -0700394 /* Inform third party apps/ril/radio.. to use Rlog or RLOG */
Wink Saville3761e962012-11-28 12:20:19 -0800395 snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
396 tag = tmp_tag;
397 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800398
Dan Albertc7aadc42015-04-02 10:32:44 -0700399#if __BIONIC__
400 if (prio == ANDROID_LOG_FATAL) {
401 android_set_abort_message(msg);
402 }
403#endif
404
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800405 vec[0].iov_base = (unsigned char *) &prio;
406 vec[0].iov_len = 1;
407 vec[1].iov_base = (void *) tag;
408 vec[1].iov_len = strlen(tag) + 1;
409 vec[2].iov_base = (void *) msg;
410 vec[2].iov_len = strlen(msg) + 1;
411
412 return write_to_log(bufID, vec, 3);
413}
414
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800415int __android_log_vprint(int prio, const char *tag, const char *fmt, va_list ap)
416{
Chris Pearson19299902010-06-02 16:25:35 -0700417 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800418
419 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
420
421 return __android_log_write(prio, tag, buf);
422}
423
424int __android_log_print(int prio, const char *tag, const char *fmt, ...)
425{
426 va_list ap;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800427 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800428
429 va_start(ap, fmt);
430 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
431 va_end(ap);
432
433 return __android_log_write(prio, tag, buf);
434}
435
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800436int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...)
437{
438 va_list ap;
439 char buf[LOG_BUF_SIZE];
440
441 va_start(ap, fmt);
442 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
443 va_end(ap);
444
445 return __android_log_buf_write(bufID, prio, tag, buf);
446}
447
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800448void __android_log_assert(const char *cond, const char *tag,
Mark Salyzyncf4aa032013-11-22 07:54:30 -0800449 const char *fmt, ...)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800450{
Chris Pearson19299902010-06-02 16:25:35 -0700451 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800452
Chris Pearson19299902010-06-02 16:25:35 -0700453 if (fmt) {
454 va_list ap;
455 va_start(ap, fmt);
456 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
457 va_end(ap);
458 } else {
459 /* Msg not provided, log condition. N.B. Do not use cond directly as
460 * format string as it could contain spurious '%' syntax (e.g.
461 * "%d" in "blocks%devs == 0").
462 */
463 if (cond)
464 snprintf(buf, LOG_BUF_SIZE, "Assertion failed: %s", cond);
465 else
466 strcpy(buf, "Unspecified assertion failed");
467 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800468
469 __android_log_write(ANDROID_LOG_FATAL, tag, buf);
Elliott Hughes02ff4b82015-03-07 11:21:37 -0800470 abort(); /* abort so we have a chance to debug the situation */
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700471 /* NOTREACHED */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800472}
473
474int __android_log_bwrite(int32_t tag, const void *payload, size_t len)
475{
476 struct iovec vec[2];
477
478 vec[0].iov_base = &tag;
479 vec[0].iov_len = sizeof(tag);
480 vec[1].iov_base = (void*)payload;
481 vec[1].iov_len = len;
482
483 return write_to_log(LOG_ID_EVENTS, vec, 2);
484}
485
486/*
487 * Like __android_log_bwrite, but takes the type as well. Doesn't work
488 * for the general case where we're generating lists of stuff, but very
489 * handy if we just want to dump an integer into the log.
490 */
491int __android_log_btwrite(int32_t tag, char type, const void *payload,
Mark Salyzyn154f4602014-02-20 14:59:07 -0800492 size_t len)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800493{
494 struct iovec vec[3];
495
496 vec[0].iov_base = &tag;
497 vec[0].iov_len = sizeof(tag);
498 vec[1].iov_base = &type;
499 vec[1].iov_len = sizeof(type);
500 vec[2].iov_base = (void*)payload;
501 vec[2].iov_len = len;
502
503 return write_to_log(LOG_ID_EVENTS, vec, 3);
504}
Nick Kralevich2a4d05a2014-07-01 10:57:16 -0700505
506/*
507 * Like __android_log_bwrite, but used for writing strings to the
508 * event log.
509 */
510int __android_log_bswrite(int32_t tag, const char *payload)
511{
512 struct iovec vec[4];
513 char type = EVENT_TYPE_STRING;
514 uint32_t len = strlen(payload);
515
516 vec[0].iov_base = &tag;
517 vec[0].iov_len = sizeof(tag);
518 vec[1].iov_base = &type;
519 vec[1].iov_len = sizeof(type);
520 vec[2].iov_base = &len;
521 vec[2].iov_len = sizeof(len);
522 vec[3].iov_base = (void*)payload;
523 vec[3].iov_len = len;
524
525 return write_to_log(LOG_ID_EVENTS, vec, 4);
526}