1 /*
2 * Copyright (c) 2021 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 "hiappevent_base.h"
17
18 #include <cmath>
19 #include <ctime>
20 #include <iomanip>
21 #include <iostream>
22 #include <sstream>
23 #include <string>
24 #include <sys/time.h>
25 #include <unistd.h>
26 #include <vector>
27
28 #include "hilog/log.h"
29
30 namespace OHOS {
31 namespace HiviewDFX {
32 namespace {
33 const HiLogLabel LABEL = { LOG_CORE, HIAPPEVENT_DOMAIN, "HiAppEvent_base" };
34 constexpr int SEC_TO_HOUR = 3600;
35 constexpr int SEC_TO_MIN = 60;
36
TrimRightZero(const std::string & str)37 std::string TrimRightZero(const std::string& str)
38 {
39 auto endIndex = str.find_last_not_of("0");
40 if (endIndex == std::string::npos) {
41 return str;
42 }
43
44 return (str[endIndex] == '.') ? str.substr(0, endIndex) : str.substr(0, endIndex + 1);
45 }
46
GetTimeInfo()47 std::string GetTimeInfo()
48 {
49 // get system timestamp
50 struct timeval tv;
51 if (gettimeofday(&tv, NULL) != 0) {
52 HiLog::Error(LABEL, "failed to execute the gettimeofday function.");
53 return "";
54 }
55 long long timeMillSec = static_cast<long long>(tv.tv_sec) * 1000 + tv.tv_usec / 1000; // 1000 means milliseconds
56 std::stringstream ss;
57 ss << "\"" << "time_" << "\":" << std::to_string(timeMillSec) << ",";
58
59 // Get system time zone
60 time_t sysSec = tv.tv_sec;
61 struct tm tmLocal;
62 if (localtime_r(&sysSec, &tmLocal) == nullptr) {
63 HiLog::Error(LABEL, "failed to get local time.");
64 return ss.str();
65 }
66 struct tm tmUtc;
67 if (gmtime_r(&sysSec, &tmUtc) == nullptr) {
68 HiLog::Error(LABEL, "failed to get GMT time.");
69 return ss.str();
70 }
71 time_t diffSec = mktime(&tmLocal) - mktime(&tmUtc);
72 ss << "\"tz_\":\"" << ((diffSec < 0) ? "-" : "+");
73
74 int tzHour = std::abs(diffSec) / SEC_TO_HOUR;
75 if (tzHour > 12) { // max time zone is 12
76 HiLog::Error(LABEL, "failed to get hours for time zone, set to 0.");
77 tzHour = 0;
78 }
79 int tzMin = (std::abs(diffSec) % SEC_TO_HOUR) / SEC_TO_MIN;
80 ss << std::setw(2) << std::setfill('0') << tzHour; // the number of digits in the hour is 2
81 ss << std::setw(2) << std::setfill('0') << tzMin << "\","; // the number of digits in the min is 2
82
83 return ss.str();
84 }
85
InitValueByBaseType(AppEventParamValue * value,const AppEventParamValue & other)86 void InitValueByBaseType(AppEventParamValue* value, const AppEventParamValue& other)
87 {
88 if (value == nullptr) {
89 return;
90 }
91
92 switch (other.type) {
93 case AppEventParamType::BOOL:
94 value->valueUnion.b_ = other.valueUnion.b_;
95 break;
96 case AppEventParamType::CHAR:
97 value->valueUnion.c_ = other.valueUnion.c_;
98 break;
99 case AppEventParamType::SHORT:
100 value->valueUnion.sh_ = other.valueUnion.sh_;
101 break;
102 case AppEventParamType::INTEGER:
103 value->valueUnion.i_ = other.valueUnion.i_;
104 break;
105 case AppEventParamType::LONG:
106 value->valueUnion.l_ = other.valueUnion.l_;
107 break;
108 case AppEventParamType::LONGLONG:
109 value->valueUnion.ll_ = other.valueUnion.ll_;
110 break;
111 case AppEventParamType::FLOAT:
112 value->valueUnion.f_ = other.valueUnion.f_;
113 break;
114 case AppEventParamType::DOUBLE:
115 value->valueUnion.d_ = other.valueUnion.d_;
116 break;
117 default:
118 break;
119 }
120 }
121
InitValueByReferType(AppEventParamValue * value,const AppEventParamValue & other)122 void InitValueByReferType(AppEventParamValue* value, const AppEventParamValue& other)
123 {
124 if (value == nullptr) {
125 return;
126 }
127
128 switch (other.type) {
129 case AppEventParamType::STRING:
130 new (&value->valueUnion.str_) auto(other.valueUnion.str_);
131 break;
132 case AppEventParamType::BVECTOR:
133 new (&value->valueUnion.bs_) auto(other.valueUnion.bs_);
134 break;
135 case AppEventParamType::CVECTOR:
136 new (&value->valueUnion.cs_) auto(other.valueUnion.cs_);
137 break;
138 case AppEventParamType::SHVECTOR:
139 new (&value->valueUnion.shs_) auto(other.valueUnion.shs_);
140 break;
141 case AppEventParamType::IVECTOR:
142 new (&value->valueUnion.is_) auto(other.valueUnion.is_);
143 break;
144 case AppEventParamType::LVECTOR:
145 new (&value->valueUnion.ls_) auto(other.valueUnion.ls_);
146 break;
147 case AppEventParamType::LLVECTOR:
148 new (&value->valueUnion.lls_) auto(other.valueUnion.lls_);
149 break;
150 case AppEventParamType::FVECTOR:
151 new (&value->valueUnion.fs_) auto(other.valueUnion.fs_);
152 break;
153 case AppEventParamType::DVECTOR:
154 new (&value->valueUnion.ds_) auto(other.valueUnion.ds_);
155 break;
156 case AppEventParamType::STRVECTOR:
157 new (&value->valueUnion.strs_) auto(other.valueUnion.strs_);
158 break;
159 default:
160 break;
161 }
162 }
163 }
164
AppEventParamValue(AppEventParamType t)165 AppEventParamValue::AppEventParamValue(AppEventParamType t) : type(t), valueUnion(t)
166 {}
167
AppEventParamValue(const AppEventParamValue & other)168 AppEventParamValue::AppEventParamValue(const AppEventParamValue& other) : type(other.type)
169 {
170 if (other.type < AppEventParamType::STRING) {
171 InitValueByBaseType(this, other);
172 } else {
173 InitValueByReferType(this, other);
174 }
175 }
176
~AppEventParamValue()177 AppEventParamValue::~AppEventParamValue()
178 {
179 switch (type) {
180 case AppEventParamType::STRING:
181 valueUnion.str_.~basic_string();
182 break;
183 case AppEventParamType::BVECTOR:
184 valueUnion.bs_.~vector();
185 break;
186 case AppEventParamType::CVECTOR:
187 valueUnion.cs_.~vector();
188 break;
189 case AppEventParamType::SHVECTOR:
190 valueUnion.shs_.~vector();
191 break;
192 case AppEventParamType::IVECTOR:
193 valueUnion.is_.~vector();
194 break;
195 case AppEventParamType::LVECTOR:
196 valueUnion.ls_.~vector();
197 break;
198 case AppEventParamType::LLVECTOR:
199 valueUnion.lls_.~vector();
200 break;
201 case AppEventParamType::FVECTOR:
202 valueUnion.fs_.~vector();
203 break;
204 case AppEventParamType::DVECTOR:
205 valueUnion.ds_.~vector();
206 break;
207 case AppEventParamType::STRVECTOR:
208 valueUnion.strs_.~vector();
209 break;
210 default:
211 break;
212 }
213 }
214
AppEventParam(std::string n,AppEventParamType t)215 AppEventParam::AppEventParam(std::string n, AppEventParamType t) : name(n), type(t), value(t)
216 {}
217
AppEventParam(const AppEventParam & param)218 AppEventParam::AppEventParam(const AppEventParam& param) : name(param.name), type(param.type), value(param.value)
219 {}
220
~AppEventParam()221 AppEventParam::~AppEventParam()
222 {}
223
AppEventPack(const std::string & eventName,int type)224 AppEventPack::AppEventPack(const std::string& eventName, int type): eventName_(eventName), type_(type)
225 {}
226
~AppEventPack()227 AppEventPack::~AppEventPack()
228 {}
229
AddParam(const std::string & key)230 void AppEventPack::AddParam(const std::string& key)
231 {
232 AppEventParam appEventParam(key, AppEventParamType::EMPTY);
233 baseParams_.emplace_back(appEventParam);
234 }
235
AddParam(const std::string & key,bool b)236 void AppEventPack::AddParam(const std::string& key, bool b)
237 {
238 AppEventParam appEventParam(key, AppEventParamType::BOOL);
239 appEventParam.value.valueUnion.b_ = b;
240 baseParams_.emplace_back(appEventParam);
241 }
242
AddParam(const std::string & key,char c)243 void AppEventPack::AddParam(const std::string& key, char c)
244 {
245 AppEventParam appEventParam(key, AppEventParamType::CHAR);
246 appEventParam.value.valueUnion.c_ = c;
247 baseParams_.emplace_back(appEventParam);
248 }
249
AddParam(const std::string & key,int8_t num)250 void AppEventPack::AddParam(const std::string& key, int8_t num)
251 {
252 AppEventParam appEventParam(key, AppEventParamType::SHORT);
253 appEventParam.value.valueUnion.sh_ = static_cast<int16_t>(num);
254 baseParams_.emplace_back(appEventParam);
255 }
256
AddParam(const std::string & key,short s)257 void AppEventPack::AddParam(const std::string& key, short s)
258 {
259 AppEventParam appEventParam(key, AppEventParamType::SHORT);
260 appEventParam.value.valueUnion.sh_ = s;
261 baseParams_.emplace_back(appEventParam);
262 }
263
AddParam(const std::string & key,int i)264 void AppEventPack::AddParam(const std::string& key, int i)
265 {
266 AppEventParam appEventParam(key, AppEventParamType::INTEGER);
267 appEventParam.value.valueUnion.i_ = i;
268 baseParams_.emplace_back(appEventParam);
269 }
270
AddParam(const std::string & key,long l)271 void AppEventPack::AddParam(const std::string& key, long l)
272 {
273 AppEventParam appEventParam(key, AppEventParamType::LONG);
274 appEventParam.value.valueUnion.l_ = l;
275 baseParams_.emplace_back(appEventParam);
276 }
277
AddParam(const std::string & key,long long ll)278 void AppEventPack::AddParam(const std::string& key, long long ll)
279 {
280 AppEventParam appEventParam(key, AppEventParamType::LONGLONG);
281 appEventParam.value.valueUnion.ll_ = ll;
282 baseParams_.emplace_back(appEventParam);
283 }
284
AddParam(const std::string & key,float f)285 void AppEventPack::AddParam(const std::string& key, float f)
286 {
287 AppEventParam appEventParam(key, AppEventParamType::FLOAT);
288 appEventParam.value.valueUnion.f_ = f;
289 baseParams_.emplace_back(appEventParam);
290 }
291
AddParam(const std::string & key,double d)292 void AppEventPack::AddParam(const std::string& key, double d)
293 {
294 AppEventParam appEventParam(key, AppEventParamType::DOUBLE);
295 appEventParam.value.valueUnion.d_ = d;
296 baseParams_.emplace_back(appEventParam);
297 }
298
AddParam(const std::string & key,const char * s)299 void AppEventPack::AddParam(const std::string& key, const char *s)
300 {
301 AppEventParam appEventParam(key, AppEventParamType::STRING);
302 appEventParam.value.valueUnion.str_ = s;
303 baseParams_.push_back(appEventParam);
304 }
305
AddParam(const std::string & key,const std::string & s)306 void AppEventPack::AddParam(const std::string& key, const std::string& s)
307 {
308 AppEventParam appEventParam(key, AppEventParamType::STRING);
309 appEventParam.value.valueUnion.str_ = s;
310 baseParams_.push_back(appEventParam);
311 }
312
AddParam(const std::string & key,const std::vector<bool> & bs)313 void AppEventPack::AddParam(const std::string& key, const std::vector<bool>& bs)
314 {
315 AppEventParam appEventParam(key, AppEventParamType::BVECTOR);
316 appEventParam.value.valueUnion.bs_.assign(bs.begin(), bs.end());
317 baseParams_.push_back(appEventParam);
318 }
319
AddParam(const std::string & key,const std::vector<char> & cs)320 void AppEventPack::AddParam(const std::string& key, const std::vector<char>& cs)
321 {
322 AppEventParam appEventParam(key, AppEventParamType::CVECTOR);
323 appEventParam.value.valueUnion.cs_.assign(cs.begin(), cs.end());
324 baseParams_.push_back(appEventParam);
325 }
326
AddParam(const std::string & key,const std::vector<int8_t> & shs)327 void AppEventPack::AddParam(const std::string& key, const std::vector<int8_t>& shs)
328 {
329 AppEventParam appEventParam(key, AppEventParamType::SHVECTOR);
330 appEventParam.value.valueUnion.shs_.assign(shs.begin(), shs.end());
331 baseParams_.push_back(appEventParam);
332 }
333
AddParam(const std::string & key,const std::vector<short> & shs)334 void AppEventPack::AddParam(const std::string& key, const std::vector<short>& shs)
335 {
336 AppEventParam appEventParam(key, AppEventParamType::SHVECTOR);
337 appEventParam.value.valueUnion.shs_.assign(shs.begin(), shs.end());
338 baseParams_.push_back(appEventParam);
339 }
340
AddParam(const std::string & key,const std::vector<int> & is)341 void AppEventPack::AddParam(const std::string& key, const std::vector<int>& is)
342 {
343 AppEventParam appEventParam(key, AppEventParamType::IVECTOR);
344 appEventParam.value.valueUnion.is_.assign(is.begin(), is.end());
345 baseParams_.push_back(appEventParam);
346 }
347
AddParam(const std::string & key,const std::vector<long> & ls)348 void AppEventPack::AddParam(const std::string& key, const std::vector<long>& ls)
349 {
350 AppEventParam appEventParam(key, AppEventParamType::LVECTOR);
351 appEventParam.value.valueUnion.ls_.assign(ls.begin(), ls.end());
352 baseParams_.push_back(appEventParam);
353 }
354
AddParam(const std::string & key,const std::vector<long long> & lls)355 void AppEventPack::AddParam(const std::string& key, const std::vector<long long>& lls)
356 {
357 AppEventParam appEventParam(key, AppEventParamType::LLVECTOR);
358 appEventParam.value.valueUnion.lls_.assign(lls.begin(), lls.end());
359 baseParams_.push_back(appEventParam);
360 }
361
AddParam(const std::string & key,const std::vector<float> & fs)362 void AppEventPack::AddParam(const std::string& key, const std::vector<float>& fs)
363 {
364 AppEventParam appEventParam(key, AppEventParamType::FVECTOR);
365 appEventParam.value.valueUnion.fs_.assign(fs.begin(), fs.end());
366 baseParams_.push_back(appEventParam);
367 }
368
AddParam(const std::string & key,const std::vector<double> & ds)369 void AppEventPack::AddParam(const std::string& key, const std::vector<double>& ds)
370 {
371 AppEventParam appEventParam(key, AppEventParamType::DVECTOR);
372 appEventParam.value.valueUnion.ds_.assign(ds.begin(), ds.end());
373 baseParams_.push_back(appEventParam);
374 }
375
AddParam(const std::string & key,const std::vector<const char * > & cps)376 void AppEventPack::AddParam(const std::string& key, const std::vector<const char*>& cps)
377 {
378 AppEventParam appEventParam(key, AppEventParamType::STRVECTOR);
379 std::vector<std::string> strs;
380 if (cps.size() != 0) {
381 for (auto cp : cps) {
382 if (cp != nullptr) {
383 strs.push_back(cp);
384 }
385 }
386 }
387 appEventParam.value.valueUnion.strs_.assign(strs.begin(), strs.end());
388 baseParams_.push_back(appEventParam);
389 }
390
AddParam(const std::string & key,const std::vector<const std::string> & strs)391 void AppEventPack::AddParam(const std::string& key, const std::vector<const std::string>& strs)
392 {
393 AppEventParam appEventParam(key, AppEventParamType::STRVECTOR);
394 appEventParam.value.valueUnion.strs_.assign(strs.begin(), strs.end());
395 baseParams_.push_back(appEventParam);
396 }
397
AddBaseInfoToJsonString(std::stringstream & jsonStr) const398 void AppEventPack::AddBaseInfoToJsonString(std::stringstream& jsonStr) const
399 {
400 jsonStr << "\"" << "name_" << "\":" << "\"" << eventName_ << "\",";
401 jsonStr << "\"" << "type_" << "\":" << type_ << ",";
402 jsonStr << GetTimeInfo();
403 jsonStr << "\"" << "pid_" << "\":" << getpid() << ",";
404 jsonStr << "\"" << "tid_" << "\":" << gettid() << ",";
405 }
406
AddVectorToJsonString(std::stringstream & jsonStr,const std::vector<bool> & bs) const407 void AppEventPack::AddVectorToJsonString(std::stringstream& jsonStr, const std::vector<bool>& bs) const
408 {
409 jsonStr << "[";
410 size_t len = bs.size();
411 if (len == 0) {
412 jsonStr << "],";
413 HiLog::Info(LABEL, "The vector<bool> value added to JsonString is empty.");
414 return;
415 }
416
417 for (size_t i = 0; i < len - 1; i++) {
418 jsonStr << ((bs[i] == true) ? "true" : "false") << ",";
419 }
420 jsonStr << ((bs[len - 1] == true) ? "true" : "false") << "],";
421 }
422
AddVectorToJsonString(std::stringstream & jsonStr,const std::vector<char> & cs) const423 void AppEventPack::AddVectorToJsonString(std::stringstream& jsonStr, const std::vector<char>& cs) const
424 {
425 jsonStr << "[";
426 size_t len = cs.size();
427 if (len == 0) {
428 jsonStr << "],";
429 HiLog::Info(LABEL, "The vector<char> value added to JsonString is empty.");
430 return;
431 }
432
433 for (size_t i = 0; i < len - 1; i++) {
434 jsonStr << "\"" << cs[i] << "\"" << ",";
435 }
436 jsonStr << "\"" << cs[len - 1] << "\"" << "],";
437 }
438
AddVectorToJsonString(std::stringstream & jsonStr,const std::vector<short> & shs) const439 void AppEventPack::AddVectorToJsonString(std::stringstream& jsonStr, const std::vector<short>& shs) const
440 {
441 jsonStr << "[";
442 size_t len = shs.size();
443 if (len == 0) {
444 jsonStr << "],";
445 HiLog::Info(LABEL, "The vector<short> value added to JsonString is empty.");
446 return;
447 }
448
449 for (size_t i = 0; i < len - 1; i++) {
450 jsonStr << shs[i] << ",";
451 }
452 jsonStr << shs[len - 1] << "],";
453 }
454
AddVectorToJsonString(std::stringstream & jsonStr,const std::vector<int> & is) const455 void AppEventPack::AddVectorToJsonString(std::stringstream& jsonStr, const std::vector<int>& is) const
456 {
457 jsonStr << "[";
458 size_t len = is.size();
459 if (len == 0) {
460 jsonStr << "],";
461 HiLog::Info(LABEL, "The vector<int> value added to JsonString is empty.");
462 return;
463 }
464
465 for (size_t i = 0; i < len - 1; i++) {
466 jsonStr << is[i] << ",";
467 }
468 jsonStr << is[len - 1] << "],";
469 }
470
AddVectorToJsonString(std::stringstream & jsonStr,const std::vector<long> & ls) const471 void AppEventPack::AddVectorToJsonString(std::stringstream& jsonStr, const std::vector<long>& ls) const
472 {
473 jsonStr << "[";
474 size_t len = ls.size();
475 if (len == 0) {
476 jsonStr << "],";
477 HiLog::Info(LABEL, "The vector<long> value added to JsonString is empty.");
478 return;
479 }
480
481 for (size_t i = 0; i < len - 1; i++) {
482 jsonStr << ls[i] << ",";
483 }
484 jsonStr << ls[len - 1] << "],";
485 }
486
AddVectorToJsonString(std::stringstream & jsonStr,const std::vector<long long> & lls) const487 void AppEventPack::AddVectorToJsonString(std::stringstream& jsonStr, const std::vector<long long>& lls) const
488 {
489 jsonStr << "[";
490 size_t len = lls.size();
491 if (len == 0) {
492 jsonStr << "],";
493 HiLog::Info(LABEL, "The vector<long long> value added to JsonString is empty.");
494 return;
495 }
496
497 for (size_t i = 0; i < len - 1; i++) {
498 jsonStr << lls[i] << ",";
499 }
500 jsonStr << lls[len - 1] << "],";
501 }
502
AddVectorToJsonString(std::stringstream & jsonStr,const std::vector<float> & fs) const503 void AppEventPack::AddVectorToJsonString(std::stringstream& jsonStr, const std::vector<float>& fs) const
504 {
505 jsonStr << "[";
506 size_t len = fs.size();
507 if (len == 0) {
508 jsonStr << "],";
509 HiLog::Info(LABEL, "The vector<float> value added to JsonString is empty.");
510 return;
511 }
512
513 for (size_t i = 0; i < len - 1; i++) {
514 jsonStr << TrimRightZero(std::to_string(fs[i])) << ",";
515 }
516 jsonStr << TrimRightZero(std::to_string(fs[len - 1])) << "],";
517 }
518
AddVectorToJsonString(std::stringstream & jsonStr,const std::vector<double> & ds) const519 void AppEventPack::AddVectorToJsonString(std::stringstream& jsonStr, const std::vector<double>& ds) const
520 {
521 jsonStr << "[";
522 size_t len = ds.size();
523 if (len == 0) {
524 jsonStr << "],";
525 HiLog::Info(LABEL, "The vector<double> value added to JsonString is empty.");
526 return;
527 }
528
529 for (size_t i = 0; i < len - 1; i++) {
530 jsonStr << TrimRightZero(std::to_string(ds[i])) << ",";
531 }
532 jsonStr << TrimRightZero(std::to_string(ds[len - 1])) << "],";
533 }
534
AddVectorToJsonString(std::stringstream & jsonStr,const std::vector<std::string> & strs) const535 void AppEventPack::AddVectorToJsonString(std::stringstream& jsonStr, const std::vector<std::string>& strs) const
536 {
537 jsonStr << "[";
538 size_t len = strs.size();
539 if (len == 0) {
540 jsonStr << "],";
541 HiLog::Info(LABEL, "The vector<string> value added to JsonString is empty.");
542 return;
543 }
544
545 for (size_t i = 0; i < len - 1; i++) {
546 jsonStr << "\"" << strs[i] << "\",";
547 }
548 jsonStr << "\"" << strs[len - 1] << "\"],";
549 }
550
AddOthersToJsonString(std::stringstream & jsonStr,const AppEventParam param) const551 void AppEventPack::AddOthersToJsonString(std::stringstream& jsonStr, const AppEventParam param) const
552 {
553 if (param.type == AppEventParamType::BVECTOR) {
554 AddVectorToJsonString(jsonStr, param.value.valueUnion.bs_);
555 } else if (param.type == AppEventParamType::CVECTOR) {
556 AddVectorToJsonString(jsonStr, param.value.valueUnion.cs_);
557 } else if (param.type == AppEventParamType::SHVECTOR) {
558 AddVectorToJsonString(jsonStr, param.value.valueUnion.shs_);
559 } else if (param.type == AppEventParamType::IVECTOR) {
560 AddVectorToJsonString(jsonStr, param.value.valueUnion.is_);
561 } else if (param.type == AppEventParamType::LVECTOR) {
562 AddVectorToJsonString(jsonStr, param.value.valueUnion.ls_);
563 } else if (param.type == AppEventParamType::LLVECTOR) {
564 AddVectorToJsonString(jsonStr, param.value.valueUnion.lls_);
565 } else if (param.type == AppEventParamType::FVECTOR) {
566 AddVectorToJsonString(jsonStr, param.value.valueUnion.fs_);
567 } else if (param.type == AppEventParamType::DVECTOR) {
568 AddVectorToJsonString(jsonStr, param.value.valueUnion.ds_);
569 } else if (param.type == AppEventParamType::STRVECTOR) {
570 AddVectorToJsonString(jsonStr, param.value.valueUnion.strs_);
571 } else if (param.type == AppEventParamType::EMPTY) {
572 jsonStr << "[]" << ",";
573 } else {
574 jsonStr << "\"\",";
575 }
576 }
577
GetEventName() const578 const std::string AppEventPack::GetEventName() const
579 {
580 return eventName_;
581 }
582
GetType() const583 int AppEventPack::GetType() const
584 {
585 return type_;
586 }
587
GetJsonString() const588 std::string AppEventPack::GetJsonString() const
589 {
590 std::stringstream jsonStr;
591 jsonStr << "{";
592 AddBaseInfoToJsonString(jsonStr);
593 for (auto it = baseParams_.begin(); it != baseParams_.end(); it++) {
594 jsonStr << "\"" << it->name << "\":";
595 if (it->type == AppEventParamType::BOOL) {
596 jsonStr << ((it->value.valueUnion.b_ == true) ? "true" : "false") << ",";
597 } else if (it->type == AppEventParamType::CHAR) {
598 jsonStr << "\"" << it->value.valueUnion.c_ << "\"" << ",";
599 } else if (it->type == AppEventParamType::SHORT) {
600 jsonStr << it->value.valueUnion.sh_ << ",";
601 } else if (it->type == AppEventParamType::INTEGER) {
602 jsonStr << it->value.valueUnion.i_ << ",";
603 } else if (it->type == AppEventParamType::LONG) {
604 jsonStr << it->value.valueUnion.l_ << ",";
605 } else if (it->type == AppEventParamType::LONGLONG) {
606 jsonStr << it->value.valueUnion.ll_ << ",";
607 } else if (it->type == AppEventParamType::FLOAT) {
608 jsonStr << TrimRightZero(std::to_string(it->value.valueUnion.f_)) << ",";
609 } else if (it->type == AppEventParamType::DOUBLE) {
610 jsonStr << TrimRightZero(std::to_string(it->value.valueUnion.d_)) << ",";
611 } else if (it->type == AppEventParamType::STRING) {
612 jsonStr << "\"" << it->value.valueUnion.str_ << "\",";
613 } else {
614 AddOthersToJsonString(jsonStr, *it);
615 }
616 }
617
618 jsonStr.seekp(-1, std::ios_base::end);
619 jsonStr << "}" << std::endl;
620 return jsonStr.str();
621 }
622 } // HiviewDFX
623 } // OHOS