1dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project/*
2dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Copyright (C) 2007 The Android Open Source Project
3dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *
4dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
5dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * you may not use this file except in compliance with the License.
6dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * You may obtain a copy of the License at
7dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *
8dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
9dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *
10dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
11dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
12dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * See the License for the specific language governing permissions and
14dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * limitations under the License.
15dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project */
16dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <time.h>
17dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <stdio.h>
18dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#ifdef HAVE_PTHREADS
19dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <pthread.h>
20dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#endif
21dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <unistd.h>
22dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <errno.h>
23dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <fcntl.h>
24dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <string.h>
25dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <stdlib.h>
26dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <stdarg.h>
27dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
28dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <cutils/logger.h>
29dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <cutils/logd.h>
30e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato#include <cutils/log.h>
31dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
32dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#define LOG_BUF_SIZE	1024
33dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
34dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#if FAKE_LOG_DEVICE
35dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project// This will be defined when building for the host.
36dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#define log_open(pathname, flags) fakeLogOpen(pathname, flags)
37dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#define log_writev(filedes, vector, count) fakeLogWritev(filedes, vector, count)
38dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#define log_close(filedes) fakeLogClose(filedes)
39dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#else
40dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#define log_open(pathname, flags) open(pathname, flags)
41dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#define log_writev(filedes, vector, count) writev(filedes, vector, count)
42dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#define log_close(filedes) close(filedes)
43dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#endif
44dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
45dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic int __write_to_log_init(log_id_t, struct iovec *vec, size_t nr);
46e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onoratostatic int (*write_to_log)(log_id_t, struct iovec *vec, size_t nr) = __write_to_log_init;
47dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#ifdef HAVE_PTHREADS
48dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
49dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#endif
50dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
51e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onoratostatic int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1, -1 };
52dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
53dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project/*
54dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * This is used by the C++ code to decide if it should write logs through
55dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * the C code.  Basically, if /dev/log/... is available, we're running in
56dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * the simulator rather than a desktop tool and want to use the device.
57dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project */
58dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic enum {
5919299904343daf191267564fe32e6cd5c165cd42Chris Pearson    kLogUninitialized, kLogNotAvailable, kLogAvailable
60dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project} g_log_status = kLogUninitialized;
61dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint __android_log_dev_available(void)
62dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
63dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (g_log_status == kLogUninitialized) {
64dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if (access("/dev/"LOGGER_LOG_MAIN, W_OK) == 0)
65dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            g_log_status = kLogAvailable;
66dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        else
67dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            g_log_status = kLogNotAvailable;
68dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
69dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
70dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return (g_log_status == kLogAvailable);
71dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
72dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
73dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic int __write_to_log_null(log_id_t log_fd, struct iovec *vec, size_t nr)
74dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
75dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return -1;
76dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
77dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
78dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic int __write_to_log_kernel(log_id_t log_id, struct iovec *vec, size_t nr)
79dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
80dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    ssize_t ret;
81dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int log_fd;
82dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
83dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (/*(int)log_id >= 0 &&*/ (int)log_id < (int)LOG_ID_MAX) {
84dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        log_fd = log_fds[(int)log_id];
85dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    } else {
86dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return EBADF;
87dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
88dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
89dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    do {
90dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        ret = log_writev(log_fd, vec, nr);
91dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    } while (ret < 0 && errno == EINTR);
92dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
93dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return ret;
94dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
95dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
96dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr)
97dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
98dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#ifdef HAVE_PTHREADS
99dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    pthread_mutex_lock(&log_init_lock);
100dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#endif
101dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
102dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (write_to_log == __write_to_log_init) {
103dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        log_fds[LOG_ID_MAIN] = log_open("/dev/"LOGGER_LOG_MAIN, O_WRONLY);
104dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        log_fds[LOG_ID_RADIO] = log_open("/dev/"LOGGER_LOG_RADIO, O_WRONLY);
105dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        log_fds[LOG_ID_EVENTS] = log_open("/dev/"LOGGER_LOG_EVENTS, O_WRONLY);
106e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato        log_fds[LOG_ID_SYSTEM] = log_open("/dev/"LOGGER_LOG_SYSTEM, O_WRONLY);
107dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
108dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        write_to_log = __write_to_log_kernel;
109dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
110dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if (log_fds[LOG_ID_MAIN] < 0 || log_fds[LOG_ID_RADIO] < 0 ||
111dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                log_fds[LOG_ID_EVENTS] < 0) {
112dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            log_close(log_fds[LOG_ID_MAIN]);
113dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            log_close(log_fds[LOG_ID_RADIO]);
114dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            log_close(log_fds[LOG_ID_EVENTS]);
115dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            log_fds[LOG_ID_MAIN] = -1;
116dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            log_fds[LOG_ID_RADIO] = -1;
117dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            log_fds[LOG_ID_EVENTS] = -1;
118dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            write_to_log = __write_to_log_null;
119dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
120e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato
121e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato        if (log_fds[LOG_ID_SYSTEM] < 0) {
122e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato            log_fds[LOG_ID_SYSTEM] = log_fds[LOG_ID_MAIN];
123e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato        }
124dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
125dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
126dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#ifdef HAVE_PTHREADS
127dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    pthread_mutex_unlock(&log_init_lock);
128dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#endif
129dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
130dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return write_to_log(log_id, vec, nr);
131dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
132dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
133dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint __android_log_write(int prio, const char *tag, const char *msg)
134dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
135dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    struct iovec vec[3];
136dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    log_id_t log_id = LOG_ID_MAIN;
137dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
138dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (!tag)
139dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        tag = "";
140dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
141dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    /* XXX: This needs to go! */
142dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (!strcmp(tag, "HTC_RIL") ||
143ed7ccae2968a75e09c22a3da90a9762170f96477John Michelau        !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
14484dcf091482b8d0f484e665915d354069a8c879bJeff Sharkey        !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
145dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        !strcmp(tag, "AT") ||
146dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        !strcmp(tag, "GSM") ||
14789efdc9c7f4005032dad86dd6dd220c23a7ff4d7Wink Saville        !strcmp(tag, "STK") ||
14889efdc9c7f4005032dad86dd6dd220c23a7ff4d7Wink Saville        !strcmp(tag, "CDMA") ||
14989efdc9c7f4005032dad86dd6dd220c23a7ff4d7Wink Saville        !strcmp(tag, "PHONE") ||
15084dcf091482b8d0f484e665915d354069a8c879bJeff Sharkey        !strcmp(tag, "SMS"))
151dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            log_id = LOG_ID_RADIO;
152dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
153dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    vec[0].iov_base   = (unsigned char *) &prio;
154dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    vec[0].iov_len    = 1;
155dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    vec[1].iov_base   = (void *) tag;
156dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    vec[1].iov_len    = strlen(tag) + 1;
157dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    vec[2].iov_base   = (void *) msg;
158dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    vec[2].iov_len    = strlen(msg) + 1;
159dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
160dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return write_to_log(log_id, vec, 3);
161dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
162dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
163e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onoratoint __android_log_buf_write(int bufID, int prio, const char *tag, const char *msg)
164e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato{
165e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato    struct iovec vec[3];
166e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato
167e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato    if (!tag)
168e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato        tag = "";
169e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato
170e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato    /* XXX: This needs to go! */
171e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato    if (!strcmp(tag, "HTC_RIL") ||
172e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato        !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
17384dcf091482b8d0f484e665915d354069a8c879bJeff Sharkey        !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
174e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato        !strcmp(tag, "AT") ||
175e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato        !strcmp(tag, "GSM") ||
176e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato        !strcmp(tag, "STK") ||
177e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato        !strcmp(tag, "CDMA") ||
178e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato        !strcmp(tag, "PHONE") ||
17984dcf091482b8d0f484e665915d354069a8c879bJeff Sharkey        !strcmp(tag, "SMS"))
180e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato            bufID = LOG_ID_RADIO;
181e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato
182e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato    vec[0].iov_base   = (unsigned char *) &prio;
183e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato    vec[0].iov_len    = 1;
184e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato    vec[1].iov_base   = (void *) tag;
185e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato    vec[1].iov_len    = strlen(tag) + 1;
186e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato    vec[2].iov_base   = (void *) msg;
187e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato    vec[2].iov_len    = strlen(msg) + 1;
188e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato
189e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato    return write_to_log(bufID, vec, 3);
190e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato}
191e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato
192dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint __android_log_vprint(int prio, const char *tag, const char *fmt, va_list ap)
193dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
19419299904343daf191267564fe32e6cd5c165cd42Chris Pearson    char buf[LOG_BUF_SIZE];
195dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
196dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
197dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
198dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return __android_log_write(prio, tag, buf);
199dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
200dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
201dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint __android_log_print(int prio, const char *tag, const char *fmt, ...)
202dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
203dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    va_list ap;
204e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato    char buf[LOG_BUF_SIZE];
205dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
206dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    va_start(ap, fmt);
207dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
208dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    va_end(ap);
209dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
210dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return __android_log_write(prio, tag, buf);
211dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
212dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
213e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onoratoint __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...)
214e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato{
215e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato    va_list ap;
216e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato    char buf[LOG_BUF_SIZE];
217e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato
218e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato    va_start(ap, fmt);
219e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato    vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
220e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato    va_end(ap);
221e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato
222e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato    return __android_log_buf_write(bufID, prio, tag, buf);
223e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato}
224e2bf2ea4d2846031edfc52b942ad53e5467243f6Joe Onorato
225dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectvoid __android_log_assert(const char *cond, const char *tag,
226dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			  const char *fmt, ...)
227dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
22819299904343daf191267564fe32e6cd5c165cd42Chris Pearson    char buf[LOG_BUF_SIZE];
229dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
23019299904343daf191267564fe32e6cd5c165cd42Chris Pearson    if (fmt) {
23119299904343daf191267564fe32e6cd5c165cd42Chris Pearson        va_list ap;
23219299904343daf191267564fe32e6cd5c165cd42Chris Pearson        va_start(ap, fmt);
23319299904343daf191267564fe32e6cd5c165cd42Chris Pearson        vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
23419299904343daf191267564fe32e6cd5c165cd42Chris Pearson        va_end(ap);
23519299904343daf191267564fe32e6cd5c165cd42Chris Pearson    } else {
23619299904343daf191267564fe32e6cd5c165cd42Chris Pearson        /* Msg not provided, log condition.  N.B. Do not use cond directly as
23719299904343daf191267564fe32e6cd5c165cd42Chris Pearson         * format string as it could contain spurious '%' syntax (e.g.
23819299904343daf191267564fe32e6cd5c165cd42Chris Pearson         * "%d" in "blocks%devs == 0").
23919299904343daf191267564fe32e6cd5c165cd42Chris Pearson         */
24019299904343daf191267564fe32e6cd5c165cd42Chris Pearson        if (cond)
24119299904343daf191267564fe32e6cd5c165cd42Chris Pearson            snprintf(buf, LOG_BUF_SIZE, "Assertion failed: %s", cond);
24219299904343daf191267564fe32e6cd5c165cd42Chris Pearson        else
24319299904343daf191267564fe32e6cd5c165cd42Chris Pearson            strcpy(buf, "Unspecified assertion failed");
24419299904343daf191267564fe32e6cd5c165cd42Chris Pearson    }
245dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
246dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    __android_log_write(ANDROID_LOG_FATAL, tag, buf);
247dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
248dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    __builtin_trap(); /* trap so we have a chance to debug the situation */
249dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
250dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
251dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint __android_log_bwrite(int32_t tag, const void *payload, size_t len)
252dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
253dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    struct iovec vec[2];
254dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
255dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    vec[0].iov_base = &tag;
256dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    vec[0].iov_len = sizeof(tag);
257dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    vec[1].iov_base = (void*)payload;
258dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    vec[1].iov_len = len;
259dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
260dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return write_to_log(LOG_ID_EVENTS, vec, 2);
261dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
262dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
263dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project/*
264dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Like __android_log_bwrite, but takes the type as well.  Doesn't work
265dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * for the general case where we're generating lists of stuff, but very
266dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * handy if we just want to dump an integer into the log.
267dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project */
268dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint __android_log_btwrite(int32_t tag, char type, const void *payload,
269dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    size_t len)
270dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
271dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    struct iovec vec[3];
272dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
273dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    vec[0].iov_base = &tag;
274dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    vec[0].iov_len = sizeof(tag);
275dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    vec[1].iov_base = &type;
276dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    vec[1].iov_len = sizeof(type);
277dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    vec[2].iov_base = (void*)payload;
278dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    vec[2].iov_len = len;
279dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
280dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return write_to_log(LOG_ID_EVENTS, vec, 3);
281dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
282