blob: d660b5f9fe02ed28a89f44e59bf62f98fc492634 [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
Chenjie Yub3dda412017-10-24 13:41:59 -070017#define DEBUG true // STOPSHIP if true
Joe Onoratoc4dfae52017-10-17 23:38:21 -070018#include "logd/LogEvent.h"
19
20#include <sstream>
Yao Chen5110bed2017-10-23 12:50:02 -070021#include "stats_util.h"
Joe Onoratoc4dfae52017-10-17 23:38:21 -070022
23namespace android {
24namespace os {
25namespace statsd {
26
yro24809bd2017-10-31 23:06:53 -070027using namespace android::util;
Joe Onoratoc4dfae52017-10-17 23:38:21 -070028using std::ostringstream;
David Chen1481fe12017-10-16 13:16:34 -070029using std::string;
Yao Chen5110bed2017-10-23 12:50:02 -070030using android::util::ProtoOutputStream;
Joe Onoratoc4dfae52017-10-17 23:38:21 -070031
Yao Chen80235402017-11-13 20:42:25 -080032LogEvent::LogEvent(log_msg& msg) {
Yao Chenae6a83a2017-11-18 18:56:52 -080033 mContext =
Yao Chen80235402017-11-13 20:42:25 -080034 create_android_log_parser(msg.msg() + sizeof(uint32_t), msg.len() - sizeof(uint32_t));
35 mTimestampNs = msg.entry_v1.sec * NS_PER_SEC + msg.entry_v1.nsec;
Yao Chend10f7b12017-12-18 12:53:50 -080036 mLogUid = msg.entry_v4.uid;
Yao Chenae6a83a2017-11-18 18:56:52 -080037 init(mContext);
Joe Onoratoc4dfae52017-10-17 23:38:21 -070038}
39
Yao Chen80235402017-11-13 20:42:25 -080040LogEvent::LogEvent(int32_t tagId, uint64_t timestampNs) {
41 mTimestampNs = timestampNs;
42 mTagId = tagId;
43 mContext = create_android_logger(1937006964); // the event tag shared by all stats logs
44 if (mContext) {
45 android_log_write_int32(mContext, tagId);
46 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -070047}
48
David Chen1481fe12017-10-16 13:16:34 -070049void LogEvent::init() {
Yao Chen80235402017-11-13 20:42:25 -080050 if (mContext) {
51 const char* buffer;
52 size_t len = android_log_write_list_buffer(mContext, &buffer);
53 // turns to reader mode
54 mContext = create_android_log_parser(buffer, len);
55 init(mContext);
56 }
57}
58
59bool LogEvent::write(int32_t value) {
60 if (mContext) {
61 return android_log_write_int32(mContext, value) >= 0;
62 }
63 return false;
64}
65
66bool LogEvent::write(uint32_t value) {
67 if (mContext) {
68 return android_log_write_int32(mContext, value) >= 0;
69 }
70 return false;
71}
72
Chenjie Yud9dfda72017-12-11 17:41:20 -080073bool LogEvent::write(int64_t value) {
74 if (mContext) {
75 return android_log_write_int64(mContext, value) >= 0;
76 }
77 return false;
78}
79
Yao Chen80235402017-11-13 20:42:25 -080080bool LogEvent::write(uint64_t value) {
81 if (mContext) {
82 return android_log_write_int64(mContext, value) >= 0;
83 }
84 return false;
85}
86
87bool LogEvent::write(const string& value) {
88 if (mContext) {
89 return android_log_write_string8_len(mContext, value.c_str(), value.length()) >= 0;
90 }
91 return false;
92}
93
94bool LogEvent::write(float value) {
95 if (mContext) {
96 return android_log_write_float32(mContext, value) >= 0;
97 }
98 return false;
99}
100
101LogEvent::~LogEvent() {
102 if (mContext) {
103 android_log_destroy(&mContext);
104 }
David Chen1481fe12017-10-16 13:16:34 -0700105}
106
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700107/**
108 * The elements of each log event are stored as a vector of android_log_list_elements.
109 * The goal is to do as little preprocessing as possible, because we read a tiny fraction
110 * of the elements that are written to the log.
111 */
Yao Chen80235402017-11-13 20:42:25 -0800112void LogEvent::init(android_log_context context) {
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700113 mElements.clear();
114 android_log_list_element elem;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700115 // TODO: The log is actually structured inside one list. This is convenient
116 // because we'll be able to use it to put the attribution (WorkSource) block first
117 // without doing our own tagging scheme. Until that change is in, just drop the
118 // list-related log elements and the order we get there is our index-keyed data
119 // structure.
Yao Chen80235402017-11-13 20:42:25 -0800120 int i = 0;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700121 do {
Yao Chen80235402017-11-13 20:42:25 -0800122 elem = android_log_read_next(context);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700123 switch ((int)elem.type) {
124 case EVENT_TYPE_INT:
Yao Chen80235402017-11-13 20:42:25 -0800125 // elem at [0] is EVENT_TYPE_LIST, [1] is the tag id. If we add WorkSource, it would
126 // be the list starting at [2].
127 if (i == 1) {
128 mTagId = elem.data.int32;
129 break;
130 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700131 case EVENT_TYPE_FLOAT:
132 case EVENT_TYPE_STRING:
133 case EVENT_TYPE_LONG:
134 mElements.push_back(elem);
135 break;
136 case EVENT_TYPE_LIST:
137 break;
138 case EVENT_TYPE_LIST_STOP:
139 break;
140 case EVENT_TYPE_UNKNOWN:
141 break;
142 default:
143 break;
144 }
Yao Chen80235402017-11-13 20:42:25 -0800145 i++;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700146 } while ((elem.type != EVENT_TYPE_UNKNOWN) && !elem.complete);
147}
148
149int64_t LogEvent::GetLong(size_t key, status_t* err) const {
150 if (key < 1 || (key - 1) >= mElements.size()) {
151 *err = BAD_INDEX;
152 return 0;
153 }
154 key--;
155 const android_log_list_element& elem = mElements[key];
156 if (elem.type == EVENT_TYPE_INT) {
157 return elem.data.int32;
158 } else if (elem.type == EVENT_TYPE_LONG) {
159 return elem.data.int64;
160 } else if (elem.type == EVENT_TYPE_FLOAT) {
161 return (int64_t)elem.data.float32;
162 } else {
163 *err = BAD_TYPE;
164 return 0;
165 }
166}
167
168const char* LogEvent::GetString(size_t key, status_t* err) const {
169 if (key < 1 || (key - 1) >= mElements.size()) {
170 *err = BAD_INDEX;
171 return NULL;
172 }
173 key--;
174 const android_log_list_element& elem = mElements[key];
175 if (elem.type != EVENT_TYPE_STRING) {
176 *err = BAD_TYPE;
177 return NULL;
178 }
David Chen1481fe12017-10-16 13:16:34 -0700179 // Need to add the '/0' at the end by specifying the length of the string.
180 return string(elem.data.string, elem.len).c_str();
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700181}
182
183bool LogEvent::GetBool(size_t key, status_t* err) const {
184 if (key < 1 || (key - 1) >= mElements.size()) {
185 *err = BAD_INDEX;
186 return 0;
187 }
188 key--;
189 const android_log_list_element& elem = mElements[key];
190 if (elem.type == EVENT_TYPE_INT) {
191 return elem.data.int32 != 0;
192 } else if (elem.type == EVENT_TYPE_LONG) {
193 return elem.data.int64 != 0;
194 } else if (elem.type == EVENT_TYPE_FLOAT) {
195 return elem.data.float32 != 0;
196 } else {
197 *err = BAD_TYPE;
198 return 0;
199 }
200}
201
202float LogEvent::GetFloat(size_t key, status_t* err) const {
203 if (key < 1 || (key - 1) >= mElements.size()) {
204 *err = BAD_INDEX;
205 return 0;
206 }
207 key--;
208 const android_log_list_element& elem = mElements[key];
209 if (elem.type == EVENT_TYPE_INT) {
210 return (float)elem.data.int32;
211 } else if (elem.type == EVENT_TYPE_LONG) {
212 return (float)elem.data.int64;
213 } else if (elem.type == EVENT_TYPE_FLOAT) {
214 return elem.data.float32;
215 } else {
216 *err = BAD_TYPE;
217 return 0;
218 }
219}
220
Yao Chen729093d2017-10-16 10:33:26 -0700221KeyValuePair LogEvent::GetKeyValueProto(size_t key) const {
222 KeyValuePair pair;
223 pair.set_key(key);
224 // If the value is not valid, return the KeyValuePair without assigning the value.
225 // Caller can detect the error by checking the enum for "one of" proto type.
226 if (key < 1 || (key - 1) >= mElements.size()) {
227 return pair;
228 }
229 key--;
230
231 const android_log_list_element& elem = mElements[key];
232 if (elem.type == EVENT_TYPE_INT) {
233 pair.set_value_int(elem.data.int32);
234 } else if (elem.type == EVENT_TYPE_LONG) {
Chenjie Yud9dfda72017-12-11 17:41:20 -0800235 pair.set_value_long(elem.data.int64);
Yao Chen729093d2017-10-16 10:33:26 -0700236 } else if (elem.type == EVENT_TYPE_STRING) {
237 pair.set_value_str(elem.data.string);
238 } else if (elem.type == EVENT_TYPE_FLOAT) {
239 pair.set_value_float(elem.data.float32);
240 }
241 return pair;
242}
243
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700244string LogEvent::ToString() const {
245 ostringstream result;
246 result << "{ " << mTimestampNs << " (" << mTagId << ")";
247 const size_t N = mElements.size();
248 for (size_t i=0; i<N; i++) {
249 result << " ";
250 result << (i + 1);
251 result << "->";
252 const android_log_list_element& elem = mElements[i];
253 if (elem.type == EVENT_TYPE_INT) {
254 result << elem.data.int32;
255 } else if (elem.type == EVENT_TYPE_LONG) {
256 result << elem.data.int64;
257 } else if (elem.type == EVENT_TYPE_FLOAT) {
258 result << elem.data.float32;
259 } else if (elem.type == EVENT_TYPE_STRING) {
David Chen1481fe12017-10-16 13:16:34 -0700260 // Need to add the '/0' at the end by specifying the length of the string.
261 result << string(elem.data.string, elem.len).c_str();
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700262 }
263 }
264 result << " }";
265 return result.str();
266}
267
Yao Chen5110bed2017-10-23 12:50:02 -0700268void LogEvent::ToProto(ProtoOutputStream& proto) const {
yro24809bd2017-10-31 23:06:53 -0700269 long long atomToken = proto.start(FIELD_TYPE_MESSAGE | mTagId);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700270 const size_t N = mElements.size();
271 for (size_t i=0; i<N; i++) {
272 const int key = i + 1;
273
274 const android_log_list_element& elem = mElements[i];
275 if (elem.type == EVENT_TYPE_INT) {
yro24809bd2017-10-31 23:06:53 -0700276 proto.write(FIELD_TYPE_INT32 | key, elem.data.int32);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700277 } else if (elem.type == EVENT_TYPE_LONG) {
yro24809bd2017-10-31 23:06:53 -0700278 proto.write(FIELD_TYPE_INT64 | key, (long long)elem.data.int64);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700279 } else if (elem.type == EVENT_TYPE_FLOAT) {
yro24809bd2017-10-31 23:06:53 -0700280 proto.write(FIELD_TYPE_FLOAT | key, elem.data.float32);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700281 } else if (elem.type == EVENT_TYPE_STRING) {
yro24809bd2017-10-31 23:06:53 -0700282 proto.write(FIELD_TYPE_STRING | key, elem.data.string);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700283 }
284 }
Yao Chen5110bed2017-10-23 12:50:02 -0700285 proto.end(atomToken);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700286}
287
288} // namespace statsd
289} // namespace os
290} // namespace android