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 | |
| 17 | #pragma once |
| 18 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 19 | #include "field_util.h" |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 20 | #include "frameworks/base/cmds/statsd/src/stats_log.pb.h" |
| 21 | |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 22 | #include <android/util/ProtoOutputStream.h> |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 23 | #include <log/log_event_list.h> |
| 24 | #include <log/log_read.h> |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 25 | #include <private/android_logger.h> |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 26 | #include <utils/Errors.h> |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 27 | #include <utils/JenkinsHash.h> |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 28 | |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 29 | #include <memory> |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 30 | #include <string> |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 31 | #include <map> |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 32 | #include <vector> |
| 33 | |
| 34 | namespace android { |
| 35 | namespace os { |
| 36 | namespace statsd { |
| 37 | |
| 38 | using std::string; |
| 39 | using std::vector; |
| 40 | |
| 41 | /** |
| 42 | * Wrapper for the log_msg structure. |
| 43 | */ |
| 44 | class LogEvent { |
| 45 | public: |
| 46 | /** |
| 47 | * Read a LogEvent from a log_msg. |
| 48 | */ |
David Chen | a3bf050 | 2017-11-01 16:57:43 -0700 | [diff] [blame] | 49 | explicit LogEvent(log_msg& msg); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 50 | |
| 51 | /** |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 52 | * Constructs a LogEvent with synthetic data for testing. Must call init() before reading. |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 53 | */ |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 54 | explicit LogEvent(int32_t tagId, uint64_t timestampNs); |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 55 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 56 | ~LogEvent(); |
| 57 | |
| 58 | /** |
| 59 | * Get the timestamp associated with this event. |
| 60 | */ |
| 61 | uint64_t GetTimestampNs() const { return mTimestampNs; } |
| 62 | |
| 63 | /** |
| 64 | * Get the tag for this event. |
| 65 | */ |
| 66 | int GetTagId() const { return mTagId; } |
| 67 | |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 68 | uint32_t GetUid() const { |
| 69 | return mLogUid; |
| 70 | } |
| 71 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 72 | /** |
| 73 | * Get the nth value, starting at 1. |
| 74 | * |
| 75 | * Returns BAD_INDEX if the index is larger than the number of elements. |
| 76 | * Returns BAD_TYPE if the index is available but the data is the wrong type. |
| 77 | */ |
| 78 | int64_t GetLong(size_t key, status_t* err) const; |
| 79 | const char* GetString(size_t key, status_t* err) const; |
| 80 | bool GetBool(size_t key, status_t* err) const; |
| 81 | float GetFloat(size_t key, status_t* err) const; |
| 82 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 83 | /* |
| 84 | * Get DimensionsValue proto objects from FieldMatcher. |
| 85 | */ |
| 86 | void GetAtomDimensionsValueProtos( |
| 87 | const FieldMatcher& matcher, std::vector<DimensionsValue> *dimensionsValues) const; |
| 88 | bool GetAtomDimensionsValueProto( |
| 89 | const FieldMatcher& matcher, DimensionsValue* dimensionsValue) const; |
| 90 | |
| 91 | /* |
| 92 | * Get a DimensionsValue proto objects from Field. |
| 93 | */ |
| 94 | bool GetSimpleAtomDimensionsValueProto(size_t field, DimensionsValue* dimensionsValue) const; |
| 95 | DimensionsValue GetSimpleAtomDimensionsValueProto(size_t atomField) const; |
| 96 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 97 | /** |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 98 | * Write test data to the LogEvent. This can only be used when the LogEvent is constructed |
| 99 | * using LogEvent(tagId, timestampNs). You need to call init() before you can read from it. |
| 100 | */ |
| 101 | bool write(uint32_t value); |
| 102 | bool write(int32_t value); |
| 103 | bool write(uint64_t value); |
| 104 | bool write(int64_t value); |
| 105 | bool write(const string& value); |
| 106 | bool write(float value); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 107 | bool write(const std::vector<AttributionNode>& nodes); |
| 108 | bool write(const AttributionNode& node); |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 109 | |
| 110 | /** |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 111 | * Return a string representation of this event. |
| 112 | */ |
| 113 | string ToString() const; |
| 114 | |
| 115 | /** |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 116 | * Write this object to a ProtoOutputStream. |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 117 | */ |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 118 | void ToProto(android::util::ProtoOutputStream& out) const; |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 119 | |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 120 | /** |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 121 | * Used with the constructor where tag is passed in. Converts the log_event_list to read mode |
| 122 | * and prepares the list for reading. |
| 123 | */ |
| 124 | void init(); |
| 125 | |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 126 | /** |
| 127 | * Set timestamp if the original timestamp is missing. |
| 128 | */ |
| 129 | void setTimestampNs(uint64_t timestampNs) {mTimestampNs = timestampNs;} |
| 130 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 131 | inline int size() const { |
| 132 | return mFieldValueMap.size(); |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 135 | /** |
| 136 | * Returns the mutable DimensionsValue proto for the specific the field. |
| 137 | */ |
| 138 | DimensionsValue* findFieldValueOrNull(const Field& field); |
| 139 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 140 | inline const FieldValueMap& getFieldValueMap() const { return mFieldValueMap; } |
| 141 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 142 | private: |
| 143 | /** |
| 144 | * Don't copy, it's slower. If we really need this we can add it but let's try to |
| 145 | * avoid it. |
| 146 | */ |
| 147 | explicit LogEvent(const LogEvent&); |
| 148 | |
| 149 | /** |
| 150 | * Parses a log_msg into a LogEvent object. |
| 151 | */ |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 152 | void init(android_log_context context); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 153 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 154 | FieldValueMap mFieldValueMap; |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 155 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 156 | // This field is used when statsD wants to create log event object and write fields to it. After |
| 157 | // calling init() function, this object would be destroyed to save memory usage. |
| 158 | // When the log event is created from log msg, this field is never initiated. |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 159 | android_log_context mContext; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 160 | |
| 161 | uint64_t mTimestampNs; |
| 162 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 163 | int mTagId; |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 164 | |
| 165 | uint32_t mLogUid; |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | } // namespace statsd |
| 169 | } // namespace os |
| 170 | } // namespace android |
| 171 | |