blob: 6ae339fdc1be8be13d0525bddf990996c507a1a2 [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>
Nick Kralevicha1703222013-03-15 09:45:12 -070027#include <sys/types.h>
28#include <sys/stat.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080029
Colin Cross9227bd32013-07-23 16:59:20 -070030#include <log/logger.h>
31#include <log/logd.h>
32#include <log/log.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033
Mark Salyzync1215c02013-11-22 07:54:30 -080034#define LOG_BUF_SIZE 1024
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035
36#if FAKE_LOG_DEVICE
37// This will be defined when building for the host.
Kristian Monsen52aa2462013-11-01 15:44:18 -070038#include "fake_log_device.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080039#define log_open(pathname, flags) fakeLogOpen(pathname, flags)
40#define log_writev(filedes, vector, count) fakeLogWritev(filedes, vector, count)
41#define log_close(filedes) fakeLogClose(filedes)
42#else
Nick Kralevicha1703222013-03-15 09:45:12 -070043#define log_open(pathname, flags) open(pathname, (flags) | O_CLOEXEC)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080044#define log_writev(filedes, vector, count) writev(filedes, vector, count)
45#define log_close(filedes) close(filedes)
46#endif
47
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048static int __write_to_log_init(log_id_t, struct iovec *vec, size_t nr);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -080049static 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 -080050#ifdef HAVE_PTHREADS
51static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
52#endif
53
Kristian Monsen52aa2462013-11-01 15:44:18 -070054#define UNUSED __attribute__((__unused__))
55
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -080056static int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1, -1 };
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057
58/*
59 * This is used by the C++ code to decide if it should write logs through
60 * the C code. Basically, if /dev/log/... is available, we're running in
61 * the simulator rather than a desktop tool and want to use the device.
62 */
63static enum {
Chris Pearson19299902010-06-02 16:25:35 -070064 kLogUninitialized, kLogNotAvailable, kLogAvailable
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080065} g_log_status = kLogUninitialized;
66int __android_log_dev_available(void)
67{
68 if (g_log_status == kLogUninitialized) {
69 if (access("/dev/"LOGGER_LOG_MAIN, W_OK) == 0)
70 g_log_status = kLogAvailable;
71 else
72 g_log_status = kLogNotAvailable;
73 }
74
75 return (g_log_status == kLogAvailable);
76}
77
Kristian Monsen52aa2462013-11-01 15:44:18 -070078static int __write_to_log_null(UNUSED log_id_t log_fd, UNUSED struct iovec *vec,
79 UNUSED size_t nr)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080{
81 return -1;
82}
83
84static int __write_to_log_kernel(log_id_t log_id, struct iovec *vec, size_t nr)
85{
86 ssize_t ret;
87 int log_fd;
88
89 if (/*(int)log_id >= 0 &&*/ (int)log_id < (int)LOG_ID_MAX) {
90 log_fd = log_fds[(int)log_id];
91 } else {
92 return EBADF;
93 }
94
95 do {
96 ret = log_writev(log_fd, vec, nr);
97 } while (ret < 0 && errno == EINTR);
98
99 return ret;
100}
101
102static int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr)
103{
104#ifdef HAVE_PTHREADS
105 pthread_mutex_lock(&log_init_lock);
106#endif
107
108 if (write_to_log == __write_to_log_init) {
109 log_fds[LOG_ID_MAIN] = log_open("/dev/"LOGGER_LOG_MAIN, O_WRONLY);
110 log_fds[LOG_ID_RADIO] = log_open("/dev/"LOGGER_LOG_RADIO, O_WRONLY);
111 log_fds[LOG_ID_EVENTS] = log_open("/dev/"LOGGER_LOG_EVENTS, O_WRONLY);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800112 log_fds[LOG_ID_SYSTEM] = log_open("/dev/"LOGGER_LOG_SYSTEM, O_WRONLY);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800113
114 write_to_log = __write_to_log_kernel;
115
116 if (log_fds[LOG_ID_MAIN] < 0 || log_fds[LOG_ID_RADIO] < 0 ||
117 log_fds[LOG_ID_EVENTS] < 0) {
118 log_close(log_fds[LOG_ID_MAIN]);
119 log_close(log_fds[LOG_ID_RADIO]);
120 log_close(log_fds[LOG_ID_EVENTS]);
121 log_fds[LOG_ID_MAIN] = -1;
122 log_fds[LOG_ID_RADIO] = -1;
123 log_fds[LOG_ID_EVENTS] = -1;
124 write_to_log = __write_to_log_null;
125 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800126
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800127 if (log_fds[LOG_ID_SYSTEM] < 0) {
128 log_fds[LOG_ID_SYSTEM] = log_fds[LOG_ID_MAIN];
129 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130 }
131
132#ifdef HAVE_PTHREADS
133 pthread_mutex_unlock(&log_init_lock);
134#endif
135
136 return write_to_log(log_id, vec, nr);
137}
138
139int __android_log_write(int prio, const char *tag, const char *msg)
140{
141 struct iovec vec[3];
142 log_id_t log_id = LOG_ID_MAIN;
Wink Saville3761e962012-11-28 12:20:19 -0800143 char tmp_tag[32];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800144
145 if (!tag)
146 tag = "";
147
148 /* XXX: This needs to go! */
149 if (!strcmp(tag, "HTC_RIL") ||
John Michelaued7ccae2009-08-19 10:01:55 -0500150 !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
Jeff Sharkey84dcf092012-08-13 11:27:30 -0700151 !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800152 !strcmp(tag, "AT") ||
153 !strcmp(tag, "GSM") ||
Wink Saville89efdc92009-04-02 11:00:57 -0700154 !strcmp(tag, "STK") ||
155 !strcmp(tag, "CDMA") ||
156 !strcmp(tag, "PHONE") ||
Wink Saville3761e962012-11-28 12:20:19 -0800157 !strcmp(tag, "SMS")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800158 log_id = LOG_ID_RADIO;
Wink Saville3761e962012-11-28 12:20:19 -0800159 // Inform third party apps/ril/radio.. to use Rlog or RLOG
160 snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
161 tag = tmp_tag;
162 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800163
164 vec[0].iov_base = (unsigned char *) &prio;
165 vec[0].iov_len = 1;
166 vec[1].iov_base = (void *) tag;
167 vec[1].iov_len = strlen(tag) + 1;
168 vec[2].iov_base = (void *) msg;
169 vec[2].iov_len = strlen(msg) + 1;
170
171 return write_to_log(log_id, vec, 3);
172}
173
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800174int __android_log_buf_write(int bufID, int prio, const char *tag, const char *msg)
175{
176 struct iovec vec[3];
Wink Saville3761e962012-11-28 12:20:19 -0800177 char tmp_tag[32];
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800178
179 if (!tag)
180 tag = "";
181
182 /* XXX: This needs to go! */
Wink Saville3761e962012-11-28 12:20:19 -0800183 if ((bufID != LOG_ID_RADIO) &&
184 (!strcmp(tag, "HTC_RIL") ||
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800185 !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
Jeff Sharkey84dcf092012-08-13 11:27:30 -0700186 !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800187 !strcmp(tag, "AT") ||
188 !strcmp(tag, "GSM") ||
189 !strcmp(tag, "STK") ||
190 !strcmp(tag, "CDMA") ||
191 !strcmp(tag, "PHONE") ||
Wink Saville3761e962012-11-28 12:20:19 -0800192 !strcmp(tag, "SMS"))) {
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800193 bufID = LOG_ID_RADIO;
Wink Saville3761e962012-11-28 12:20:19 -0800194 // Inform third party apps/ril/radio.. to use Rlog or RLOG
195 snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
196 tag = tmp_tag;
197 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800198
199 vec[0].iov_base = (unsigned char *) &prio;
200 vec[0].iov_len = 1;
201 vec[1].iov_base = (void *) tag;
202 vec[1].iov_len = strlen(tag) + 1;
203 vec[2].iov_base = (void *) msg;
204 vec[2].iov_len = strlen(msg) + 1;
205
206 return write_to_log(bufID, vec, 3);
207}
208
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209int __android_log_vprint(int prio, const char *tag, const char *fmt, va_list ap)
210{
Chris Pearson19299902010-06-02 16:25:35 -0700211 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800212
213 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
214
215 return __android_log_write(prio, tag, buf);
216}
217
218int __android_log_print(int prio, const char *tag, const char *fmt, ...)
219{
220 va_list ap;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800221 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222
223 va_start(ap, fmt);
224 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
225 va_end(ap);
226
227 return __android_log_write(prio, tag, buf);
228}
229
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800230int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...)
231{
232 va_list ap;
233 char buf[LOG_BUF_SIZE];
234
235 va_start(ap, fmt);
236 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
237 va_end(ap);
238
239 return __android_log_buf_write(bufID, prio, tag, buf);
240}
241
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242void __android_log_assert(const char *cond, const char *tag,
Mark Salyzync1215c02013-11-22 07:54:30 -0800243 const char *fmt, ...)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800244{
Chris Pearson19299902010-06-02 16:25:35 -0700245 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800246
Chris Pearson19299902010-06-02 16:25:35 -0700247 if (fmt) {
248 va_list ap;
249 va_start(ap, fmt);
250 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
251 va_end(ap);
252 } else {
253 /* Msg not provided, log condition. N.B. Do not use cond directly as
254 * format string as it could contain spurious '%' syntax (e.g.
255 * "%d" in "blocks%devs == 0").
256 */
257 if (cond)
258 snprintf(buf, LOG_BUF_SIZE, "Assertion failed: %s", cond);
259 else
260 strcpy(buf, "Unspecified assertion failed");
261 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800262
263 __android_log_write(ANDROID_LOG_FATAL, tag, buf);
264
265 __builtin_trap(); /* trap so we have a chance to debug the situation */
266}
267
268int __android_log_bwrite(int32_t tag, const void *payload, size_t len)
269{
270 struct iovec vec[2];
271
272 vec[0].iov_base = &tag;
273 vec[0].iov_len = sizeof(tag);
274 vec[1].iov_base = (void*)payload;
275 vec[1].iov_len = len;
276
277 return write_to_log(LOG_ID_EVENTS, vec, 2);
278}
279
280/*
281 * Like __android_log_bwrite, but takes the type as well. Doesn't work
282 * for the general case where we're generating lists of stuff, but very
283 * handy if we just want to dump an integer into the log.
284 */
285int __android_log_btwrite(int32_t tag, char type, const void *payload,
286 size_t len)
287{
288 struct iovec vec[3];
289
290 vec[0].iov_base = &tag;
291 vec[0].iov_len = sizeof(tag);
292 vec[1].iov_base = &type;
293 vec[1].iov_len = sizeof(type);
294 vec[2].iov_base = (void*)payload;
295 vec[2].iov_len = len;
296
297 return write_to_log(LOG_ID_EVENTS, vec, 3);
298}