Mark Salyzyn | cf83b74 | 2017-02-28 12:59:01 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | /* |
| 18 | * stderr write handler. Output is logcat-like, and responds to |
| 19 | * logcat's environment variables ANDROID_PRINTF_LOG and |
| 20 | * ANDROID_LOG_TAGS to filter output. |
| 21 | * |
| 22 | * This transport only provides a writer, that means that it does not |
| 23 | * provide an End-To-End capability as the logs are effectively _lost_ |
| 24 | * to the stderr file stream. The purpose of this transport is to |
| 25 | * supply a means for command line tools to report their logging |
| 26 | * to the stderr stream, in line with all other activities. |
| 27 | */ |
| 28 | |
| 29 | #include <errno.h> |
| 30 | #include <stdbool.h> |
| 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
| 34 | #include <sys/types.h> |
| 35 | #include <unistd.h> |
| 36 | |
| 37 | #include <log/event_tag_map.h> |
| 38 | #include <log/log.h> |
| 39 | #include <log/logprint.h> |
| 40 | #include <log/uio.h> |
| 41 | |
| 42 | #include "log_portability.h" |
| 43 | #include "logger.h" |
| 44 | |
| 45 | static int stderrOpen(); |
| 46 | static void stderrClose(); |
| 47 | static int stderrAvailable(log_id_t logId); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 48 | static int stderrWrite(log_id_t logId, struct timespec* ts, struct iovec* vec, |
| 49 | size_t nr); |
Mark Salyzyn | cf83b74 | 2017-02-28 12:59:01 -0800 | [diff] [blame] | 50 | |
| 51 | struct stderrContext { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 52 | AndroidLogFormat* logformat; |
Mark Salyzyn | cf83b74 | 2017-02-28 12:59:01 -0800 | [diff] [blame] | 53 | #if defined(__ANDROID__) |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 54 | EventTagMap* eventTagMap; |
Mark Salyzyn | cf83b74 | 2017-02-28 12:59:01 -0800 | [diff] [blame] | 55 | #endif |
| 56 | }; |
| 57 | |
| 58 | LIBLOG_HIDDEN struct android_log_transport_write stderrLoggerWrite = { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 59 | .node = { &stderrLoggerWrite.node, &stderrLoggerWrite.node }, |
Mark Salyzyn | 62d6a9d | 2017-03-08 15:03:20 -0800 | [diff] [blame] | 60 | .context.priv = NULL, |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 61 | .name = "stderr", |
| 62 | .available = stderrAvailable, |
| 63 | .open = stderrOpen, |
| 64 | .close = stderrClose, |
| 65 | .write = stderrWrite, |
Mark Salyzyn | cf83b74 | 2017-02-28 12:59:01 -0800 | [diff] [blame] | 66 | }; |
| 67 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 68 | static int stderrOpen() { |
| 69 | struct stderrContext* ctx; |
| 70 | const char* envStr; |
| 71 | bool setFormat; |
Mark Salyzyn | cf83b74 | 2017-02-28 12:59:01 -0800 | [diff] [blame] | 72 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 73 | if (!stderr || (fileno(stderr) < 0)) { |
| 74 | return -EBADF; |
| 75 | } |
Mark Salyzyn | cf83b74 | 2017-02-28 12:59:01 -0800 | [diff] [blame] | 76 | |
Mark Salyzyn | 62d6a9d | 2017-03-08 15:03:20 -0800 | [diff] [blame] | 77 | if (stderrLoggerWrite.context.priv) { |
Mark Salyzyn | cf83b74 | 2017-02-28 12:59:01 -0800 | [diff] [blame] | 78 | return fileno(stderr); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | ctx = calloc(1, sizeof(struct stderrContext)); |
| 82 | if (!ctx) { |
| 83 | return -ENOMEM; |
| 84 | } |
| 85 | |
| 86 | ctx->logformat = android_log_format_new(); |
| 87 | if (!ctx->logformat) { |
| 88 | free(ctx); |
| 89 | return -ENOMEM; |
| 90 | } |
| 91 | |
| 92 | envStr = getenv("ANDROID_PRINTF_LOG"); |
| 93 | setFormat = false; |
| 94 | |
| 95 | if (envStr) { |
| 96 | char* formats = strdup(envStr); |
| 97 | char* sv = NULL; |
| 98 | char* arg = formats; |
| 99 | while (!!(arg = strtok_r(arg, ",:; \t\n\r\f", &sv))) { |
| 100 | AndroidLogPrintFormat format = android_log_formatFromString(arg); |
| 101 | arg = NULL; |
| 102 | if (format == FORMAT_OFF) { |
| 103 | continue; |
| 104 | } |
| 105 | if (android_log_setPrintFormat(ctx->logformat, format) <= 0) { |
| 106 | continue; |
| 107 | } |
| 108 | setFormat = true; |
| 109 | } |
| 110 | free(formats); |
| 111 | } |
| 112 | if (!setFormat) { |
| 113 | AndroidLogPrintFormat format = android_log_formatFromString("threadtime"); |
| 114 | android_log_setPrintFormat(ctx->logformat, format); |
| 115 | } |
| 116 | envStr = getenv("ANDROID_LOG_TAGS"); |
| 117 | if (envStr) { |
| 118 | android_log_addFilterString(ctx->logformat, envStr); |
| 119 | } |
Mark Salyzyn | 62d6a9d | 2017-03-08 15:03:20 -0800 | [diff] [blame] | 120 | stderrLoggerWrite.context.priv = ctx; |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 121 | |
| 122 | return fileno(stderr); |
Mark Salyzyn | cf83b74 | 2017-02-28 12:59:01 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 125 | static void stderrClose() { |
Mark Salyzyn | 62d6a9d | 2017-03-08 15:03:20 -0800 | [diff] [blame] | 126 | struct stderrContext* ctx = stderrLoggerWrite.context.priv; |
Mark Salyzyn | cf83b74 | 2017-02-28 12:59:01 -0800 | [diff] [blame] | 127 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 128 | if (ctx) { |
Mark Salyzyn | 62d6a9d | 2017-03-08 15:03:20 -0800 | [diff] [blame] | 129 | stderrLoggerWrite.context.priv = NULL; |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 130 | if (ctx->logformat) { |
| 131 | android_log_format_free(ctx->logformat); |
| 132 | ctx->logformat = NULL; |
| 133 | } |
Mark Salyzyn | cf83b74 | 2017-02-28 12:59:01 -0800 | [diff] [blame] | 134 | #if defined(__ANDROID__) |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 135 | if (ctx->eventTagMap) { |
| 136 | android_closeEventTagMap(ctx->eventTagMap); |
| 137 | ctx->eventTagMap = NULL; |
| 138 | } |
Mark Salyzyn | cf83b74 | 2017-02-28 12:59:01 -0800 | [diff] [blame] | 139 | #endif |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 140 | } |
Mark Salyzyn | cf83b74 | 2017-02-28 12:59:01 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 143 | static int stderrAvailable(log_id_t logId) { |
| 144 | if ((logId >= LOG_ID_MAX) || (logId == LOG_ID_KERNEL)) { |
| 145 | return -EINVAL; |
| 146 | } |
| 147 | return 1; |
Mark Salyzyn | cf83b74 | 2017-02-28 12:59:01 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 150 | static int stderrWrite(log_id_t logId, struct timespec* ts, struct iovec* vec, |
| 151 | size_t nr) { |
| 152 | struct log_msg log_msg; |
| 153 | AndroidLogEntry entry; |
| 154 | char binaryMsgBuf[1024]; |
| 155 | int err; |
| 156 | size_t i; |
Mark Salyzyn | 62d6a9d | 2017-03-08 15:03:20 -0800 | [diff] [blame] | 157 | struct stderrContext* ctx = stderrLoggerWrite.context.priv; |
Mark Salyzyn | cf83b74 | 2017-02-28 12:59:01 -0800 | [diff] [blame] | 158 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 159 | if (!ctx) return -EBADF; |
| 160 | if (!vec || !nr) return -EINVAL; |
Mark Salyzyn | cf83b74 | 2017-02-28 12:59:01 -0800 | [diff] [blame] | 161 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 162 | log_msg.entry.len = 0; |
| 163 | log_msg.entry.hdr_size = sizeof(log_msg.entry); |
| 164 | log_msg.entry.pid = getpid(); |
Mark Salyzyn | cf83b74 | 2017-02-28 12:59:01 -0800 | [diff] [blame] | 165 | #ifdef __BIONIC__ |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 166 | log_msg.entry.tid = gettid(); |
Mark Salyzyn | cf83b74 | 2017-02-28 12:59:01 -0800 | [diff] [blame] | 167 | #else |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 168 | log_msg.entry.tid = getpid(); |
Mark Salyzyn | cf83b74 | 2017-02-28 12:59:01 -0800 | [diff] [blame] | 169 | #endif |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 170 | log_msg.entry.sec = ts->tv_sec; |
| 171 | log_msg.entry.nsec = ts->tv_nsec; |
| 172 | log_msg.entry.lid = logId; |
| 173 | log_msg.entry.uid = __android_log_uid(); |
Mark Salyzyn | cf83b74 | 2017-02-28 12:59:01 -0800 | [diff] [blame] | 174 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 175 | for (i = 0; i < nr; ++i) { |
| 176 | size_t len = vec[i].iov_len; |
| 177 | if ((log_msg.entry.len + len) > LOGGER_ENTRY_MAX_PAYLOAD) { |
| 178 | len = LOGGER_ENTRY_MAX_PAYLOAD - log_msg.entry.len; |
Mark Salyzyn | cf83b74 | 2017-02-28 12:59:01 -0800 | [diff] [blame] | 179 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 180 | if (!len) continue; |
| 181 | memcpy(log_msg.entry.msg + log_msg.entry.len, vec[i].iov_base, len); |
| 182 | log_msg.entry.len += len; |
| 183 | } |
Mark Salyzyn | cf83b74 | 2017-02-28 12:59:01 -0800 | [diff] [blame] | 184 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 185 | if ((logId == LOG_ID_EVENTS) || (logId == LOG_ID_SECURITY)) { |
Mark Salyzyn | cf83b74 | 2017-02-28 12:59:01 -0800 | [diff] [blame] | 186 | #if defined(__ANDROID__) |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 187 | if (!ctx->eventTagMap) { |
| 188 | ctx->eventTagMap = android_openEventTagMap(NULL); |
| 189 | } |
Mark Salyzyn | cf83b74 | 2017-02-28 12:59:01 -0800 | [diff] [blame] | 190 | #endif |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 191 | err = android_log_processBinaryLogBuffer(&log_msg.entry_v1, &entry, |
Mark Salyzyn | cf83b74 | 2017-02-28 12:59:01 -0800 | [diff] [blame] | 192 | #if defined(__ANDROID__) |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 193 | ctx->eventTagMap, |
Mark Salyzyn | cf83b74 | 2017-02-28 12:59:01 -0800 | [diff] [blame] | 194 | #else |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 195 | NULL, |
Mark Salyzyn | cf83b74 | 2017-02-28 12:59:01 -0800 | [diff] [blame] | 196 | #endif |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 197 | binaryMsgBuf, sizeof(binaryMsgBuf)); |
| 198 | } else { |
| 199 | err = android_log_processLogBuffer(&log_msg.entry_v1, &entry); |
| 200 | } |
Mark Salyzyn | cf83b74 | 2017-02-28 12:59:01 -0800 | [diff] [blame] | 201 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 202 | /* print known truncated data, in essence logcat --debug */ |
| 203 | if ((err < 0) && !entry.message) return -EINVAL; |
Mark Salyzyn | cf83b74 | 2017-02-28 12:59:01 -0800 | [diff] [blame] | 204 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 205 | if (!android_log_shouldPrintLine(ctx->logformat, entry.tag, entry.priority)) { |
Mark Salyzyn | cf83b74 | 2017-02-28 12:59:01 -0800 | [diff] [blame] | 206 | return log_msg.entry.len; |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | err = android_log_printLogLine(ctx->logformat, fileno(stderr), &entry); |
| 210 | if (err < 0) return errno ? -errno : -EINVAL; |
| 211 | return log_msg.entry.len; |
Mark Salyzyn | cf83b74 | 2017-02-28 12:59:01 -0800 | [diff] [blame] | 212 | } |