blob: 5ef349b02f618ffcdeb070f7cd9ba018636ca0dc [file] [log] [blame]
Mark Salyzyn154f4602014-02-20 14:59:07 -08001/*
2 * Copyright (C) 2007-2014 The Android Open Source Project
3 *
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 */
16
17#include <errno.h>
18#include <fcntl.h>
19#ifdef HAVE_PTHREADS
20#include <pthread.h>
21#endif
22#include <stdarg.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/stat.h>
27#include <sys/types.h>
28#include <time.h>
29#include <unistd.h>
30
31#include <log/log.h>
32#include <log/logd.h>
33#include <log/logger.h>
34
35#define LOGGER_LOG_MAIN "log/main"
36#define LOGGER_LOG_RADIO "log/radio"
37#define LOGGER_LOG_EVENTS "log/events"
38#define LOGGER_LOG_SYSTEM "log/system"
39
40#define LOG_BUF_SIZE 1024
41
42#if FAKE_LOG_DEVICE
43/* This will be defined when building for the host. */
44#include "fake_log_device.h"
45#define log_open(pathname, flags) fakeLogOpen(pathname, flags)
46#define log_writev(filedes, vector, count) fakeLogWritev(filedes, vector, count)
47#define log_close(filedes) fakeLogClose(filedes)
48#else
49#define log_open(pathname, flags) open(pathname, (flags) | O_CLOEXEC)
50#define log_writev(filedes, vector, count) writev(filedes, vector, count)
51#define log_close(filedes) close(filedes)
52#endif
53
54static int __write_to_log_init(log_id_t, struct iovec *vec, size_t nr);
55static int (*write_to_log)(log_id_t, struct iovec *vec, size_t nr) = __write_to_log_init;
56#ifdef HAVE_PTHREADS
57static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
58#endif
59
60#define UNUSED __attribute__((__unused__))
61
62static int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1, -1 };
63
64/*
65 * This is used by the C++ code to decide if it should write logs through
66 * the C code. Basically, if /dev/log/... is available, we're running in
67 * the simulator rather than a desktop tool and want to use the device.
68 */
69static enum {
70 kLogUninitialized, kLogNotAvailable, kLogAvailable
71} g_log_status = kLogUninitialized;
72int __android_log_dev_available(void)
73{
74 if (g_log_status == kLogUninitialized) {
75 if (access("/dev/"LOGGER_LOG_MAIN, W_OK) == 0)
76 g_log_status = kLogAvailable;
77 else
78 g_log_status = kLogNotAvailable;
79 }
80
81 return (g_log_status == kLogAvailable);
82}
83
84static int __write_to_log_null(log_id_t log_fd UNUSED, struct iovec *vec UNUSED,
85 size_t nr UNUSED)
86{
87 return -1;
88}
89
90static int __write_to_log_kernel(log_id_t log_id, struct iovec *vec, size_t nr)
91{
92 ssize_t ret;
93 int log_fd;
94
95 if (/*(int)log_id >= 0 &&*/ (int)log_id < (int)LOG_ID_MAX) {
96 log_fd = log_fds[(int)log_id];
97 } else {
Mark Salyzyn8245af12014-03-25 13:45:33 -070098 return -EBADF;
Mark Salyzyn154f4602014-02-20 14:59:07 -080099 }
100
101 do {
102 ret = log_writev(log_fd, vec, nr);
Mark Salyzyn8245af12014-03-25 13:45:33 -0700103 if (ret < 0) {
104 ret = -errno;
105 }
106 } while (ret == -EINTR);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800107
108 return ret;
109}
110
111static int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr)
112{
113#ifdef HAVE_PTHREADS
114 pthread_mutex_lock(&log_init_lock);
115#endif
116
117 if (write_to_log == __write_to_log_init) {
118 log_fds[LOG_ID_MAIN] = log_open("/dev/"LOGGER_LOG_MAIN, O_WRONLY);
119 log_fds[LOG_ID_RADIO] = log_open("/dev/"LOGGER_LOG_RADIO, O_WRONLY);
120 log_fds[LOG_ID_EVENTS] = log_open("/dev/"LOGGER_LOG_EVENTS, O_WRONLY);
121 log_fds[LOG_ID_SYSTEM] = log_open("/dev/"LOGGER_LOG_SYSTEM, O_WRONLY);
122
123 write_to_log = __write_to_log_kernel;
124
125 if (log_fds[LOG_ID_MAIN] < 0 || log_fds[LOG_ID_RADIO] < 0 ||
126 log_fds[LOG_ID_EVENTS] < 0) {
127 log_close(log_fds[LOG_ID_MAIN]);
128 log_close(log_fds[LOG_ID_RADIO]);
129 log_close(log_fds[LOG_ID_EVENTS]);
130 log_fds[LOG_ID_MAIN] = -1;
131 log_fds[LOG_ID_RADIO] = -1;
132 log_fds[LOG_ID_EVENTS] = -1;
133 write_to_log = __write_to_log_null;
134 }
135
136 if (log_fds[LOG_ID_SYSTEM] < 0) {
137 log_fds[LOG_ID_SYSTEM] = log_fds[LOG_ID_MAIN];
138 }
139 }
140
141#ifdef HAVE_PTHREADS
142 pthread_mutex_unlock(&log_init_lock);
143#endif
144
145 return write_to_log(log_id, vec, nr);
146}
147
148int __android_log_write(int prio, const char *tag, const char *msg)
149{
150 struct iovec vec[3];
151 log_id_t log_id = LOG_ID_MAIN;
152 char tmp_tag[32];
153
154 if (!tag)
155 tag = "";
156
157 /* XXX: This needs to go! */
158 if (!strcmp(tag, "HTC_RIL") ||
159 !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
160 !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
161 !strcmp(tag, "AT") ||
162 !strcmp(tag, "GSM") ||
163 !strcmp(tag, "STK") ||
164 !strcmp(tag, "CDMA") ||
165 !strcmp(tag, "PHONE") ||
166 !strcmp(tag, "SMS")) {
167 log_id = LOG_ID_RADIO;
168 /* Inform third party apps/ril/radio.. to use Rlog or RLOG */
169 snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
170 tag = tmp_tag;
171 }
172
173 vec[0].iov_base = (unsigned char *) &prio;
174 vec[0].iov_len = 1;
175 vec[1].iov_base = (void *) tag;
176 vec[1].iov_len = strlen(tag) + 1;
177 vec[2].iov_base = (void *) msg;
178 vec[2].iov_len = strlen(msg) + 1;
179
180 return write_to_log(log_id, vec, 3);
181}
182
183int __android_log_buf_write(int bufID, int prio, const char *tag, const char *msg)
184{
185 struct iovec vec[3];
186 char tmp_tag[32];
187
188 if (!tag)
189 tag = "";
190
191 /* XXX: This needs to go! */
192 if ((bufID != LOG_ID_RADIO) &&
193 (!strcmp(tag, "HTC_RIL") ||
194 !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
195 !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
196 !strcmp(tag, "AT") ||
197 !strcmp(tag, "GSM") ||
198 !strcmp(tag, "STK") ||
199 !strcmp(tag, "CDMA") ||
200 !strcmp(tag, "PHONE") ||
201 !strcmp(tag, "SMS"))) {
202 bufID = LOG_ID_RADIO;
203 /* Inform third party apps/ril/radio.. to use Rlog or RLOG */
204 snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
205 tag = tmp_tag;
206 }
207
208 vec[0].iov_base = (unsigned char *) &prio;
209 vec[0].iov_len = 1;
210 vec[1].iov_base = (void *) tag;
211 vec[1].iov_len = strlen(tag) + 1;
212 vec[2].iov_base = (void *) msg;
213 vec[2].iov_len = strlen(msg) + 1;
214
215 return write_to_log(bufID, vec, 3);
216}
217
218int __android_log_vprint(int prio, const char *tag, const char *fmt, va_list ap)
219{
220 char buf[LOG_BUF_SIZE];
221
222 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
223
224 return __android_log_write(prio, tag, buf);
225}
226
227int __android_log_print(int prio, const char *tag, const char *fmt, ...)
228{
229 va_list ap;
230 char buf[LOG_BUF_SIZE];
231
232 va_start(ap, fmt);
233 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
234 va_end(ap);
235
236 return __android_log_write(prio, tag, buf);
237}
238
239int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...)
240{
241 va_list ap;
242 char buf[LOG_BUF_SIZE];
243
244 va_start(ap, fmt);
245 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
246 va_end(ap);
247
248 return __android_log_buf_write(bufID, prio, tag, buf);
249}
250
251void __android_log_assert(const char *cond, const char *tag,
252 const char *fmt, ...)
253{
254 char buf[LOG_BUF_SIZE];
255
256 if (fmt) {
257 va_list ap;
258 va_start(ap, fmt);
259 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
260 va_end(ap);
261 } else {
262 /* Msg not provided, log condition. N.B. Do not use cond directly as
263 * format string as it could contain spurious '%' syntax (e.g.
264 * "%d" in "blocks%devs == 0").
265 */
266 if (cond)
267 snprintf(buf, LOG_BUF_SIZE, "Assertion failed: %s", cond);
268 else
269 strcpy(buf, "Unspecified assertion failed");
270 }
271
272 __android_log_write(ANDROID_LOG_FATAL, tag, buf);
273
274 __builtin_trap(); /* trap so we have a chance to debug the situation */
275}
276
277int __android_log_bwrite(int32_t tag, const void *payload, size_t len)
278{
279 struct iovec vec[2];
280
281 vec[0].iov_base = &tag;
282 vec[0].iov_len = sizeof(tag);
283 vec[1].iov_base = (void*)payload;
284 vec[1].iov_len = len;
285
286 return write_to_log(LOG_ID_EVENTS, vec, 2);
287}
288
289/*
290 * Like __android_log_bwrite, but takes the type as well. Doesn't work
291 * for the general case where we're generating lists of stuff, but very
292 * handy if we just want to dump an integer into the log.
293 */
294int __android_log_btwrite(int32_t tag, char type, const void *payload,
295 size_t len)
296{
297 struct iovec vec[3];
298
299 vec[0].iov_base = &tag;
300 vec[0].iov_len = sizeof(tag);
301 vec[1].iov_base = &type;
302 vec[1].iov_len = sizeof(type);
303 vec[2].iov_base = (void*)payload;
304 vec[2].iov_len = len;
305
306 return write_to_log(LOG_ID_EVENTS, vec, 3);
307}