1 /*
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>
30
31 #define LOG_BUF_SIZE 1024
32
33 #if FAKE_LOG_DEVICE
34 // This will be defined when building for the host.
35 #define log_open(pathname, flags) fakeLogOpen(pathname, flags)
36 #define log_writev(filedes, vector, count) fakeLogWritev(filedes, vector, count)
37 #define log_close(filedes) fakeLogClose(filedes)
38 #else
39 #define log_open(pathname, flags) open(pathname, flags)
40 #define log_writev(filedes, vector, count) writev(filedes, vector, count)
41 #define log_close(filedes) close(filedes)
42 #endif
43
44 typedef enum {
45 LOG_ID_MAIN = 0,
46 LOG_ID_RADIO,
47 LOG_ID_EVENTS,
48 LOG_ID_MAX
49 } log_id_t;
50
51 static int __write_to_log_init(log_id_t, struct iovec *vec, size_t nr);
52 static int (*write_to_log)(log_id_t, struct iovec *vec, size_t nr) =
53 __write_to_log_init;
54 #ifdef HAVE_PTHREADS
55 static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
56 #endif
57
58 static int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1 };
59
60 /*
61 * This is used by the C++ code to decide if it should write logs through
62 * the C code. Basically, if /dev/log/... is available, we're running in
63 * the simulator rather than a desktop tool and want to use the device.
64 */
65 static enum {
66 kLogUninitialized, kLogNotAvailable, kLogAvailable
67 } g_log_status = kLogUninitialized;
__android_log_dev_available(void)68 int __android_log_dev_available(void)
69 {
70 if (g_log_status == kLogUninitialized) {
71 if (access("/dev/"LOGGER_LOG_MAIN, W_OK) == 0)
72 g_log_status = kLogAvailable;
73 else
74 g_log_status = kLogNotAvailable;
75 }
76
77 return (g_log_status == kLogAvailable);
78 }
79
__write_to_log_null(log_id_t log_fd,struct iovec * vec,size_t nr)80 static int __write_to_log_null(log_id_t log_fd, struct iovec *vec, size_t nr)
81 {
82 return -1;
83 }
84
__write_to_log_kernel(log_id_t log_id,struct iovec * vec,size_t nr)85 static int __write_to_log_kernel(log_id_t log_id, struct iovec *vec, size_t nr)
86 {
87 ssize_t ret;
88 int log_fd;
89
90 if (/*(int)log_id >= 0 &&*/ (int)log_id < (int)LOG_ID_MAX) {
91 log_fd = log_fds[(int)log_id];
92 } else {
93 return EBADF;
94 }
95
96 do {
97 ret = log_writev(log_fd, vec, nr);
98 } while (ret < 0 && errno == EINTR);
99
100 return ret;
101 }
102
__write_to_log_init(log_id_t log_id,struct iovec * vec,size_t nr)103 static int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr)
104 {
105 #ifdef HAVE_PTHREADS
106 pthread_mutex_lock(&log_init_lock);
107 #endif
108
109 if (write_to_log == __write_to_log_init) {
110 log_fds[LOG_ID_MAIN] = log_open("/dev/"LOGGER_LOG_MAIN, O_WRONLY);
111 log_fds[LOG_ID_RADIO] = log_open("/dev/"LOGGER_LOG_RADIO, O_WRONLY);
112 log_fds[LOG_ID_EVENTS] = log_open("/dev/"LOGGER_LOG_EVENTS, O_WRONLY);
113
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 }
126 }
127
128 #ifdef HAVE_PTHREADS
129 pthread_mutex_unlock(&log_init_lock);
130 #endif
131
132 return write_to_log(log_id, vec, nr);
133 }
134
__android_log_write(int prio,const char * tag,const char * msg)135 int __android_log_write(int prio, const char *tag, const char *msg)
136 {
137 struct iovec vec[3];
138 log_id_t log_id = LOG_ID_MAIN;
139
140 if (!tag)
141 tag = "";
142
143 /* XXX: This needs to go! */
144 if (!strcmp(tag, "HTC_RIL") ||
145 !strcmp(tag, "RILJ") ||
146 !strcmp(tag, "RILB") ||
147 !strcmp(tag, "RILC") ||
148 !strcmp(tag, "RILD") ||
149 !strcmp(tag, "RIL") ||
150 !strcmp(tag, "AT") ||
151 !strcmp(tag, "GSM") ||
152 !strcmp(tag, "STK") ||
153 !strcmp(tag, "CDMA") ||
154 !strcmp(tag, "PHONE") ||
155 !strcmp(tag, "SMS"))
156 log_id = LOG_ID_RADIO;
157
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
__android_log_vprint(int prio,const char * tag,const char * fmt,va_list ap)168 int __android_log_vprint(int prio, const char *tag, const char *fmt, va_list ap)
169 {
170 char buf[LOG_BUF_SIZE];
171
172 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
173
174 return __android_log_write(prio, tag, buf);
175 }
176
__android_log_print(int prio,const char * tag,const char * fmt,...)177 int __android_log_print(int prio, const char *tag, const char *fmt, ...)
178 {
179 va_list ap;
180 char buf[LOG_BUF_SIZE];
181
182 va_start(ap, fmt);
183 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
184 va_end(ap);
185
186 return __android_log_write(prio, tag, buf);
187 }
188
__android_log_assert(const char * cond,const char * tag,const char * fmt,...)189 void __android_log_assert(const char *cond, const char *tag,
190 const char *fmt, ...)
191 {
192 va_list ap;
193 char buf[LOG_BUF_SIZE];
194
195 va_start(ap, fmt);
196 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
197 va_end(ap);
198
199 __android_log_write(ANDROID_LOG_FATAL, tag, buf);
200
201 __builtin_trap(); /* trap so we have a chance to debug the situation */
202 }
203
__android_log_bwrite(int32_t tag,const void * payload,size_t len)204 int __android_log_bwrite(int32_t tag, const void *payload, size_t len)
205 {
206 struct iovec vec[2];
207
208 vec[0].iov_base = &tag;
209 vec[0].iov_len = sizeof(tag);
210 vec[1].iov_base = (void*)payload;
211 vec[1].iov_len = len;
212
213 return write_to_log(LOG_ID_EVENTS, vec, 2);
214 }
215
216 /*
217 * Like __android_log_bwrite, but takes the type as well. Doesn't work
218 * for the general case where we're generating lists of stuff, but very
219 * handy if we just want to dump an integer into the log.
220 */
__android_log_btwrite(int32_t tag,char type,const void * payload,size_t len)221 int __android_log_btwrite(int32_t tag, char type, const void *payload,
222 size_t len)
223 {
224 struct iovec vec[3];
225
226 vec[0].iov_base = &tag;
227 vec[0].iov_len = sizeof(tag);
228 vec[1].iov_base = &type;
229 vec[1].iov_len = sizeof(type);
230 vec[2].iov_base = (void*)payload;
231 vec[2].iov_len = len;
232
233 return write_to_log(LOG_ID_EVENTS, vec, 3);
234 }
235