blob: d812abce9fcf15512331650f13d2e889361e262f [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2007 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#include <time.h>
17#include <stdio.h>
18#ifdef HAVE_PTHREADS
19#include <pthread.h>
20#endif
21#include <unistd.h>
22#include <errno.h>
23#include <fcntl.h>
24#include <string.h>
25#include <stdlib.h>
26#include <stdarg.h>
27
28#include <cutils/logger.h>
29#include <cutils/logd.h>
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -080030#include <cutils/log.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080031
32#define LOG_BUF_SIZE 1024
33
34#if FAKE_LOG_DEVICE
35// This will be defined when building for the host.
36#define log_open(pathname, flags) fakeLogOpen(pathname, flags)
37#define log_writev(filedes, vector, count) fakeLogWritev(filedes, vector, count)
38#define log_close(filedes) fakeLogClose(filedes)
39#else
40#define log_open(pathname, flags) open(pathname, flags)
41#define log_writev(filedes, vector, count) writev(filedes, vector, count)
42#define log_close(filedes) close(filedes)
43#endif
44
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045static int __write_to_log_init(log_id_t, struct iovec *vec, size_t nr);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -080046static 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 -080047#ifdef HAVE_PTHREADS
48static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
49#endif
50
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -080051static int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1, -1 };
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080052
53/*
54 * This is used by the C++ code to decide if it should write logs through
55 * the C code. Basically, if /dev/log/... is available, we're running in
56 * the simulator rather than a desktop tool and want to use the device.
57 */
58static enum {
Chris Pearson19299902010-06-02 16:25:35 -070059 kLogUninitialized, kLogNotAvailable, kLogAvailable
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060} g_log_status = kLogUninitialized;
61int __android_log_dev_available(void)
62{
63 if (g_log_status == kLogUninitialized) {
64 if (access("/dev/"LOGGER_LOG_MAIN, W_OK) == 0)
65 g_log_status = kLogAvailable;
66 else
67 g_log_status = kLogNotAvailable;
68 }
69
70 return (g_log_status == kLogAvailable);
71}
72
73static int __write_to_log_null(log_id_t log_fd, struct iovec *vec, size_t nr)
74{
75 return -1;
76}
77
78static int __write_to_log_kernel(log_id_t log_id, struct iovec *vec, size_t nr)
79{
80 ssize_t ret;
81 int log_fd;
82
83 if (/*(int)log_id >= 0 &&*/ (int)log_id < (int)LOG_ID_MAX) {
84 log_fd = log_fds[(int)log_id];
85 } else {
86 return EBADF;
87 }
88
89 do {
90 ret = log_writev(log_fd, vec, nr);
91 } while (ret < 0 && errno == EINTR);
92
93 return ret;
94}
95
96static int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr)
97{
98#ifdef HAVE_PTHREADS
99 pthread_mutex_lock(&log_init_lock);
100#endif
101
102 if (write_to_log == __write_to_log_init) {
103 log_fds[LOG_ID_MAIN] = log_open("/dev/"LOGGER_LOG_MAIN, O_WRONLY);
104 log_fds[LOG_ID_RADIO] = log_open("/dev/"LOGGER_LOG_RADIO, O_WRONLY);
105 log_fds[LOG_ID_EVENTS] = log_open("/dev/"LOGGER_LOG_EVENTS, O_WRONLY);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800106 log_fds[LOG_ID_SYSTEM] = log_open("/dev/"LOGGER_LOG_SYSTEM, O_WRONLY);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800107
108 write_to_log = __write_to_log_kernel;
109
110 if (log_fds[LOG_ID_MAIN] < 0 || log_fds[LOG_ID_RADIO] < 0 ||
111 log_fds[LOG_ID_EVENTS] < 0) {
112 log_close(log_fds[LOG_ID_MAIN]);
113 log_close(log_fds[LOG_ID_RADIO]);
114 log_close(log_fds[LOG_ID_EVENTS]);
115 log_fds[LOG_ID_MAIN] = -1;
116 log_fds[LOG_ID_RADIO] = -1;
117 log_fds[LOG_ID_EVENTS] = -1;
118 write_to_log = __write_to_log_null;
119 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800120
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800121 if (log_fds[LOG_ID_SYSTEM] < 0) {
122 log_fds[LOG_ID_SYSTEM] = log_fds[LOG_ID_MAIN];
123 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800124 }
125
126#ifdef HAVE_PTHREADS
127 pthread_mutex_unlock(&log_init_lock);
128#endif
129
130 return write_to_log(log_id, vec, nr);
131}
132
133int __android_log_write(int prio, const char *tag, const char *msg)
134{
135 struct iovec vec[3];
136 log_id_t log_id = LOG_ID_MAIN;
Wink Saville3761e962012-11-28 12:20:19 -0800137 char tmp_tag[32];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800138
139 if (!tag)
140 tag = "";
141
142 /* XXX: This needs to go! */
143 if (!strcmp(tag, "HTC_RIL") ||
John Michelaued7ccae2009-08-19 10:01:55 -0500144 !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
Jeff Sharkey84dcf092012-08-13 11:27:30 -0700145 !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800146 !strcmp(tag, "AT") ||
147 !strcmp(tag, "GSM") ||
Wink Saville89efdc92009-04-02 11:00:57 -0700148 !strcmp(tag, "STK") ||
149 !strcmp(tag, "CDMA") ||
150 !strcmp(tag, "PHONE") ||
Wink Saville3761e962012-11-28 12:20:19 -0800151 !strcmp(tag, "SMS")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800152 log_id = LOG_ID_RADIO;
Wink Saville3761e962012-11-28 12:20:19 -0800153 // Inform third party apps/ril/radio.. to use Rlog or RLOG
154 snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
155 tag = tmp_tag;
156 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800157
158 vec[0].iov_base = (unsigned char *) &prio;
159 vec[0].iov_len = 1;
160 vec[1].iov_base = (void *) tag;
161 vec[1].iov_len = strlen(tag) + 1;
162 vec[2].iov_base = (void *) msg;
163 vec[2].iov_len = strlen(msg) + 1;
164
165 return write_to_log(log_id, vec, 3);
166}
167
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800168int __android_log_buf_write(int bufID, int prio, const char *tag, const char *msg)
169{
170 struct iovec vec[3];
Wink Saville3761e962012-11-28 12:20:19 -0800171 char tmp_tag[32];
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800172
173 if (!tag)
174 tag = "";
175
176 /* XXX: This needs to go! */
Wink Saville3761e962012-11-28 12:20:19 -0800177 if ((bufID != LOG_ID_RADIO) &&
178 (!strcmp(tag, "HTC_RIL") ||
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800179 !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
Jeff Sharkey84dcf092012-08-13 11:27:30 -0700180 !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800181 !strcmp(tag, "AT") ||
182 !strcmp(tag, "GSM") ||
183 !strcmp(tag, "STK") ||
184 !strcmp(tag, "CDMA") ||
185 !strcmp(tag, "PHONE") ||
Wink Saville3761e962012-11-28 12:20:19 -0800186 !strcmp(tag, "SMS"))) {
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800187 bufID = LOG_ID_RADIO;
Wink Saville3761e962012-11-28 12:20:19 -0800188 // Inform third party apps/ril/radio.. to use Rlog or RLOG
189 snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
190 tag = tmp_tag;
191 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800192
193 vec[0].iov_base = (unsigned char *) &prio;
194 vec[0].iov_len = 1;
195 vec[1].iov_base = (void *) tag;
196 vec[1].iov_len = strlen(tag) + 1;
197 vec[2].iov_base = (void *) msg;
198 vec[2].iov_len = strlen(msg) + 1;
199
200 return write_to_log(bufID, vec, 3);
201}
202
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203int __android_log_vprint(int prio, const char *tag, const char *fmt, va_list ap)
204{
Chris Pearson19299902010-06-02 16:25:35 -0700205 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800206
207 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
208
209 return __android_log_write(prio, tag, buf);
210}
211
212int __android_log_print(int prio, const char *tag, const char *fmt, ...)
213{
214 va_list ap;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800215 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800216
217 va_start(ap, fmt);
218 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
219 va_end(ap);
220
221 return __android_log_write(prio, tag, buf);
222}
223
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800224int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...)
225{
226 va_list ap;
227 char buf[LOG_BUF_SIZE];
228
229 va_start(ap, fmt);
230 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
231 va_end(ap);
232
233 return __android_log_buf_write(bufID, prio, tag, buf);
234}
235
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800236void __android_log_assert(const char *cond, const char *tag,
237 const char *fmt, ...)
238{
Chris Pearson19299902010-06-02 16:25:35 -0700239 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800240
Chris Pearson19299902010-06-02 16:25:35 -0700241 if (fmt) {
242 va_list ap;
243 va_start(ap, fmt);
244 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
245 va_end(ap);
246 } else {
247 /* Msg not provided, log condition. N.B. Do not use cond directly as
248 * format string as it could contain spurious '%' syntax (e.g.
249 * "%d" in "blocks%devs == 0").
250 */
251 if (cond)
252 snprintf(buf, LOG_BUF_SIZE, "Assertion failed: %s", cond);
253 else
254 strcpy(buf, "Unspecified assertion failed");
255 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800256
257 __android_log_write(ANDROID_LOG_FATAL, tag, buf);
258
259 __builtin_trap(); /* trap so we have a chance to debug the situation */
260}
261
262int __android_log_bwrite(int32_t tag, const void *payload, size_t len)
263{
264 struct iovec vec[2];
265
266 vec[0].iov_base = &tag;
267 vec[0].iov_len = sizeof(tag);
268 vec[1].iov_base = (void*)payload;
269 vec[1].iov_len = len;
270
271 return write_to_log(LOG_ID_EVENTS, vec, 2);
272}
273
274/*
275 * Like __android_log_bwrite, but takes the type as well. Doesn't work
276 * for the general case where we're generating lists of stuff, but very
277 * handy if we just want to dump an integer into the log.
278 */
279int __android_log_btwrite(int32_t tag, char type, const void *payload,
280 size_t len)
281{
282 struct iovec vec[3];
283
284 vec[0].iov_base = &tag;
285 vec[0].iov_len = sizeof(tag);
286 vec[1].iov_base = &type;
287 vec[1].iov_len = sizeof(type);
288 vec[2].iov_base = (void*)payload;
289 vec[2].iov_len = len;
290
291 return write_to_log(LOG_ID_EVENTS, vec, 3);
292}