2
* Copyright (C) 2007 The Android Open Source Project
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
28
#include <cutils/logger.h>
29
#include <cutils/logd.h>
30
#include <cutils/log.h>
32
#define LOG_BUF_SIZE 1024
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)
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)
45
static int __write_to_log_init(log_id_t, struct iovec *vec, size_t nr);
46
static int (*write_to_log)(log_id_t, struct iovec *vec, size_t nr) = __write_to_log_init;
48
static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
51
static int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1, -1 };
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.
59
kLogUninitialized, kLogNotAvailable, kLogAvailable
60
} g_log_status = kLogUninitialized;
61
int __android_log_dev_available(void)
63
if (g_log_status == kLogUninitialized) {
64
if (access("/dev/"LOGGER_LOG_MAIN, W_OK) == 0)
65
g_log_status = kLogAvailable;
67
g_log_status = kLogNotAvailable;
70
return (g_log_status == kLogAvailable);
73
static int __write_to_log_null(log_id_t log_fd, struct iovec *vec, size_t nr)
78
static int __write_to_log_kernel(log_id_t log_id, struct iovec *vec, size_t nr)
83
if (/*(int)log_id >= 0 &&*/ (int)log_id < (int)LOG_ID_MAX) {
84
log_fd = log_fds[(int)log_id];
90
ret = log_writev(log_fd, vec, nr);
91
} while (ret < 0 && errno == EINTR);
96
static int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr)
99
pthread_mutex_lock(&log_init_lock);
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);
106
log_fds[LOG_ID_SYSTEM] = log_open("/dev/"LOGGER_LOG_SYSTEM, O_WRONLY);
108
write_to_log = __write_to_log_kernel;
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;
121
if (log_fds[LOG_ID_SYSTEM] < 0) {
122
log_fds[LOG_ID_SYSTEM] = log_fds[LOG_ID_MAIN];
127
pthread_mutex_unlock(&log_init_lock);
130
return write_to_log(log_id, vec, nr);
133
int __android_log_write(int prio, const char *tag, const char *msg)
136
log_id_t log_id = LOG_ID_MAIN;
141
/* XXX: This needs to go! */
142
if (!strcmp(tag, "HTC_RIL") ||
143
!strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
144
!strcmp(tag, "AT") ||
145
!strcmp(tag, "GSM") ||
146
!strcmp(tag, "STK") ||
147
!strcmp(tag, "CDMA") ||
148
!strcmp(tag, "PHONE") ||
150
log_id = LOG_ID_RADIO;
152
vec[0].iov_base = (unsigned char *) &prio;
154
vec[1].iov_base = (void *) tag;
155
vec[1].iov_len = strlen(tag) + 1;
156
vec[2].iov_base = (void *) msg;
157
vec[2].iov_len = strlen(msg) + 1;
159
return write_to_log(log_id, vec, 3);
162
int __android_log_buf_write(int bufID, int prio, const char *tag, const char *msg)
169
/* XXX: This needs to go! */
170
if (!strcmp(tag, "HTC_RIL") ||
171
!strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
172
!strcmp(tag, "AT") ||
173
!strcmp(tag, "GSM") ||
174
!strcmp(tag, "STK") ||
175
!strcmp(tag, "CDMA") ||
176
!strcmp(tag, "PHONE") ||
178
bufID = LOG_ID_RADIO;
180
vec[0].iov_base = (unsigned char *) &prio;
182
vec[1].iov_base = (void *) tag;
183
vec[1].iov_len = strlen(tag) + 1;
184
vec[2].iov_base = (void *) msg;
185
vec[2].iov_len = strlen(msg) + 1;
187
return write_to_log(bufID, vec, 3);
190
int __android_log_vprint(int prio, const char *tag, const char *fmt, va_list ap)
192
char buf[LOG_BUF_SIZE];
194
vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
196
return __android_log_write(prio, tag, buf);
199
int __android_log_print(int prio, const char *tag, const char *fmt, ...)
202
char buf[LOG_BUF_SIZE];
205
vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
208
return __android_log_write(prio, tag, buf);
211
int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...)
214
char buf[LOG_BUF_SIZE];
217
vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
220
return __android_log_buf_write(bufID, prio, tag, buf);
223
void __android_log_assert(const char *cond, const char *tag,
224
const char *fmt, ...)
226
char buf[LOG_BUF_SIZE];
231
vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
234
/* Msg not provided, log condition. N.B. Do not use cond directly as
235
* format string as it could contain spurious '%' syntax (e.g.
236
* "%d" in "blocks%devs == 0").
239
snprintf(buf, LOG_BUF_SIZE, "Assertion failed: %s", cond);
241
strcpy(buf, "Unspecified assertion failed");
244
__android_log_write(ANDROID_LOG_FATAL, tag, buf);
246
__builtin_trap(); /* trap so we have a chance to debug the situation */
249
int __android_log_bwrite(int32_t tag, const void *payload, size_t len)
253
vec[0].iov_base = &tag;
254
vec[0].iov_len = sizeof(tag);
255
vec[1].iov_base = (void*)payload;
256
vec[1].iov_len = len;
258
return write_to_log(LOG_ID_EVENTS, vec, 2);
262
* Like __android_log_bwrite, but takes the type as well. Doesn't work
263
* for the general case where we're generating lists of stuff, but very
264
* handy if we just want to dump an integer into the log.
266
int __android_log_btwrite(int32_t tag, char type, const void *payload,
271
vec[0].iov_base = &tag;
272
vec[0].iov_len = sizeof(tag);
273
vec[1].iov_base = &type;
274
vec[1].iov_len = sizeof(type);
275
vec[2].iov_base = (void*)payload;
276
vec[2].iov_len = len;
278
return write_to_log(LOG_ID_EVENTS, vec, 3);