blob: 83c6dc29624e09277dd48a074b5d65970e6f0bd4 [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 Salyzynb6bee332015-09-08 08:56:32 -0700215 if (android_log_timestamp() == 'm') {
216 clock_gettime(CLOCK_MONOTONIC, &ts);
217 } else {
218 clock_gettime(CLOCK_REALTIME, &ts);
219 }
Mark Salyzyn076ba812014-05-29 17:53:41 -0700220
Mark Salyzynd91ab582014-12-15 10:52:12 -0800221 pmsg_header.magic = LOGGER_MAGIC;
222 pmsg_header.len = sizeof(pmsg_header) + sizeof(header);
223 pmsg_header.uid = last_uid;
224 pmsg_header.pid = last_pid;
225
Mark Salyzyn7a809402015-01-16 13:38:07 -0800226 header.tid = gettid();
227 header.realtime.tv_sec = ts.tv_sec;
228 header.realtime.tv_nsec = ts.tv_nsec;
Mark Salyzyn154f4602014-02-20 14:59:07 -0800229
Mark Salyzynd91ab582014-12-15 10:52:12 -0800230 newVec[0].iov_base = (unsigned char *) &pmsg_header;
231 newVec[0].iov_len = sizeof(pmsg_header);
232 newVec[1].iov_base = (unsigned char *) &header;
233 newVec[1].iov_len = sizeof(header);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800234
Mark Salyzynd45d36e2015-02-12 15:14:26 -0800235 if (logd_fd > 0) {
236 int32_t snapshot = atomic_exchange_explicit(&dropped, 0, memory_order_relaxed);
237 if (snapshot) {
238 android_log_event_int_t buffer;
239
240 header.id = LOG_ID_EVENTS;
241 buffer.header.tag = htole32(LIBLOG_LOG_TAG);
242 buffer.payload.type = EVENT_TYPE_INT;
243 buffer.payload.data = htole32(snapshot);
244
245 newVec[2].iov_base = &buffer;
246 newVec[2].iov_len = sizeof(buffer);
247
248 ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, 2));
249 if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) {
250 atomic_fetch_add_explicit(&dropped, snapshot, memory_order_relaxed);
251 }
252 }
253 }
254
255 header.id = log_id;
256
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700257 for (payload_size = 0, i = header_length; i < nr + header_length; i++) {
258 newVec[i].iov_base = vec[i - header_length].iov_base;
259 payload_size += newVec[i].iov_len = vec[i - header_length].iov_len;
260
261 if (payload_size > LOGGER_ENTRY_MAX_PAYLOAD) {
262 newVec[i].iov_len -= payload_size - LOGGER_ENTRY_MAX_PAYLOAD;
263 if (newVec[i].iov_len) {
264 ++i;
265 }
Mark Salyzynd91ab582014-12-15 10:52:12 -0800266 payload_size = LOGGER_ENTRY_MAX_PAYLOAD;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700267 break;
268 }
Mark Salyzyn154f4602014-02-20 14:59:07 -0800269 }
Mark Salyzynd91ab582014-12-15 10:52:12 -0800270 pmsg_header.len += payload_size;
271
272 if (pstore_fd >= 0) {
273 TEMP_FAILURE_RETRY(writev(pstore_fd, newVec, i));
274 }
275
276 if (last_uid == AID_LOGD) { /* logd, after initialization and priv drop */
277 /*
278 * ignore log messages we send to ourself (logd).
279 * Such log messages are often generated by libraries we depend on
280 * which use standard Android logging.
281 */
282 return 0;
283 }
284
285 if (logd_fd < 0) {
286 return -EBADF;
287 }
Mark Salyzyn154f4602014-02-20 14:59:07 -0800288
Mark Salyzyn8245af12014-03-25 13:45:33 -0700289 /*
290 * The write below could be lost, but will never block.
291 *
Mark Salyzynd91ab582014-12-15 10:52:12 -0800292 * To logd, we drop the pmsg_header
293 *
Mark Salyzyn8245af12014-03-25 13:45:33 -0700294 * ENOTCONN occurs if logd dies.
295 * EAGAIN occurs if logd is overloaded.
296 */
Mark Salyzynd91ab582014-12-15 10:52:12 -0800297 ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, i - 1));
Mark Salyzyn8245af12014-03-25 13:45:33 -0700298 if (ret < 0) {
299 ret = -errno;
300 if (ret == -ENOTCONN) {
Mark Salyzyn2d2e0a52015-11-06 12:26:52 -0800301 lock();
Mark Salyzyn0d00a442015-03-13 12:15:57 -0700302 close(logd_fd);
303 logd_fd = -1;
Mark Salyzyn8245af12014-03-25 13:45:33 -0700304 ret = __write_to_log_initialize();
Mark Salyzyn2d2e0a52015-11-06 12:26:52 -0800305 unlock();
Mark Salyzyn8245af12014-03-25 13:45:33 -0700306
307 if (ret < 0) {
308 return ret;
309 }
310
Mark Salyzynd91ab582014-12-15 10:52:12 -0800311 ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, i - 1));
Mark Salyzyn8245af12014-03-25 13:45:33 -0700312 if (ret < 0) {
313 ret = -errno;
314 }
315 }
316 }
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700317
Mark Salyzyn7a809402015-01-16 13:38:07 -0800318 if (ret > (ssize_t)sizeof(header)) {
319 ret -= sizeof(header);
Mark Salyzynd45d36e2015-02-12 15:14:26 -0800320 } else if (ret == -EAGAIN) {
321 atomic_fetch_add_explicit(&dropped, 1, memory_order_relaxed);
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700322 }
Mark Salyzyn154f4602014-02-20 14:59:07 -0800323#endif
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700324
325 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800326}
327
Mark Salyzyn154f4602014-02-20 14:59:07 -0800328#if FAKE_LOG_DEVICE
329static const char *LOG_NAME[LOG_ID_MAX] = {
330 [LOG_ID_MAIN] = "main",
331 [LOG_ID_RADIO] = "radio",
332 [LOG_ID_EVENTS] = "events",
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700333 [LOG_ID_SYSTEM] = "system",
Mark Salyzyn440e1092014-10-10 15:13:15 -0700334 [LOG_ID_CRASH] = "crash",
335 [LOG_ID_KERNEL] = "kernel",
Mark Salyzyn154f4602014-02-20 14:59:07 -0800336};
337
Adam Lesinskia1ac84c2014-10-20 12:31:30 -0700338const char *android_log_id_to_name(log_id_t log_id)
Mark Salyzyn154f4602014-02-20 14:59:07 -0800339{
340 if (log_id >= LOG_ID_MAX) {
341 log_id = LOG_ID_MAIN;
342 }
343 return LOG_NAME[log_id];
344}
345#endif
346
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800347static int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr)
348{
Mark Salyzyn2d2e0a52015-11-06 12:26:52 -0800349 lock();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800350
351 if (write_to_log == __write_to_log_init) {
Mark Salyzyn8245af12014-03-25 13:45:33 -0700352 int ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800353
Mark Salyzyn8245af12014-03-25 13:45:33 -0700354 ret = __write_to_log_initialize();
355 if (ret < 0) {
Mark Salyzyn2d2e0a52015-11-06 12:26:52 -0800356 unlock();
Mark Salyzyn0d00a442015-03-13 12:15:57 -0700357#if (FAKE_LOG_DEVICE == 0)
358 if (pstore_fd >= 0) {
359 __write_to_log_daemon(log_id, vec, nr);
360 }
361#endif
Mark Salyzyn8245af12014-03-25 13:45:33 -0700362 return ret;
363 }
364
Mark Salyzyn53016d82015-02-04 12:28:47 -0800365 write_to_log = __write_to_log_daemon;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800366 }
367
Mark Salyzyn2d2e0a52015-11-06 12:26:52 -0800368 unlock();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800369
370 return write_to_log(log_id, vec, nr);
371}
372
373int __android_log_write(int prio, const char *tag, const char *msg)
374{
Dan Albertc7aadc42015-04-02 10:32:44 -0700375 return __android_log_buf_write(LOG_ID_MAIN, prio, tag, msg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800376}
377
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800378int __android_log_buf_write(int bufID, int prio, const char *tag, const char *msg)
379{
380 struct iovec vec[3];
Wink Saville3761e962012-11-28 12:20:19 -0800381 char tmp_tag[32];
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800382
383 if (!tag)
384 tag = "";
385
386 /* XXX: This needs to go! */
Wink Saville3761e962012-11-28 12:20:19 -0800387 if ((bufID != LOG_ID_RADIO) &&
388 (!strcmp(tag, "HTC_RIL") ||
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800389 !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
Jeff Sharkey84dcf092012-08-13 11:27:30 -0700390 !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800391 !strcmp(tag, "AT") ||
392 !strcmp(tag, "GSM") ||
393 !strcmp(tag, "STK") ||
394 !strcmp(tag, "CDMA") ||
395 !strcmp(tag, "PHONE") ||
Wink Saville3761e962012-11-28 12:20:19 -0800396 !strcmp(tag, "SMS"))) {
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800397 bufID = LOG_ID_RADIO;
Mark Salyzyn8245af12014-03-25 13:45:33 -0700398 /* Inform third party apps/ril/radio.. to use Rlog or RLOG */
Wink Saville3761e962012-11-28 12:20:19 -0800399 snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
400 tag = tmp_tag;
401 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800402
Dan Albertc7aadc42015-04-02 10:32:44 -0700403#if __BIONIC__
404 if (prio == ANDROID_LOG_FATAL) {
405 android_set_abort_message(msg);
406 }
407#endif
408
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800409 vec[0].iov_base = (unsigned char *) &prio;
410 vec[0].iov_len = 1;
411 vec[1].iov_base = (void *) tag;
412 vec[1].iov_len = strlen(tag) + 1;
413 vec[2].iov_base = (void *) msg;
414 vec[2].iov_len = strlen(msg) + 1;
415
416 return write_to_log(bufID, vec, 3);
417}
418
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800419int __android_log_vprint(int prio, const char *tag, const char *fmt, va_list ap)
420{
Chris Pearson19299902010-06-02 16:25:35 -0700421 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800422
423 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
424
425 return __android_log_write(prio, tag, buf);
426}
427
428int __android_log_print(int prio, const char *tag, const char *fmt, ...)
429{
430 va_list ap;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800431 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800432
433 va_start(ap, fmt);
434 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
435 va_end(ap);
436
437 return __android_log_write(prio, tag, buf);
438}
439
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800440int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...)
441{
442 va_list ap;
443 char buf[LOG_BUF_SIZE];
444
445 va_start(ap, fmt);
446 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
447 va_end(ap);
448
449 return __android_log_buf_write(bufID, prio, tag, buf);
450}
451
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800452void __android_log_assert(const char *cond, const char *tag,
Mark Salyzyncf4aa032013-11-22 07:54:30 -0800453 const char *fmt, ...)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800454{
Chris Pearson19299902010-06-02 16:25:35 -0700455 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800456
Chris Pearson19299902010-06-02 16:25:35 -0700457 if (fmt) {
458 va_list ap;
459 va_start(ap, fmt);
460 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
461 va_end(ap);
462 } else {
463 /* Msg not provided, log condition. N.B. Do not use cond directly as
464 * format string as it could contain spurious '%' syntax (e.g.
465 * "%d" in "blocks%devs == 0").
466 */
467 if (cond)
468 snprintf(buf, LOG_BUF_SIZE, "Assertion failed: %s", cond);
469 else
470 strcpy(buf, "Unspecified assertion failed");
471 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800472
473 __android_log_write(ANDROID_LOG_FATAL, tag, buf);
Elliott Hughes02ff4b82015-03-07 11:21:37 -0800474 abort(); /* abort so we have a chance to debug the situation */
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700475 /* NOTREACHED */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800476}
477
478int __android_log_bwrite(int32_t tag, const void *payload, size_t len)
479{
480 struct iovec vec[2];
481
482 vec[0].iov_base = &tag;
483 vec[0].iov_len = sizeof(tag);
484 vec[1].iov_base = (void*)payload;
485 vec[1].iov_len = len;
486
487 return write_to_log(LOG_ID_EVENTS, vec, 2);
488}
489
490/*
491 * Like __android_log_bwrite, but takes the type as well. Doesn't work
492 * for the general case where we're generating lists of stuff, but very
493 * handy if we just want to dump an integer into the log.
494 */
495int __android_log_btwrite(int32_t tag, char type, const void *payload,
Mark Salyzyn154f4602014-02-20 14:59:07 -0800496 size_t len)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800497{
498 struct iovec vec[3];
499
500 vec[0].iov_base = &tag;
501 vec[0].iov_len = sizeof(tag);
502 vec[1].iov_base = &type;
503 vec[1].iov_len = sizeof(type);
504 vec[2].iov_base = (void*)payload;
505 vec[2].iov_len = len;
506
507 return write_to_log(LOG_ID_EVENTS, vec, 3);
508}
Nick Kralevich2a4d05a2014-07-01 10:57:16 -0700509
510/*
511 * Like __android_log_bwrite, but used for writing strings to the
512 * event log.
513 */
514int __android_log_bswrite(int32_t tag, const char *payload)
515{
516 struct iovec vec[4];
517 char type = EVENT_TYPE_STRING;
518 uint32_t len = strlen(payload);
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 = &len;
525 vec[2].iov_len = sizeof(len);
526 vec[3].iov_base = (void*)payload;
527 vec[3].iov_len = len;
528
529 return write_to_log(LOG_ID_EVENTS, vec, 4);
530}