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 #define DEBUG false  // STOPSHIP if true
18 #include "logd/LogEvent.h"
19 
20 #include "stats_log_util.h"
21 #include "statslog.h"
22 
23 #include <binder/IPCThreadState.h>
24 #include <private/android_filesystem_config.h>
25 
26 namespace android {
27 namespace os {
28 namespace statsd {
29 
30 // for TrainInfo experiment id serialization
31 const int FIELD_ID_EXPERIMENT_ID = 1;
32 
33 using namespace android::util;
34 using android::util::ProtoOutputStream;
35 using std::string;
36 using std::vector;
37 
LogEvent(log_msg & msg)38 LogEvent::LogEvent(log_msg& msg) {
39     mContext =
40             create_android_log_parser(msg.msg() + sizeof(uint32_t), msg.len() - sizeof(uint32_t));
41     mLogdTimestampNs = msg.entry.sec * NS_PER_SEC + msg.entry.nsec;
42     mLogUid = msg.entry.uid;
43     init(mContext);
44     if (mContext) {
45         // android_log_destroy will set mContext to NULL
46         android_log_destroy(&mContext);
47     }
48 }
49 
LogEvent(const LogEvent & event)50 LogEvent::LogEvent(const LogEvent& event) {
51     mTagId = event.mTagId;
52     mLogUid = event.mLogUid;
53     mElapsedTimestampNs = event.mElapsedTimestampNs;
54     mLogdTimestampNs = event.mLogdTimestampNs;
55     mValues = event.mValues;
56 }
57 
LogEvent(const StatsLogEventWrapper & statsLogEventWrapper,int workChainIndex)58 LogEvent::LogEvent(const StatsLogEventWrapper& statsLogEventWrapper, int workChainIndex) {
59     mTagId = statsLogEventWrapper.getTagId();
60     mLogdTimestampNs = statsLogEventWrapper.getWallClockTimeNs();
61     mElapsedTimestampNs = statsLogEventWrapper.getElapsedRealTimeNs();
62     mLogUid = 0;
63     int workChainPosOffset = 0;
64     if (workChainIndex != -1) {
65         const WorkChain& wc = statsLogEventWrapper.getWorkChains()[workChainIndex];
66         // chains are at field 1, level 2
67         int depth = 2;
68         for (int i = 0; i < (int)wc.uids.size(); i++) {
69             int pos[] = {1, i + 1, 1};
70             mValues.push_back(FieldValue(Field(mTagId, pos, depth), Value(wc.uids[i])));
71             pos[2]++;
72             mValues.push_back(FieldValue(Field(mTagId, pos, depth), Value(wc.tags[i])));
73             mValues.back().mField.decorateLastPos(2);
74         }
75         mValues.back().mField.decorateLastPos(1);
76         workChainPosOffset = 1;
77     }
78     for (int i = 0; i < (int)statsLogEventWrapper.getElements().size(); i++) {
79         Field field(statsLogEventWrapper.getTagId(), getSimpleField(i + 1 + workChainPosOffset));
80         switch (statsLogEventWrapper.getElements()[i].type) {
81             case android::os::StatsLogValue::STATS_LOG_VALUE_TYPE::INT:
82                 mValues.push_back(
83                         FieldValue(field, Value(statsLogEventWrapper.getElements()[i].int_value)));
84                 break;
85             case android::os::StatsLogValue::STATS_LOG_VALUE_TYPE::LONG:
86                 mValues.push_back(
87                         FieldValue(field, Value(statsLogEventWrapper.getElements()[i].long_value)));
88                 break;
89             case android::os::StatsLogValue::STATS_LOG_VALUE_TYPE::FLOAT:
90                 mValues.push_back(FieldValue(
91                         field, Value(statsLogEventWrapper.getElements()[i].float_value)));
92                 break;
93             case android::os::StatsLogValue::STATS_LOG_VALUE_TYPE::DOUBLE:
94                 mValues.push_back(FieldValue(
95                         field, Value(statsLogEventWrapper.getElements()[i].double_value)));
96                 break;
97             case android::os::StatsLogValue::STATS_LOG_VALUE_TYPE::STRING:
98                 mValues.push_back(
99                         FieldValue(field, Value(statsLogEventWrapper.getElements()[i].str_value)));
100                 break;
101             case android::os::StatsLogValue::STATS_LOG_VALUE_TYPE::STORAGE:
102                 mValues.push_back(FieldValue(
103                         field, Value(statsLogEventWrapper.getElements()[i].storage_value)));
104                 break;
105             default:
106                 break;
107         }
108     }
109 }
110 
createLogEvents(const StatsLogEventWrapper & statsLogEventWrapper,std::vector<std::shared_ptr<LogEvent>> & logEvents)111 void LogEvent::createLogEvents(const StatsLogEventWrapper& statsLogEventWrapper,
112                                std::vector<std::shared_ptr<LogEvent>>& logEvents) {
113     if (statsLogEventWrapper.getWorkChains().size() == 0) {
114         logEvents.push_back(std::make_shared<LogEvent>(statsLogEventWrapper, -1));
115     } else {
116         for (size_t i = 0; i < statsLogEventWrapper.getWorkChains().size(); i++) {
117             logEvents.push_back(std::make_shared<LogEvent>(statsLogEventWrapper, i));
118         }
119     }
120 }
121 
LogEvent(int32_t tagId,int64_t wallClockTimestampNs,int64_t elapsedTimestampNs)122 LogEvent::LogEvent(int32_t tagId, int64_t wallClockTimestampNs, int64_t elapsedTimestampNs) {
123     mLogdTimestampNs = wallClockTimestampNs;
124     mElapsedTimestampNs = elapsedTimestampNs;
125     mTagId = tagId;
126     mLogUid = 0;
127     mContext = create_android_logger(1937006964); // the event tag shared by all stats logs
128     if (mContext) {
129         android_log_write_int64(mContext, elapsedTimestampNs);
130         android_log_write_int32(mContext, tagId);
131     }
132 }
133 
LogEvent(int32_t tagId,int64_t wallClockTimestampNs,int64_t elapsedTimestampNs,int32_t uid,const std::map<int32_t,int32_t> & int_map,const std::map<int32_t,int64_t> & long_map,const std::map<int32_t,std::string> & string_map,const std::map<int32_t,float> & float_map)134 LogEvent::LogEvent(int32_t tagId, int64_t wallClockTimestampNs, int64_t elapsedTimestampNs,
135                    int32_t uid,
136                    const std::map<int32_t, int32_t>& int_map,
137                    const std::map<int32_t, int64_t>& long_map,
138                    const std::map<int32_t, std::string>& string_map,
139                    const std::map<int32_t, float>& float_map) {
140     mLogdTimestampNs = wallClockTimestampNs;
141     mElapsedTimestampNs = elapsedTimestampNs;
142     mTagId = android::util::KEY_VALUE_PAIRS_ATOM;
143     mLogUid = uid;
144 
145     int pos[] = {1, 1, 1};
146 
147     mValues.push_back(FieldValue(Field(mTagId, pos, 0 /* depth */), Value(uid)));
148     pos[0]++;
149     for (const auto&itr : int_map) {
150         pos[2] = 1;
151         mValues.push_back(FieldValue(Field(mTagId, pos, 2 /* depth */), Value(itr.first)));
152         pos[2] = 2;
153         mValues.push_back(FieldValue(Field(mTagId, pos, 2 /* depth */), Value(itr.second)));
154         mValues.back().mField.decorateLastPos(2);
155         pos[1]++;
156     }
157 
158     for (const auto&itr : long_map) {
159         pos[2] = 1;
160         mValues.push_back(FieldValue(Field(mTagId, pos, 2 /* depth */), Value(itr.first)));
161         pos[2] = 3;
162         mValues.push_back(FieldValue(Field(mTagId, pos, 2 /* depth */), Value(itr.second)));
163         mValues.back().mField.decorateLastPos(2);
164         pos[1]++;
165     }
166 
167     for (const auto&itr : string_map) {
168         pos[2] = 1;
169         mValues.push_back(FieldValue(Field(mTagId, pos, 2 /* depth */), Value(itr.first)));
170         pos[2] = 4;
171         mValues.push_back(FieldValue(Field(mTagId, pos, 2 /* depth */), Value(itr.second)));
172         mValues.back().mField.decorateLastPos(2);
173         pos[1]++;
174     }
175 
176     for (const auto&itr : float_map) {
177         pos[2] = 1;
178         mValues.push_back(FieldValue(Field(mTagId, pos, 2 /* depth */), Value(itr.first)));
179         pos[2] = 5;
180         mValues.push_back(FieldValue(Field(mTagId, pos, 2 /* depth */), Value(itr.second)));
181         mValues.back().mField.decorateLastPos(2);
182         pos[1]++;
183     }
184     if (!mValues.empty()) {
185         mValues.back().mField.decorateLastPos(1);
186         mValues.at(mValues.size() - 2).mField.decorateLastPos(1);
187     }
188 }
189 
LogEvent(const string & trainName,int64_t trainVersionCode,bool requiresStaging,bool rollbackEnabled,bool requiresLowLatencyMonitor,int32_t state,const std::vector<uint8_t> & experimentIds,int32_t userId)190 LogEvent::LogEvent(const string& trainName, int64_t trainVersionCode, bool requiresStaging,
191                    bool rollbackEnabled, bool requiresLowLatencyMonitor, int32_t state,
192                    const std::vector<uint8_t>& experimentIds, int32_t userId) {
193     mLogdTimestampNs = getWallClockNs();
194     mElapsedTimestampNs = getElapsedRealtimeNs();
195     mTagId = android::util::BINARY_PUSH_STATE_CHANGED;
196     mLogUid = android::IPCThreadState::self()->getCallingUid();
197 
198     mValues.push_back(FieldValue(Field(mTagId, getSimpleField(1)), Value(trainName)));
199     mValues.push_back(FieldValue(Field(mTagId, getSimpleField(2)), Value(trainVersionCode)));
200     mValues.push_back(FieldValue(Field(mTagId, getSimpleField(3)), Value((int)requiresStaging)));
201     mValues.push_back(FieldValue(Field(mTagId, getSimpleField(4)), Value((int)rollbackEnabled)));
202     mValues.push_back(
203             FieldValue(Field(mTagId, getSimpleField(5)), Value((int)requiresLowLatencyMonitor)));
204     mValues.push_back(FieldValue(Field(mTagId, getSimpleField(6)), Value(state)));
205     mValues.push_back(FieldValue(Field(mTagId, getSimpleField(7)), Value(experimentIds)));
206     mValues.push_back(FieldValue(Field(mTagId, getSimpleField(8)), Value(userId)));
207 }
208 
LogEvent(int64_t wallClockTimestampNs,int64_t elapsedTimestampNs,const InstallTrainInfo & trainInfo)209 LogEvent::LogEvent(int64_t wallClockTimestampNs, int64_t elapsedTimestampNs,
210                    const InstallTrainInfo& trainInfo) {
211     mLogdTimestampNs = wallClockTimestampNs;
212     mElapsedTimestampNs = elapsedTimestampNs;
213     mTagId = android::util::TRAIN_INFO;
214 
215     mValues.push_back(
216             FieldValue(Field(mTagId, getSimpleField(1)), Value(trainInfo.trainVersionCode)));
217     std::vector<uint8_t> experimentIdsProto;
218     writeExperimentIdsToProto(trainInfo.experimentIds, &experimentIdsProto);
219     mValues.push_back(FieldValue(Field(mTagId, getSimpleField(2)), Value(experimentIdsProto)));
220     mValues.push_back(FieldValue(Field(mTagId, getSimpleField(3)), Value(trainInfo.trainName)));
221     mValues.push_back(FieldValue(Field(mTagId, getSimpleField(4)), Value(trainInfo.status)));
222 }
223 
LogEvent(int32_t tagId,int64_t timestampNs)224 LogEvent::LogEvent(int32_t tagId, int64_t timestampNs) : LogEvent(tagId, timestampNs, timestampNs) {
225 }
226 
LogEvent(int32_t tagId,int64_t timestampNs,int32_t uid)227 LogEvent::LogEvent(int32_t tagId, int64_t timestampNs, int32_t uid) {
228     mLogdTimestampNs = timestampNs;
229     mTagId = tagId;
230     mLogUid = uid;
231     mContext = create_android_logger(1937006964); // the event tag shared by all stats logs
232     if (mContext) {
233         android_log_write_int64(mContext, timestampNs);
234         android_log_write_int32(mContext, tagId);
235     }
236 }
237 
init()238 void LogEvent::init() {
239     if (mContext) {
240         const char* buffer;
241         size_t len = android_log_write_list_buffer(mContext, &buffer);
242         // turns to reader mode
243         android_log_context contextForRead = create_android_log_parser(buffer, len);
244         if (contextForRead) {
245             init(contextForRead);
246             // destroy the context to save memory.
247             // android_log_destroy will set mContext to NULL
248             android_log_destroy(&contextForRead);
249         }
250         android_log_destroy(&mContext);
251     }
252 }
253 
~LogEvent()254 LogEvent::~LogEvent() {
255     if (mContext) {
256         // This is for the case when LogEvent is created using the test interface
257         // but init() isn't called.
258         android_log_destroy(&mContext);
259     }
260 }
261 
write(int32_t value)262 bool LogEvent::write(int32_t value) {
263     if (mContext) {
264         return android_log_write_int32(mContext, value) >= 0;
265     }
266     return false;
267 }
268 
write(uint32_t value)269 bool LogEvent::write(uint32_t value) {
270     if (mContext) {
271         return android_log_write_int32(mContext, value) >= 0;
272     }
273     return false;
274 }
275 
write(int64_t value)276 bool LogEvent::write(int64_t value) {
277     if (mContext) {
278         return android_log_write_int64(mContext, value) >= 0;
279     }
280     return false;
281 }
282 
write(uint64_t value)283 bool LogEvent::write(uint64_t value) {
284     if (mContext) {
285         return android_log_write_int64(mContext, value) >= 0;
286     }
287     return false;
288 }
289 
write(const string & value)290 bool LogEvent::write(const string& value) {
291     if (mContext) {
292         return android_log_write_string8_len(mContext, value.c_str(), value.length()) >= 0;
293     }
294     return false;
295 }
296 
write(float value)297 bool LogEvent::write(float value) {
298     if (mContext) {
299         return android_log_write_float32(mContext, value) >= 0;
300     }
301     return false;
302 }
303 
writeKeyValuePairs(int32_t uid,const std::map<int32_t,int32_t> & int_map,const std::map<int32_t,int64_t> & long_map,const std::map<int32_t,std::string> & string_map,const std::map<int32_t,float> & float_map)304 bool LogEvent::writeKeyValuePairs(int32_t uid,
305                                   const std::map<int32_t, int32_t>& int_map,
306                                   const std::map<int32_t, int64_t>& long_map,
307                                   const std::map<int32_t, std::string>& string_map,
308                                   const std::map<int32_t, float>& float_map) {
309     if (mContext) {
310          if (android_log_write_list_begin(mContext) < 0) {
311             return false;
312          }
313          write(uid);
314          for (const auto& itr : int_map) {
315              if (android_log_write_list_begin(mContext) < 0) {
316                 return false;
317              }
318              write(itr.first);
319              write(itr.second);
320              if (android_log_write_list_end(mContext) < 0) {
321                 return false;
322              }
323          }
324 
325          for (const auto& itr : long_map) {
326              if (android_log_write_list_begin(mContext) < 0) {
327                 return false;
328              }
329              write(itr.first);
330              write(itr.second);
331              if (android_log_write_list_end(mContext) < 0) {
332                 return false;
333              }
334          }
335 
336          for (const auto& itr : string_map) {
337              if (android_log_write_list_begin(mContext) < 0) {
338                 return false;
339              }
340              write(itr.first);
341              write(itr.second.c_str());
342              if (android_log_write_list_end(mContext) < 0) {
343                 return false;
344              }
345          }
346 
347          for (const auto& itr : float_map) {
348              if (android_log_write_list_begin(mContext) < 0) {
349                 return false;
350              }
351              write(itr.first);
352              write(itr.second);
353              if (android_log_write_list_end(mContext) < 0) {
354                 return false;
355              }
356          }
357 
358          if (android_log_write_list_end(mContext) < 0) {
359             return false;
360          }
361          return true;
362     }
363     return false;
364 }
365 
write(const std::vector<AttributionNodeInternal> & nodes)366 bool LogEvent::write(const std::vector<AttributionNodeInternal>& nodes) {
367     if (mContext) {
368          if (android_log_write_list_begin(mContext) < 0) {
369             return false;
370          }
371          for (size_t i = 0; i < nodes.size(); ++i) {
372              if (!write(nodes[i])) {
373                 return false;
374              }
375          }
376          if (android_log_write_list_end(mContext) < 0) {
377             return false;
378          }
379          return true;
380     }
381     return false;
382 }
383 
write(const AttributionNodeInternal & node)384 bool LogEvent::write(const AttributionNodeInternal& node) {
385     if (mContext) {
386          if (android_log_write_list_begin(mContext) < 0) {
387             return false;
388          }
389          if (android_log_write_int32(mContext, node.uid()) < 0) {
390             return false;
391          }
392          if (android_log_write_string8(mContext, node.tag().c_str()) < 0) {
393             return false;
394          }
395          if (android_log_write_list_end(mContext) < 0) {
396             return false;
397          }
398          return true;
399     }
400     return false;
401 }
402 
403 /**
404  * The elements of each log event are stored as a vector of android_log_list_elements.
405  * The goal is to do as little preprocessing as possible, because we read a tiny fraction
406  * of the elements that are written to the log.
407  *
408  * The idea here is to read through the log items once, we get as much information we need for
409  * matching as possible. Because this log will be matched against lots of matchers.
410  */
init(android_log_context context)411 void LogEvent::init(android_log_context context) {
412     android_log_list_element elem;
413     int i = 0;
414     int depth = -1;
415     int pos[] = {1, 1, 1};
416     bool isKeyValuePairAtom = false;
417     do {
418         elem = android_log_read_next(context);
419         switch ((int)elem.type) {
420             case EVENT_TYPE_INT:
421                 // elem at [0] is EVENT_TYPE_LIST, [1] is the timestamp, [2] is tag id.
422                 if (i == 2) {
423                     mTagId = elem.data.int32;
424                     isKeyValuePairAtom = (mTagId == android::util::KEY_VALUE_PAIRS_ATOM);
425                 } else {
426                     if (depth < 0 || depth > 2) {
427                         return;
428                     }
429 
430                     mValues.push_back(
431                             FieldValue(Field(mTagId, pos, depth), Value((int32_t)elem.data.int32)));
432 
433                     pos[depth]++;
434                 }
435                 break;
436             case EVENT_TYPE_FLOAT: {
437                 if (depth < 0 || depth > 2) {
438                     ALOGE("Depth > 2. Not supported!");
439                     return;
440                 }
441 
442                 // Handles the oneof field in KeyValuePair atom.
443                 if (isKeyValuePairAtom && depth == 2) {
444                     pos[depth] = 5;
445                 }
446 
447                 mValues.push_back(FieldValue(Field(mTagId, pos, depth), Value(elem.data.float32)));
448 
449                 pos[depth]++;
450 
451             } break;
452             case EVENT_TYPE_STRING: {
453                 if (depth < 0 || depth > 2) {
454                     ALOGE("Depth > 2. Not supported!");
455                     return;
456                 }
457 
458                 // Handles the oneof field in KeyValuePair atom.
459                 if (isKeyValuePairAtom && depth == 2) {
460                     pos[depth] = 4;
461                 }
462                 mValues.push_back(FieldValue(Field(mTagId, pos, depth),
463                                              Value(string(elem.data.string, elem.len))));
464 
465                 pos[depth]++;
466 
467             } break;
468             case EVENT_TYPE_LONG: {
469                 if (i == 1) {
470                     mElapsedTimestampNs = elem.data.int64;
471                 } else {
472                     if (depth < 0 || depth > 2) {
473                         ALOGE("Depth > 2. Not supported!");
474                         return;
475                     }
476                     // Handles the oneof field in KeyValuePair atom.
477                     if (isKeyValuePairAtom && depth == 2) {
478                         pos[depth] = 3;
479                     }
480                     mValues.push_back(
481                             FieldValue(Field(mTagId, pos, depth), Value((int64_t)elem.data.int64)));
482 
483                     pos[depth]++;
484                 }
485             } break;
486             case EVENT_TYPE_LIST:
487                 depth++;
488                 if (depth > 2) {
489                     ALOGE("Depth > 2. Not supported!");
490                     return;
491                 }
492                 pos[depth] = 1;
493 
494                 break;
495             case EVENT_TYPE_LIST_STOP: {
496                 int prevDepth = depth;
497                 depth--;
498                 if (depth >= 0 && depth < 2) {
499                     // Now go back to decorate the previous items that are last at prevDepth.
500                     // So that we can later easily match them with Position=Last matchers.
501                     pos[prevDepth]--;
502                     int path = getEncodedField(pos, prevDepth, false);
503                     for (auto it = mValues.rbegin(); it != mValues.rend(); ++it) {
504                         if (it->mField.getDepth() >= prevDepth &&
505                             it->mField.getPath(prevDepth) == path) {
506                             it->mField.decorateLastPos(prevDepth);
507                         } else {
508                             // Safe to break, because the items are in DFS order.
509                             break;
510                         }
511                     }
512                     pos[depth]++;
513                 }
514                 break;
515             }
516             case EVENT_TYPE_UNKNOWN:
517                 break;
518             default:
519                 break;
520         }
521         i++;
522     } while ((elem.type != EVENT_TYPE_UNKNOWN) && !elem.complete);
523     if (isKeyValuePairAtom && mValues.size() > 0) {
524         mValues[0] = FieldValue(Field(android::util::KEY_VALUE_PAIRS_ATOM, getSimpleField(1)),
525                                 Value((int32_t)mLogUid));
526     }
527 }
528 
GetLong(size_t key,status_t * err) const529 int64_t LogEvent::GetLong(size_t key, status_t* err) const {
530     // TODO(b/110561208): encapsulate the magical operations in Field struct as static functions
531     int field = getSimpleField(key);
532     for (const auto& value : mValues) {
533         if (value.mField.getField() == field) {
534             if (value.mValue.getType() == LONG) {
535                 return value.mValue.long_value;
536             } else if (value.mValue.getType() == INT) {
537                 return value.mValue.int_value;
538             } else {
539                 *err = BAD_TYPE;
540                 return 0;
541             }
542         }
543         if ((size_t)value.mField.getPosAtDepth(0) > key) {
544             break;
545         }
546     }
547 
548     *err = BAD_INDEX;
549     return 0;
550 }
551 
GetInt(size_t key,status_t * err) const552 int LogEvent::GetInt(size_t key, status_t* err) const {
553     int field = getSimpleField(key);
554     for (const auto& value : mValues) {
555         if (value.mField.getField() == field) {
556             if (value.mValue.getType() == INT) {
557                 return value.mValue.int_value;
558             } else {
559                 *err = BAD_TYPE;
560                 return 0;
561             }
562         }
563         if ((size_t)value.mField.getPosAtDepth(0) > key) {
564             break;
565         }
566     }
567 
568     *err = BAD_INDEX;
569     return 0;
570 }
571 
GetString(size_t key,status_t * err) const572 const char* LogEvent::GetString(size_t key, status_t* err) const {
573     int field = getSimpleField(key);
574     for (const auto& value : mValues) {
575         if (value.mField.getField() == field) {
576             if (value.mValue.getType() == STRING) {
577                 return value.mValue.str_value.c_str();
578             } else {
579                 *err = BAD_TYPE;
580                 return 0;
581             }
582         }
583         if ((size_t)value.mField.getPosAtDepth(0) > key) {
584             break;
585         }
586     }
587 
588     *err = BAD_INDEX;
589     return NULL;
590 }
591 
GetBool(size_t key,status_t * err) const592 bool LogEvent::GetBool(size_t key, status_t* err) const {
593     int field = getSimpleField(key);
594     for (const auto& value : mValues) {
595         if (value.mField.getField() == field) {
596             if (value.mValue.getType() == INT) {
597                 return value.mValue.int_value != 0;
598             } else if (value.mValue.getType() == LONG) {
599                 return value.mValue.long_value != 0;
600             } else {
601                 *err = BAD_TYPE;
602                 return false;
603             }
604         }
605         if ((size_t)value.mField.getPosAtDepth(0) > key) {
606             break;
607         }
608     }
609 
610     *err = BAD_INDEX;
611     return false;
612 }
613 
GetFloat(size_t key,status_t * err) const614 float LogEvent::GetFloat(size_t key, status_t* err) const {
615     int field = getSimpleField(key);
616     for (const auto& value : mValues) {
617         if (value.mField.getField() == field) {
618             if (value.mValue.getType() == FLOAT) {
619                 return value.mValue.float_value;
620             } else {
621                 *err = BAD_TYPE;
622                 return 0.0;
623             }
624         }
625         if ((size_t)value.mField.getPosAtDepth(0) > key) {
626             break;
627         }
628     }
629 
630     *err = BAD_INDEX;
631     return 0.0;
632 }
633 
ToString() const634 string LogEvent::ToString() const {
635     string result;
636     result += StringPrintf("{ uid(%d) %lld %lld (%d)", mLogUid, (long long)mLogdTimestampNs,
637                            (long long)mElapsedTimestampNs, mTagId);
638     for (const auto& value : mValues) {
639         result +=
640                 StringPrintf("%#x", value.mField.getField()) + "->" + value.mValue.toString() + " ";
641     }
642     result += " }";
643     return result;
644 }
645 
ToProto(ProtoOutputStream & protoOutput) const646 void LogEvent::ToProto(ProtoOutputStream& protoOutput) const {
647     writeFieldValueTreeToStream(mTagId, getValues(), &protoOutput);
648 }
649 
writeExperimentIdsToProto(const std::vector<int64_t> & experimentIds,std::vector<uint8_t> * protoOut)650 void writeExperimentIdsToProto(const std::vector<int64_t>& experimentIds,
651                                std::vector<uint8_t>* protoOut) {
652     ProtoOutputStream proto;
653     for (const auto& expId : experimentIds) {
654         proto.write(FIELD_TYPE_INT64 | FIELD_COUNT_REPEATED | FIELD_ID_EXPERIMENT_ID,
655                     (long long)expId);
656     }
657 
658     protoOut->resize(proto.size());
659     size_t pos = 0;
660     sp<ProtoReader> reader = proto.data();
661     while (reader->readBuffer() != NULL) {
662         size_t toRead = reader->currentToRead();
663         std::memcpy(protoOut->data() + pos, reader->readBuffer(), toRead);
664         pos += toRead;
665         reader->move(toRead);
666     }
667 }
668 
669 }  // namespace statsd
670 }  // namespace os
671 }  // namespace android
672