blob: d9f5415463e334d4dd39eb90c87bb0777486be6f [file] [log] [blame]
Joe Onoratoc4dfae52017-10-17 23:38:21 -07001/*
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
Tej Singh484524a2018-02-01 15:10:05 -080017#define DEBUG false // STOPSHIP if true
Joe Onoratoc4dfae52017-10-17 23:38:21 -070018#include "logd/LogEvent.h"
19
Yangster-mac20877162017-12-22 17:19:39 -080020#include "stats_log_util.h"
Yangster-mac48b3d622018-08-18 12:38:11 -070021#include "statslog.h"
Joe Onoratoc4dfae52017-10-17 23:38:21 -070022
Chenjie Yu6b1667c2019-01-18 10:09:33 -080023#include <binder/IPCThreadState.h>
Tej Singh8928ed72019-02-22 19:06:22 -080024#include <private/android_filesystem_config.h>
Chenjie Yu6b1667c2019-01-18 10:09:33 -080025
Joe Onoratoc4dfae52017-10-17 23:38:21 -070026namespace android {
27namespace os {
28namespace statsd {
29
yro24809bd2017-10-31 23:06:53 -070030using namespace android::util;
Yao Chen9c1debe2018-02-19 14:39:19 -080031using android::util::ProtoOutputStream;
David Chen1481fe12017-10-16 13:16:34 -070032using std::string;
Yao Chen9c1debe2018-02-19 14:39:19 -080033using std::vector;
Joe Onoratoc4dfae52017-10-17 23:38:21 -070034
Yao Chen80235402017-11-13 20:42:25 -080035LogEvent::LogEvent(log_msg& msg) {
Chenjie Yu3ca36832018-01-22 15:10:54 -080036 mContext =
Yao Chen80235402017-11-13 20:42:25 -080037 create_android_log_parser(msg.msg() + sizeof(uint32_t), msg.len() - sizeof(uint32_t));
Yangster-mac330af582018-02-08 15:24:38 -080038 mLogdTimestampNs = msg.entry_v1.sec * NS_PER_SEC + msg.entry_v1.nsec;
Yao Chend10f7b12017-12-18 12:53:50 -080039 mLogUid = msg.entry_v4.uid;
Chenjie Yu3ca36832018-01-22 15:10:54 -080040 init(mContext);
41 if (mContext) {
Yao Chen48d75182018-01-23 09:40:48 -080042 // android_log_destroy will set mContext to NULL
Chenjie Yu3ca36832018-01-22 15:10:54 -080043 android_log_destroy(&mContext);
Yangster-mac20877162017-12-22 17:19:39 -080044 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -070045}
46
Chenjie Yu0bd73db2018-12-16 07:37:04 -080047LogEvent::LogEvent(const LogEvent& event) {
48 mTagId = event.mTagId;
49 mLogUid = event.mLogUid;
50 mElapsedTimestampNs = event.mElapsedTimestampNs;
51 mLogdTimestampNs = event.mLogdTimestampNs;
52 mValues = event.mValues;
53}
54
Chenjie Yud7e3a222018-11-28 21:29:44 +000055LogEvent::LogEvent(const StatsLogEventWrapper& statsLogEventWrapper, int workChainIndex) {
Chenjie Yu12e5e672018-09-14 15:54:59 -070056 mTagId = statsLogEventWrapper.getTagId();
57 mLogdTimestampNs = statsLogEventWrapper.getWallClockTimeNs();
58 mElapsedTimestampNs = statsLogEventWrapper.getElapsedRealTimeNs();
59 mLogUid = 0;
Chenjie Yud7e3a222018-11-28 21:29:44 +000060 int workChainPosOffset = 0;
61 if (workChainIndex != -1) {
62 const WorkChain& wc = statsLogEventWrapper.getWorkChains()[workChainIndex];
63 // chains are at field 1, level 2
64 int depth = 2;
65 for (int i = 0; i < (int)wc.uids.size(); i++) {
66 int pos[] = {1, i + 1, 1};
67 mValues.push_back(FieldValue(Field(mTagId, pos, depth), Value(wc.uids[i])));
68 pos[2]++;
69 mValues.push_back(FieldValue(Field(mTagId, pos, depth), Value(wc.tags[i])));
70 mValues.back().mField.decorateLastPos(2);
71 }
72 mValues.back().mField.decorateLastPos(1);
73 workChainPosOffset = 1;
74 }
Chenjie Yu12e5e672018-09-14 15:54:59 -070075 for (int i = 0; i < (int)statsLogEventWrapper.getElements().size(); i++) {
Chenjie Yud7e3a222018-11-28 21:29:44 +000076 Field field(statsLogEventWrapper.getTagId(), getSimpleField(i + 1 + workChainPosOffset));
Chenjie Yu12e5e672018-09-14 15:54:59 -070077 switch (statsLogEventWrapper.getElements()[i].type) {
78 case android::os::StatsLogValue::STATS_LOG_VALUE_TYPE::INT:
79 mValues.push_back(
80 FieldValue(field, Value(statsLogEventWrapper.getElements()[i].int_value)));
81 break;
82 case android::os::StatsLogValue::STATS_LOG_VALUE_TYPE::LONG:
83 mValues.push_back(
84 FieldValue(field, Value(statsLogEventWrapper.getElements()[i].long_value)));
85 break;
86 case android::os::StatsLogValue::STATS_LOG_VALUE_TYPE::FLOAT:
87 mValues.push_back(FieldValue(
88 field, Value(statsLogEventWrapper.getElements()[i].float_value)));
89 break;
90 case android::os::StatsLogValue::STATS_LOG_VALUE_TYPE::DOUBLE:
91 mValues.push_back(FieldValue(
92 field, Value(statsLogEventWrapper.getElements()[i].double_value)));
93 break;
94 case android::os::StatsLogValue::STATS_LOG_VALUE_TYPE::STRING:
95 mValues.push_back(
96 FieldValue(field, Value(statsLogEventWrapper.getElements()[i].str_value)));
97 break;
98 case android::os::StatsLogValue::STATS_LOG_VALUE_TYPE::STORAGE:
99 mValues.push_back(FieldValue(
100 field, Value(statsLogEventWrapper.getElements()[i].storage_value)));
101 break;
102 default:
103 break;
104 }
105 }
106}
107
Chenjie Yud7e3a222018-11-28 21:29:44 +0000108void LogEvent::createLogEvents(const StatsLogEventWrapper& statsLogEventWrapper,
109 std::vector<std::shared_ptr<LogEvent>>& logEvents) {
110 if (statsLogEventWrapper.getWorkChains().size() == 0) {
111 logEvents.push_back(std::make_shared<LogEvent>(statsLogEventWrapper, -1));
112 } else {
113 for (size_t i = 0; i < statsLogEventWrapper.getWorkChains().size(); i++) {
114 logEvents.push_back(std::make_shared<LogEvent>(statsLogEventWrapper, i));
115 }
116 }
117}
118
Yangster-mac330af582018-02-08 15:24:38 -0800119LogEvent::LogEvent(int32_t tagId, int64_t wallClockTimestampNs, int64_t elapsedTimestampNs) {
120 mLogdTimestampNs = wallClockTimestampNs;
Yao Chen80235402017-11-13 20:42:25 -0800121 mTagId = tagId;
Yangster-mac20877162017-12-22 17:19:39 -0800122 mLogUid = 0;
Yao Chen80235402017-11-13 20:42:25 -0800123 mContext = create_android_logger(1937006964); // the event tag shared by all stats logs
124 if (mContext) {
Yangster-mac330af582018-02-08 15:24:38 -0800125 android_log_write_int64(mContext, elapsedTimestampNs);
126 android_log_write_int32(mContext, tagId);
127 }
128}
129
Yangster-mac48b3d622018-08-18 12:38:11 -0700130LogEvent::LogEvent(int32_t tagId, int64_t wallClockTimestampNs, int64_t elapsedTimestampNs,
131 int32_t uid,
Howard Ro4078dd42018-09-27 17:41:08 -0700132 const std::map<int32_t, int32_t>& int_map,
133 const std::map<int32_t, int64_t>& long_map,
Yangster-mac48b3d622018-08-18 12:38:11 -0700134 const std::map<int32_t, std::string>& string_map,
135 const std::map<int32_t, float>& float_map) {
136 mLogdTimestampNs = wallClockTimestampNs;
137 mElapsedTimestampNs = elapsedTimestampNs;
138 mTagId = android::util::KEY_VALUE_PAIRS_ATOM;
139 mLogUid = uid;
140
141 int pos[] = {1, 1, 1};
142
143 mValues.push_back(FieldValue(Field(mTagId, pos, 0 /* depth */), Value(uid)));
144 pos[0]++;
145 for (const auto&itr : int_map) {
146 pos[2] = 1;
147 mValues.push_back(FieldValue(Field(mTagId, pos, 2 /* depth */), Value(itr.first)));
148 pos[2] = 2;
149 mValues.push_back(FieldValue(Field(mTagId, pos, 2 /* depth */), Value(itr.second)));
150 mValues.back().mField.decorateLastPos(2);
151 pos[1]++;
152 }
153
Howard Ro4078dd42018-09-27 17:41:08 -0700154 for (const auto&itr : long_map) {
Yangster-mac48b3d622018-08-18 12:38:11 -0700155 pos[2] = 1;
156 mValues.push_back(FieldValue(Field(mTagId, pos, 2 /* depth */), Value(itr.first)));
157 pos[2] = 3;
158 mValues.push_back(FieldValue(Field(mTagId, pos, 2 /* depth */), Value(itr.second)));
159 mValues.back().mField.decorateLastPos(2);
160 pos[1]++;
161 }
162
Howard Ro4078dd42018-09-27 17:41:08 -0700163 for (const auto&itr : string_map) {
Yangster-mac48b3d622018-08-18 12:38:11 -0700164 pos[2] = 1;
165 mValues.push_back(FieldValue(Field(mTagId, pos, 2 /* depth */), Value(itr.first)));
166 pos[2] = 4;
167 mValues.push_back(FieldValue(Field(mTagId, pos, 2 /* depth */), Value(itr.second)));
168 mValues.back().mField.decorateLastPos(2);
169 pos[1]++;
170 }
Howard Ro4078dd42018-09-27 17:41:08 -0700171
172 for (const auto&itr : float_map) {
173 pos[2] = 1;
174 mValues.push_back(FieldValue(Field(mTagId, pos, 2 /* depth */), Value(itr.first)));
175 pos[2] = 5;
176 mValues.push_back(FieldValue(Field(mTagId, pos, 2 /* depth */), Value(itr.second)));
177 mValues.back().mField.decorateLastPos(2);
178 pos[1]++;
179 }
Yangster-mac48b3d622018-08-18 12:38:11 -0700180 if (!mValues.empty()) {
181 mValues.back().mField.decorateLastPos(1);
182 mValues.at(mValues.size() - 2).mField.decorateLastPos(1);
183 }
184}
185
Chenjie Yu6b1667c2019-01-18 10:09:33 -0800186LogEvent::LogEvent(const string& trainName, int64_t trainVersionCode, bool requiresStaging,
187 bool rollbackEnabled, bool requiresLowLatencyMonitor, int32_t state,
188 const std::vector<uint8_t>& experimentIds, int32_t userId) {
189 mLogdTimestampNs = getWallClockNs();
190 mElapsedTimestampNs = getElapsedRealtimeNs();
191 mTagId = android::util::BINARY_PUSH_STATE_CHANGED;
192 mLogUid = android::IPCThreadState::self()->getCallingUid();
193
194 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(1)), Value(trainName)));
195 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(2)), Value(trainVersionCode)));
196 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(3)), Value((int)requiresStaging)));
197 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(4)), Value((int)rollbackEnabled)));
198 mValues.push_back(
199 FieldValue(Field(mTagId, getSimpleField(5)), Value((int)requiresLowLatencyMonitor)));
200 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(6)), Value(state)));
201 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(7)), Value(experimentIds)));
202 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(8)), Value(userId)));
203}
204
Howard Roa46b6582018-09-18 16:45:02 -0700205LogEvent::LogEvent(int64_t wallClockTimestampNs, int64_t elapsedTimestampNs,
Maggie White58174da2019-01-18 15:23:35 -0800206 const VendorAtom& vendorAtom) {
207 mLogdTimestampNs = wallClockTimestampNs;
208 mElapsedTimestampNs = elapsedTimestampNs;
209 mTagId = vendorAtom.atomId;
Tej Singh8928ed72019-02-22 19:06:22 -0800210 mLogUid = AID_STATSD;
Maggie White58174da2019-01-18 15:23:35 -0800211
212 mValues.push_back(
213 FieldValue(Field(mTagId, getSimpleField(1)), Value(vendorAtom.reverseDomainName)));
214 for (int i = 0; i < (int)vendorAtom.values.size(); i++) {
215 switch (vendorAtom.values[i].getDiscriminator()) {
216 case VendorAtom::Value::hidl_discriminator::intValue:
217 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(i + 2)),
218 Value(vendorAtom.values[i].intValue())));
219 break;
220 case VendorAtom::Value::hidl_discriminator::longValue:
221 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(i + 2)),
222 Value(vendorAtom.values[i].longValue())));
223 break;
224 case VendorAtom::Value::hidl_discriminator::floatValue:
225 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(i + 2)),
226 Value(vendorAtom.values[i].floatValue())));
227 break;
228 case VendorAtom::Value::hidl_discriminator::stringValue:
229 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(i + 2)),
230 Value(vendorAtom.values[i].stringValue())));
231 break;
232 }
233 }
234}
235
Chenjie Yu97dbb202019-02-13 16:42:04 -0800236LogEvent::LogEvent(int64_t wallClockTimestampNs, int64_t elapsedTimestampNs,
237 const InstallTrainInfo& trainInfo) {
238 mLogdTimestampNs = wallClockTimestampNs;
239 mElapsedTimestampNs = elapsedTimestampNs;
240 mTagId = android::util::TRAIN_INFO;
Muhammad Qureshif4ca8242019-03-01 09:20:15 -0800241
Chenjie Yu97dbb202019-02-13 16:42:04 -0800242 mValues.push_back(
243 FieldValue(Field(mTagId, getSimpleField(1)), Value(trainInfo.trainVersionCode)));
244 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(2)), Value(trainInfo.experimentIds)));
Muhammad Qureshif4ca8242019-03-01 09:20:15 -0800245 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(3)), Value(trainInfo.trainName)));
246 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(4)), Value(trainInfo.status)));
Chenjie Yu97dbb202019-02-13 16:42:04 -0800247}
248
Howard Ro1a2a3992018-10-22 22:51:57 -0700249LogEvent::LogEvent(int32_t tagId, int64_t timestampNs) : LogEvent(tagId, timestampNs, 0) {}
250
251LogEvent::LogEvent(int32_t tagId, int64_t timestampNs, int32_t uid) {
Yangster-mac330af582018-02-08 15:24:38 -0800252 mLogdTimestampNs = timestampNs;
253 mTagId = tagId;
Howard Ro1a2a3992018-10-22 22:51:57 -0700254 mLogUid = uid;
Yangster-mac330af582018-02-08 15:24:38 -0800255 mContext = create_android_logger(1937006964); // the event tag shared by all stats logs
256 if (mContext) {
257 android_log_write_int64(mContext, timestampNs);
Yao Chen80235402017-11-13 20:42:25 -0800258 android_log_write_int32(mContext, tagId);
259 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700260}
261
David Chen1481fe12017-10-16 13:16:34 -0700262void LogEvent::init() {
Yao Chen80235402017-11-13 20:42:25 -0800263 if (mContext) {
264 const char* buffer;
265 size_t len = android_log_write_list_buffer(mContext, &buffer);
266 // turns to reader mode
Chenjie Yuc1fe6f42018-02-01 23:14:18 -0800267 android_log_context contextForRead = create_android_log_parser(buffer, len);
268 if (contextForRead) {
269 init(contextForRead);
270 // destroy the context to save memory.
Yao Chen48d75182018-01-23 09:40:48 -0800271 // android_log_destroy will set mContext to NULL
Chenjie Yuc1fe6f42018-02-01 23:14:18 -0800272 android_log_destroy(&contextForRead);
Yao Chen48d75182018-01-23 09:40:48 -0800273 }
Chenjie Yuc1fe6f42018-02-01 23:14:18 -0800274 android_log_destroy(&mContext);
Yangster-mac20877162017-12-22 17:19:39 -0800275 }
276}
277
278LogEvent::~LogEvent() {
279 if (mContext) {
Yao Chen48d75182018-01-23 09:40:48 -0800280 // This is for the case when LogEvent is created using the test interface
281 // but init() isn't called.
Yangster-mac20877162017-12-22 17:19:39 -0800282 android_log_destroy(&mContext);
Yao Chen80235402017-11-13 20:42:25 -0800283 }
284}
285
286bool LogEvent::write(int32_t value) {
287 if (mContext) {
288 return android_log_write_int32(mContext, value) >= 0;
289 }
290 return false;
291}
292
293bool LogEvent::write(uint32_t value) {
294 if (mContext) {
295 return android_log_write_int32(mContext, value) >= 0;
296 }
297 return false;
298}
299
Chenjie Yud9dfda72017-12-11 17:41:20 -0800300bool LogEvent::write(int64_t value) {
301 if (mContext) {
302 return android_log_write_int64(mContext, value) >= 0;
303 }
304 return false;
305}
306
Yao Chen80235402017-11-13 20:42:25 -0800307bool LogEvent::write(uint64_t value) {
308 if (mContext) {
309 return android_log_write_int64(mContext, value) >= 0;
310 }
311 return false;
312}
313
314bool LogEvent::write(const string& value) {
315 if (mContext) {
316 return android_log_write_string8_len(mContext, value.c_str(), value.length()) >= 0;
317 }
318 return false;
319}
320
321bool LogEvent::write(float value) {
322 if (mContext) {
323 return android_log_write_float32(mContext, value) >= 0;
324 }
325 return false;
326}
327
Howard Ro1a2a3992018-10-22 22:51:57 -0700328bool LogEvent::writeKeyValuePairs(int32_t uid,
329 const std::map<int32_t, int32_t>& int_map,
Howard Ro4078dd42018-09-27 17:41:08 -0700330 const std::map<int32_t, int64_t>& long_map,
Yangster-mace124e422018-08-16 10:30:28 -0700331 const std::map<int32_t, std::string>& string_map,
332 const std::map<int32_t, float>& float_map) {
333 if (mContext) {
334 if (android_log_write_list_begin(mContext) < 0) {
335 return false;
336 }
Howard Ro1a2a3992018-10-22 22:51:57 -0700337 write(uid);
Yangster-mace124e422018-08-16 10:30:28 -0700338 for (const auto& itr : int_map) {
339 if (android_log_write_list_begin(mContext) < 0) {
340 return false;
341 }
342 write(itr.first);
343 write(itr.second);
344 if (android_log_write_list_end(mContext) < 0) {
345 return false;
346 }
347 }
348
Howard Ro4078dd42018-09-27 17:41:08 -0700349 for (const auto& itr : long_map) {
350 if (android_log_write_list_begin(mContext) < 0) {
351 return false;
352 }
353 write(itr.first);
354 write(itr.second);
355 if (android_log_write_list_end(mContext) < 0) {
356 return false;
357 }
358 }
359
Yangster-mace124e422018-08-16 10:30:28 -0700360 for (const auto& itr : string_map) {
361 if (android_log_write_list_begin(mContext) < 0) {
362 return false;
363 }
364 write(itr.first);
365 write(itr.second.c_str());
366 if (android_log_write_list_end(mContext) < 0) {
367 return false;
368 }
369 }
370
371 for (const auto& itr : float_map) {
372 if (android_log_write_list_begin(mContext) < 0) {
373 return false;
374 }
375 write(itr.first);
376 write(itr.second);
377 if (android_log_write_list_end(mContext) < 0) {
378 return false;
379 }
380 }
381
382 if (android_log_write_list_end(mContext) < 0) {
383 return false;
384 }
385 return true;
386 }
387 return false;
388}
389
Yao Chen9c1debe2018-02-19 14:39:19 -0800390bool LogEvent::write(const std::vector<AttributionNodeInternal>& nodes) {
Yao Chen80235402017-11-13 20:42:25 -0800391 if (mContext) {
Yangster-mac20877162017-12-22 17:19:39 -0800392 if (android_log_write_list_begin(mContext) < 0) {
393 return false;
394 }
395 for (size_t i = 0; i < nodes.size(); ++i) {
396 if (!write(nodes[i])) {
397 return false;
398 }
399 }
400 if (android_log_write_list_end(mContext) < 0) {
401 return false;
402 }
403 return true;
404 }
405 return false;
406}
407
Yao Chen9c1debe2018-02-19 14:39:19 -0800408bool LogEvent::write(const AttributionNodeInternal& node) {
Yangster-mac20877162017-12-22 17:19:39 -0800409 if (mContext) {
410 if (android_log_write_list_begin(mContext) < 0) {
411 return false;
412 }
413 if (android_log_write_int32(mContext, node.uid()) < 0) {
414 return false;
415 }
416 if (android_log_write_string8(mContext, node.tag().c_str()) < 0) {
417 return false;
418 }
Yangster-mac20877162017-12-22 17:19:39 -0800419 if (android_log_write_list_end(mContext) < 0) {
420 return false;
421 }
422 return true;
423 }
424 return false;
425}
426
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700427/**
428 * The elements of each log event are stored as a vector of android_log_list_elements.
429 * The goal is to do as little preprocessing as possible, because we read a tiny fraction
430 * of the elements that are written to the log.
Yao Chen8a8d16c2018-02-08 14:50:40 -0800431 *
432 * The idea here is to read through the log items once, we get as much information we need for
433 * matching as possible. Because this log will be matched against lots of matchers.
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700434 */
Yao Chen80235402017-11-13 20:42:25 -0800435void LogEvent::init(android_log_context context) {
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700436 android_log_list_element elem;
Yao Chen80235402017-11-13 20:42:25 -0800437 int i = 0;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800438 int depth = -1;
439 int pos[] = {1, 1, 1};
Yangster-mace124e422018-08-16 10:30:28 -0700440 bool isKeyValuePairAtom = false;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700441 do {
Yao Chen80235402017-11-13 20:42:25 -0800442 elem = android_log_read_next(context);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700443 switch ((int)elem.type) {
444 case EVENT_TYPE_INT:
Yangster-mac330af582018-02-08 15:24:38 -0800445 // elem at [0] is EVENT_TYPE_LIST, [1] is the timestamp, [2] is tag id.
446 if (i == 2) {
Yao Chen80235402017-11-13 20:42:25 -0800447 mTagId = elem.data.int32;
Yangster-mace124e422018-08-16 10:30:28 -0700448 isKeyValuePairAtom = (mTagId == android::util::KEY_VALUE_PAIRS_ATOM);
Yangster-mac20877162017-12-22 17:19:39 -0800449 } else {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800450 if (depth < 0 || depth > 2) {
Yangster-mac20877162017-12-22 17:19:39 -0800451 return;
452 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800453
454 mValues.push_back(
455 FieldValue(Field(mTagId, pos, depth), Value((int32_t)elem.data.int32)));
456
457 pos[depth]++;
Yangster-mac20877162017-12-22 17:19:39 -0800458 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700459 break;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800460 case EVENT_TYPE_FLOAT: {
461 if (depth < 0 || depth > 2) {
462 ALOGE("Depth > 2. Not supported!");
463 return;
464 }
465
Yangster-mace124e422018-08-16 10:30:28 -0700466 // Handles the oneof field in KeyValuePair atom.
467 if (isKeyValuePairAtom && depth == 2) {
Tej Singh2c96b5a2019-02-04 21:28:49 -0800468 pos[depth] = 5;
Yangster-mace124e422018-08-16 10:30:28 -0700469 }
470
Yao Chen8a8d16c2018-02-08 14:50:40 -0800471 mValues.push_back(FieldValue(Field(mTagId, pos, depth), Value(elem.data.float32)));
472
473 pos[depth]++;
474
475 } break;
476 case EVENT_TYPE_STRING: {
477 if (depth < 0 || depth > 2) {
478 ALOGE("Depth > 2. Not supported!");
479 return;
480 }
481
Yangster-mace124e422018-08-16 10:30:28 -0700482 // Handles the oneof field in KeyValuePair atom.
483 if (isKeyValuePairAtom && depth == 2) {
Tej Singh2c96b5a2019-02-04 21:28:49 -0800484 pos[depth] = 4;
Yangster-mace124e422018-08-16 10:30:28 -0700485 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800486 mValues.push_back(FieldValue(Field(mTagId, pos, depth),
487 Value(string(elem.data.string, elem.len))));
488
489 pos[depth]++;
490
491 } break;
492 case EVENT_TYPE_LONG: {
Yangster-mac330af582018-02-08 15:24:38 -0800493 if (i == 1) {
494 mElapsedTimestampNs = elem.data.int64;
495 } else {
496 if (depth < 0 || depth > 2) {
497 ALOGE("Depth > 2. Not supported!");
498 return;
499 }
Yangster-mace124e422018-08-16 10:30:28 -0700500 // Handles the oneof field in KeyValuePair atom.
501 if (isKeyValuePairAtom && depth == 2) {
Tej Singh2c96b5a2019-02-04 21:28:49 -0800502 pos[depth] = 3;
Yangster-mace124e422018-08-16 10:30:28 -0700503 }
Yangster-mac330af582018-02-08 15:24:38 -0800504 mValues.push_back(
505 FieldValue(Field(mTagId, pos, depth), Value((int64_t)elem.data.int64)));
506
507 pos[depth]++;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800508 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800509 } break;
510 case EVENT_TYPE_LIST:
511 depth++;
512 if (depth > 2) {
513 ALOGE("Depth > 2. Not supported!");
514 return;
515 }
516 pos[depth] = 1;
517
518 break;
519 case EVENT_TYPE_LIST_STOP: {
520 int prevDepth = depth;
521 depth--;
522 if (depth >= 0 && depth < 2) {
523 // Now go back to decorate the previous items that are last at prevDepth.
524 // So that we can later easily match them with Position=Last matchers.
525 pos[prevDepth]--;
526 int path = getEncodedField(pos, prevDepth, false);
Yao Chendb43afc2018-02-13 09:37:27 -0800527 for (auto it = mValues.rbegin(); it != mValues.rend(); ++it) {
528 if (it->mField.getDepth() >= prevDepth &&
529 it->mField.getPath(prevDepth) == path) {
530 it->mField.decorateLastPos(prevDepth);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800531 } else {
532 // Safe to break, because the items are in DFS order.
533 break;
534 }
Yangster-mac20877162017-12-22 17:19:39 -0800535 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800536 pos[depth]++;
Yangster-mac20877162017-12-22 17:19:39 -0800537 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700538 break;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800539 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700540 case EVENT_TYPE_UNKNOWN:
541 break;
542 default:
543 break;
544 }
Yao Chen80235402017-11-13 20:42:25 -0800545 i++;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700546 } while ((elem.type != EVENT_TYPE_UNKNOWN) && !elem.complete);
Howard Ro1a2a3992018-10-22 22:51:57 -0700547 if (isKeyValuePairAtom && mValues.size() > 0) {
548 mValues[0] = FieldValue(Field(android::util::KEY_VALUE_PAIRS_ATOM, getSimpleField(1)),
549 Value((int32_t)mLogUid));
550 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700551}
552
553int64_t LogEvent::GetLong(size_t key, status_t* err) const {
Yao Chen5bfffb52018-06-21 16:58:51 -0700554 // TODO(b/110561208): encapsulate the magical operations in Field struct as static functions
Yao Chen8a8d16c2018-02-08 14:50:40 -0800555 int field = getSimpleField(key);
556 for (const auto& value : mValues) {
557 if (value.mField.getField() == field) {
Yao Chenab92a1f2018-02-13 15:17:55 -0800558 if (value.mValue.getType() == LONG) {
559 return value.mValue.long_value;
560 } else if (value.mValue.getType() == INT) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800561 return value.mValue.int_value;
562 } else {
563 *err = BAD_TYPE;
564 return 0;
565 }
566 }
567 if ((size_t)value.mField.getPosAtDepth(0) > key) {
568 break;
Yangster-mac20877162017-12-22 17:19:39 -0800569 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700570 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800571
572 *err = BAD_INDEX;
573 return 0;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700574}
575
Chenjie Yu80f91122018-01-31 20:24:50 -0800576int LogEvent::GetInt(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800577 int field = getSimpleField(key);
578 for (const auto& value : mValues) {
579 if (value.mField.getField() == field) {
580 if (value.mValue.getType() == INT) {
581 return value.mValue.int_value;
582 } else {
583 *err = BAD_TYPE;
584 return 0;
585 }
586 }
587 if ((size_t)value.mField.getPosAtDepth(0) > key) {
588 break;
589 }
590 }
591
Chenjie Yu80f91122018-01-31 20:24:50 -0800592 *err = BAD_INDEX;
593 return 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800594}
595
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700596const char* LogEvent::GetString(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800597 int field = getSimpleField(key);
598 for (const auto& value : mValues) {
599 if (value.mField.getField() == field) {
600 if (value.mValue.getType() == STRING) {
601 return value.mValue.str_value.c_str();
602 } else {
603 *err = BAD_TYPE;
604 return 0;
605 }
606 }
607 if ((size_t)value.mField.getPosAtDepth(0) > key) {
608 break;
Yangster-mac20877162017-12-22 17:19:39 -0800609 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700610 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800611
612 *err = BAD_INDEX;
613 return NULL;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700614}
615
616bool LogEvent::GetBool(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800617 int field = getSimpleField(key);
618 for (const auto& value : mValues) {
619 if (value.mField.getField() == field) {
620 if (value.mValue.getType() == INT) {
621 return value.mValue.int_value != 0;
622 } else if (value.mValue.getType() == LONG) {
623 return value.mValue.long_value != 0;
624 } else {
625 *err = BAD_TYPE;
626 return false;
627 }
628 }
629 if ((size_t)value.mField.getPosAtDepth(0) > key) {
630 break;
Yangster-mac20877162017-12-22 17:19:39 -0800631 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700632 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800633
634 *err = BAD_INDEX;
635 return false;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700636}
637
638float LogEvent::GetFloat(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800639 int field = getSimpleField(key);
640 for (const auto& value : mValues) {
641 if (value.mField.getField() == field) {
642 if (value.mValue.getType() == FLOAT) {
643 return value.mValue.float_value;
644 } else {
645 *err = BAD_TYPE;
646 return 0.0;
647 }
648 }
649 if ((size_t)value.mField.getPosAtDepth(0) > key) {
650 break;
Yangster-mac20877162017-12-22 17:19:39 -0800651 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700652 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700653
Yao Chen8a8d16c2018-02-08 14:50:40 -0800654 *err = BAD_INDEX;
655 return 0.0;
Yangster-macd40053e2018-01-09 16:29:22 -0800656}
657
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700658string LogEvent::ToString() const {
Yao Chen20e9e622018-02-28 11:18:51 -0800659 string result;
Yao Chen8e6f9982018-11-29 09:39:45 -0800660 result += StringPrintf("{ uid(%d) %lld %lld (%d)", mLogUid, (long long)mLogdTimestampNs,
Yao Chen20e9e622018-02-28 11:18:51 -0800661 (long long)mElapsedTimestampNs, mTagId);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800662 for (const auto& value : mValues) {
Yao Chen20e9e622018-02-28 11:18:51 -0800663 result +=
664 StringPrintf("%#x", value.mField.getField()) + "->" + value.mValue.toString() + " ";
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700665 }
Yao Chen20e9e622018-02-28 11:18:51 -0800666 result += " }";
667 return result;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700668}
669
Yangster-mac20877162017-12-22 17:19:39 -0800670void LogEvent::ToProto(ProtoOutputStream& protoOutput) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800671 writeFieldValueTreeToStream(mTagId, getValues(), &protoOutput);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700672}
673
674} // namespace statsd
675} // namespace os
676} // namespace android