blob: 2ee6bdf7c7ba6d5784431581e9732c6832f16235 [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
Yao Chen8a8d16c2018-02-08 14:50:40 -080019#include "FieldValue.h"
Joe Onoratoc4dfae52017-10-17 23:38:21 -070020
Yao Chen5110bed2017-10-23 12:50:02 -070021#include <android/util/ProtoOutputStream.h>
Joe Onoratoc4dfae52017-10-17 23:38:21 -070022#include <log/log_event_list.h>
23#include <log/log_read.h>
Yao Chen80235402017-11-13 20:42:25 -080024#include <private/android_logger.h>
Yao Chen5110bed2017-10-23 12:50:02 -070025#include <utils/Errors.h>
Joe Onoratoc4dfae52017-10-17 23:38:21 -070026
27#include <string>
28#include <vector>
29
30namespace android {
31namespace os {
32namespace statsd {
33
Yao Chen9c1debe2018-02-19 14:39:19 -080034struct AttributionNodeInternal {
35 void set_uid(int32_t id) {
36 mUid = id;
37 }
38
39 void set_tag(const std::string& value) {
40 mTag = value;
41 }
42
43 int32_t uid() const {
44 return mUid;
45 }
46
47 const std::string& tag() const {
48 return mTag;
49 }
50
51 int32_t mUid;
52 std::string mTag;
53};
Joe Onoratoc4dfae52017-10-17 23:38:21 -070054/**
55 * Wrapper for the log_msg structure.
56 */
57class LogEvent {
58public:
59 /**
60 * Read a LogEvent from a log_msg.
61 */
David Chena3bf0502017-11-01 16:57:43 -070062 explicit LogEvent(log_msg& msg);
Joe Onoratoc4dfae52017-10-17 23:38:21 -070063
64 /**
Yao Chen80235402017-11-13 20:42:25 -080065 * Constructs a LogEvent with synthetic data for testing. Must call init() before reading.
Joe Onoratoc4dfae52017-10-17 23:38:21 -070066 */
Yangster-mac330af582018-02-08 15:24:38 -080067 explicit LogEvent(int32_t tagId, int64_t wallClockTimestampNs, int64_t elapsedTimestampNs);
68
69 // For testing. The timestamp is used as both elapsed real time and logd timestamp.
70 explicit LogEvent(int32_t tagId, int64_t timestampNs);
David Chen1481fe12017-10-16 13:16:34 -070071
Yangster-mac48b3d622018-08-18 12:38:11 -070072 /**
73 * Constructs a KeyValuePairsAtom LogEvent from value maps.
74 */
75 explicit LogEvent(int32_t tagId, int64_t wallClockTimestampNs, int64_t elapsedTimestampNs,
76 int32_t uid,
77 const std::map<int32_t, int64_t>& int_map,
78 const std::map<int32_t, std::string>& string_map,
79 const std::map<int32_t, float>& float_map);
80
Joe Onoratoc4dfae52017-10-17 23:38:21 -070081 ~LogEvent();
82
83 /**
84 * Get the timestamp associated with this event.
85 */
Yangster-mac330af582018-02-08 15:24:38 -080086 inline int64_t GetLogdTimestampNs() const { return mLogdTimestampNs; }
87 inline int64_t GetElapsedTimestampNs() const { return mElapsedTimestampNs; }
Joe Onoratoc4dfae52017-10-17 23:38:21 -070088
89 /**
90 * Get the tag for this event.
91 */
Yangster-mac68985802018-01-21 10:05:09 -080092 inline int GetTagId() const { return mTagId; }
Joe Onoratoc4dfae52017-10-17 23:38:21 -070093
Yangster-mac68985802018-01-21 10:05:09 -080094 inline uint32_t GetUid() const {
Yao Chend10f7b12017-12-18 12:53:50 -080095 return mLogUid;
96 }
97
Joe Onoratoc4dfae52017-10-17 23:38:21 -070098 /**
99 * Get the nth value, starting at 1.
100 *
101 * Returns BAD_INDEX if the index is larger than the number of elements.
102 * Returns BAD_TYPE if the index is available but the data is the wrong type.
103 */
104 int64_t GetLong(size_t key, status_t* err) const;
Chenjie Yu80f91122018-01-31 20:24:50 -0800105 int GetInt(size_t key, status_t* err) const;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700106 const char* GetString(size_t key, status_t* err) const;
107 bool GetBool(size_t key, status_t* err) const;
108 float GetFloat(size_t key, status_t* err) const;
109
110 /**
Yao Chen80235402017-11-13 20:42:25 -0800111 * Write test data to the LogEvent. This can only be used when the LogEvent is constructed
112 * using LogEvent(tagId, timestampNs). You need to call init() before you can read from it.
113 */
114 bool write(uint32_t value);
115 bool write(int32_t value);
116 bool write(uint64_t value);
117 bool write(int64_t value);
Yao Chen9c1debe2018-02-19 14:39:19 -0800118 bool write(const std::string& value);
Yao Chen80235402017-11-13 20:42:25 -0800119 bool write(float value);
Yao Chen9c1debe2018-02-19 14:39:19 -0800120 bool write(const std::vector<AttributionNodeInternal>& nodes);
121 bool write(const AttributionNodeInternal& node);
Yangster-mace124e422018-08-16 10:30:28 -0700122 bool writeKeyValuePairs(const std::map<int32_t, int64_t>& int_map,
123 const std::map<int32_t, std::string>& string_map,
124 const std::map<int32_t, float>& float_map);
Yao Chen80235402017-11-13 20:42:25 -0800125
126 /**
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700127 * Return a string representation of this event.
128 */
Yao Chen9c1debe2018-02-19 14:39:19 -0800129 std::string ToString() const;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700130
131 /**
Yao Chen5110bed2017-10-23 12:50:02 -0700132 * Write this object to a ProtoOutputStream.
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700133 */
Yao Chen5110bed2017-10-23 12:50:02 -0700134 void ToProto(android::util::ProtoOutputStream& out) const;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700135
David Chen1481fe12017-10-16 13:16:34 -0700136 /**
David Chen1481fe12017-10-16 13:16:34 -0700137 * Used with the constructor where tag is passed in. Converts the log_event_list to read mode
138 * and prepares the list for reading.
139 */
140 void init();
141
Chenjie Yua7259ab2017-12-10 08:31:05 -0800142 /**
Yangster-mac330af582018-02-08 15:24:38 -0800143 * Set elapsed timestamp if the original timestamp is missing.
Chenjie Yua7259ab2017-12-10 08:31:05 -0800144 */
Yangster-mac330af582018-02-08 15:24:38 -0800145 void setElapsedTimestampNs(int64_t timestampNs) {
146 mElapsedTimestampNs = timestampNs;
147 }
148
149 /**
150 * Set the timestamp if the original logd timestamp is missing.
151 */
152 void setLogdWallClockTimestampNs(int64_t timestampNs) {
153 mLogdTimestampNs = timestampNs;
154 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800155
Yangster-mac20877162017-12-22 17:19:39 -0800156 inline int size() const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800157 return mValues.size();
Chenjie Yud9dfda72017-12-11 17:41:20 -0800158 }
159
Yao Chen8a8d16c2018-02-08 14:50:40 -0800160 const std::vector<FieldValue>& getValues() const {
161 return mValues;
162 }
Yangster-macd40053e2018-01-09 16:29:22 -0800163
Yao Chen8a8d16c2018-02-08 14:50:40 -0800164 std::vector<FieldValue>* getMutableValues() {
165 return &mValues;
166 }
Yangster-mac20877162017-12-22 17:19:39 -0800167
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700168private:
169 /**
170 * Don't copy, it's slower. If we really need this we can add it but let's try to
171 * avoid it.
172 */
173 explicit LogEvent(const LogEvent&);
174
175 /**
176 * Parses a log_msg into a LogEvent object.
177 */
Yao Chen80235402017-11-13 20:42:25 -0800178 void init(android_log_context context);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700179
Yao Chen8a8d16c2018-02-08 14:50:40 -0800180 // The items are naturally sorted in DFS order as we read them. this allows us to do fast
181 // matching.
182 std::vector<FieldValue> mValues;
Yao Chen80235402017-11-13 20:42:25 -0800183
Yangster-mac20877162017-12-22 17:19:39 -0800184 // This field is used when statsD wants to create log event object and write fields to it. After
185 // calling init() function, this object would be destroyed to save memory usage.
186 // When the log event is created from log msg, this field is never initiated.
Yao Chen48d75182018-01-23 09:40:48 -0800187 android_log_context mContext = NULL;
Yao Chen93fe3a32017-11-02 13:52:59 -0700188
Yangster-mac330af582018-02-08 15:24:38 -0800189 // The timestamp set by the logd.
190 int64_t mLogdTimestampNs;
191
192 // The elapsed timestamp set by statsd log writer.
193 int64_t mElapsedTimestampNs;
Yao Chen93fe3a32017-11-02 13:52:59 -0700194
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700195 int mTagId;
Yao Chend10f7b12017-12-18 12:53:50 -0800196
197 uint32_t mLogUid;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700198};
199
200} // namespace statsd
201} // namespace os
202} // namespace android
203