blob: 01487f040dbbea4d777f2a8e052ba978995a4480 [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 Chenae6a83a2017-11-18 18:56:52 -080036 init(mContext);
Joe Onoratoc4dfae52017-10-17 23:38:21 -070037}
38
Yao Chen80235402017-11-13 20:42:25 -080039LogEvent::LogEvent(int32_t tagId, uint64_t timestampNs) {
40 mTimestampNs = timestampNs;
41 mTagId = tagId;
42 mContext = create_android_logger(1937006964); // the event tag shared by all stats logs
43 if (mContext) {
44 android_log_write_int32(mContext, tagId);
45 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -070046}
47
David Chen1481fe12017-10-16 13:16:34 -070048void LogEvent::init() {
Yao Chen80235402017-11-13 20:42:25 -080049 if (mContext) {
50 const char* buffer;
51 size_t len = android_log_write_list_buffer(mContext, &buffer);
52 // turns to reader mode
53 mContext = create_android_log_parser(buffer, len);
54 init(mContext);
55 }
56}
57
58bool LogEvent::write(int32_t value) {
59 if (mContext) {
60 return android_log_write_int32(mContext, value) >= 0;
61 }
62 return false;
63}
64
65bool LogEvent::write(uint32_t value) {
66 if (mContext) {
67 return android_log_write_int32(mContext, value) >= 0;
68 }
69 return false;
70}
71
Chenjie Yud9dfda72017-12-11 17:41:20 -080072bool LogEvent::write(int64_t value) {
73 if (mContext) {
74 return android_log_write_int64(mContext, value) >= 0;
75 }
76 return false;
77}
78
Yao Chen80235402017-11-13 20:42:25 -080079bool LogEvent::write(uint64_t value) {
80 if (mContext) {
81 return android_log_write_int64(mContext, value) >= 0;
82 }
83 return false;
84}
85
86bool LogEvent::write(const string& value) {
87 if (mContext) {
88 return android_log_write_string8_len(mContext, value.c_str(), value.length()) >= 0;
89 }
90 return false;
91}
92
93bool LogEvent::write(float value) {
94 if (mContext) {
95 return android_log_write_float32(mContext, value) >= 0;
96 }
97 return false;
98}
99
100LogEvent::~LogEvent() {
101 if (mContext) {
102 android_log_destroy(&mContext);
103 }
David Chen1481fe12017-10-16 13:16:34 -0700104}
105
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700106/**
107 * The elements of each log event are stored as a vector of android_log_list_elements.
108 * The goal is to do as little preprocessing as possible, because we read a tiny fraction
109 * of the elements that are written to the log.
110 */
Yao Chen80235402017-11-13 20:42:25 -0800111void LogEvent::init(android_log_context context) {
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700112 mElements.clear();
113 android_log_list_element elem;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700114 // TODO: The log is actually structured inside one list. This is convenient
115 // because we'll be able to use it to put the attribution (WorkSource) block first
116 // without doing our own tagging scheme. Until that change is in, just drop the
117 // list-related log elements and the order we get there is our index-keyed data
118 // structure.
Yao Chen80235402017-11-13 20:42:25 -0800119 int i = 0;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700120 do {
Yao Chen80235402017-11-13 20:42:25 -0800121 elem = android_log_read_next(context);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700122 switch ((int)elem.type) {
123 case EVENT_TYPE_INT:
Yao Chen80235402017-11-13 20:42:25 -0800124 // elem at [0] is EVENT_TYPE_LIST, [1] is the tag id. If we add WorkSource, it would
125 // be the list starting at [2].
126 if (i == 1) {
127 mTagId = elem.data.int32;
128 break;
129 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700130 case EVENT_TYPE_FLOAT:
131 case EVENT_TYPE_STRING:
132 case EVENT_TYPE_LONG:
133 mElements.push_back(elem);
134 break;
135 case EVENT_TYPE_LIST:
136 break;
137 case EVENT_TYPE_LIST_STOP:
138 break;
139 case EVENT_TYPE_UNKNOWN:
140 break;
141 default:
142 break;
143 }
Yao Chen80235402017-11-13 20:42:25 -0800144 i++;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700145 } while ((elem.type != EVENT_TYPE_UNKNOWN) && !elem.complete);
146}
147
148int64_t LogEvent::GetLong(size_t key, status_t* err) const {
149 if (key < 1 || (key - 1) >= mElements.size()) {
150 *err = BAD_INDEX;
151 return 0;
152 }
153 key--;
154 const android_log_list_element& elem = mElements[key];
155 if (elem.type == EVENT_TYPE_INT) {
156 return elem.data.int32;
157 } else if (elem.type == EVENT_TYPE_LONG) {
158 return elem.data.int64;
159 } else if (elem.type == EVENT_TYPE_FLOAT) {
160 return (int64_t)elem.data.float32;
161 } else {
162 *err = BAD_TYPE;
163 return 0;
164 }
165}
166
167const char* LogEvent::GetString(size_t key, status_t* err) const {
168 if (key < 1 || (key - 1) >= mElements.size()) {
169 *err = BAD_INDEX;
170 return NULL;
171 }
172 key--;
173 const android_log_list_element& elem = mElements[key];
174 if (elem.type != EVENT_TYPE_STRING) {
175 *err = BAD_TYPE;
176 return NULL;
177 }
David Chen1481fe12017-10-16 13:16:34 -0700178 // Need to add the '/0' at the end by specifying the length of the string.
179 return string(elem.data.string, elem.len).c_str();
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700180}
181
182bool LogEvent::GetBool(size_t key, status_t* err) const {
183 if (key < 1 || (key - 1) >= mElements.size()) {
184 *err = BAD_INDEX;
185 return 0;
186 }
187 key--;
188 const android_log_list_element& elem = mElements[key];
189 if (elem.type == EVENT_TYPE_INT) {
190 return elem.data.int32 != 0;
191 } else if (elem.type == EVENT_TYPE_LONG) {
192 return elem.data.int64 != 0;
193 } else if (elem.type == EVENT_TYPE_FLOAT) {
194 return elem.data.float32 != 0;
195 } else {
196 *err = BAD_TYPE;
197 return 0;
198 }
199}
200
201float LogEvent::GetFloat(size_t key, status_t* err) const {
202 if (key < 1 || (key - 1) >= mElements.size()) {
203 *err = BAD_INDEX;
204 return 0;
205 }
206 key--;
207 const android_log_list_element& elem = mElements[key];
208 if (elem.type == EVENT_TYPE_INT) {
209 return (float)elem.data.int32;
210 } else if (elem.type == EVENT_TYPE_LONG) {
211 return (float)elem.data.int64;
212 } else if (elem.type == EVENT_TYPE_FLOAT) {
213 return elem.data.float32;
214 } else {
215 *err = BAD_TYPE;
216 return 0;
217 }
218}
219
Yao Chen729093d2017-10-16 10:33:26 -0700220KeyValuePair LogEvent::GetKeyValueProto(size_t key) const {
221 KeyValuePair pair;
222 pair.set_key(key);
223 // If the value is not valid, return the KeyValuePair without assigning the value.
224 // Caller can detect the error by checking the enum for "one of" proto type.
225 if (key < 1 || (key - 1) >= mElements.size()) {
226 return pair;
227 }
228 key--;
229
230 const android_log_list_element& elem = mElements[key];
231 if (elem.type == EVENT_TYPE_INT) {
232 pair.set_value_int(elem.data.int32);
233 } else if (elem.type == EVENT_TYPE_LONG) {
Chenjie Yud9dfda72017-12-11 17:41:20 -0800234 pair.set_value_long(elem.data.int64);
Yao Chen729093d2017-10-16 10:33:26 -0700235 } else if (elem.type == EVENT_TYPE_STRING) {
236 pair.set_value_str(elem.data.string);
237 } else if (elem.type == EVENT_TYPE_FLOAT) {
238 pair.set_value_float(elem.data.float32);
239 }
240 return pair;
241}
242
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700243string LogEvent::ToString() const {
244 ostringstream result;
245 result << "{ " << mTimestampNs << " (" << mTagId << ")";
246 const size_t N = mElements.size();
247 for (size_t i=0; i<N; i++) {
248 result << " ";
249 result << (i + 1);
250 result << "->";
251 const android_log_list_element& elem = mElements[i];
252 if (elem.type == EVENT_TYPE_INT) {
253 result << elem.data.int32;
254 } else if (elem.type == EVENT_TYPE_LONG) {
255 result << elem.data.int64;
256 } else if (elem.type == EVENT_TYPE_FLOAT) {
257 result << elem.data.float32;
258 } else if (elem.type == EVENT_TYPE_STRING) {
David Chen1481fe12017-10-16 13:16:34 -0700259 // Need to add the '/0' at the end by specifying the length of the string.
260 result << string(elem.data.string, elem.len).c_str();
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700261 }
262 }
263 result << " }";
264 return result.str();
265}
266
Yao Chen5110bed2017-10-23 12:50:02 -0700267void LogEvent::ToProto(ProtoOutputStream& proto) const {
yro24809bd2017-10-31 23:06:53 -0700268 long long atomToken = proto.start(FIELD_TYPE_MESSAGE | mTagId);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700269 const size_t N = mElements.size();
270 for (size_t i=0; i<N; i++) {
271 const int key = i + 1;
272
273 const android_log_list_element& elem = mElements[i];
274 if (elem.type == EVENT_TYPE_INT) {
yro24809bd2017-10-31 23:06:53 -0700275 proto.write(FIELD_TYPE_INT32 | key, elem.data.int32);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700276 } else if (elem.type == EVENT_TYPE_LONG) {
yro24809bd2017-10-31 23:06:53 -0700277 proto.write(FIELD_TYPE_INT64 | key, (long long)elem.data.int64);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700278 } else if (elem.type == EVENT_TYPE_FLOAT) {
yro24809bd2017-10-31 23:06:53 -0700279 proto.write(FIELD_TYPE_FLOAT | key, elem.data.float32);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700280 } else if (elem.type == EVENT_TYPE_STRING) {
yro24809bd2017-10-31 23:06:53 -0700281 proto.write(FIELD_TYPE_STRING | key, elem.data.string);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700282 }
283 }
Yao Chen5110bed2017-10-23 12:50:02 -0700284 proto.end(atomToken);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700285}
286
287} // namespace statsd
288} // namespace os
289} // namespace android