blob: dbe5309da8af31799af03602e82b904adf56c851 [file] [log] [blame]
Mark Salyzyncf83b742017-02-28 12:59:01 -08001/*
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
45static int stderrOpen();
46static void stderrClose();
47static int stderrAvailable(log_id_t logId);
Mark Salyzyn6e315682017-03-09 08:09:43 -080048static int stderrWrite(log_id_t logId, struct timespec* ts, struct iovec* vec,
49 size_t nr);
Mark Salyzyncf83b742017-02-28 12:59:01 -080050
51struct stderrContext {
Mark Salyzyn6e315682017-03-09 08:09:43 -080052 AndroidLogFormat* logformat;
Mark Salyzyncf83b742017-02-28 12:59:01 -080053#if defined(__ANDROID__)
Mark Salyzyn6e315682017-03-09 08:09:43 -080054 EventTagMap* eventTagMap;
Mark Salyzyncf83b742017-02-28 12:59:01 -080055#endif
56};
57
58LIBLOG_HIDDEN struct android_log_transport_write stderrLoggerWrite = {
Mark Salyzyn6e315682017-03-09 08:09:43 -080059 .node = { &stderrLoggerWrite.node, &stderrLoggerWrite.node },
Mark Salyzyn62d6a9d2017-03-08 15:03:20 -080060 .context.priv = NULL,
Mark Salyzyn6e315682017-03-09 08:09:43 -080061 .name = "stderr",
62 .available = stderrAvailable,
63 .open = stderrOpen,
64 .close = stderrClose,
65 .write = stderrWrite,
Mark Salyzyncf83b742017-02-28 12:59:01 -080066};
67
Mark Salyzyn6e315682017-03-09 08:09:43 -080068static int stderrOpen() {
69 struct stderrContext* ctx;
70 const char* envStr;
71 bool setFormat;
Mark Salyzyncf83b742017-02-28 12:59:01 -080072
Mark Salyzyn6e315682017-03-09 08:09:43 -080073 if (!stderr || (fileno(stderr) < 0)) {
74 return -EBADF;
75 }
Mark Salyzyncf83b742017-02-28 12:59:01 -080076
Mark Salyzyn62d6a9d2017-03-08 15:03:20 -080077 if (stderrLoggerWrite.context.priv) {
Mark Salyzyncf83b742017-02-28 12:59:01 -080078 return fileno(stderr);
Mark Salyzyn6e315682017-03-09 08:09:43 -080079 }
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 Salyzyn62d6a9d2017-03-08 15:03:20 -0800120 stderrLoggerWrite.context.priv = ctx;
Mark Salyzyn6e315682017-03-09 08:09:43 -0800121
122 return fileno(stderr);
Mark Salyzyncf83b742017-02-28 12:59:01 -0800123}
124
Mark Salyzyn6e315682017-03-09 08:09:43 -0800125static void stderrClose() {
Mark Salyzyn62d6a9d2017-03-08 15:03:20 -0800126 struct stderrContext* ctx = stderrLoggerWrite.context.priv;
Mark Salyzyncf83b742017-02-28 12:59:01 -0800127
Mark Salyzyn6e315682017-03-09 08:09:43 -0800128 if (ctx) {
Mark Salyzyn62d6a9d2017-03-08 15:03:20 -0800129 stderrLoggerWrite.context.priv = NULL;
Mark Salyzyn6e315682017-03-09 08:09:43 -0800130 if (ctx->logformat) {
131 android_log_format_free(ctx->logformat);
132 ctx->logformat = NULL;
133 }
Mark Salyzyncf83b742017-02-28 12:59:01 -0800134#if defined(__ANDROID__)
Mark Salyzyn6e315682017-03-09 08:09:43 -0800135 if (ctx->eventTagMap) {
136 android_closeEventTagMap(ctx->eventTagMap);
137 ctx->eventTagMap = NULL;
138 }
Mark Salyzyncf83b742017-02-28 12:59:01 -0800139#endif
Mark Salyzyn6e315682017-03-09 08:09:43 -0800140 }
Mark Salyzyncf83b742017-02-28 12:59:01 -0800141}
142
Mark Salyzyn6e315682017-03-09 08:09:43 -0800143static int stderrAvailable(log_id_t logId) {
144 if ((logId >= LOG_ID_MAX) || (logId == LOG_ID_KERNEL)) {
145 return -EINVAL;
146 }
147 return 1;
Mark Salyzyncf83b742017-02-28 12:59:01 -0800148}
149
Mark Salyzyn6e315682017-03-09 08:09:43 -0800150static 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 Salyzyn62d6a9d2017-03-08 15:03:20 -0800157 struct stderrContext* ctx = stderrLoggerWrite.context.priv;
Mark Salyzyncf83b742017-02-28 12:59:01 -0800158
Mark Salyzyn6e315682017-03-09 08:09:43 -0800159 if (!ctx) return -EBADF;
160 if (!vec || !nr) return -EINVAL;
Mark Salyzyncf83b742017-02-28 12:59:01 -0800161
Mark Salyzyn6e315682017-03-09 08:09:43 -0800162 log_msg.entry.len = 0;
163 log_msg.entry.hdr_size = sizeof(log_msg.entry);
164 log_msg.entry.pid = getpid();
Mark Salyzyncf83b742017-02-28 12:59:01 -0800165#ifdef __BIONIC__
Mark Salyzyn6e315682017-03-09 08:09:43 -0800166 log_msg.entry.tid = gettid();
Mark Salyzyncf83b742017-02-28 12:59:01 -0800167#else
Mark Salyzyn6e315682017-03-09 08:09:43 -0800168 log_msg.entry.tid = getpid();
Mark Salyzyncf83b742017-02-28 12:59:01 -0800169#endif
Mark Salyzyn6e315682017-03-09 08:09:43 -0800170 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 Salyzyncf83b742017-02-28 12:59:01 -0800174
Mark Salyzyn6e315682017-03-09 08:09:43 -0800175 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 Salyzyncf83b742017-02-28 12:59:01 -0800179 }
Mark Salyzyn6e315682017-03-09 08:09:43 -0800180 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 Salyzyncf83b742017-02-28 12:59:01 -0800184
Mark Salyzyn6e315682017-03-09 08:09:43 -0800185 if ((logId == LOG_ID_EVENTS) || (logId == LOG_ID_SECURITY)) {
Mark Salyzyncf83b742017-02-28 12:59:01 -0800186#if defined(__ANDROID__)
Mark Salyzyn6e315682017-03-09 08:09:43 -0800187 if (!ctx->eventTagMap) {
188 ctx->eventTagMap = android_openEventTagMap(NULL);
189 }
Mark Salyzyncf83b742017-02-28 12:59:01 -0800190#endif
Mark Salyzyn6e315682017-03-09 08:09:43 -0800191 err = android_log_processBinaryLogBuffer(&log_msg.entry_v1, &entry,
Mark Salyzyncf83b742017-02-28 12:59:01 -0800192#if defined(__ANDROID__)
Mark Salyzyn6e315682017-03-09 08:09:43 -0800193 ctx->eventTagMap,
Mark Salyzyncf83b742017-02-28 12:59:01 -0800194#else
Mark Salyzyn6e315682017-03-09 08:09:43 -0800195 NULL,
Mark Salyzyncf83b742017-02-28 12:59:01 -0800196#endif
Mark Salyzyn6e315682017-03-09 08:09:43 -0800197 binaryMsgBuf, sizeof(binaryMsgBuf));
198 } else {
199 err = android_log_processLogBuffer(&log_msg.entry_v1, &entry);
200 }
Mark Salyzyncf83b742017-02-28 12:59:01 -0800201
Mark Salyzyn6e315682017-03-09 08:09:43 -0800202 /* print known truncated data, in essence logcat --debug */
203 if ((err < 0) && !entry.message) return -EINVAL;
Mark Salyzyncf83b742017-02-28 12:59:01 -0800204
Mark Salyzyn6e315682017-03-09 08:09:43 -0800205 if (!android_log_shouldPrintLine(ctx->logformat, entry.tag, entry.priority)) {
Mark Salyzyncf83b742017-02-28 12:59:01 -0800206 return log_msg.entry.len;
Mark Salyzyn6e315682017-03-09 08:09:43 -0800207 }
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 Salyzyncf83b742017-02-28 12:59:01 -0800212}