blob: 6b35a0f22287afe5b272c19bedd02e2e7daa3a9f [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
Mark Salyzyn73459a52014-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 */
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 Salyzyn73459a52014-01-02 13:52:29 -080034#define LOGGER_LOG_MAIN "log/main"
35#define LOGGER_LOG_RADIO "log/radio"
36#define LOGGER_LOG_EVENTS "log/events"
37#define LOGGER_LOG_SYSTEM "log/system"
38
Mark Salyzync1215c02013-11-22 07:54:30 -080039#define LOG_BUF_SIZE 1024
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040
41#if FAKE_LOG_DEVICE
42// This will be defined when building for the host.
Kristian Monsen52aa2462013-11-01 15:44:18 -070043#include "fake_log_device.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080044#define log_open(pathname, flags) fakeLogOpen(pathname, flags)
45#define log_writev(filedes, vector, count) fakeLogWritev(filedes, vector, count)
46#define log_close(filedes) fakeLogClose(filedes)
47#else
Nick Kralevicha1703222013-03-15 09:45:12 -070048#define log_open(pathname, flags) open(pathname, (flags) | O_CLOEXEC)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049#define log_writev(filedes, vector, count) writev(filedes, vector, count)
50#define log_close(filedes) close(filedes)
51#endif
52
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053static int __write_to_log_init(log_id_t, struct iovec *vec, size_t nr);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -080054static 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 -080055#ifdef HAVE_PTHREADS
56static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
57#endif
58
Kristian Monsen52aa2462013-11-01 15:44:18 -070059#define UNUSED __attribute__((__unused__))
60
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -080061static int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1, -1 };
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080062
63/*
64 * This is used by the C++ code to decide if it should write logs through
65 * the C code. Basically, if /dev/log/... is available, we're running in
66 * the simulator rather than a desktop tool and want to use the device.
67 */
68static enum {
Chris Pearson19299902010-06-02 16:25:35 -070069 kLogUninitialized, kLogNotAvailable, kLogAvailable
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070} g_log_status = kLogUninitialized;
71int __android_log_dev_available(void)
72{
73 if (g_log_status == kLogUninitialized) {
74 if (access("/dev/"LOGGER_LOG_MAIN, W_OK) == 0)
75 g_log_status = kLogAvailable;
76 else
77 g_log_status = kLogNotAvailable;
78 }
79
80 return (g_log_status == kLogAvailable);
81}
82
Kristian Monsen52aa2462013-11-01 15:44:18 -070083static int __write_to_log_null(UNUSED log_id_t log_fd, UNUSED struct iovec *vec,
84 UNUSED size_t nr)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080085{
86 return -1;
87}
88
89static int __write_to_log_kernel(log_id_t log_id, struct iovec *vec, size_t nr)
90{
91 ssize_t ret;
92 int log_fd;
93
94 if (/*(int)log_id >= 0 &&*/ (int)log_id < (int)LOG_ID_MAX) {
95 log_fd = log_fds[(int)log_id];
96 } else {
97 return EBADF;
98 }
99
100 do {
101 ret = log_writev(log_fd, vec, nr);
102 } while (ret < 0 && errno == EINTR);
103
104 return ret;
105}
106
107static int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr)
108{
109#ifdef HAVE_PTHREADS
110 pthread_mutex_lock(&log_init_lock);
111#endif
112
113 if (write_to_log == __write_to_log_init) {
114 log_fds[LOG_ID_MAIN] = log_open("/dev/"LOGGER_LOG_MAIN, O_WRONLY);
115 log_fds[LOG_ID_RADIO] = log_open("/dev/"LOGGER_LOG_RADIO, O_WRONLY);
116 log_fds[LOG_ID_EVENTS] = log_open("/dev/"LOGGER_LOG_EVENTS, O_WRONLY);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800117 log_fds[LOG_ID_SYSTEM] = log_open("/dev/"LOGGER_LOG_SYSTEM, O_WRONLY);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800118
119 write_to_log = __write_to_log_kernel;
120
121 if (log_fds[LOG_ID_MAIN] < 0 || log_fds[LOG_ID_RADIO] < 0 ||
122 log_fds[LOG_ID_EVENTS] < 0) {
123 log_close(log_fds[LOG_ID_MAIN]);
124 log_close(log_fds[LOG_ID_RADIO]);
125 log_close(log_fds[LOG_ID_EVENTS]);
126 log_fds[LOG_ID_MAIN] = -1;
127 log_fds[LOG_ID_RADIO] = -1;
128 log_fds[LOG_ID_EVENTS] = -1;
129 write_to_log = __write_to_log_null;
130 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800131
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800132 if (log_fds[LOG_ID_SYSTEM] < 0) {
133 log_fds[LOG_ID_SYSTEM] = log_fds[LOG_ID_MAIN];
134 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800135 }
136
137#ifdef HAVE_PTHREADS
138 pthread_mutex_unlock(&log_init_lock);
139#endif
140
141 return write_to_log(log_id, vec, nr);
142}
143
144int __android_log_write(int prio, const char *tag, const char *msg)
145{
146 struct iovec vec[3];
147 log_id_t log_id = LOG_ID_MAIN;
Wink Saville3761e962012-11-28 12:20:19 -0800148 char tmp_tag[32];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149
150 if (!tag)
151 tag = "";
152
153 /* XXX: This needs to go! */
154 if (!strcmp(tag, "HTC_RIL") ||
John Michelaued7ccae2009-08-19 10:01:55 -0500155 !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
Jeff Sharkey84dcf092012-08-13 11:27:30 -0700156 !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800157 !strcmp(tag, "AT") ||
158 !strcmp(tag, "GSM") ||
Wink Saville89efdc92009-04-02 11:00:57 -0700159 !strcmp(tag, "STK") ||
160 !strcmp(tag, "CDMA") ||
161 !strcmp(tag, "PHONE") ||
Wink Saville3761e962012-11-28 12:20:19 -0800162 !strcmp(tag, "SMS")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800163 log_id = LOG_ID_RADIO;
Wink Saville3761e962012-11-28 12:20:19 -0800164 // Inform third party apps/ril/radio.. to use Rlog or RLOG
165 snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
166 tag = tmp_tag;
167 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168
169 vec[0].iov_base = (unsigned char *) &prio;
170 vec[0].iov_len = 1;
171 vec[1].iov_base = (void *) tag;
172 vec[1].iov_len = strlen(tag) + 1;
173 vec[2].iov_base = (void *) msg;
174 vec[2].iov_len = strlen(msg) + 1;
175
176 return write_to_log(log_id, vec, 3);
177}
178
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800179int __android_log_buf_write(int bufID, int prio, const char *tag, const char *msg)
180{
181 struct iovec vec[3];
Wink Saville3761e962012-11-28 12:20:19 -0800182 char tmp_tag[32];
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800183
184 if (!tag)
185 tag = "";
186
187 /* XXX: This needs to go! */
Wink Saville3761e962012-11-28 12:20:19 -0800188 if ((bufID != LOG_ID_RADIO) &&
189 (!strcmp(tag, "HTC_RIL") ||
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800190 !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
Jeff Sharkey84dcf092012-08-13 11:27:30 -0700191 !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800192 !strcmp(tag, "AT") ||
193 !strcmp(tag, "GSM") ||
194 !strcmp(tag, "STK") ||
195 !strcmp(tag, "CDMA") ||
196 !strcmp(tag, "PHONE") ||
Wink Saville3761e962012-11-28 12:20:19 -0800197 !strcmp(tag, "SMS"))) {
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800198 bufID = LOG_ID_RADIO;
Wink Saville3761e962012-11-28 12:20:19 -0800199 // Inform third party apps/ril/radio.. to use Rlog or RLOG
200 snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
201 tag = tmp_tag;
202 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800203
204 vec[0].iov_base = (unsigned char *) &prio;
205 vec[0].iov_len = 1;
206 vec[1].iov_base = (void *) tag;
207 vec[1].iov_len = strlen(tag) + 1;
208 vec[2].iov_base = (void *) msg;
209 vec[2].iov_len = strlen(msg) + 1;
210
211 return write_to_log(bufID, vec, 3);
212}
213
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800214int __android_log_vprint(int prio, const char *tag, const char *fmt, va_list ap)
215{
Chris Pearson19299902010-06-02 16:25:35 -0700216 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800217
218 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
219
220 return __android_log_write(prio, tag, buf);
221}
222
223int __android_log_print(int prio, const char *tag, const char *fmt, ...)
224{
225 va_list ap;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800226 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800227
228 va_start(ap, fmt);
229 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
230 va_end(ap);
231
232 return __android_log_write(prio, tag, buf);
233}
234
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800235int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...)
236{
237 va_list ap;
238 char buf[LOG_BUF_SIZE];
239
240 va_start(ap, fmt);
241 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
242 va_end(ap);
243
244 return __android_log_buf_write(bufID, prio, tag, buf);
245}
246
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800247void __android_log_assert(const char *cond, const char *tag,
Mark Salyzync1215c02013-11-22 07:54:30 -0800248 const char *fmt, ...)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249{
Chris Pearson19299902010-06-02 16:25:35 -0700250 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800251
Chris Pearson19299902010-06-02 16:25:35 -0700252 if (fmt) {
253 va_list ap;
254 va_start(ap, fmt);
255 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
256 va_end(ap);
257 } else {
258 /* Msg not provided, log condition. N.B. Do not use cond directly as
259 * format string as it could contain spurious '%' syntax (e.g.
260 * "%d" in "blocks%devs == 0").
261 */
262 if (cond)
263 snprintf(buf, LOG_BUF_SIZE, "Assertion failed: %s", cond);
264 else
265 strcpy(buf, "Unspecified assertion failed");
266 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800267
268 __android_log_write(ANDROID_LOG_FATAL, tag, buf);
269
270 __builtin_trap(); /* trap so we have a chance to debug the situation */
271}
272
273int __android_log_bwrite(int32_t tag, const void *payload, size_t len)
274{
275 struct iovec vec[2];
276
277 vec[0].iov_base = &tag;
278 vec[0].iov_len = sizeof(tag);
279 vec[1].iov_base = (void*)payload;
280 vec[1].iov_len = len;
281
282 return write_to_log(LOG_ID_EVENTS, vec, 2);
283}
284
285/*
286 * Like __android_log_bwrite, but takes the type as well. Doesn't work
287 * for the general case where we're generating lists of stuff, but very
288 * handy if we just want to dump an integer into the log.
289 */
290int __android_log_btwrite(int32_t tag, char type, const void *payload,
291 size_t len)
292{
293 struct iovec vec[3];
294
295 vec[0].iov_base = &tag;
296 vec[0].iov_len = sizeof(tag);
297 vec[1].iov_base = &type;
298 vec[1].iov_len = sizeof(type);
299 vec[2].iov_base = (void*)payload;
300 vec[2].iov_len = len;
301
302 return write_to_log(LOG_ID_EVENTS, vec, 3);
303}