blob: d5e66fdf9790465f8c48ca104c1a076b4d7841cc [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 Chen0f861862019-03-27 11:51:15 -0700121 mElapsedTimestampNs = elapsedTimestampNs;
Yao Chen80235402017-11-13 20:42:25 -0800122 mTagId = tagId;
Yangster-mac20877162017-12-22 17:19:39 -0800123 mLogUid = 0;
Yao Chen80235402017-11-13 20:42:25 -0800124 mContext = create_android_logger(1937006964); // the event tag shared by all stats logs
125 if (mContext) {
Yangster-mac330af582018-02-08 15:24:38 -0800126 android_log_write_int64(mContext, elapsedTimestampNs);
127 android_log_write_int32(mContext, tagId);
128 }
129}
130
Yangster-mac48b3d622018-08-18 12:38:11 -0700131LogEvent::LogEvent(int32_t tagId, int64_t wallClockTimestampNs, int64_t elapsedTimestampNs,
132 int32_t uid,
Howard Ro4078dd42018-09-27 17:41:08 -0700133 const std::map<int32_t, int32_t>& int_map,
134 const std::map<int32_t, int64_t>& long_map,
Yangster-mac48b3d622018-08-18 12:38:11 -0700135 const std::map<int32_t, std::string>& string_map,
136 const std::map<int32_t, float>& float_map) {
137 mLogdTimestampNs = wallClockTimestampNs;
138 mElapsedTimestampNs = elapsedTimestampNs;
139 mTagId = android::util::KEY_VALUE_PAIRS_ATOM;
140 mLogUid = uid;
141
142 int pos[] = {1, 1, 1};
143
144 mValues.push_back(FieldValue(Field(mTagId, pos, 0 /* depth */), Value(uid)));
145 pos[0]++;
146 for (const auto&itr : int_map) {
147 pos[2] = 1;
148 mValues.push_back(FieldValue(Field(mTagId, pos, 2 /* depth */), Value(itr.first)));
149 pos[2] = 2;
150 mValues.push_back(FieldValue(Field(mTagId, pos, 2 /* depth */), Value(itr.second)));
151 mValues.back().mField.decorateLastPos(2);
152 pos[1]++;
153 }
154
Howard Ro4078dd42018-09-27 17:41:08 -0700155 for (const auto&itr : long_map) {
Yangster-mac48b3d622018-08-18 12:38:11 -0700156 pos[2] = 1;
157 mValues.push_back(FieldValue(Field(mTagId, pos, 2 /* depth */), Value(itr.first)));
158 pos[2] = 3;
159 mValues.push_back(FieldValue(Field(mTagId, pos, 2 /* depth */), Value(itr.second)));
160 mValues.back().mField.decorateLastPos(2);
161 pos[1]++;
162 }
163
Howard Ro4078dd42018-09-27 17:41:08 -0700164 for (const auto&itr : string_map) {
Yangster-mac48b3d622018-08-18 12:38:11 -0700165 pos[2] = 1;
166 mValues.push_back(FieldValue(Field(mTagId, pos, 2 /* depth */), Value(itr.first)));
167 pos[2] = 4;
168 mValues.push_back(FieldValue(Field(mTagId, pos, 2 /* depth */), Value(itr.second)));
169 mValues.back().mField.decorateLastPos(2);
170 pos[1]++;
171 }
Howard Ro4078dd42018-09-27 17:41:08 -0700172
173 for (const auto&itr : float_map) {
174 pos[2] = 1;
175 mValues.push_back(FieldValue(Field(mTagId, pos, 2 /* depth */), Value(itr.first)));
176 pos[2] = 5;
177 mValues.push_back(FieldValue(Field(mTagId, pos, 2 /* depth */), Value(itr.second)));
178 mValues.back().mField.decorateLastPos(2);
179 pos[1]++;
180 }
Yangster-mac48b3d622018-08-18 12:38:11 -0700181 if (!mValues.empty()) {
182 mValues.back().mField.decorateLastPos(1);
183 mValues.at(mValues.size() - 2).mField.decorateLastPos(1);
184 }
185}
186
Chenjie Yu6b1667c2019-01-18 10:09:33 -0800187LogEvent::LogEvent(const string& trainName, int64_t trainVersionCode, bool requiresStaging,
188 bool rollbackEnabled, bool requiresLowLatencyMonitor, int32_t state,
189 const std::vector<uint8_t>& experimentIds, int32_t userId) {
190 mLogdTimestampNs = getWallClockNs();
191 mElapsedTimestampNs = getElapsedRealtimeNs();
192 mTagId = android::util::BINARY_PUSH_STATE_CHANGED;
193 mLogUid = android::IPCThreadState::self()->getCallingUid();
194
195 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(1)), Value(trainName)));
196 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(2)), Value(trainVersionCode)));
197 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(3)), Value((int)requiresStaging)));
198 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(4)), Value((int)rollbackEnabled)));
199 mValues.push_back(
200 FieldValue(Field(mTagId, getSimpleField(5)), Value((int)requiresLowLatencyMonitor)));
201 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(6)), Value(state)));
202 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(7)), Value(experimentIds)));
203 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(8)), Value(userId)));
204}
205
Howard Roa46b6582018-09-18 16:45:02 -0700206LogEvent::LogEvent(int64_t wallClockTimestampNs, int64_t elapsedTimestampNs,
Maggie White58174da2019-01-18 15:23:35 -0800207 const VendorAtom& vendorAtom) {
208 mLogdTimestampNs = wallClockTimestampNs;
209 mElapsedTimestampNs = elapsedTimestampNs;
210 mTagId = vendorAtom.atomId;
Tej Singh8928ed72019-02-22 19:06:22 -0800211 mLogUid = AID_STATSD;
Maggie White58174da2019-01-18 15:23:35 -0800212
213 mValues.push_back(
214 FieldValue(Field(mTagId, getSimpleField(1)), Value(vendorAtom.reverseDomainName)));
215 for (int i = 0; i < (int)vendorAtom.values.size(); i++) {
216 switch (vendorAtom.values[i].getDiscriminator()) {
217 case VendorAtom::Value::hidl_discriminator::intValue:
218 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(i + 2)),
219 Value(vendorAtom.values[i].intValue())));
220 break;
221 case VendorAtom::Value::hidl_discriminator::longValue:
222 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(i + 2)),
223 Value(vendorAtom.values[i].longValue())));
224 break;
225 case VendorAtom::Value::hidl_discriminator::floatValue:
226 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(i + 2)),
227 Value(vendorAtom.values[i].floatValue())));
228 break;
229 case VendorAtom::Value::hidl_discriminator::stringValue:
230 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(i + 2)),
231 Value(vendorAtom.values[i].stringValue())));
232 break;
233 }
234 }
235}
236
Chenjie Yu97dbb202019-02-13 16:42:04 -0800237LogEvent::LogEvent(int64_t wallClockTimestampNs, int64_t elapsedTimestampNs,
238 const InstallTrainInfo& trainInfo) {
239 mLogdTimestampNs = wallClockTimestampNs;
240 mElapsedTimestampNs = elapsedTimestampNs;
241 mTagId = android::util::TRAIN_INFO;
Muhammad Qureshif4ca8242019-03-01 09:20:15 -0800242
Chenjie Yu97dbb202019-02-13 16:42:04 -0800243 mValues.push_back(
244 FieldValue(Field(mTagId, getSimpleField(1)), Value(trainInfo.trainVersionCode)));
245 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(2)), Value(trainInfo.experimentIds)));
Muhammad Qureshif4ca8242019-03-01 09:20:15 -0800246 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(3)), Value(trainInfo.trainName)));
247 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(4)), Value(trainInfo.status)));
Chenjie Yu97dbb202019-02-13 16:42:04 -0800248}
249
Yao Chen0f861862019-03-27 11:51:15 -0700250LogEvent::LogEvent(int32_t tagId, int64_t timestampNs) : LogEvent(tagId, timestampNs, timestampNs) {
251}
Howard Ro1a2a3992018-10-22 22:51:57 -0700252
253LogEvent::LogEvent(int32_t tagId, int64_t timestampNs, int32_t uid) {
Yangster-mac330af582018-02-08 15:24:38 -0800254 mLogdTimestampNs = timestampNs;
255 mTagId = tagId;
Howard Ro1a2a3992018-10-22 22:51:57 -0700256 mLogUid = uid;
Yangster-mac330af582018-02-08 15:24:38 -0800257 mContext = create_android_logger(1937006964); // the event tag shared by all stats logs
258 if (mContext) {
259 android_log_write_int64(mContext, timestampNs);
Yao Chen80235402017-11-13 20:42:25 -0800260 android_log_write_int32(mContext, tagId);
261 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700262}
263
David Chen1481fe12017-10-16 13:16:34 -0700264void LogEvent::init() {
Yao Chen80235402017-11-13 20:42:25 -0800265 if (mContext) {
266 const char* buffer;
267 size_t len = android_log_write_list_buffer(mContext, &buffer);
268 // turns to reader mode
Chenjie Yuc1fe6f42018-02-01 23:14:18 -0800269 android_log_context contextForRead = create_android_log_parser(buffer, len);
270 if (contextForRead) {
271 init(contextForRead);
272 // destroy the context to save memory.
Yao Chen48d75182018-01-23 09:40:48 -0800273 // android_log_destroy will set mContext to NULL
Chenjie Yuc1fe6f42018-02-01 23:14:18 -0800274 android_log_destroy(&contextForRead);
Yao Chen48d75182018-01-23 09:40:48 -0800275 }
Chenjie Yuc1fe6f42018-02-01 23:14:18 -0800276 android_log_destroy(&mContext);
Yangster-mac20877162017-12-22 17:19:39 -0800277 }
278}
279
280LogEvent::~LogEvent() {
281 if (mContext) {
Yao Chen48d75182018-01-23 09:40:48 -0800282 // This is for the case when LogEvent is created using the test interface
283 // but init() isn't called.
Yangster-mac20877162017-12-22 17:19:39 -0800284 android_log_destroy(&mContext);
Yao Chen80235402017-11-13 20:42:25 -0800285 }
286}
287
288bool LogEvent::write(int32_t value) {
289 if (mContext) {
290 return android_log_write_int32(mContext, value) >= 0;
291 }
292 return false;
293}
294
295bool LogEvent::write(uint32_t value) {
296 if (mContext) {
297 return android_log_write_int32(mContext, value) >= 0;
298 }
299 return false;
300}
301
Chenjie Yud9dfda72017-12-11 17:41:20 -0800302bool LogEvent::write(int64_t value) {
303 if (mContext) {
304 return android_log_write_int64(mContext, value) >= 0;
305 }
306 return false;
307}
308
Yao Chen80235402017-11-13 20:42:25 -0800309bool LogEvent::write(uint64_t value) {
310 if (mContext) {
311 return android_log_write_int64(mContext, value) >= 0;
312 }
313 return false;
314}
315
316bool LogEvent::write(const string& value) {
317 if (mContext) {
318 return android_log_write_string8_len(mContext, value.c_str(), value.length()) >= 0;
319 }
320 return false;
321}
322
323bool LogEvent::write(float value) {
324 if (mContext) {
325 return android_log_write_float32(mContext, value) >= 0;
326 }
327 return false;
328}
329
Howard Ro1a2a3992018-10-22 22:51:57 -0700330bool LogEvent::writeKeyValuePairs(int32_t uid,
331 const std::map<int32_t, int32_t>& int_map,
Howard Ro4078dd42018-09-27 17:41:08 -0700332 const std::map<int32_t, int64_t>& long_map,
Yangster-mace124e422018-08-16 10:30:28 -0700333 const std::map<int32_t, std::string>& string_map,
334 const std::map<int32_t, float>& float_map) {
335 if (mContext) {
336 if (android_log_write_list_begin(mContext) < 0) {
337 return false;
338 }
Howard Ro1a2a3992018-10-22 22:51:57 -0700339 write(uid);
Yangster-mace124e422018-08-16 10:30:28 -0700340 for (const auto& itr : int_map) {
341 if (android_log_write_list_begin(mContext) < 0) {
342 return false;
343 }
344 write(itr.first);
345 write(itr.second);
346 if (android_log_write_list_end(mContext) < 0) {
347 return false;
348 }
349 }
350
Howard Ro4078dd42018-09-27 17:41:08 -0700351 for (const auto& itr : long_map) {
352 if (android_log_write_list_begin(mContext) < 0) {
353 return false;
354 }
355 write(itr.first);
356 write(itr.second);
357 if (android_log_write_list_end(mContext) < 0) {
358 return false;
359 }
360 }
361
Yangster-mace124e422018-08-16 10:30:28 -0700362 for (const auto& itr : string_map) {
363 if (android_log_write_list_begin(mContext) < 0) {
364 return false;
365 }
366 write(itr.first);
367 write(itr.second.c_str());
368 if (android_log_write_list_end(mContext) < 0) {
369 return false;
370 }
371 }
372
373 for (const auto& itr : float_map) {
374 if (android_log_write_list_begin(mContext) < 0) {
375 return false;
376 }
377 write(itr.first);
378 write(itr.second);
379 if (android_log_write_list_end(mContext) < 0) {
380 return false;
381 }
382 }
383
384 if (android_log_write_list_end(mContext) < 0) {
385 return false;
386 }
387 return true;
388 }
389 return false;
390}
391
Yao Chen9c1debe2018-02-19 14:39:19 -0800392bool LogEvent::write(const std::vector<AttributionNodeInternal>& nodes) {
Yao Chen80235402017-11-13 20:42:25 -0800393 if (mContext) {
Yangster-mac20877162017-12-22 17:19:39 -0800394 if (android_log_write_list_begin(mContext) < 0) {
395 return false;
396 }
397 for (size_t i = 0; i < nodes.size(); ++i) {
398 if (!write(nodes[i])) {
399 return false;
400 }
401 }
402 if (android_log_write_list_end(mContext) < 0) {
403 return false;
404 }
405 return true;
406 }
407 return false;
408}
409
Yao Chen9c1debe2018-02-19 14:39:19 -0800410bool LogEvent::write(const AttributionNodeInternal& node) {
Yangster-mac20877162017-12-22 17:19:39 -0800411 if (mContext) {
412 if (android_log_write_list_begin(mContext) < 0) {
413 return false;
414 }
415 if (android_log_write_int32(mContext, node.uid()) < 0) {
416 return false;
417 }
418 if (android_log_write_string8(mContext, node.tag().c_str()) < 0) {
419 return false;
420 }
Yangster-mac20877162017-12-22 17:19:39 -0800421 if (android_log_write_list_end(mContext) < 0) {
422 return false;
423 }
424 return true;
425 }
426 return false;
427}
428
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700429/**
430 * The elements of each log event are stored as a vector of android_log_list_elements.
431 * The goal is to do as little preprocessing as possible, because we read a tiny fraction
432 * of the elements that are written to the log.
Yao Chen8a8d16c2018-02-08 14:50:40 -0800433 *
434 * The idea here is to read through the log items once, we get as much information we need for
435 * matching as possible. Because this log will be matched against lots of matchers.
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700436 */
Yao Chen80235402017-11-13 20:42:25 -0800437void LogEvent::init(android_log_context context) {
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700438 android_log_list_element elem;
Yao Chen80235402017-11-13 20:42:25 -0800439 int i = 0;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800440 int depth = -1;
441 int pos[] = {1, 1, 1};
Yangster-mace124e422018-08-16 10:30:28 -0700442 bool isKeyValuePairAtom = false;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700443 do {
Yao Chen80235402017-11-13 20:42:25 -0800444 elem = android_log_read_next(context);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700445 switch ((int)elem.type) {
446 case EVENT_TYPE_INT:
Yangster-mac330af582018-02-08 15:24:38 -0800447 // elem at [0] is EVENT_TYPE_LIST, [1] is the timestamp, [2] is tag id.
448 if (i == 2) {
Yao Chen80235402017-11-13 20:42:25 -0800449 mTagId = elem.data.int32;
Yangster-mace124e422018-08-16 10:30:28 -0700450 isKeyValuePairAtom = (mTagId == android::util::KEY_VALUE_PAIRS_ATOM);
Yangster-mac20877162017-12-22 17:19:39 -0800451 } else {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800452 if (depth < 0 || depth > 2) {
Yangster-mac20877162017-12-22 17:19:39 -0800453 return;
454 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800455
456 mValues.push_back(
457 FieldValue(Field(mTagId, pos, depth), Value((int32_t)elem.data.int32)));
458
459 pos[depth]++;
Yangster-mac20877162017-12-22 17:19:39 -0800460 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700461 break;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800462 case EVENT_TYPE_FLOAT: {
463 if (depth < 0 || depth > 2) {
464 ALOGE("Depth > 2. Not supported!");
465 return;
466 }
467
Yangster-mace124e422018-08-16 10:30:28 -0700468 // Handles the oneof field in KeyValuePair atom.
469 if (isKeyValuePairAtom && depth == 2) {
Tej Singh2c96b5a2019-02-04 21:28:49 -0800470 pos[depth] = 5;
Yangster-mace124e422018-08-16 10:30:28 -0700471 }
472
Yao Chen8a8d16c2018-02-08 14:50:40 -0800473 mValues.push_back(FieldValue(Field(mTagId, pos, depth), Value(elem.data.float32)));
474
475 pos[depth]++;
476
477 } break;
478 case EVENT_TYPE_STRING: {
479 if (depth < 0 || depth > 2) {
480 ALOGE("Depth > 2. Not supported!");
481 return;
482 }
483
Yangster-mace124e422018-08-16 10:30:28 -0700484 // Handles the oneof field in KeyValuePair atom.
485 if (isKeyValuePairAtom && depth == 2) {
Tej Singh2c96b5a2019-02-04 21:28:49 -0800486 pos[depth] = 4;
Yangster-mace124e422018-08-16 10:30:28 -0700487 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800488 mValues.push_back(FieldValue(Field(mTagId, pos, depth),
489 Value(string(elem.data.string, elem.len))));
490
491 pos[depth]++;
492
493 } break;
494 case EVENT_TYPE_LONG: {
Yangster-mac330af582018-02-08 15:24:38 -0800495 if (i == 1) {
496 mElapsedTimestampNs = elem.data.int64;
497 } else {
498 if (depth < 0 || depth > 2) {
499 ALOGE("Depth > 2. Not supported!");
500 return;
501 }
Yangster-mace124e422018-08-16 10:30:28 -0700502 // Handles the oneof field in KeyValuePair atom.
503 if (isKeyValuePairAtom && depth == 2) {
Tej Singh2c96b5a2019-02-04 21:28:49 -0800504 pos[depth] = 3;
Yangster-mace124e422018-08-16 10:30:28 -0700505 }
Yangster-mac330af582018-02-08 15:24:38 -0800506 mValues.push_back(
507 FieldValue(Field(mTagId, pos, depth), Value((int64_t)elem.data.int64)));
508
509 pos[depth]++;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800510 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800511 } break;
512 case EVENT_TYPE_LIST:
513 depth++;
514 if (depth > 2) {
515 ALOGE("Depth > 2. Not supported!");
516 return;
517 }
518 pos[depth] = 1;
519
520 break;
521 case EVENT_TYPE_LIST_STOP: {
522 int prevDepth = depth;
523 depth--;
524 if (depth >= 0 && depth < 2) {
525 // Now go back to decorate the previous items that are last at prevDepth.
526 // So that we can later easily match them with Position=Last matchers.
527 pos[prevDepth]--;
528 int path = getEncodedField(pos, prevDepth, false);
Yao Chendb43afc2018-02-13 09:37:27 -0800529 for (auto it = mValues.rbegin(); it != mValues.rend(); ++it) {
530 if (it->mField.getDepth() >= prevDepth &&
531 it->mField.getPath(prevDepth) == path) {
532 it->mField.decorateLastPos(prevDepth);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800533 } else {
534 // Safe to break, because the items are in DFS order.
535 break;
536 }
Yangster-mac20877162017-12-22 17:19:39 -0800537 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800538 pos[depth]++;
Yangster-mac20877162017-12-22 17:19:39 -0800539 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700540 break;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800541 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700542 case EVENT_TYPE_UNKNOWN:
543 break;
544 default:
545 break;
546 }
Yao Chen80235402017-11-13 20:42:25 -0800547 i++;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700548 } while ((elem.type != EVENT_TYPE_UNKNOWN) && !elem.complete);
Howard Ro1a2a3992018-10-22 22:51:57 -0700549 if (isKeyValuePairAtom && mValues.size() > 0) {
550 mValues[0] = FieldValue(Field(android::util::KEY_VALUE_PAIRS_ATOM, getSimpleField(1)),
551 Value((int32_t)mLogUid));
552 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700553}
554
555int64_t LogEvent::GetLong(size_t key, status_t* err) const {
Yao Chen5bfffb52018-06-21 16:58:51 -0700556 // TODO(b/110561208): encapsulate the magical operations in Field struct as static functions
Yao Chen8a8d16c2018-02-08 14:50:40 -0800557 int field = getSimpleField(key);
558 for (const auto& value : mValues) {
559 if (value.mField.getField() == field) {
Yao Chenab92a1f2018-02-13 15:17:55 -0800560 if (value.mValue.getType() == LONG) {
561 return value.mValue.long_value;
562 } else if (value.mValue.getType() == INT) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800563 return value.mValue.int_value;
564 } else {
565 *err = BAD_TYPE;
566 return 0;
567 }
568 }
569 if ((size_t)value.mField.getPosAtDepth(0) > key) {
570 break;
Yangster-mac20877162017-12-22 17:19:39 -0800571 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700572 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800573
574 *err = BAD_INDEX;
575 return 0;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700576}
577
Chenjie Yu80f91122018-01-31 20:24:50 -0800578int LogEvent::GetInt(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800579 int field = getSimpleField(key);
580 for (const auto& value : mValues) {
581 if (value.mField.getField() == field) {
582 if (value.mValue.getType() == INT) {
583 return value.mValue.int_value;
584 } else {
585 *err = BAD_TYPE;
586 return 0;
587 }
588 }
589 if ((size_t)value.mField.getPosAtDepth(0) > key) {
590 break;
591 }
592 }
593
Chenjie Yu80f91122018-01-31 20:24:50 -0800594 *err = BAD_INDEX;
595 return 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800596}
597
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700598const char* LogEvent::GetString(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800599 int field = getSimpleField(key);
600 for (const auto& value : mValues) {
601 if (value.mField.getField() == field) {
602 if (value.mValue.getType() == STRING) {
603 return value.mValue.str_value.c_str();
604 } else {
605 *err = BAD_TYPE;
606 return 0;
607 }
608 }
609 if ((size_t)value.mField.getPosAtDepth(0) > key) {
610 break;
Yangster-mac20877162017-12-22 17:19:39 -0800611 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700612 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800613
614 *err = BAD_INDEX;
615 return NULL;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700616}
617
618bool LogEvent::GetBool(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800619 int field = getSimpleField(key);
620 for (const auto& value : mValues) {
621 if (value.mField.getField() == field) {
622 if (value.mValue.getType() == INT) {
623 return value.mValue.int_value != 0;
624 } else if (value.mValue.getType() == LONG) {
625 return value.mValue.long_value != 0;
626 } else {
627 *err = BAD_TYPE;
628 return false;
629 }
630 }
631 if ((size_t)value.mField.getPosAtDepth(0) > key) {
632 break;
Yangster-mac20877162017-12-22 17:19:39 -0800633 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700634 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800635
636 *err = BAD_INDEX;
637 return false;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700638}
639
640float LogEvent::GetFloat(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800641 int field = getSimpleField(key);
642 for (const auto& value : mValues) {
643 if (value.mField.getField() == field) {
644 if (value.mValue.getType() == FLOAT) {
645 return value.mValue.float_value;
646 } else {
647 *err = BAD_TYPE;
648 return 0.0;
649 }
650 }
651 if ((size_t)value.mField.getPosAtDepth(0) > key) {
652 break;
Yangster-mac20877162017-12-22 17:19:39 -0800653 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700654 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700655
Yao Chen8a8d16c2018-02-08 14:50:40 -0800656 *err = BAD_INDEX;
657 return 0.0;
Yangster-macd40053e2018-01-09 16:29:22 -0800658}
659
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700660string LogEvent::ToString() const {
Yao Chen20e9e622018-02-28 11:18:51 -0800661 string result;
Yao Chen8e6f9982018-11-29 09:39:45 -0800662 result += StringPrintf("{ uid(%d) %lld %lld (%d)", mLogUid, (long long)mLogdTimestampNs,
Yao Chen20e9e622018-02-28 11:18:51 -0800663 (long long)mElapsedTimestampNs, mTagId);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800664 for (const auto& value : mValues) {
Yao Chen20e9e622018-02-28 11:18:51 -0800665 result +=
666 StringPrintf("%#x", value.mField.getField()) + "->" + value.mValue.toString() + " ";
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700667 }
Yao Chen20e9e622018-02-28 11:18:51 -0800668 result += " }";
669 return result;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700670}
671
Yangster-mac20877162017-12-22 17:19:39 -0800672void LogEvent::ToProto(ProtoOutputStream& protoOutput) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800673 writeFieldValueTreeToStream(mTagId, getValues(), &protoOutput);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700674}
675
676} // namespace statsd
677} // namespace os
678} // namespace android