1 /*
2 * Copyright (c) 2021-2024 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 <ctime>
19 #include <iomanip>
20 #include <sstream>
21 #include <string>
22 #include <unistd.h>
23 #include <vector>
24
25 #include "hiappevent_config.h"
26 #include "hilog/log.h"
27 #include "hitrace/trace.h"
28 #include "time_util.h"
29
30 #undef LOG_DOMAIN
31 #define LOG_DOMAIN 0xD002D07
32
33 #undef LOG_TAG
34 #define LOG_TAG "EventBase"
35
36 namespace OHOS {
37 namespace HiviewDFX {
38 namespace {
39 const std::string DEFAULT_DOMAIN = "default";
40 constexpr size_t MIN_PARAM_STR_LEN = 3; // 3: '{}\0'
41
TrimRightZero(const std::string & str)42 std::string TrimRightZero(const std::string& str)
43 {
44 auto endIndex = str.find_last_not_of("0");
45 if (endIndex == std::string::npos) {
46 return str;
47 }
48
49 return (str[endIndex] == '.') ? str.substr(0, endIndex) : str.substr(0, endIndex + 1);
50 }
51
InitValueByBaseType(AppEventParamValue * value,const AppEventParamValue & other)52 void InitValueByBaseType(AppEventParamValue* value, const AppEventParamValue& other)
53 {
54 if (value == nullptr) {
55 return;
56 }
57
58 switch (other.type) {
59 case AppEventParamType::BOOL:
60 value->valueUnion.b_ = other.valueUnion.b_;
61 break;
62 case AppEventParamType::CHAR:
63 value->valueUnion.c_ = other.valueUnion.c_;
64 break;
65 case AppEventParamType::SHORT:
66 value->valueUnion.sh_ = other.valueUnion.sh_;
67 break;
68 case AppEventParamType::INTEGER:
69 value->valueUnion.i_ = other.valueUnion.i_;
70 break;
71 case AppEventParamType::LONGLONG:
72 value->valueUnion.ll_ = other.valueUnion.ll_;
73 break;
74 case AppEventParamType::FLOAT:
75 value->valueUnion.f_ = other.valueUnion.f_;
76 break;
77 case AppEventParamType::DOUBLE:
78 value->valueUnion.d_ = other.valueUnion.d_;
79 break;
80 default:
81 break;
82 }
83 }
84
InitValueByReferType(AppEventParamValue * value,const AppEventParamValue & other)85 void InitValueByReferType(AppEventParamValue* value, const AppEventParamValue& other)
86 {
87 if (value == nullptr) {
88 return;
89 }
90
91 switch (other.type) {
92 case AppEventParamType::STRING:
93 new (&value->valueUnion.str_) auto(other.valueUnion.str_);
94 break;
95 case AppEventParamType::BVECTOR:
96 new (&value->valueUnion.bs_) auto(other.valueUnion.bs_);
97 break;
98 case AppEventParamType::CVECTOR:
99 new (&value->valueUnion.cs_) auto(other.valueUnion.cs_);
100 break;
101 case AppEventParamType::SHVECTOR:
102 new (&value->valueUnion.shs_) auto(other.valueUnion.shs_);
103 break;
104 case AppEventParamType::IVECTOR:
105 new (&value->valueUnion.is_) auto(other.valueUnion.is_);
106 break;
107 case AppEventParamType::LLVECTOR:
108 new (&value->valueUnion.lls_) auto(other.valueUnion.lls_);
109 break;
110 case AppEventParamType::FVECTOR:
111 new (&value->valueUnion.fs_) auto(other.valueUnion.fs_);
112 break;
113 case AppEventParamType::DVECTOR:
114 new (&value->valueUnion.ds_) auto(other.valueUnion.ds_);
115 break;
116 case AppEventParamType::STRVECTOR:
117 new (&value->valueUnion.strs_) auto(other.valueUnion.strs_);
118 break;
119 default:
120 break;
121 }
122 }
123
124 template<typename T>
GetValueStr(const T & value)125 std::string GetValueStr(const T& value)
126 {
127 return std::to_string(value);
128 }
129
GetValueStr(bool value)130 std::string GetValueStr(bool value)
131 {
132 return value ? "true" : "false";
133 }
134
GetValueStr(char value)135 std::string GetValueStr(char value)
136 {
137 return "\"" + std::to_string(value) + "\"";
138 }
139
GetValueStr(float value)140 std::string GetValueStr(float value)
141 {
142 return TrimRightZero(std::to_string(value));
143 }
144
GetValueStr(double value)145 std::string GetValueStr(double value)
146 {
147 return TrimRightZero(std::to_string(value));
148 }
149
GetValueStr(const std::string & value)150 std::string GetValueStr(const std::string& value)
151 {
152 return "\"" + value + "\"";
153 }
154
155 template<typename T>
GetValuesStr(const std::vector<T> & values)156 std::string GetValuesStr(const std::vector<T>& values)
157 {
158 std::string valuesStr;
159 valuesStr.append("[");
160 size_t valuesSize = values.size();
161 for (size_t i = 0; i < valuesSize; ++i) {
162 if constexpr (std::is_same_v<std::decay_t<T>, bool>) { // vector<bool> is stored as bit type
163 bool bValue = values[i];
164 valuesStr.append(GetValueStr(bValue));
165 } else {
166 valuesStr.append(GetValueStr(values[i]));
167 }
168 if (i != (valuesSize - 1)) { // -1 for last value
169 valuesStr.append(",");
170 }
171 }
172 valuesStr.append("]");
173 return valuesStr;
174 }
175
GetEmptyParamValueStr(const AppEventParamValue & value)176 std::string GetEmptyParamValueStr(const AppEventParamValue& value)
177 {
178 return "\"\"";
179 }
180
GetBoolParamValueStr(const AppEventParamValue & value)181 std::string GetBoolParamValueStr(const AppEventParamValue& value)
182 {
183 return GetValueStr(value.valueUnion.b_);
184 }
185
GetCharParamValueStr(const AppEventParamValue & value)186 std::string GetCharParamValueStr(const AppEventParamValue& value)
187 {
188 return GetValueStr(value.valueUnion.c_);
189 }
190
GetShortParamValueStr(const AppEventParamValue & value)191 std::string GetShortParamValueStr(const AppEventParamValue& value)
192 {
193 return GetValueStr(value.valueUnion.sh_);
194 }
195
GetIntParamValueStr(const AppEventParamValue & value)196 std::string GetIntParamValueStr(const AppEventParamValue& value)
197 {
198 return GetValueStr(value.valueUnion.i_);
199 }
200
GetLongParamValueStr(const AppEventParamValue & value)201 std::string GetLongParamValueStr(const AppEventParamValue& value)
202 {
203 return GetValueStr(value.valueUnion.ll_);
204 }
205
GetFloatParamValueStr(const AppEventParamValue & value)206 std::string GetFloatParamValueStr(const AppEventParamValue& value)
207 {
208 return GetValueStr(value.valueUnion.f_);
209 }
210
GetDoubleParamValueStr(const AppEventParamValue & value)211 std::string GetDoubleParamValueStr(const AppEventParamValue& value)
212 {
213 return GetValueStr(value.valueUnion.d_);
214 }
215
GetStrParamValueStr(const AppEventParamValue & value)216 std::string GetStrParamValueStr(const AppEventParamValue& value)
217 {
218 return GetValueStr(value.valueUnion.str_);
219 }
220
GetBoolsParamValueStr(const AppEventParamValue & value)221 std::string GetBoolsParamValueStr(const AppEventParamValue& value)
222 {
223 return GetValuesStr(value.valueUnion.bs_);
224 }
225
GetCharsParamValueStr(const AppEventParamValue & value)226 std::string GetCharsParamValueStr(const AppEventParamValue& value)
227 {
228 return GetValuesStr(value.valueUnion.cs_);
229 }
230
GetShortsParamValueStr(const AppEventParamValue & value)231 std::string GetShortsParamValueStr(const AppEventParamValue& value)
232 {
233 return GetValuesStr(value.valueUnion.shs_);
234 }
235
GetIntsParamValueStr(const AppEventParamValue & value)236 std::string GetIntsParamValueStr(const AppEventParamValue& value)
237 {
238 return GetValuesStr(value.valueUnion.is_);
239 }
240
GetLongsParamValueStr(const AppEventParamValue & value)241 std::string GetLongsParamValueStr(const AppEventParamValue& value)
242 {
243 return GetValuesStr(value.valueUnion.lls_);
244 }
245
GetFloatsParamValueStr(const AppEventParamValue & value)246 std::string GetFloatsParamValueStr(const AppEventParamValue& value)
247 {
248 return GetValuesStr(value.valueUnion.fs_);
249 }
250
GetDoublesParamValueStr(const AppEventParamValue & value)251 std::string GetDoublesParamValueStr(const AppEventParamValue& value)
252 {
253 return GetValuesStr(value.valueUnion.ds_);
254 }
255
GetStrsParamValueStr(const AppEventParamValue & value)256 std::string GetStrsParamValueStr(const AppEventParamValue& value)
257 {
258 return GetValuesStr(value.valueUnion.strs_);
259 }
260
261 using GetParamValueFunc = std::string (*)(const AppEventParamValue& value);
262 const std::unordered_map<AppEventParamType, GetParamValueFunc> GET_PARAM_VALUE_FUNCS = {
263 {EMPTY, &GetEmptyParamValueStr},
264 {BOOL, &GetBoolParamValueStr},
265 {CHAR, &GetCharParamValueStr},
266 {SHORT, &GetShortParamValueStr},
267 {INTEGER, &GetIntParamValueStr},
268 {LONGLONG, &GetLongParamValueStr},
269 {FLOAT, &GetFloatParamValueStr},
270 {DOUBLE, &GetDoubleParamValueStr},
271 {STRING, &GetStrParamValueStr},
272 {BVECTOR, &GetBoolsParamValueStr},
273 {CVECTOR, &GetCharsParamValueStr},
274 {SHVECTOR, &GetShortsParamValueStr},
275 {IVECTOR, &GetIntsParamValueStr},
276 {LLVECTOR, &GetLongsParamValueStr},
277 {FVECTOR, &GetFloatsParamValueStr},
278 {DVECTOR, &GetDoublesParamValueStr},
279 {STRVECTOR, &GetStrsParamValueStr},
280 };
281
GetParamValueStr(const AppEventParam & param)282 std::string GetParamValueStr(const AppEventParam& param)
283 {
284 if (GET_PARAM_VALUE_FUNCS.find(param.value.type) == GET_PARAM_VALUE_FUNCS.end()) {
285 HILOG_WARN(LOG_CORE, "Invalid param value");
286 return "";
287 }
288 return GET_PARAM_VALUE_FUNCS.at(param.value.type)(param.value);
289 }
290 }
291
AppEventParamValue(AppEventParamType t)292 AppEventParamValue::AppEventParamValue(AppEventParamType t) : type(t), valueUnion(t)
293 {}
294
AppEventParamValue(const AppEventParamValue & other)295 AppEventParamValue::AppEventParamValue(const AppEventParamValue& other) : type(other.type)
296 {
297 if (other.type < AppEventParamType::STRING) {
298 InitValueByBaseType(this, other);
299 } else {
300 InitValueByReferType(this, other);
301 }
302 }
303
~AppEventParamValue()304 AppEventParamValue::~AppEventParamValue()
305 {
306 switch (type) {
307 case AppEventParamType::STRING:
308 valueUnion.str_.~basic_string();
309 break;
310 case AppEventParamType::BVECTOR:
311 valueUnion.bs_.~vector();
312 break;
313 case AppEventParamType::CVECTOR:
314 valueUnion.cs_.~vector();
315 break;
316 case AppEventParamType::SHVECTOR:
317 valueUnion.shs_.~vector();
318 break;
319 case AppEventParamType::IVECTOR:
320 valueUnion.is_.~vector();
321 break;
322 case AppEventParamType::LLVECTOR:
323 valueUnion.lls_.~vector();
324 break;
325 case AppEventParamType::FVECTOR:
326 valueUnion.fs_.~vector();
327 break;
328 case AppEventParamType::DVECTOR:
329 valueUnion.ds_.~vector();
330 break;
331 case AppEventParamType::STRVECTOR:
332 valueUnion.strs_.~vector();
333 break;
334 default:
335 break;
336 }
337 }
338
AppEventParam(std::string n,AppEventParamType t)339 AppEventParam::AppEventParam(std::string n, AppEventParamType t) : name(n), type(t), value(t)
340 {}
341
AppEventParam(const AppEventParam & param)342 AppEventParam::AppEventParam(const AppEventParam& param) : name(param.name), type(param.type), value(param.value)
343 {}
344
~AppEventParam()345 AppEventParam::~AppEventParam()
346 {}
347
AppEventPack(const std::string & name,int type)348 AppEventPack::AppEventPack(const std::string& name, int type) : AppEventPack(DEFAULT_DOMAIN, name, type)
349 {}
350
AppEventPack(const std::string & domain,const std::string & name,int type)351 AppEventPack::AppEventPack(const std::string& domain, const std::string& name, int type)
352 : domain_(domain), name_(name), type_(type)
353 {
354 InitTime();
355 InitTimeZone();
356 InitProcessInfo();
357 InitTraceInfo();
358 InitRunningId();
359 }
360
InitTime()361 void AppEventPack::InitTime()
362 {
363 time_ = TimeUtil::GetMilliseconds();
364 }
365
InitTimeZone()366 void AppEventPack::InitTimeZone()
367 {
368 timeZone_ = TimeUtil::GetTimeZone();
369 }
370
InitProcessInfo()371 void AppEventPack::InitProcessInfo()
372 {
373 pid_ = getprocpid();
374 tid_ = getproctid();
375 }
376
InitTraceInfo()377 void AppEventPack::InitTraceInfo()
378 {
379 HiTraceId hitraceId = HiTraceChain::GetId();
380 if (!hitraceId.IsValid()) {
381 return;
382 }
383 traceId_ = static_cast<int64_t>(hitraceId.GetChainId());
384 spanId_ = static_cast<int64_t>(hitraceId.GetSpanId());
385 pspanId_ = static_cast<int64_t>(hitraceId.GetParentSpanId());
386 traceFlag_ = hitraceId.GetFlags();
387 }
388
InitRunningId()389 void AppEventPack::InitRunningId()
390 {
391 runningId_ = HiAppEventConfig::GetInstance().GetRunningId();
392 }
393
AddParam(const std::string & key)394 void AppEventPack::AddParam(const std::string& key)
395 {
396 AppEventParam appEventParam(key, AppEventParamType::EMPTY);
397 baseParams_.emplace_back(appEventParam);
398 }
399
AddParam(const std::string & key,bool b)400 void AppEventPack::AddParam(const std::string& key, bool b)
401 {
402 AppEventParam appEventParam(key, AppEventParamType::BOOL);
403 appEventParam.value.valueUnion.b_ = b;
404 baseParams_.emplace_back(appEventParam);
405 }
406
AddParam(const std::string & key,char c)407 void AppEventPack::AddParam(const std::string& key, char c)
408 {
409 AppEventParam appEventParam(key, AppEventParamType::CHAR);
410 appEventParam.value.valueUnion.c_ = c;
411 baseParams_.emplace_back(appEventParam);
412 }
413
AddParam(const std::string & key,int8_t num)414 void AppEventPack::AddParam(const std::string& key, int8_t num)
415 {
416 AppEventParam appEventParam(key, AppEventParamType::SHORT);
417 appEventParam.value.valueUnion.sh_ = static_cast<int16_t>(num);
418 baseParams_.emplace_back(appEventParam);
419 }
420
AddParam(const std::string & key,int16_t s)421 void AppEventPack::AddParam(const std::string& key, int16_t s)
422 {
423 AppEventParam appEventParam(key, AppEventParamType::SHORT);
424 appEventParam.value.valueUnion.sh_ = s;
425 baseParams_.emplace_back(appEventParam);
426 }
427
AddParam(const std::string & key,int i)428 void AppEventPack::AddParam(const std::string& key, int i)
429 {
430 AppEventParam appEventParam(key, AppEventParamType::INTEGER);
431 appEventParam.value.valueUnion.i_ = i;
432 baseParams_.emplace_back(appEventParam);
433 }
434
AddParam(const std::string & key,int64_t ll)435 void AppEventPack::AddParam(const std::string& key, int64_t ll)
436 {
437 AppEventParam appEventParam(key, AppEventParamType::LONGLONG);
438 appEventParam.value.valueUnion.ll_ = ll;
439 baseParams_.emplace_back(appEventParam);
440 }
441
AddParam(const std::string & key,float f)442 void AppEventPack::AddParam(const std::string& key, float f)
443 {
444 AppEventParam appEventParam(key, AppEventParamType::FLOAT);
445 appEventParam.value.valueUnion.f_ = f;
446 baseParams_.emplace_back(appEventParam);
447 }
448
AddParam(const std::string & key,double d)449 void AppEventPack::AddParam(const std::string& key, double d)
450 {
451 AppEventParam appEventParam(key, AppEventParamType::DOUBLE);
452 appEventParam.value.valueUnion.d_ = d;
453 baseParams_.emplace_back(appEventParam);
454 }
455
AddParam(const std::string & key,const char * s)456 void AppEventPack::AddParam(const std::string& key, const char *s)
457 {
458 AppEventParam appEventParam(key, AppEventParamType::STRING);
459 appEventParam.value.valueUnion.str_ = s;
460 baseParams_.push_back(appEventParam);
461 }
462
AddParam(const std::string & key,const std::string & s)463 void AppEventPack::AddParam(const std::string& key, const std::string& s)
464 {
465 AppEventParam appEventParam(key, AppEventParamType::STRING);
466 appEventParam.value.valueUnion.str_ = s;
467 baseParams_.push_back(appEventParam);
468 }
469
AddParam(const std::string & key,const std::vector<bool> & bs)470 void AppEventPack::AddParam(const std::string& key, const std::vector<bool>& bs)
471 {
472 AppEventParam appEventParam(key, AppEventParamType::BVECTOR);
473 appEventParam.value.valueUnion.bs_.assign(bs.begin(), bs.end());
474 baseParams_.push_back(appEventParam);
475 }
476
AddParam(const std::string & key,const std::vector<char> & cs)477 void AppEventPack::AddParam(const std::string& key, const std::vector<char>& cs)
478 {
479 AppEventParam appEventParam(key, AppEventParamType::CVECTOR);
480 appEventParam.value.valueUnion.cs_.assign(cs.begin(), cs.end());
481 baseParams_.push_back(appEventParam);
482 }
483
AddParam(const std::string & key,const std::vector<int8_t> & shs)484 void AppEventPack::AddParam(const std::string& key, const std::vector<int8_t>& shs)
485 {
486 AppEventParam appEventParam(key, AppEventParamType::SHVECTOR);
487 appEventParam.value.valueUnion.shs_.assign(shs.begin(), shs.end());
488 baseParams_.push_back(appEventParam);
489 }
490
AddParam(const std::string & key,const std::vector<int16_t> & shs)491 void AppEventPack::AddParam(const std::string& key, const std::vector<int16_t>& shs)
492 {
493 AppEventParam appEventParam(key, AppEventParamType::SHVECTOR);
494 appEventParam.value.valueUnion.shs_.assign(shs.begin(), shs.end());
495 baseParams_.push_back(appEventParam);
496 }
497
AddParam(const std::string & key,const std::vector<int> & is)498 void AppEventPack::AddParam(const std::string& key, const std::vector<int>& is)
499 {
500 AppEventParam appEventParam(key, AppEventParamType::IVECTOR);
501 appEventParam.value.valueUnion.is_.assign(is.begin(), is.end());
502 baseParams_.push_back(appEventParam);
503 }
504
AddParam(const std::string & key,const std::vector<int64_t> & lls)505 void AppEventPack::AddParam(const std::string& key, const std::vector<int64_t>& lls)
506 {
507 AppEventParam appEventParam(key, AppEventParamType::LLVECTOR);
508 appEventParam.value.valueUnion.lls_.assign(lls.begin(), lls.end());
509 baseParams_.push_back(appEventParam);
510 }
511
AddParam(const std::string & key,const std::vector<float> & fs)512 void AppEventPack::AddParam(const std::string& key, const std::vector<float>& fs)
513 {
514 AppEventParam appEventParam(key, AppEventParamType::FVECTOR);
515 appEventParam.value.valueUnion.fs_.assign(fs.begin(), fs.end());
516 baseParams_.push_back(appEventParam);
517 }
518
AddParam(const std::string & key,const std::vector<double> & ds)519 void AppEventPack::AddParam(const std::string& key, const std::vector<double>& ds)
520 {
521 AppEventParam appEventParam(key, AppEventParamType::DVECTOR);
522 appEventParam.value.valueUnion.ds_.assign(ds.begin(), ds.end());
523 baseParams_.push_back(appEventParam);
524 }
525
AddParam(const std::string & key,const std::vector<const char * > & cps)526 void AppEventPack::AddParam(const std::string& key, const std::vector<const char*>& cps)
527 {
528 AppEventParam appEventParam(key, AppEventParamType::STRVECTOR);
529 std::vector<std::string> strs;
530 if (cps.size() != 0) {
531 for (auto cp : cps) {
532 if (cp != nullptr) {
533 strs.push_back(cp);
534 }
535 }
536 }
537 appEventParam.value.valueUnion.strs_.assign(strs.begin(), strs.end());
538 baseParams_.push_back(appEventParam);
539 }
540
AddParam(const std::string & key,const std::vector<std::string> & strs)541 void AppEventPack::AddParam(const std::string& key, const std::vector<std::string>& strs)
542 {
543 AppEventParam appEventParam(key, AppEventParamType::STRVECTOR);
544 appEventParam.value.valueUnion.strs_.assign(strs.begin(), strs.end());
545 baseParams_.push_back(appEventParam);
546 }
547
AddCustomParams(const std::unordered_map<std::string,std::string> & customParams)548 void AppEventPack::AddCustomParams(const std::unordered_map<std::string, std::string>& customParams)
549 {
550 if (customParams.empty()) {
551 return;
552 }
553 std::string paramStr = GetParamStr();
554 if (paramStr.size() >= MIN_PARAM_STR_LEN) {
555 std::stringstream jsonStr;
556 if (paramStr.size() > MIN_PARAM_STR_LEN) {
557 jsonStr << ",";
558 }
559 for (auto it = customParams.begin(); it != customParams.end(); ++it) {
560 jsonStr << "\"" << it->first << "\":" << it->second << ",";
561 }
562 std::string customParamStr = jsonStr.str();
563 customParamStr.erase(customParamStr.end() - 1); // -1 for delete ','
564 paramStr.insert(paramStr.size() - 2, customParamStr); // 2 for '}\0'
565 paramStr_ = paramStr;
566 }
567 }
568
GetEventStr() const569 std::string AppEventPack::GetEventStr() const
570 {
571 std::stringstream jsonStr;
572 jsonStr << "{";
573 AddBaseInfoToJsonString(jsonStr);
574 AddParamsInfoToJsonString(jsonStr);
575 jsonStr << "}" << std::endl;
576 return jsonStr.str();
577 }
578
GetParamStr() const579 std::string AppEventPack::GetParamStr() const
580 {
581 if (!paramStr_.empty()) {
582 return paramStr_;
583 }
584
585 std::stringstream jsonStr;
586 jsonStr << "{";
587 AddParamsToJsonString(jsonStr);
588 jsonStr << "}" << std::endl;
589 return jsonStr.str();
590 }
591
AddBaseInfoToJsonString(std::stringstream & jsonStr) const592 void AppEventPack::AddBaseInfoToJsonString(std::stringstream& jsonStr) const
593 {
594 jsonStr << "\"" << "domain_" << "\":" << "\"" << domain_ << "\",";
595 jsonStr << "\"" << "name_" << "\":" << "\"" << name_ << "\",";
596 jsonStr << "\"" << "type_" << "\":" << type_ << ",";
597 jsonStr << "\"" << "time_" << "\":" << std::to_string(time_) << ",";
598 jsonStr << "\"tz_\":\"" << timeZone_ << "\",";
599 jsonStr << "\"" << "pid_" << "\":" << pid_ << ",";
600 jsonStr << "\"" << "tid_" << "\":" << tid_;
601 AddTraceInfoToJsonString(jsonStr);
602 }
603
AddTraceInfoToJsonString(std::stringstream & jsonStr) const604 void AppEventPack::AddTraceInfoToJsonString(std::stringstream& jsonStr) const
605 {
606 if (traceId_ == 0) {
607 return;
608 }
609 jsonStr << "," << "\"" << "traceid_" << "\":" << traceId_;
610 jsonStr << "," << "\"" << "spanid_" << "\":" << spanId_;
611 jsonStr << "," << "\"" << "pspanid_" << "\":" << pspanId_;
612 jsonStr << "," << "\"" << "trace_flag_" << "\":" << traceFlag_;
613 }
614
AddParamsInfoToJsonString(std::stringstream & jsonStr) const615 void AppEventPack::AddParamsInfoToJsonString(std::stringstream& jsonStr) const
616 {
617 // for event from writing
618 if (baseParams_.size() != 0) {
619 jsonStr << ",";
620 AddParamsToJsonString(jsonStr);
621 return;
622 }
623
624 // for event from the db
625 size_t paramStrLen = paramStr_.length();
626 if (paramStrLen > MIN_PARAM_STR_LEN) {
627 jsonStr << "," << paramStr_.substr(1, paramStrLen - MIN_PARAM_STR_LEN); // 1: '{' for next char
628 }
629 }
630
AddParamsToJsonString(std::stringstream & jsonStr) const631 void AppEventPack::AddParamsToJsonString(std::stringstream& jsonStr) const
632 {
633 if (baseParams_.empty()) {
634 return;
635 }
636 for (const auto& param : baseParams_) {
637 jsonStr << "\"" << param.name << "\":" << GetParamValueStr(param) << ",";
638 }
639 jsonStr.seekp(-1, std::ios_base::end); // -1 for delete ','
640 }
641
GetCustomParams(std::vector<CustomEventParam> & customParams) const642 void AppEventPack::GetCustomParams(std::vector<CustomEventParam>& customParams) const
643 {
644 for (const auto& param : baseParams_) {
645 CustomEventParam customParam = {
646 .key = param.name,
647 .value = GetParamValueStr(param),
648 .type = param.value.type,
649 };
650 customParams.push_back(customParam);
651 }
652 }
653
GetSeq() const654 int64_t AppEventPack::GetSeq() const
655 {
656 return seq_;
657 }
658
GetDomain() const659 std::string AppEventPack::GetDomain() const
660 {
661 return domain_;
662 }
663
GetName() const664 std::string AppEventPack::GetName() const
665 {
666 return name_;
667 }
668
GetType() const669 int AppEventPack::GetType() const
670 {
671 return type_;
672 }
673
GetTime() const674 uint64_t AppEventPack::GetTime() const
675 {
676 return time_;
677 }
678
GetTimeZone() const679 std::string AppEventPack::GetTimeZone() const
680 {
681 return timeZone_;
682 }
683
GetPid() const684 int AppEventPack::GetPid() const
685 {
686 return pid_;
687 }
688
GetTid() const689 int AppEventPack::GetTid() const
690 {
691 return tid_;
692 }
693
GetTraceId() const694 int64_t AppEventPack::GetTraceId() const
695 {
696 return traceId_;
697 }
698
GetSpanId() const699 int64_t AppEventPack::GetSpanId() const
700 {
701 return spanId_;
702 }
703
GetPspanId() const704 int64_t AppEventPack::GetPspanId() const
705 {
706 return pspanId_;
707 }
708
GetTraceFlag() const709 int AppEventPack::GetTraceFlag() const
710 {
711 return traceFlag_;
712 }
713
GetRunningId() const714 std::string AppEventPack::GetRunningId() const
715 {
716 return runningId_;
717 }
718
SetSeq(int64_t seq)719 void AppEventPack::SetSeq(int64_t seq)
720 {
721 seq_ = seq;
722 }
723
SetDomain(const std::string & domain)724 void AppEventPack::SetDomain(const std::string& domain)
725 {
726 domain_ = domain;
727 }
728
SetName(const std::string & name)729 void AppEventPack::SetName(const std::string& name)
730 {
731 name_ = name;
732 }
733
SetType(int type)734 void AppEventPack::SetType(int type)
735 {
736 type_ = type;
737 }
738
SetTime(uint64_t time)739 void AppEventPack::SetTime(uint64_t time)
740 {
741 time_ = time;
742 }
743
SetTimeZone(const std::string & timeZone)744 void AppEventPack::SetTimeZone(const std::string& timeZone)
745 {
746 timeZone_ = timeZone;
747 }
748
SetPid(int pid)749 void AppEventPack::SetPid(int pid)
750 {
751 pid_ = pid;
752 }
753
SetTid(int tid)754 void AppEventPack::SetTid(int tid)
755 {
756 tid_ = tid;
757 }
758
SetTraceId(int64_t traceId)759 void AppEventPack::SetTraceId(int64_t traceId)
760 {
761 traceId_ = traceId;
762 }
763
SetSpanId(int64_t spanId)764 void AppEventPack::SetSpanId(int64_t spanId)
765 {
766 spanId_ = spanId;
767 }
768
SetPspanId(int64_t pspanId)769 void AppEventPack::SetPspanId(int64_t pspanId)
770 {
771 pspanId_ = pspanId;
772 }
773
SetTraceFlag(int traceFlag)774 void AppEventPack::SetTraceFlag(int traceFlag)
775 {
776 traceFlag_ = traceFlag;
777 }
778
SetParamStr(const std::string & paramStr)779 void AppEventPack::SetParamStr(const std::string& paramStr)
780 {
781 paramStr_ = paramStr;
782 }
783
SetRunningId(const std::string & runningId)784 void AppEventPack::SetRunningId(const std::string& runningId)
785 {
786 runningId_ = runningId;
787 }
788 } // namespace HiviewDFX
789 } // namespace OHOS
790