blob: fdfa32eac8ae1cfcf522fd021409a933848a744c [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
17#pragma once
18
Yangster-mac20877162017-12-22 17:19:39 -080019#include "field_util.h"
Joe Onoratoc4dfae52017-10-17 23:38:21 -070020#include "frameworks/base/cmds/statsd/src/stats_log.pb.h"
21
Yao Chen5110bed2017-10-23 12:50:02 -070022#include <android/util/ProtoOutputStream.h>
Joe Onoratoc4dfae52017-10-17 23:38:21 -070023#include <log/log_event_list.h>
24#include <log/log_read.h>
Yao Chen80235402017-11-13 20:42:25 -080025#include <private/android_logger.h>
Yao Chen5110bed2017-10-23 12:50:02 -070026#include <utils/Errors.h>
Yangster-mac20877162017-12-22 17:19:39 -080027#include <utils/JenkinsHash.h>
Joe Onoratoc4dfae52017-10-17 23:38:21 -070028
David Chen1481fe12017-10-16 13:16:34 -070029#include <memory>
Joe Onoratoc4dfae52017-10-17 23:38:21 -070030#include <string>
Yangster-mac20877162017-12-22 17:19:39 -080031#include <map>
Joe Onoratoc4dfae52017-10-17 23:38:21 -070032#include <vector>
33
34namespace android {
35namespace os {
36namespace statsd {
37
38using std::string;
39using std::vector;
40
41/**
42 * Wrapper for the log_msg structure.
43 */
44class LogEvent {
45public:
46 /**
47 * Read a LogEvent from a log_msg.
48 */
David Chena3bf0502017-11-01 16:57:43 -070049 explicit LogEvent(log_msg& msg);
Joe Onoratoc4dfae52017-10-17 23:38:21 -070050
51 /**
Yao Chen80235402017-11-13 20:42:25 -080052 * Constructs a LogEvent with synthetic data for testing. Must call init() before reading.
Joe Onoratoc4dfae52017-10-17 23:38:21 -070053 */
Yao Chen80235402017-11-13 20:42:25 -080054 explicit LogEvent(int32_t tagId, uint64_t timestampNs);
David Chen1481fe12017-10-16 13:16:34 -070055
Joe Onoratoc4dfae52017-10-17 23:38:21 -070056 ~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 Chend10f7b12017-12-18 12:53:50 -080068 uint32_t GetUid() const {
69 return mLogUid;
70 }
71
Joe Onoratoc4dfae52017-10-17 23:38:21 -070072 /**
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-mac20877162017-12-22 17:19:39 -080083 /*
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 Onoratoc4dfae52017-10-17 23:38:21 -070097 /**
Yao Chen80235402017-11-13 20:42:25 -080098 * 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-mac20877162017-12-22 17:19:39 -0800107 bool write(const std::vector<AttributionNode>& nodes);
108 bool write(const AttributionNode& node);
Yao Chen80235402017-11-13 20:42:25 -0800109
110 /**
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700111 * Return a string representation of this event.
112 */
113 string ToString() const;
114
115 /**
Yao Chen5110bed2017-10-23 12:50:02 -0700116 * Write this object to a ProtoOutputStream.
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700117 */
Yao Chen5110bed2017-10-23 12:50:02 -0700118 void ToProto(android::util::ProtoOutputStream& out) const;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700119
David Chen1481fe12017-10-16 13:16:34 -0700120 /**
David Chen1481fe12017-10-16 13:16:34 -0700121 * 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 Yua7259ab2017-12-10 08:31:05 -0800126 /**
127 * Set timestamp if the original timestamp is missing.
128 */
129 void setTimestampNs(uint64_t timestampNs) {mTimestampNs = timestampNs;}
130
Yangster-mac20877162017-12-22 17:19:39 -0800131 inline int size() const {
132 return mFieldValueMap.size();
Chenjie Yud9dfda72017-12-11 17:41:20 -0800133 }
134
Yangster-macd40053e2018-01-09 16:29:22 -0800135 /**
136 * Returns the mutable DimensionsValue proto for the specific the field.
137 */
138 DimensionsValue* findFieldValueOrNull(const Field& field);
139
Yangster-mac20877162017-12-22 17:19:39 -0800140 inline const FieldValueMap& getFieldValueMap() const { return mFieldValueMap; }
141
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700142private:
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 Chen80235402017-11-13 20:42:25 -0800152 void init(android_log_context context);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700153
Yangster-mac20877162017-12-22 17:19:39 -0800154 FieldValueMap mFieldValueMap;
Yao Chen80235402017-11-13 20:42:25 -0800155
Yangster-mac20877162017-12-22 17:19:39 -0800156 // 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 Chen80235402017-11-13 20:42:25 -0800159 android_log_context mContext;
Yao Chen93fe3a32017-11-02 13:52:59 -0700160
161 uint64_t mTimestampNs;
162
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700163 int mTagId;
Yao Chend10f7b12017-12-18 12:53:50 -0800164
165 uint32_t mLogUid;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700166};
167
168} // namespace statsd
169} // namespace os
170} // namespace android
171