Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007-2014 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 | |
| 17 | #include <errno.h> |
| 18 | #include <fcntl.h> |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 19 | #include <pthread.h> |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 20 | #include <stdarg.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <time.h> |
| 27 | #include <unistd.h> |
| 28 | |
Dan Albert | 4047075 | 2014-08-18 17:29:34 -0700 | [diff] [blame] | 29 | #include <android/set_abort_message.h> |
Dan Albert | 4047075 | 2014-08-18 17:29:34 -0700 | [diff] [blame] | 30 | |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 31 | #include <log/log.h> |
| 32 | #include <log/logd.h> |
| 33 | #include <log/logger.h> |
| 34 | |
| 35 | #define LOGGER_LOG_MAIN "log/main" |
| 36 | #define LOGGER_LOG_RADIO "log/radio" |
| 37 | #define LOGGER_LOG_EVENTS "log/events" |
| 38 | #define LOGGER_LOG_SYSTEM "log/system" |
| 39 | |
| 40 | #define LOG_BUF_SIZE 1024 |
| 41 | |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 42 | #define log_open(pathname, flags) open(pathname, (flags) | O_CLOEXEC) |
| 43 | #define log_writev(filedes, vector, count) writev(filedes, vector, count) |
| 44 | #define log_close(filedes) close(filedes) |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 45 | |
| 46 | static int __write_to_log_init(log_id_t, struct iovec *vec, size_t nr); |
| 47 | static int (*write_to_log)(log_id_t, struct iovec *vec, size_t nr) = __write_to_log_init; |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 48 | |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 49 | static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER; |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 50 | |
Mark Salyzyn | a04464a | 2014-04-30 08:50:53 -0700 | [diff] [blame] | 51 | #ifndef __unused |
| 52 | #define __unused __attribute__((__unused__)) |
| 53 | #endif |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 54 | |
| 55 | static int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1, -1 }; |
| 56 | |
| 57 | /* |
| 58 | * This is used by the C++ code to decide if it should write logs through |
| 59 | * the C code. Basically, if /dev/log/... is available, we're running in |
| 60 | * the simulator rather than a desktop tool and want to use the device. |
| 61 | */ |
| 62 | static enum { |
| 63 | kLogUninitialized, kLogNotAvailable, kLogAvailable |
| 64 | } g_log_status = kLogUninitialized; |
| 65 | int __android_log_dev_available(void) |
| 66 | { |
| 67 | if (g_log_status == kLogUninitialized) { |
| 68 | if (access("/dev/"LOGGER_LOG_MAIN, W_OK) == 0) |
| 69 | g_log_status = kLogAvailable; |
| 70 | else |
| 71 | g_log_status = kLogNotAvailable; |
| 72 | } |
| 73 | |
| 74 | return (g_log_status == kLogAvailable); |
| 75 | } |
| 76 | |
Mark Salyzyn | a04464a | 2014-04-30 08:50:53 -0700 | [diff] [blame] | 77 | static int __write_to_log_null(log_id_t log_fd __unused, struct iovec *vec __unused, |
| 78 | size_t nr __unused) |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 79 | { |
| 80 | return -1; |
| 81 | } |
| 82 | |
| 83 | static int __write_to_log_kernel(log_id_t log_id, struct iovec *vec, size_t nr) |
| 84 | { |
| 85 | ssize_t ret; |
| 86 | int log_fd; |
| 87 | |
| 88 | if (/*(int)log_id >= 0 &&*/ (int)log_id < (int)LOG_ID_MAX) { |
Mark Salyzyn | 99f47a9 | 2014-04-07 14:58:08 -0700 | [diff] [blame] | 89 | if (log_id == LOG_ID_CRASH) { |
| 90 | log_id = LOG_ID_MAIN; |
| 91 | } |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 92 | log_fd = log_fds[(int)log_id]; |
| 93 | } else { |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 94 | return -EBADF; |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | do { |
| 98 | ret = log_writev(log_fd, vec, nr); |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 99 | if (ret < 0) { |
| 100 | ret = -errno; |
| 101 | } |
| 102 | } while (ret == -EINTR); |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 103 | |
| 104 | return ret; |
| 105 | } |
| 106 | |
| 107 | static int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr) |
| 108 | { |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 109 | pthread_mutex_lock(&log_init_lock); |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 110 | |
| 111 | if (write_to_log == __write_to_log_init) { |
| 112 | log_fds[LOG_ID_MAIN] = log_open("/dev/"LOGGER_LOG_MAIN, O_WRONLY); |
| 113 | log_fds[LOG_ID_RADIO] = log_open("/dev/"LOGGER_LOG_RADIO, O_WRONLY); |
| 114 | log_fds[LOG_ID_EVENTS] = log_open("/dev/"LOGGER_LOG_EVENTS, O_WRONLY); |
| 115 | log_fds[LOG_ID_SYSTEM] = log_open("/dev/"LOGGER_LOG_SYSTEM, O_WRONLY); |
| 116 | |
| 117 | write_to_log = __write_to_log_kernel; |
| 118 | |
| 119 | if (log_fds[LOG_ID_MAIN] < 0 || log_fds[LOG_ID_RADIO] < 0 || |
| 120 | log_fds[LOG_ID_EVENTS] < 0) { |
| 121 | log_close(log_fds[LOG_ID_MAIN]); |
| 122 | log_close(log_fds[LOG_ID_RADIO]); |
| 123 | log_close(log_fds[LOG_ID_EVENTS]); |
| 124 | log_fds[LOG_ID_MAIN] = -1; |
| 125 | log_fds[LOG_ID_RADIO] = -1; |
| 126 | log_fds[LOG_ID_EVENTS] = -1; |
| 127 | write_to_log = __write_to_log_null; |
| 128 | } |
| 129 | |
| 130 | if (log_fds[LOG_ID_SYSTEM] < 0) { |
| 131 | log_fds[LOG_ID_SYSTEM] = log_fds[LOG_ID_MAIN]; |
| 132 | } |
| 133 | } |
| 134 | |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 135 | pthread_mutex_unlock(&log_init_lock); |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 136 | |
| 137 | return write_to_log(log_id, vec, nr); |
| 138 | } |
| 139 | |
| 140 | int __android_log_write(int prio, const char *tag, const char *msg) |
| 141 | { |
Dan Albert | c7aadc4 | 2015-04-02 10:32:44 -0700 | [diff] [blame] | 142 | return __android_log_buf_write(LOG_ID_MAIN, prio, tag, msg); |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | int __android_log_buf_write(int bufID, int prio, const char *tag, const char *msg) |
| 146 | { |
| 147 | struct iovec vec[3]; |
| 148 | char tmp_tag[32]; |
| 149 | |
| 150 | if (!tag) |
| 151 | tag = ""; |
| 152 | |
| 153 | /* XXX: This needs to go! */ |
| 154 | if ((bufID != LOG_ID_RADIO) && |
| 155 | (!strcmp(tag, "HTC_RIL") || |
| 156 | !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */ |
| 157 | !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */ |
| 158 | !strcmp(tag, "AT") || |
| 159 | !strcmp(tag, "GSM") || |
| 160 | !strcmp(tag, "STK") || |
| 161 | !strcmp(tag, "CDMA") || |
| 162 | !strcmp(tag, "PHONE") || |
| 163 | !strcmp(tag, "SMS"))) { |
| 164 | bufID = LOG_ID_RADIO; |
| 165 | /* Inform third party apps/ril/radio.. to use Rlog or RLOG */ |
| 166 | snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag); |
| 167 | tag = tmp_tag; |
| 168 | } |
| 169 | |
Dan Albert | c7aadc4 | 2015-04-02 10:32:44 -0700 | [diff] [blame] | 170 | if (prio == ANDROID_LOG_FATAL) { |
| 171 | android_set_abort_message(msg); |
| 172 | } |
| 173 | |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 174 | vec[0].iov_base = (unsigned char *) &prio; |
| 175 | vec[0].iov_len = 1; |
| 176 | vec[1].iov_base = (void *) tag; |
| 177 | vec[1].iov_len = strlen(tag) + 1; |
| 178 | vec[2].iov_base = (void *) msg; |
| 179 | vec[2].iov_len = strlen(msg) + 1; |
| 180 | |
| 181 | return write_to_log(bufID, vec, 3); |
| 182 | } |
| 183 | |
| 184 | int __android_log_vprint(int prio, const char *tag, const char *fmt, va_list ap) |
| 185 | { |
| 186 | char buf[LOG_BUF_SIZE]; |
| 187 | |
| 188 | vsnprintf(buf, LOG_BUF_SIZE, fmt, ap); |
| 189 | |
| 190 | return __android_log_write(prio, tag, buf); |
| 191 | } |
| 192 | |
| 193 | int __android_log_print(int prio, const char *tag, const char *fmt, ...) |
| 194 | { |
| 195 | va_list ap; |
| 196 | char buf[LOG_BUF_SIZE]; |
| 197 | |
| 198 | va_start(ap, fmt); |
| 199 | vsnprintf(buf, LOG_BUF_SIZE, fmt, ap); |
| 200 | va_end(ap); |
| 201 | |
| 202 | return __android_log_write(prio, tag, buf); |
| 203 | } |
| 204 | |
| 205 | int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...) |
| 206 | { |
| 207 | va_list ap; |
| 208 | char buf[LOG_BUF_SIZE]; |
| 209 | |
| 210 | va_start(ap, fmt); |
| 211 | vsnprintf(buf, LOG_BUF_SIZE, fmt, ap); |
| 212 | va_end(ap); |
| 213 | |
| 214 | return __android_log_buf_write(bufID, prio, tag, buf); |
| 215 | } |
| 216 | |
| 217 | void __android_log_assert(const char *cond, const char *tag, |
| 218 | const char *fmt, ...) |
| 219 | { |
| 220 | char buf[LOG_BUF_SIZE]; |
| 221 | |
| 222 | if (fmt) { |
| 223 | va_list ap; |
| 224 | va_start(ap, fmt); |
| 225 | vsnprintf(buf, LOG_BUF_SIZE, fmt, ap); |
| 226 | va_end(ap); |
| 227 | } else { |
| 228 | /* Msg not provided, log condition. N.B. Do not use cond directly as |
| 229 | * format string as it could contain spurious '%' syntax (e.g. |
| 230 | * "%d" in "blocks%devs == 0"). |
| 231 | */ |
| 232 | if (cond) |
| 233 | snprintf(buf, LOG_BUF_SIZE, "Assertion failed: %s", cond); |
| 234 | else |
| 235 | strcpy(buf, "Unspecified assertion failed"); |
| 236 | } |
| 237 | |
| 238 | __android_log_write(ANDROID_LOG_FATAL, tag, buf); |
Elliott Hughes | 02ff4b8 | 2015-03-07 11:21:37 -0800 | [diff] [blame] | 239 | abort(); /* abort so we have a chance to debug the situation */ |
Elliott Hughes | da6b2e2 | 2014-04-23 14:57:32 -0700 | [diff] [blame] | 240 | /* NOTREACHED */ |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | int __android_log_bwrite(int32_t tag, const void *payload, size_t len) |
| 244 | { |
| 245 | struct iovec vec[2]; |
| 246 | |
| 247 | vec[0].iov_base = &tag; |
| 248 | vec[0].iov_len = sizeof(tag); |
| 249 | vec[1].iov_base = (void*)payload; |
| 250 | vec[1].iov_len = len; |
| 251 | |
| 252 | return write_to_log(LOG_ID_EVENTS, vec, 2); |
| 253 | } |
| 254 | |
| 255 | /* |
| 256 | * Like __android_log_bwrite, but takes the type as well. Doesn't work |
| 257 | * for the general case where we're generating lists of stuff, but very |
| 258 | * handy if we just want to dump an integer into the log. |
| 259 | */ |
| 260 | int __android_log_btwrite(int32_t tag, char type, const void *payload, |
| 261 | size_t len) |
| 262 | { |
| 263 | struct iovec vec[3]; |
| 264 | |
| 265 | vec[0].iov_base = &tag; |
| 266 | vec[0].iov_len = sizeof(tag); |
| 267 | vec[1].iov_base = &type; |
| 268 | vec[1].iov_len = sizeof(type); |
| 269 | vec[2].iov_base = (void*)payload; |
| 270 | vec[2].iov_len = len; |
| 271 | |
| 272 | return write_to_log(LOG_ID_EVENTS, vec, 3); |
| 273 | } |
Nick Kralevich | 2a4d05a | 2014-07-01 10:57:16 -0700 | [diff] [blame] | 274 | |
| 275 | /* |
| 276 | * Like __android_log_bwrite, but used for writing strings to the |
| 277 | * event log. |
| 278 | */ |
| 279 | int __android_log_bswrite(int32_t tag, const char *payload) |
| 280 | { |
| 281 | struct iovec vec[4]; |
| 282 | char type = EVENT_TYPE_STRING; |
| 283 | uint32_t len = strlen(payload); |
| 284 | |
| 285 | vec[0].iov_base = &tag; |
| 286 | vec[0].iov_len = sizeof(tag); |
| 287 | vec[1].iov_base = &type; |
| 288 | vec[1].iov_len = sizeof(type); |
| 289 | vec[2].iov_base = &len; |
| 290 | vec[2].iov_len = sizeof(len); |
| 291 | vec[3].iov_base = (void*)payload; |
| 292 | vec[3].iov_len = len; |
| 293 | |
| 294 | return write_to_log(LOG_ID_EVENTS, vec, 4); |
| 295 | } |