The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 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 |
| 13 | * distribution. |
| 14 | * |
| 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 |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | #include <time.h> |
| 29 | #include <stdio.h> |
| 30 | #include <pthread.h> |
| 31 | #include <unistd.h> |
| 32 | #include <sys/types.h> |
| 33 | #include <sys/uio.h> |
| 34 | #include <arpa/inet.h> |
| 35 | #include <errno.h> |
| 36 | #include <string.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <stdarg.h> |
| 39 | #include <fcntl.h> |
| 40 | |
| 41 | #include <cutils/logger.h> |
| 42 | #include "logd.h" |
| 43 | |
| 44 | #include <pthread.h> |
| 45 | |
| 46 | #define LOG_BUF_SIZE 1024 |
| 47 | |
| 48 | typedef enum { |
Alexey Tarasov | cc05d12 | 2008-12-13 08:24:44 +1000 | [diff] [blame] | 49 | LOG_ID_NONE = 0, |
| 50 | LOG_ID_MAIN, |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 51 | LOG_ID_RADIO, |
| 52 | LOG_ID_MAX |
| 53 | } log_id_t; |
| 54 | |
Alexey Tarasov | cc05d12 | 2008-12-13 08:24:44 +1000 | [diff] [blame] | 55 | /* logger handles writing to object, pointed by log channel id */ |
| 56 | typedef int (*logger_function_t)(log_id_t log_id, struct iovec *vec); |
| 57 | |
| 58 | typedef struct { |
| 59 | logger_function_t logger; |
| 60 | int fd; |
| 61 | const char *path; |
| 62 | } log_channel_t; |
| 63 | |
| 64 | static int __write_to_log_init(log_id_t log_id, struct iovec *vec); |
| 65 | static int __write_to_log_null(log_id_t log_id, struct iovec *vec); |
| 66 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 67 | static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER; |
| 68 | |
Andy McFadden | 5cdb2b7 | 2009-11-30 17:09:45 -0800 | [diff] [blame^] | 69 | static log_channel_t log_channels[LOG_ID_MAX] = { |
Alexey Tarasov | cc05d12 | 2008-12-13 08:24:44 +1000 | [diff] [blame] | 70 | { __write_to_log_null, -1, NULL }, |
| 71 | { __write_to_log_init, -1, "/dev/"LOGGER_LOG_MAIN }, |
| 72 | { __write_to_log_init, -1, "/dev/"LOGGER_LOG_RADIO } |
| 73 | }; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 74 | |
Alexey Tarasov | cc05d12 | 2008-12-13 08:24:44 +1000 | [diff] [blame] | 75 | static int __write_to_log_null(log_id_t log_id, struct iovec *vec) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 76 | { |
Alexey Tarasov | cc05d12 | 2008-12-13 08:24:44 +1000 | [diff] [blame] | 77 | /* |
| 78 | * ALTERED behaviour from previous version |
| 79 | * always returns successful result |
| 80 | */ |
| 81 | int i = 0; |
| 82 | size_t res = 0; |
| 83 | |
| 84 | for ( ; i < 3; ++i) { |
| 85 | res += vec[i].iov_len; |
| 86 | } |
| 87 | |
| 88 | return (int)res; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Alexey Tarasov | cc05d12 | 2008-12-13 08:24:44 +1000 | [diff] [blame] | 91 | /* |
| 92 | * it's supposed, that log_id contains valid id always. |
| 93 | * this check must be performed in higher level functions |
| 94 | */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 95 | static int __write_to_log_kernel(log_id_t log_id, struct iovec *vec) |
| 96 | { |
| 97 | ssize_t ret; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 98 | |
| 99 | do { |
Alexey Tarasov | cc05d12 | 2008-12-13 08:24:44 +1000 | [diff] [blame] | 100 | ret = writev(log_channels[log_id].fd, vec, 3); |
| 101 | } while ((ret < 0) && (errno == EINTR)); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 102 | |
| 103 | return ret; |
| 104 | } |
| 105 | |
| 106 | static int __write_to_log_init(log_id_t log_id, struct iovec *vec) |
| 107 | { |
Alexey Tarasov | cc05d12 | 2008-12-13 08:24:44 +1000 | [diff] [blame] | 108 | if ((LOG_ID_NONE < log_id) && (log_id < LOG_ID_MAX)) { |
| 109 | pthread_mutex_lock(&log_init_lock); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 110 | |
Alexey Tarasov | cc05d12 | 2008-12-13 08:24:44 +1000 | [diff] [blame] | 111 | int fd = open(log_channels[log_id].path, O_WRONLY); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 112 | |
Alexey Tarasov | cc05d12 | 2008-12-13 08:24:44 +1000 | [diff] [blame] | 113 | log_channels[log_id].logger = |
| 114 | (fd < 0) ? __write_to_log_null : __write_to_log_kernel; |
Andy McFadden | 5cdb2b7 | 2009-11-30 17:09:45 -0800 | [diff] [blame^] | 115 | log_channels[log_id].fd = fd; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 116 | |
Alexey Tarasov | cc05d12 | 2008-12-13 08:24:44 +1000 | [diff] [blame] | 117 | pthread_mutex_unlock(&log_init_lock); |
| 118 | |
| 119 | return log_channels[log_id].logger(log_id, vec); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Alexey Tarasov | cc05d12 | 2008-12-13 08:24:44 +1000 | [diff] [blame] | 122 | /* log_id is invalid */ |
| 123 | return -1; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static int __android_log_write(int prio, const char *tag, const char *msg) |
| 127 | { |
| 128 | struct iovec vec[3]; |
| 129 | log_id_t log_id = LOG_ID_MAIN; |
| 130 | |
Alexey Tarasov | cc05d12 | 2008-12-13 08:24:44 +1000 | [diff] [blame] | 131 | if (tag == NULL) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 132 | tag = ""; |
| 133 | |
| 134 | if (!strcmp(tag, "HTC_RIL")) |
| 135 | log_id = LOG_ID_RADIO; |
| 136 | |
| 137 | vec[0].iov_base = (unsigned char *) &prio; |
| 138 | vec[0].iov_len = 1; |
| 139 | vec[1].iov_base = (void *) tag; |
| 140 | vec[1].iov_len = strlen(tag) + 1; |
| 141 | vec[2].iov_base = (void *) msg; |
| 142 | vec[2].iov_len = strlen(msg) + 1; |
| 143 | |
Alexey Tarasov | cc05d12 | 2008-12-13 08:24:44 +1000 | [diff] [blame] | 144 | return log_channels[log_id].logger(log_id, vec); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | |
David 'Digit' Turner | c4eee37 | 2009-07-08 14:22:41 +0200 | [diff] [blame] | 148 | int __libc_android_log_vprint(int prio, const char *tag, const char *fmt, |
| 149 | va_list ap) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 150 | { |
David 'Digit' Turner | c4eee37 | 2009-07-08 14:22:41 +0200 | [diff] [blame] | 151 | char buf[LOG_BUF_SIZE]; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 152 | |
| 153 | vsnprintf(buf, LOG_BUF_SIZE, fmt, ap); |
| 154 | |
| 155 | return __android_log_write(prio, tag, buf); |
| 156 | } |
| 157 | |
| 158 | int __libc_android_log_print(int prio, const char *tag, const char *fmt, ...) |
| 159 | { |
| 160 | va_list ap; |
David 'Digit' Turner | c4eee37 | 2009-07-08 14:22:41 +0200 | [diff] [blame] | 161 | char buf[LOG_BUF_SIZE]; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 162 | |
| 163 | va_start(ap, fmt); |
| 164 | vsnprintf(buf, LOG_BUF_SIZE, fmt, ap); |
| 165 | va_end(ap); |
| 166 | |
| 167 | return __android_log_write(prio, tag, buf); |
| 168 | } |
| 169 | |
| 170 | int __libc_android_log_assert(const char *cond, const char *tag, |
| 171 | const char *fmt, ...) |
| 172 | { |
| 173 | va_list ap; |
| 174 | char buf[LOG_BUF_SIZE]; |
| 175 | |
| 176 | va_start(ap, fmt); |
| 177 | vsnprintf(buf, LOG_BUF_SIZE, fmt, ap); |
| 178 | va_end(ap); |
| 179 | |
| 180 | __android_log_write(ANDROID_LOG_FATAL, tag, buf); |
| 181 | |
| 182 | exit(1); |
| 183 | |
| 184 | return -1; |
| 185 | } |