1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "hisysevent_record.h"
17
18 #include "hilog/log.h"
19 #include "hisysevent_value.h"
20
21 namespace OHOS {
22 namespace HiviewDFX {
23 namespace {
24 constexpr HiLogLabel LABEL = { LOG_CORE, 0xD002D08, "HISYSEVENT_RECORD" };
25 }
26
GetDomain() const27 std::string HiSysEventRecord::GetDomain() const
28 {
29 return GetStringValueByKey("domain_");
30 }
31
GetEventName() const32 std::string HiSysEventRecord::GetEventName() const
33 {
34 return GetStringValueByKey("name_");
35 }
36
GetEventType() const37 HiSysEvent::EventType HiSysEventRecord::GetEventType() const
38 {
39 return HiSysEvent::EventType(GetIntValueByKey("type_"));
40 }
41
GetTime() const42 uint64_t HiSysEventRecord::GetTime() const
43 {
44 return GetUInt64ValueByKey("time_");
45 }
46
GetTimeZone() const47 std::string HiSysEventRecord::GetTimeZone() const
48 {
49 return GetStringValueByKey("tz_");
50 }
51
GetPid() const52 int64_t HiSysEventRecord::GetPid() const
53 {
54 return GetInt64ValueByKey("pid_");
55 }
56
GetTid() const57 int64_t HiSysEventRecord::GetTid() const
58 {
59 return GetInt64ValueByKey("tid_");
60 }
61
GetUid() const62 int64_t HiSysEventRecord::GetUid() const
63 {
64 return GetInt64ValueByKey("uid_");
65 }
66
GetTraceId() const67 uint64_t HiSysEventRecord::GetTraceId() const
68 {
69 return GetUInt64ValueByKey("traceid_");
70 }
71
GetSpanId() const72 uint64_t HiSysEventRecord::GetSpanId() const
73 {
74 return GetUInt64ValueByKey("spanid_");
75 }
76
GetPspanId() const77 uint64_t HiSysEventRecord::GetPspanId() const
78 {
79 return GetUInt64ValueByKey("pspanid_");
80 }
81
GetTraceFlag() const82 int HiSysEventRecord::GetTraceFlag() const
83 {
84 return static_cast<int>(GetInt64ValueByKey("trace_flag_"));
85 }
86
GetLevel() const87 std::string HiSysEventRecord::GetLevel() const
88 {
89 return GetStringValueByKey("level_");
90 }
91
GetTag() const92 std::string HiSysEventRecord::GetTag() const
93 {
94 return GetStringValueByKey("tag_");
95 }
96
GetParamNames(std::vector<std::string> & params) const97 void HiSysEventRecord::GetParamNames(std::vector<std::string>& params) const
98 {
99 jsonVal_->GetParamNames(params);
100 }
101
AsJson() const102 std::string HiSysEventRecord::AsJson() const
103 {
104 return jsonStr_;
105 }
106
GetIntValueByKey(const std::string key) const107 int HiSysEventRecord::GetIntValueByKey(const std::string key) const
108 {
109 return static_cast<int>(GetInt64ValueByKey(key));
110 }
111
GetInt64ValueByKey(const std::string key) const112 int64_t HiSysEventRecord::GetInt64ValueByKey(const std::string key) const
113 {
114 int64_t value = 0;
115 (void)GetParamValue(key, value);
116 return value;
117 }
118
GetUInt64ValueByKey(const std::string key) const119 uint64_t HiSysEventRecord::GetUInt64ValueByKey(const std::string key) const
120 {
121 uint64_t value = 0;
122 (void)GetParamValue(key, value);
123 return value;
124 }
125
GetStringValueByKey(const std::string key) const126 std::string HiSysEventRecord::GetStringValueByKey(const std::string key) const
127 {
128 std::string value;
129 (void)GetParamValue(key, value);
130 return value;
131 }
132
GetParamValue(const std::string & param,int64_t & value) const133 int HiSysEventRecord::GetParamValue(const std::string& param, int64_t& value) const
134 {
135 return GetParamValue(param,
136 [this] (JsonValue val) {
137 return !(this->IsInt64ValueType(val));
138 },
139 [&value] (JsonValue src) {
140 value = src->AsInt64();
141 });
142 }
143
GetParamValue(const std::string & param,uint64_t & value) const144 int HiSysEventRecord::GetParamValue(const std::string& param, uint64_t& value) const
145 {
146 return GetParamValue(param,
147 [this] (JsonValue val) {
148 return !(this->IsUInt64ValueType(val));
149 },
150 [&value] (JsonValue src) {
151 value = src->AsUInt64();
152 });
153 }
154
GetParamValue(const std::string & param,double & value) const155 int HiSysEventRecord::GetParamValue(const std::string& param, double& value) const
156 {
157 return GetParamValue(param,
158 [this] (JsonValue val) {
159 return !(this->IsDoubleValueType(val));
160 },
161 [&value] (JsonValue src) {
162 value = src->AsDouble();
163 });
164 }
165
GetParamValue(const std::string & param,std::string & value) const166 int HiSysEventRecord::GetParamValue(const std::string& param, std::string& value) const
167 {
168 return GetParamValue(param,
169 [this] (JsonValue val) {
170 return !(this->IsStringValueType(val));
171 },
172 [&value] (JsonValue src) {
173 value = src->AsString();
174 });
175 }
176
GetParamValue(const std::string & param,std::vector<int64_t> & value) const177 int HiSysEventRecord::GetParamValue(const std::string& param, std::vector<int64_t>& value) const
178 {
179 return GetParamValue(param,
180 [this] (JsonValue val) {
181 return !(this->IsArray(val,
182 std::bind(&HiSysEventRecord::IsInt64ValueType, this, std::placeholders::_1)));
183 },
184 [&value] (JsonValue src) {
185 int arraySize = src->Size();
186 for (int i = 0; i < arraySize; i++) {
187 value.emplace_back(src->Index(i).asInt64());
188 }
189 });
190 }
191
GetParamValue(const std::string & param,std::vector<uint64_t> & value) const192 int HiSysEventRecord::GetParamValue(const std::string& param, std::vector<uint64_t>& value) const
193 {
194 return GetParamValue(param,
195 [this] (JsonValue val) {
196 return !(this->IsArray(val,
197 std::bind(&HiSysEventRecord::IsUInt64ValueType, this, std::placeholders::_1)));
198 },
199 [&value] (JsonValue src) {
200 int arraySize = src->Size();
201 for (int i = 0; i < arraySize; i++) {
202 value.emplace_back(src->Index(i).asUInt64());
203 }
204 });
205 }
206
GetParamValue(const std::string & param,std::vector<double> & value) const207 int HiSysEventRecord::GetParamValue(const std::string& param, std::vector<double>& value) const
208 {
209 return GetParamValue(param,
210 [this] (JsonValue val) {
211 return !(this->IsArray(val,
212 std::bind(&HiSysEventRecord::IsDoubleValueType, this, std::placeholders::_1)));
213 },
214 [&value] (JsonValue src) {
215 int arraySize = src->Size();
216 for (int i = 0; i < arraySize; i++) {
217 value.emplace_back(src->Index(i).asDouble());
218 }
219 });
220 }
221
GetParamValue(const std::string & param,std::vector<std::string> & value) const222 int HiSysEventRecord::GetParamValue(const std::string& param, std::vector<std::string>& value) const
223 {
224 return GetParamValue(param,
225 [this] (JsonValue val) {
226 return !(this->IsArray(val,
227 std::bind(&HiSysEventRecord::IsStringValueType, this, std::placeholders::_1)));
228 },
229 [&value] (JsonValue src) {
230 int arraySize = src->Size();
231 for (int i = 0; i < arraySize; i++) {
232 value.emplace_back(src->Index(i).asString());
233 }
234 });
235 }
236
ParseJsonStr(const std::string jsonStr)237 void HiSysEventRecord::ParseJsonStr(const std::string jsonStr)
238 {
239 jsonVal_ = std::make_shared<HiSysEventValue>(jsonStr);
240 jsonStr_ = jsonStr;
241 }
242
GetParamValue(const std::string & param,const TypeFilter filterFunc,const ValueAssigner assignFunc) const243 int HiSysEventRecord::GetParamValue(const std::string& param, const TypeFilter filterFunc,
244 const ValueAssigner assignFunc) const
245 {
246 if (!jsonVal_->HasInitialized()) {
247 HiLog::Debug(LABEL, "this hisysevent record is not initialized");
248 return ERR_INIT_FAILED;
249 }
250 if (!jsonVal_->IsMember(param)) {
251 HiLog::Debug(LABEL, "key named \"%{public}s\" is not found in json.",
252 param.c_str());
253 return ERR_KEY_NOT_EXIST;
254 }
255 auto parsedVal = std::make_shared<HiSysEventValue>(jsonVal_->GetParamValue(param));
256 if (filterFunc(parsedVal)) {
257 HiLog::Debug(LABEL, "value type with key named \"%{public}s\" is %{public}d, not match.",
258 param.c_str(), parsedVal->Type());
259 return ERR_TYPE_NOT_MATCH;
260 }
261 assignFunc(parsedVal);
262 return VALUE_PARSED_SUCCEED;
263 }
264
IsInt64ValueType(const JsonValue val) const265 bool HiSysEventRecord::IsInt64ValueType(const JsonValue val) const
266 {
267 return val->IsInt64() || val->IsNull() || val->IsBool();
268 }
269
IsUInt64ValueType(const JsonValue val) const270 bool HiSysEventRecord::IsUInt64ValueType(const JsonValue val) const
271 {
272 return val->IsUInt64() || val->IsNull() || val->IsBool();
273 }
274
IsDoubleValueType(const JsonValue val) const275 bool HiSysEventRecord::IsDoubleValueType(const JsonValue val) const
276 {
277 return val->IsDouble() || val->IsNull() || val->IsBool();
278 }
279
IsStringValueType(const JsonValue val) const280 bool HiSysEventRecord::IsStringValueType(const JsonValue val) const
281 {
282 return val->IsNull() || val->IsBool() || val->IsNumeric() || val->IsString();
283 }
284
IsArray(const JsonValue val,const TypeFilter filterFunc) const285 bool HiSysEventRecord::IsArray(const JsonValue val, const TypeFilter filterFunc) const
286 {
287 if (!val->IsArray()) {
288 return false;
289 }
290 if (val->Size() > 0) {
291 return filterFunc(std::make_shared<HiSysEventValue>(val->Index(0)));
292 }
293 return (val->Size() == 0);
294 }
295
ParseJsonStr(const std::string jsonStr)296 void HiSysEventValue::ParseJsonStr(const std::string jsonStr)
297 {
298 #ifdef JSONCPP_VERSION_STRING
299 Json::CharReaderBuilder jsonRBuilder;
300 Json::CharReaderBuilder::strictMode(&jsonRBuilder.settings_);
301 std::unique_ptr<Json::CharReader> const reader(jsonRBuilder.newCharReader());
302 JSONCPP_STRING errs;
303 if (!reader->parse(jsonStr.data(), jsonStr.data() + jsonStr.size(), &jsonVal_, &errs)) {
304 #else
305 Json::Reader reader(Json::Features::strictMode());
306 if (!reader.parse(jsonStr, jsonVal_)) {
307 #endif
308 HiLog::Error(LABEL, "parse json file failed, please check the style of json string: %{public}s.",
309 jsonStr.c_str());
310 return;
311 }
312 hasInitialized_ = true;
313 }
314
315 bool HiSysEventValue::HasInitialized() const
316 {
317 return hasInitialized_;
318 }
319
320 void HiSysEventValue::GetParamNames(std::vector<std::string>& params) const
321 {
322 params = jsonVal_.getMemberNames();
323 }
324
325 bool HiSysEventValue::IsArray() const
326 {
327 return jsonVal_.isArray();
328 }
329
330 bool HiSysEventValue::IsMember(const std::string key) const
331 {
332 return jsonVal_.isMember(key);
333 }
334
335 bool HiSysEventValue::IsInt64() const
336 {
337 return jsonVal_.isInt64();
338 }
339
340 bool HiSysEventValue::IsUInt64() const
341 {
342 return jsonVal_.isUInt64();
343 }
344
345 bool HiSysEventValue::IsDouble() const
346 {
347 return jsonVal_.isDouble();
348 }
349
350 bool HiSysEventValue::IsString() const
351 {
352 return jsonVal_.isString();
353 }
354
355 bool HiSysEventValue::IsBool() const
356 {
357 return jsonVal_.isBool();
358 }
359
360 bool HiSysEventValue::IsNull() const
361 {
362 return jsonVal_.isNull();
363 }
364
365 bool HiSysEventValue::IsNumeric() const
366 {
367 return jsonVal_.isNumeric();
368 }
369
370 Json::Value HiSysEventValue::Index(const int index) const
371 {
372 return jsonVal_[index];
373 }
374 Json::Value HiSysEventValue::GetParamValue(const std::string& key) const
375 {
376 return jsonVal_[key];
377 }
378
379 int HiSysEventValue::Size() const
380 {
381 return jsonVal_.size();
382 }
383
384 int64_t HiSysEventValue::AsInt64() const
385 {
386 return jsonVal_.asInt64();
387 }
388
389 uint64_t HiSysEventValue::AsUInt64() const
390 {
391 return jsonVal_.asUInt64();
392 }
393
394 double HiSysEventValue::AsDouble() const
395 {
396 return jsonVal_.asDouble();
397 }
398
399 std::string HiSysEventValue::AsString() const
400 {
401 return jsonVal_.asString();
402 }
403
404 Json::ValueType HiSysEventValue::Type() const
405 {
406 return jsonVal_.type();
407 }
408 } // HiviewDFX
409 } // OHOS