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
41 #include "log_portability.h"
42 #include "logger.h"
43 #include "uio.h"
44
45 static int stderrOpen();
46 static void stderrClose();
47 static int stderrAvailable(log_id_t logId);
48 static int stderrWrite(log_id_t logId, struct timespec* ts, struct iovec* vec, size_t nr);
49
50 struct stderrContext {
51 AndroidLogFormat* logformat;
52 #if defined(__ANDROID__)
53 EventTagMap* eventTagMap;
54 #endif
55 };
56
57 struct android_log_transport_write stderrLoggerWrite = {
58 .node = {&stderrLoggerWrite.node, &stderrLoggerWrite.node},
59 .context.priv = NULL,
60 .name = "stderr",
61 .available = stderrAvailable,
62 .open = stderrOpen,
63 .close = stderrClose,
64 .write = stderrWrite,
65 };
66
stderrOpen()67 static int stderrOpen() {
68 struct stderrContext* ctx;
69 const char* envStr;
70 bool setFormat;
71
72 if (!stderr || (fileno(stderr) < 0)) {
73 return -EBADF;
74 }
75
76 if (stderrLoggerWrite.context.priv) {
77 return fileno(stderr);
78 }
79
80 ctx = static_cast<stderrContext*>(calloc(1, sizeof(stderrContext)));
81 if (!ctx) {
82 return -ENOMEM;
83 }
84
85 ctx->logformat = android_log_format_new();
86 if (!ctx->logformat) {
87 free(ctx);
88 return -ENOMEM;
89 }
90
91 envStr = getenv("ANDROID_PRINTF_LOG");
92 setFormat = false;
93
94 if (envStr) {
95 char* formats = strdup(envStr);
96 char* sv = NULL;
97 char* arg = formats;
98 while (!!(arg = strtok_r(arg, ",:; \t\n\r\f", &sv))) {
99 AndroidLogPrintFormat format = android_log_formatFromString(arg);
100 arg = NULL;
101 if (format == FORMAT_OFF) {
102 continue;
103 }
104 if (android_log_setPrintFormat(ctx->logformat, format) <= 0) {
105 continue;
106 }
107 setFormat = true;
108 }
109 free(formats);
110 }
111 if (!setFormat) {
112 AndroidLogPrintFormat format = android_log_formatFromString("threadtime");
113 android_log_setPrintFormat(ctx->logformat, format);
114 }
115 envStr = getenv("ANDROID_LOG_TAGS");
116 if (envStr) {
117 android_log_addFilterString(ctx->logformat, envStr);
118 }
119 stderrLoggerWrite.context.priv = ctx;
120
121 return fileno(stderr);
122 }
123
stderrClose()124 static void stderrClose() {
125 stderrContext* ctx = static_cast<stderrContext*>(stderrLoggerWrite.context.priv);
126
127 if (ctx) {
128 stderrLoggerWrite.context.priv = NULL;
129 if (ctx->logformat) {
130 android_log_format_free(ctx->logformat);
131 ctx->logformat = NULL;
132 }
133 #if defined(__ANDROID__)
134 if (ctx->eventTagMap) {
135 android_closeEventTagMap(ctx->eventTagMap);
136 ctx->eventTagMap = NULL;
137 }
138 #endif
139 }
140 }
141
stderrAvailable(log_id_t logId)142 static int stderrAvailable(log_id_t logId) {
143 if ((logId >= LOG_ID_MAX) || (logId == LOG_ID_KERNEL)) {
144 return -EINVAL;
145 }
146 return 1;
147 }
148
stderrWrite(log_id_t logId,struct timespec * ts,struct iovec * vec,size_t nr)149 static int stderrWrite(log_id_t logId, struct timespec* ts, struct iovec* vec, size_t nr) {
150 struct log_msg log_msg;
151 AndroidLogEntry entry;
152 char binaryMsgBuf[1024];
153 int err;
154 size_t i;
155 stderrContext* ctx = static_cast<stderrContext*>(stderrLoggerWrite.context.priv);
156
157 if (!ctx) return -EBADF;
158 if (!vec || !nr) return -EINVAL;
159
160 log_msg.entry.len = 0;
161 log_msg.entry.hdr_size = sizeof(log_msg.entry);
162 log_msg.entry.pid = getpid();
163 #ifdef __BIONIC__
164 log_msg.entry.tid = gettid();
165 #else
166 log_msg.entry.tid = getpid();
167 #endif
168 log_msg.entry.sec = ts->tv_sec;
169 log_msg.entry.nsec = ts->tv_nsec;
170 log_msg.entry.lid = logId;
171 log_msg.entry.uid = __android_log_uid();
172
173 for (i = 0; i < nr; ++i) {
174 size_t len = vec[i].iov_len;
175 if ((log_msg.entry.len + len) > LOGGER_ENTRY_MAX_PAYLOAD) {
176 len = LOGGER_ENTRY_MAX_PAYLOAD - log_msg.entry.len;
177 }
178 if (!len) continue;
179 memcpy(log_msg.entry.msg + log_msg.entry.len, vec[i].iov_base, len);
180 log_msg.entry.len += len;
181 }
182
183 if ((logId == LOG_ID_EVENTS) || (logId == LOG_ID_SECURITY)) {
184 #if defined(__ANDROID__)
185 if (!ctx->eventTagMap) {
186 ctx->eventTagMap = android_openEventTagMap(NULL);
187 }
188 #endif
189 err = android_log_processBinaryLogBuffer(&log_msg.entry_v1, &entry,
190 #if defined(__ANDROID__)
191 ctx->eventTagMap,
192 #else
193 NULL,
194 #endif
195 binaryMsgBuf, sizeof(binaryMsgBuf));
196 } else {
197 err = android_log_processLogBuffer(&log_msg.entry_v1, &entry);
198 }
199
200 /* print known truncated data, in essence logcat --debug */
201 if ((err < 0) && !entry.message) return -EINVAL;
202
203 if (!android_log_shouldPrintLine(ctx->logformat, entry.tag, entry.priority)) {
204 return log_msg.entry.len;
205 }
206
207 err = android_log_printLogLine(ctx->logformat, fileno(stderr), &entry);
208 if (err < 0) return errno ? -errno : -EINVAL;
209 return log_msg.entry.len;
210 }
211