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