2
* Copyright (C) 2008 The Android Open Source Project
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
8
* * Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
10
* * Redistributions in binary form must reproduce the above copyright
11
* notice, this list of conditions and the following disclaimer in
12
* the documentation and/or other materials provided with the
15
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32
#include <sys/types.h>
34
#include <arpa/inet.h>
43
/* should match system/core/include/cutils/logger.h */
44
#define LOGGER_LOG_MAIN "log/main"
45
#define LOGGER_LOG_RADIO "log/radio"
46
#define LOGGER_LOG_EVENTS "log/events"
47
#define LOGGER_LOG_SYSTEM "log/system"
51
/* IMPORTANT IMPORTANT IMPORTANT: TECHNICAL NOTE
53
* Some of the functions below can be called when our malloc() implementation
54
* has detected that the heap is corrupted, or even from a signal handler.
56
* These functions should *not* use a function that allocates heap memory
57
* or is not signal-safe. Using direct system calls is acceptable, and we
58
* also assume that pthread_mutex_lock/unlock can be used too.
61
#define LOG_BUF_SIZE 1024
70
/* logger handles writing to object, pointed by log channel id */
71
typedef int (*logger_function_t)(log_id_t log_id, struct iovec *vec);
74
logger_function_t logger;
79
static int __write_to_log_init(log_id_t log_id, struct iovec *vec);
80
static int __write_to_log_null(log_id_t log_id, struct iovec *vec);
82
static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
84
static log_channel_t log_channels[LOG_ID_MAX] = {
85
{ __write_to_log_null, -1, NULL },
86
{ __write_to_log_init, -1, "/dev/"LOGGER_LOG_MAIN },
87
{ __write_to_log_init, -1, "/dev/"LOGGER_LOG_RADIO }
90
/* Important: see technical note at start of source file */
91
static int __write_to_log_null(log_id_t log_id, struct iovec *vec)
94
* ALTERED behaviour from previous version
95
* always returns successful result
100
for ( ; i < 3; ++i) {
101
res += vec[i].iov_len;
108
* it's supposed, that log_id contains valid id always.
109
* this check must be performed in higher level functions
111
/* Important: see technical note at start of source file */
112
static int __write_to_log_kernel(log_id_t log_id, struct iovec *vec)
114
return TEMP_FAILURE_RETRY( writev(log_channels[log_id].fd, vec, 3) );
117
/* Important: see technical note at start of source file */
118
static int __write_to_log_init(log_id_t log_id, struct iovec *vec)
120
if ((LOG_ID_NONE < log_id) && (log_id < LOG_ID_MAX)) {
123
pthread_mutex_lock(&log_init_lock);
125
fd = TEMP_FAILURE_RETRY(open(log_channels[log_id].path, O_WRONLY));
127
log_channels[log_id].logger =
128
(fd < 0) ? __write_to_log_null : __write_to_log_kernel;
129
log_channels[log_id].fd = fd;
131
log_channels[log_id].fd = fd;
133
pthread_mutex_unlock(&log_init_lock);
135
return log_channels[log_id].logger(log_id, vec);
138
/* log_id is invalid */
142
/* Important: see technical note at start of source file */
144
int __libc_android_log_write(int prio, const char *tag, const char *msg)
147
log_id_t log_id = LOG_ID_MAIN;
152
if (!strcmp(tag, "HTC_RIL"))
153
log_id = LOG_ID_RADIO;
155
vec[0].iov_base = (unsigned char *) &prio;
157
vec[1].iov_base = (void *) tag;
158
vec[1].iov_len = strlen(tag) + 1;
159
vec[2].iov_base = (void *) msg;
160
vec[2].iov_len = strlen(msg) + 1;
162
return log_channels[log_id].logger(log_id, vec);
165
/* The functions below are not designed to be called from a heap panic
166
* function or from a signal handler. As such, they are free to use complex
167
* C library functions like vsnprintf()
170
int __libc_android_log_vprint(int prio, const char *tag, const char *fmt,
173
char buf[LOG_BUF_SIZE];
175
vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
177
return __libc_android_log_write(prio, tag, buf);
181
int __libc_android_log_print(int prio, const char *tag, const char *fmt, ...)
184
char buf[LOG_BUF_SIZE];
187
vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
190
return __libc_android_log_write(prio, tag, buf);
194
int __libc_android_log_assert(const char *cond, const char *tag,
195
const char *fmt, ...)
198
char buf[LOG_BUF_SIZE];
201
vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
204
__libc_android_log_write(ANDROID_LOG_FATAL, tag, buf);