summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMark Salyzyn2017-03-09 10:09:43 -0600
committerMark Salyzyn2017-03-09 11:36:19 -0600
commit2ed51d708eda64516ec79ac6397f690de38f0075 (patch)
tree9ea938a4ee0461c443dc99e197827fc36ecd643b /liblog/stderr_write.c
parentc1b3c8ef2629eac2a73aa4a95bf43a66edf4cd0f (diff)
downloadplatform-system-core-2ed51d708eda64516ec79ac6397f690de38f0075.tar.gz
platform-system-core-2ed51d708eda64516ec79ac6397f690de38f0075.tar.xz
platform-system-core-2ed51d708eda64516ec79ac6397f690de38f0075.zip
liblog: specify clang format
Switch coding style to match SideEffects: None Test: compile Bug: 27405083 Change-Id: Id426d5c5e3b18f2ceec22b31bbc9781aabf6bcca
Diffstat (limited to 'liblog/stderr_write.c')
-rw-r--r--liblog/stderr_write.c267
1 files changed, 130 insertions, 137 deletions
diff --git a/liblog/stderr_write.c b/liblog/stderr_write.c
index b73929923..291ce9322 100644
--- a/liblog/stderr_write.c
+++ b/liblog/stderr_write.c
@@ -45,175 +45,168 @@
45static int stderrOpen(); 45static int stderrOpen();
46static void stderrClose(); 46static void stderrClose();
47static int stderrAvailable(log_id_t logId); 47static int stderrAvailable(log_id_t logId);
48static int stderrWrite(log_id_t logId, struct timespec* ts, 48static int stderrWrite(log_id_t logId, struct timespec* ts, struct iovec* vec,
49 struct iovec* vec, size_t nr); 49 size_t nr);
50 50
51struct stderrContext { 51struct stderrContext {
52 AndroidLogFormat* logformat; 52 AndroidLogFormat* logformat;
53#if defined(__ANDROID__) 53#if defined(__ANDROID__)
54 EventTagMap* eventTagMap; 54 EventTagMap* eventTagMap;
55#endif 55#endif
56}; 56};
57 57
58LIBLOG_HIDDEN struct android_log_transport_write stderrLoggerWrite = { 58LIBLOG_HIDDEN struct android_log_transport_write stderrLoggerWrite = {
59 .node = { &stderrLoggerWrite.node, &stderrLoggerWrite.node }, 59 .node = { &stderrLoggerWrite.node, &stderrLoggerWrite.node },
60 .context.private = NULL, 60 .context.private = NULL,
61 .name = "stderr", 61 .name = "stderr",
62 .available = stderrAvailable, 62 .available = stderrAvailable,
63 .open = stderrOpen, 63 .open = stderrOpen,
64 .close = stderrClose, 64 .close = stderrClose,
65 .write = stderrWrite, 65 .write = stderrWrite,
66}; 66};
67 67
68static int stderrOpen() 68static int stderrOpen() {
69{ 69 struct stderrContext* ctx;
70 struct stderrContext* ctx; 70 const char* envStr;
71 const char* envStr; 71 bool setFormat;
72 bool setFormat;
73 72
74 if (!stderr || (fileno(stderr) < 0)) { 73 if (!stderr || (fileno(stderr) < 0)) {
75 return -EBADF; 74 return -EBADF;
76 } 75 }
77
78 if (stderrLoggerWrite.context.private) {
79 return fileno(stderr);
80 }
81
82 ctx = calloc(1, sizeof(struct stderrContext));
83 if (!ctx) {
84 return -ENOMEM;
85 }
86
87 ctx->logformat = android_log_format_new();
88 if (!ctx->logformat) {
89 free(ctx);
90 return -ENOMEM;
91 }
92
93 envStr = getenv("ANDROID_PRINTF_LOG");
94 setFormat = false;
95
96 if (envStr) {
97 char* formats = strdup(envStr);
98 char* sv = NULL;
99 char* arg = formats;
100 while (!!(arg = strtok_r(arg, ",:; \t\n\r\f", &sv))) {
101 AndroidLogPrintFormat format = android_log_formatFromString(arg);
102 arg = NULL;
103 if (format == FORMAT_OFF) {
104 continue;
105 }
106 if (android_log_setPrintFormat(ctx->logformat, format) <= 0) {
107 continue;
108 }
109 setFormat = true;
110 }
111 free(formats);
112 }
113 if (!setFormat) {
114 AndroidLogPrintFormat format = android_log_formatFromString(
115 "threadtime");
116 android_log_setPrintFormat(ctx->logformat, format);
117 }
118 envStr = getenv("ANDROID_LOG_TAGS");
119 if (envStr) {
120 android_log_addFilterString(ctx->logformat, envStr);
121 }
122 stderrLoggerWrite.context.private = ctx;
123 76
77 if (stderrLoggerWrite.context.private) {
124 return fileno(stderr); 78 return fileno(stderr);
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 }
120 stderrLoggerWrite.context.private = ctx;
121
122 return fileno(stderr);
125} 123}
126 124
127static void stderrClose() 125static void stderrClose() {
128{ 126 struct stderrContext* ctx = stderrLoggerWrite.context.private;
129 struct stderrContext* ctx = stderrLoggerWrite.context.private;
130 127
131 if (ctx) { 128 if (ctx) {
132 stderrLoggerWrite.context.private = NULL; 129 stderrLoggerWrite.context.private = NULL;
133 if (ctx->logformat) { 130 if (ctx->logformat) {
134 android_log_format_free(ctx->logformat); 131 android_log_format_free(ctx->logformat);
135 ctx->logformat = NULL; 132 ctx->logformat = NULL;
136 } 133 }
137#if defined(__ANDROID__) 134#if defined(__ANDROID__)
138 if (ctx->eventTagMap) { 135 if (ctx->eventTagMap) {
139 android_closeEventTagMap(ctx->eventTagMap); 136 android_closeEventTagMap(ctx->eventTagMap);
140 ctx->eventTagMap = NULL; 137 ctx->eventTagMap = NULL;
141 }
142#endif
143 } 138 }
139#endif
140 }
144} 141}
145 142
146static int stderrAvailable(log_id_t logId) 143static int stderrAvailable(log_id_t logId) {
147{ 144 if ((logId >= LOG_ID_MAX) || (logId == LOG_ID_KERNEL)) {
148 if ((logId >= LOG_ID_MAX) || (logId == LOG_ID_KERNEL)) { 145 return -EINVAL;
149 return -EINVAL; 146 }
150 } 147 return 1;
151 return 1;
152} 148}
153 149
154static int stderrWrite(log_id_t logId, struct timespec* ts, 150static int stderrWrite(log_id_t logId, struct timespec* ts, struct iovec* vec,
155 struct iovec* vec, size_t nr) 151 size_t nr) {
156{ 152 struct log_msg log_msg;
157 struct log_msg log_msg; 153 AndroidLogEntry entry;
158 AndroidLogEntry entry; 154 char binaryMsgBuf[1024];
159 char binaryMsgBuf[1024]; 155 int err;
160 int err; 156 size_t i;
161 size_t i; 157 struct stderrContext* ctx = stderrLoggerWrite.context.private;
162 struct stderrContext* ctx = stderrLoggerWrite.context.private; 158
163 159 if (!ctx) return -EBADF;
164 if (!ctx) return -EBADF; 160 if (!vec || !nr) return -EINVAL;
165 if (!vec || !nr) return -EINVAL; 161
166 162 log_msg.entry.len = 0;
167 log_msg.entry.len = 0; 163 log_msg.entry.hdr_size = sizeof(log_msg.entry);
168 log_msg.entry.hdr_size = sizeof(log_msg.entry); 164 log_msg.entry.pid = getpid();
169 log_msg.entry.pid = getpid();
170#ifdef __BIONIC__ 165#ifdef __BIONIC__
171 log_msg.entry.tid = gettid(); 166 log_msg.entry.tid = gettid();
172#else 167#else
173 log_msg.entry.tid = getpid(); 168 log_msg.entry.tid = getpid();
174#endif 169#endif
175 log_msg.entry.sec = ts->tv_sec; 170 log_msg.entry.sec = ts->tv_sec;
176 log_msg.entry.nsec = ts->tv_nsec; 171 log_msg.entry.nsec = ts->tv_nsec;
177 log_msg.entry.lid = logId; 172 log_msg.entry.lid = logId;
178 log_msg.entry.uid = __android_log_uid(); 173 log_msg.entry.uid = __android_log_uid();
179 174
180 for (i = 0; i < nr; ++i) { 175 for (i = 0; i < nr; ++i) {
181 size_t len = vec[i].iov_len; 176 size_t len = vec[i].iov_len;
182 if ((log_msg.entry.len + len) > LOGGER_ENTRY_MAX_PAYLOAD) { 177 if ((log_msg.entry.len + len) > LOGGER_ENTRY_MAX_PAYLOAD) {
183 len = LOGGER_ENTRY_MAX_PAYLOAD - log_msg.entry.len; 178 len = LOGGER_ENTRY_MAX_PAYLOAD - log_msg.entry.len;
184 }
185 if (!len) continue;
186 memcpy(log_msg.entry.msg + log_msg.entry.len, vec[i].iov_base, len);
187 log_msg.entry.len += len;
188 } 179 }
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 }
189 184
190 if ((logId == LOG_ID_EVENTS) || (logId == LOG_ID_SECURITY)) { 185 if ((logId == LOG_ID_EVENTS) || (logId == LOG_ID_SECURITY)) {
191#if defined(__ANDROID__) 186#if defined(__ANDROID__)
192 if (!ctx->eventTagMap) { 187 if (!ctx->eventTagMap) {
193 ctx->eventTagMap = android_openEventTagMap(NULL); 188 ctx->eventTagMap = android_openEventTagMap(NULL);
194 } 189 }
195#endif 190#endif
196 err = android_log_processBinaryLogBuffer(&log_msg.entry_v1, 191 err = android_log_processBinaryLogBuffer(&log_msg.entry_v1, &entry,
197 &entry,
198#if defined(__ANDROID__) 192#if defined(__ANDROID__)
199 ctx->eventTagMap, 193 ctx->eventTagMap,
200#else 194#else
201 NULL, 195 NULL,
202#endif 196#endif
203 binaryMsgBuf, 197 binaryMsgBuf, sizeof(binaryMsgBuf));
204 sizeof(binaryMsgBuf)); 198 } else {
205 } else { 199 err = android_log_processLogBuffer(&log_msg.entry_v1, &entry);
206 err = android_log_processLogBuffer(&log_msg.entry_v1, &entry); 200 }
207 }
208 201
209 /* print known truncated data, in essence logcat --debug */ 202 /* print known truncated data, in essence logcat --debug */
210 if ((err < 0) && !entry.message) return -EINVAL; 203 if ((err < 0) && !entry.message) return -EINVAL;
211 204
212 if (!android_log_shouldPrintLine(ctx->logformat, entry.tag, entry.priority)) { 205 if (!android_log_shouldPrintLine(ctx->logformat, entry.tag, entry.priority)) {
213 return log_msg.entry.len;
214 }
215
216 err = android_log_printLogLine(ctx->logformat, fileno(stderr), &entry);
217 if (err < 0) return errno ? -errno : -EINVAL;
218 return log_msg.entry.len; 206 return log_msg.entry.len;
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;
219} 212}