Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 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 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 17 | #define DEBUG true // STOPSHIP if true |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 18 | #include "logd/LogEvent.h" |
| 19 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 20 | #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" |
| 21 | |
| 22 | #include <set> |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 23 | #include <sstream> |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 24 | |
| 25 | #include "field_util.h" |
| 26 | #include "dimension.h" |
| 27 | #include "stats_log_util.h" |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | namespace os { |
| 31 | namespace statsd { |
| 32 | |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 33 | using namespace android::util; |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 34 | using std::ostringstream; |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 35 | using std::string; |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 36 | using android::util::ProtoOutputStream; |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 37 | |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 38 | LogEvent::LogEvent(log_msg& msg) { |
Chenjie Yu | 3ca3683 | 2018-01-22 15:10:54 -0800 | [diff] [blame] | 39 | mContext = |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 40 | create_android_log_parser(msg.msg() + sizeof(uint32_t), msg.len() - sizeof(uint32_t)); |
| 41 | mTimestampNs = msg.entry_v1.sec * NS_PER_SEC + msg.entry_v1.nsec; |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 42 | mLogUid = msg.entry_v4.uid; |
Chenjie Yu | 3ca3683 | 2018-01-22 15:10:54 -0800 | [diff] [blame] | 43 | init(mContext); |
| 44 | if (mContext) { |
Yao Chen | 48d7518 | 2018-01-23 09:40:48 -0800 | [diff] [blame] | 45 | // android_log_destroy will set mContext to NULL |
Chenjie Yu | 3ca3683 | 2018-01-22 15:10:54 -0800 | [diff] [blame] | 46 | android_log_destroy(&mContext); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 47 | } |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 50 | LogEvent::LogEvent(int32_t tagId, uint64_t timestampNs) { |
| 51 | mTimestampNs = timestampNs; |
| 52 | mTagId = tagId; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 53 | mLogUid = 0; |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 54 | mContext = create_android_logger(1937006964); // the event tag shared by all stats logs |
| 55 | if (mContext) { |
| 56 | android_log_write_int32(mContext, tagId); |
| 57 | } |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 58 | } |
| 59 | |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 60 | void LogEvent::init() { |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 61 | if (mContext) { |
| 62 | const char* buffer; |
| 63 | size_t len = android_log_write_list_buffer(mContext, &buffer); |
| 64 | // turns to reader mode |
Chenjie Yu | c1fe6f4 | 2018-02-01 23:14:18 -0800 | [diff] [blame] | 65 | android_log_context contextForRead = create_android_log_parser(buffer, len); |
| 66 | if (contextForRead) { |
| 67 | init(contextForRead); |
| 68 | // destroy the context to save memory. |
Yao Chen | 48d7518 | 2018-01-23 09:40:48 -0800 | [diff] [blame] | 69 | // android_log_destroy will set mContext to NULL |
Chenjie Yu | c1fe6f4 | 2018-02-01 23:14:18 -0800 | [diff] [blame] | 70 | android_log_destroy(&contextForRead); |
Yao Chen | 48d7518 | 2018-01-23 09:40:48 -0800 | [diff] [blame] | 71 | } |
Chenjie Yu | c1fe6f4 | 2018-02-01 23:14:18 -0800 | [diff] [blame] | 72 | android_log_destroy(&mContext); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
| 76 | LogEvent::~LogEvent() { |
| 77 | if (mContext) { |
Yao Chen | 48d7518 | 2018-01-23 09:40:48 -0800 | [diff] [blame] | 78 | // This is for the case when LogEvent is created using the test interface |
| 79 | // but init() isn't called. |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 80 | android_log_destroy(&mContext); |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | bool LogEvent::write(int32_t value) { |
| 85 | if (mContext) { |
| 86 | return android_log_write_int32(mContext, value) >= 0; |
| 87 | } |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | bool LogEvent::write(uint32_t value) { |
| 92 | if (mContext) { |
| 93 | return android_log_write_int32(mContext, value) >= 0; |
| 94 | } |
| 95 | return false; |
| 96 | } |
| 97 | |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 98 | bool LogEvent::write(int64_t value) { |
| 99 | if (mContext) { |
| 100 | return android_log_write_int64(mContext, value) >= 0; |
| 101 | } |
| 102 | return false; |
| 103 | } |
| 104 | |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 105 | bool LogEvent::write(uint64_t value) { |
| 106 | if (mContext) { |
| 107 | return android_log_write_int64(mContext, value) >= 0; |
| 108 | } |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | bool LogEvent::write(const string& value) { |
| 113 | if (mContext) { |
| 114 | return android_log_write_string8_len(mContext, value.c_str(), value.length()) >= 0; |
| 115 | } |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | bool LogEvent::write(float value) { |
| 120 | if (mContext) { |
| 121 | return android_log_write_float32(mContext, value) >= 0; |
| 122 | } |
| 123 | return false; |
| 124 | } |
| 125 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 126 | bool LogEvent::write(const std::vector<AttributionNode>& nodes) { |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 127 | if (mContext) { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 128 | if (android_log_write_list_begin(mContext) < 0) { |
| 129 | return false; |
| 130 | } |
| 131 | for (size_t i = 0; i < nodes.size(); ++i) { |
| 132 | if (!write(nodes[i])) { |
| 133 | return false; |
| 134 | } |
| 135 | } |
| 136 | if (android_log_write_list_end(mContext) < 0) { |
| 137 | return false; |
| 138 | } |
| 139 | return true; |
| 140 | } |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | bool LogEvent::write(const AttributionNode& node) { |
| 145 | if (mContext) { |
| 146 | if (android_log_write_list_begin(mContext) < 0) { |
| 147 | return false; |
| 148 | } |
| 149 | if (android_log_write_int32(mContext, node.uid()) < 0) { |
| 150 | return false; |
| 151 | } |
| 152 | if (android_log_write_string8(mContext, node.tag().c_str()) < 0) { |
| 153 | return false; |
| 154 | } |
| 155 | if (android_log_write_int32(mContext, node.uid()) < 0) { |
| 156 | return false; |
| 157 | } |
| 158 | if (android_log_write_list_end(mContext) < 0) { |
| 159 | return false; |
| 160 | } |
| 161 | return true; |
| 162 | } |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | namespace { |
| 167 | |
| 168 | void increaseField(Field *field, bool is_child) { |
| 169 | if (is_child) { |
| 170 | if (field->child_size() <= 0) { |
| 171 | field->add_child(); |
| 172 | } |
| 173 | } else { |
| 174 | field->clear_child(); |
| 175 | } |
| 176 | Field* curr = is_child ? field->mutable_child(0) : field; |
| 177 | if (!curr->has_field()) { |
| 178 | curr->set_field(1); |
| 179 | } else { |
| 180 | curr->set_field(curr->field() + 1); |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 181 | } |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 184 | } // namespace |
| 185 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 186 | /** |
| 187 | * The elements of each log event are stored as a vector of android_log_list_elements. |
| 188 | * The goal is to do as little preprocessing as possible, because we read a tiny fraction |
| 189 | * of the elements that are written to the log. |
| 190 | */ |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 191 | void LogEvent::init(android_log_context context) { |
Chenjie Yu | c1fe6f4 | 2018-02-01 23:14:18 -0800 | [diff] [blame] | 192 | if (!context) { |
| 193 | return; |
| 194 | } |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 195 | android_log_list_element elem; |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 196 | // TODO: The log is actually structured inside one list. This is convenient |
| 197 | // because we'll be able to use it to put the attribution (WorkSource) block first |
| 198 | // without doing our own tagging scheme. Until that change is in, just drop the |
| 199 | // list-related log elements and the order we get there is our index-keyed data |
| 200 | // structure. |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 201 | int i = 0; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 202 | |
| 203 | int seenListStart = 0; |
| 204 | |
Yangster-mac | 7ba8fc3 | 2018-01-24 16:16:46 -0800 | [diff] [blame] | 205 | Field fieldTree; |
| 206 | Field* atomField = fieldTree.add_child(); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 207 | do { |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 208 | elem = android_log_read_next(context); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 209 | switch ((int)elem.type) { |
| 210 | case EVENT_TYPE_INT: |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 211 | // elem at [0] is EVENT_TYPE_LIST, [1] is the tag id. |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 212 | if (i == 1) { |
| 213 | mTagId = elem.data.int32; |
Yangster-mac | 7ba8fc3 | 2018-01-24 16:16:46 -0800 | [diff] [blame] | 214 | fieldTree.set_field(mTagId); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 215 | } else { |
Yangster-mac | 7ba8fc3 | 2018-01-24 16:16:46 -0800 | [diff] [blame] | 216 | increaseField(atomField, seenListStart > 0/* is_child */); |
| 217 | mFieldValueMap[fieldTree].set_value_int(elem.data.int32); |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 218 | } |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 219 | break; |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 220 | case EVENT_TYPE_FLOAT: |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 221 | { |
Yangster-mac | 7ba8fc3 | 2018-01-24 16:16:46 -0800 | [diff] [blame] | 222 | increaseField(atomField, seenListStart > 0/* is_child */); |
| 223 | mFieldValueMap[fieldTree].set_value_float(elem.data.float32); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 224 | } |
| 225 | break; |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 226 | case EVENT_TYPE_STRING: |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 227 | { |
Yangster-mac | 7ba8fc3 | 2018-01-24 16:16:46 -0800 | [diff] [blame] | 228 | increaseField(atomField, seenListStart > 0/* is_child */); |
| 229 | mFieldValueMap[fieldTree].set_value_str( |
| 230 | string(elem.data.string, elem.len).c_str()); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 231 | } |
| 232 | break; |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 233 | case EVENT_TYPE_LONG: |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 234 | { |
Yangster-mac | 7ba8fc3 | 2018-01-24 16:16:46 -0800 | [diff] [blame] | 235 | increaseField(atomField, seenListStart > 0 /* is_child */); |
| 236 | mFieldValueMap[fieldTree].set_value_long(elem.data.int64); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 237 | } |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 238 | break; |
| 239 | case EVENT_TYPE_LIST: |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 240 | if (i >= 1) { |
| 241 | if (seenListStart > 0) { |
Yangster-mac | 7ba8fc3 | 2018-01-24 16:16:46 -0800 | [diff] [blame] | 242 | increasePosition(atomField); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 243 | } else { |
Yangster-mac | 7ba8fc3 | 2018-01-24 16:16:46 -0800 | [diff] [blame] | 244 | increaseField(atomField, false /* is_child */); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 245 | } |
| 246 | seenListStart++; |
| 247 | if (seenListStart >= 3) { |
| 248 | ALOGE("Depth > 2. Not supported!"); |
| 249 | return; |
| 250 | } |
| 251 | } |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 252 | break; |
| 253 | case EVENT_TYPE_LIST_STOP: |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 254 | seenListStart--; |
| 255 | if (seenListStart == 0) { |
Yangster-mac | 7ba8fc3 | 2018-01-24 16:16:46 -0800 | [diff] [blame] | 256 | atomField->clear_position_index(); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 257 | } else { |
Yangster-mac | 7ba8fc3 | 2018-01-24 16:16:46 -0800 | [diff] [blame] | 258 | if (atomField->child_size() > 0) { |
| 259 | atomField->mutable_child(0)->clear_field(); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 260 | } |
| 261 | } |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 262 | break; |
| 263 | case EVENT_TYPE_UNKNOWN: |
| 264 | break; |
| 265 | default: |
| 266 | break; |
| 267 | } |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 268 | i++; |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 269 | } while ((elem.type != EVENT_TYPE_UNKNOWN) && !elem.complete); |
| 270 | } |
| 271 | |
| 272 | int64_t LogEvent::GetLong(size_t key, status_t* err) const { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 273 | DimensionsValue value; |
| 274 | if (!GetSimpleAtomDimensionsValueProto(key, &value)) { |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 275 | *err = BAD_INDEX; |
| 276 | return 0; |
| 277 | } |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 278 | const DimensionsValue* leafValue = getSingleLeafValue(&value); |
| 279 | switch (leafValue->value_case()) { |
| 280 | case DimensionsValue::ValueCase::kValueInt: |
| 281 | return (int64_t)leafValue->value_int(); |
| 282 | case DimensionsValue::ValueCase::kValueLong: |
| 283 | return leafValue->value_long(); |
| 284 | case DimensionsValue::ValueCase::kValueBool: |
| 285 | return leafValue->value_bool() ? 1 : 0; |
| 286 | case DimensionsValue::ValueCase::kValueFloat: |
| 287 | return (int64_t)leafValue->value_float(); |
| 288 | case DimensionsValue::ValueCase::kValueTuple: |
| 289 | case DimensionsValue::ValueCase::kValueStr: |
| 290 | case DimensionsValue::ValueCase::VALUE_NOT_SET: { |
| 291 | *err = BAD_TYPE; |
| 292 | return 0; |
| 293 | } |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
| 297 | const char* LogEvent::GetString(size_t key, status_t* err) const { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 298 | DimensionsValue value; |
| 299 | if (!GetSimpleAtomDimensionsValueProto(key, &value)) { |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 300 | *err = BAD_INDEX; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 301 | return 0; |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 302 | } |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 303 | const DimensionsValue* leafValue = getSingleLeafValue(&value); |
| 304 | switch (leafValue->value_case()) { |
| 305 | case DimensionsValue::ValueCase::kValueStr: |
| 306 | return leafValue->value_str().c_str(); |
| 307 | case DimensionsValue::ValueCase::kValueInt: |
| 308 | case DimensionsValue::ValueCase::kValueLong: |
| 309 | case DimensionsValue::ValueCase::kValueBool: |
| 310 | case DimensionsValue::ValueCase::kValueFloat: |
| 311 | case DimensionsValue::ValueCase::kValueTuple: |
| 312 | case DimensionsValue::ValueCase::VALUE_NOT_SET: { |
| 313 | *err = BAD_TYPE; |
| 314 | return 0; |
| 315 | } |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 316 | } |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | bool LogEvent::GetBool(size_t key, status_t* err) const { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 320 | DimensionsValue value; |
| 321 | if (!GetSimpleAtomDimensionsValueProto(key, &value)) { |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 322 | *err = BAD_INDEX; |
| 323 | return 0; |
| 324 | } |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 325 | const DimensionsValue* leafValue = getSingleLeafValue(&value); |
| 326 | switch (leafValue->value_case()) { |
| 327 | case DimensionsValue::ValueCase::kValueInt: |
| 328 | return leafValue->value_int() != 0; |
| 329 | case DimensionsValue::ValueCase::kValueLong: |
| 330 | return leafValue->value_long() != 0; |
| 331 | case DimensionsValue::ValueCase::kValueBool: |
| 332 | return leafValue->value_bool(); |
| 333 | case DimensionsValue::ValueCase::kValueFloat: |
| 334 | return leafValue->value_float() != 0; |
| 335 | case DimensionsValue::ValueCase::kValueTuple: |
| 336 | case DimensionsValue::ValueCase::kValueStr: |
| 337 | case DimensionsValue::ValueCase::VALUE_NOT_SET: { |
| 338 | *err = BAD_TYPE; |
| 339 | return 0; |
| 340 | } |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | |
| 344 | float LogEvent::GetFloat(size_t key, status_t* err) const { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 345 | DimensionsValue value; |
| 346 | if (!GetSimpleAtomDimensionsValueProto(key, &value)) { |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 347 | *err = BAD_INDEX; |
| 348 | return 0; |
| 349 | } |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 350 | const DimensionsValue* leafValue = getSingleLeafValue(&value); |
| 351 | switch (leafValue->value_case()) { |
| 352 | case DimensionsValue::ValueCase::kValueInt: |
| 353 | return (float)leafValue->value_int(); |
| 354 | case DimensionsValue::ValueCase::kValueLong: |
| 355 | return (float)leafValue->value_long(); |
| 356 | case DimensionsValue::ValueCase::kValueBool: |
| 357 | return leafValue->value_bool() ? 1.0f : 0.0f; |
| 358 | case DimensionsValue::ValueCase::kValueFloat: |
| 359 | return leafValue->value_float(); |
| 360 | case DimensionsValue::ValueCase::kValueTuple: |
| 361 | case DimensionsValue::ValueCase::kValueStr: |
| 362 | case DimensionsValue::ValueCase::VALUE_NOT_SET: { |
| 363 | *err = BAD_TYPE; |
| 364 | return 0; |
| 365 | } |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 369 | void LogEvent::GetAtomDimensionsValueProtos(const FieldMatcher& matcher, |
| 370 | std::vector<DimensionsValue> *dimensionsValues) const { |
| 371 | findDimensionsValues(mFieldValueMap, matcher, dimensionsValues); |
| 372 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 373 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 374 | bool LogEvent::GetAtomDimensionsValueProto(const FieldMatcher& matcher, |
| 375 | DimensionsValue* dimensionsValue) const { |
| 376 | std::vector<DimensionsValue> rootDimensionsValues; |
| 377 | findDimensionsValues(mFieldValueMap, matcher, &rootDimensionsValues); |
| 378 | if (rootDimensionsValues.size() != 1) { |
| 379 | return false; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 380 | } |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 381 | *dimensionsValue = rootDimensionsValues.front(); |
| 382 | return true; |
| 383 | } |
| 384 | |
| 385 | bool LogEvent::GetSimpleAtomDimensionsValueProto(size_t atomField, |
| 386 | DimensionsValue* dimensionsValue) const { |
Yangster-mac | 7ba8fc3 | 2018-01-24 16:16:46 -0800 | [diff] [blame] | 387 | FieldMatcher matcher; |
| 388 | buildSimpleAtomFieldMatcher(mTagId, atomField, &matcher); |
| 389 | return GetAtomDimensionsValueProto(matcher, dimensionsValue); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 392 | DimensionsValue* LogEvent::findFieldValueOrNull(const Field& field) { |
| 393 | auto it = mFieldValueMap.find(field); |
| 394 | if (it == mFieldValueMap.end()) { |
| 395 | return nullptr; |
| 396 | } |
| 397 | return &it->second; |
| 398 | } |
| 399 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 400 | string LogEvent::ToString() const { |
| 401 | ostringstream result; |
| 402 | result << "{ " << mTimestampNs << " (" << mTagId << ")"; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 403 | for (const auto& itr : mFieldValueMap) { |
| 404 | result << FieldToString(itr.first); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 405 | result << "->"; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 406 | result << DimensionsValueToString(itr.second); |
| 407 | result << " "; |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 408 | } |
| 409 | result << " }"; |
| 410 | return result.str(); |
| 411 | } |
| 412 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 413 | void LogEvent::ToProto(ProtoOutputStream& protoOutput) const { |
| 414 | writeFieldValueTreeToStream(getFieldValueMap(), &protoOutput); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | } // namespace statsd |
| 418 | } // namespace os |
| 419 | } // namespace android |