1 /*
2 * Copyright (c) 2021-2025 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 #include "sys_event.h"
16
17 #include <chrono>
18 #include <functional>
19 #include <limits>
20 #include <memory>
21 #include <regex>
22 #include <sstream>
23 #include <string>
24 #include <sys/time.h>
25 #include <vector>
26
27 #include "encoded/raw_data_builder_json_parser.h"
28 #include "string_util.h"
29 #include "time_util.h"
30
31 namespace OHOS {
32 namespace HiviewDFX {
33 namespace EventStore {
34 std::string EventCol::DOMAIN = "domain_";
35 std::string EventCol::NAME = "name_";
36 std::string EventCol::TYPE = "type_";
37 std::string EventCol::TS = "time_";
38 std::string EventCol::TZ = "tz_";
39 std::string EventCol::PID = "pid_";
40 std::string EventCol::TID = "tid_";
41 std::string EventCol::UID = "uid_";
42 std::string EventCol::INFO = "info_";
43 std::string EventCol::LEVEL = "level_";
44 std::string EventCol::SEQ = "seq_";
45 std::string EventCol::TAG = "tag_";
46 }
47 using EventRaw::HiSysEventHeader;
48 namespace {
49 constexpr int64_t DEFAULT_INT_VALUE = 0;
50 constexpr uint64_t DEFAULT_UINT_VALUE = 0;
51 constexpr double DEFAULT_DOUBLE_VALUE = 0.0;
52 constexpr size_t BLOCK_SIZE_OFFSET = sizeof(int32_t);
53 constexpr size_t PERIOD_SEQ_INFO_ITEM_CNT = 3;
54 constexpr char REPORT_INTERVAL_KEY[] = "reportInterval_";
55
56 template<typename T>
AppendJsonValue(std::string & eventJson,const std::string & key,T val)57 void AppendJsonValue(std::string& eventJson, const std::string& key, T val)
58 {
59 if (eventJson.empty()) {
60 return;
61 }
62 std::string findKey = "\"" + key + "\":";
63 if (eventJson.find(findKey) != std::string::npos) {
64 return;
65 }
66 std::string appendStr;
67 appendStr.append(",\"").append(key).append("\":");
68 if constexpr (std::is_same_v<std::decay_t<T>, std::string>) {
69 appendStr.append("\"").append(val).append("\"");
70 } else {
71 appendStr.append(std::to_string(val));
72 }
73 eventJson.insert(eventJson.size() - 1, appendStr); // 1 for '}'
74 }
75
76 template<typename T>
ParseArrayValue(std::shared_ptr<EventRaw::RawDataBuilder> builder,const std::string & key,std::function<bool (T &)> itemHandler)77 bool ParseArrayValue(std::shared_ptr<EventRaw::RawDataBuilder> builder, const std::string& key,
78 std::function<bool(T&)> itemHandler)
79 {
80 if (builder == nullptr) {
81 return false;
82 }
83 if (std::vector<T> arr; builder->ParseValueByKey(key, arr)) {
84 if (arr.empty()) {
85 return true;
86 }
87 return all_of(arr.begin(), arr.end(), [&itemHandler] (T& item) {
88 return itemHandler(item);
89 });
90 }
91 return false;
92 }
93 }
94 using EventRaw::UnsignedVarintEncodedParam;
95 using EventRaw::SignedVarintEncodedParam;
96 using EventRaw::FloatingNumberEncodedParam;
97 using EventRaw::StringEncodedParam;
98 using EventRaw::UnsignedVarintEncodedArrayParam;
99 using EventRaw::SignedVarintEncodedArrayParam;
100 using EventRaw::FloatingNumberEncodedArrayParam;
101 using EventRaw::StringEncodedArrayParam;
102 std::atomic<uint32_t> SysEvent::totalCount_(0);
103 std::atomic<int64_t> SysEvent::totalSize_(0);
104
SysEvent(const std::string & sender,PipelineEventProducer * handler,std::shared_ptr<EventRaw::RawData> rawData,int64_t seq,const std::string & sysVersion,const std::string & patchVersion)105 SysEvent::SysEvent(const std::string& sender, PipelineEventProducer* handler,
106 std::shared_ptr<EventRaw::RawData> rawData, int64_t seq,
107 const std::string& sysVersion, const std::string& patchVersion)
108 : PipelineEvent(sender, handler), eventType_(0), preserve_(true), log_(0), seq_(seq)
109 {
110 messageType_ = Event::MessageType::SYS_EVENT;
111 if (rawData == nullptr || rawData->GetData() == nullptr ||
112 rawData->GetDataLength() < EventRaw::GetValidDataMinimumByteCount()) {
113 return;
114 }
115 sysVersion_ = sysVersion;
116 patchVersion_ = patchVersion;
117 rawData_ = rawData;
118 totalCount_.fetch_add(1);
119 totalSize_.fetch_add(*(reinterpret_cast<int32_t*>(rawData_->GetData())));
120 InitialMembers();
121 }
122
SysEvent(const std::string & sender,PipelineEventProducer * handler,std::shared_ptr<EventRaw::RawData> rawData,int64_t seq)123 SysEvent::SysEvent(const std::string& sender, PipelineEventProducer* handler,
124 std::shared_ptr<EventRaw::RawData> rawData, int64_t seq)
125 : SysEvent(sender, handler, rawData, seq, "", "")
126 {
127 }
128
SysEvent(const std::string & sender,PipelineEventProducer * handler,std::shared_ptr<EventRaw::RawData> rawData)129 SysEvent::SysEvent(const std::string& sender, PipelineEventProducer* handler,
130 std::shared_ptr<EventRaw::RawData> rawData)
131 : SysEvent(sender, handler, rawData, 0)
132 {}
133
SysEvent(const std::string & sender,PipelineEventProducer * handler,SysEventCreator & sysEventCreator)134 SysEvent::SysEvent(const std::string& sender, PipelineEventProducer* handler, SysEventCreator& sysEventCreator)
135 : SysEvent(sender, handler, sysEventCreator.GetRawData())
136 {}
137
SysEvent(const std::string & sender,PipelineEventProducer * handler,const std::string & jsonStr)138 SysEvent::SysEvent(const std::string& sender, PipelineEventProducer* handler, const std::string& jsonStr)
139 : SysEvent(sender, handler, TansJsonStrToRawData(jsonStr))
140 {}
141
~SysEvent()142 SysEvent::~SysEvent()
143 {
144 if (totalCount_ > 0) {
145 totalCount_.fetch_sub(1);
146 }
147 if (rawData_ != nullptr && rawData_->GetData() != nullptr) {
148 totalSize_.fetch_sub(*(reinterpret_cast<int32_t*>(rawData_->GetData())));
149 }
150 if (totalSize_ < 0) {
151 totalSize_.store(0);
152 }
153 }
154
InitialMembers()155 void SysEvent::InitialMembers()
156 {
157 HiSysEventHeader header = *(reinterpret_cast<struct HiSysEventHeader*>(rawData_->GetData() + BLOCK_SIZE_OFFSET));
158 domain_ = header.domain;
159 eventName_ = header.name;
160 eventType_ = static_cast<int32_t>(header.type) + 1;
161 happenTime_ = header.timestamp;
162 if (happenTime_ == 0) {
163 auto currentTimeStamp = TimeUtil::GetMilliseconds();
164 happenTime_ = currentTimeStamp;
165 }
166 tz_ = static_cast<int16_t>(header.timeZone);
167 pid_ = static_cast<int32_t>(header.pid);
168 tid_ = static_cast<int32_t>(header.tid);
169 uid_ = static_cast<int32_t>(header.uid);
170 log_ = header.log;
171 }
172
InitBuilder()173 bool SysEvent::InitBuilder()
174 {
175 if (builder_ != nullptr) {
176 return true;
177 }
178 builder_ = std::make_shared<EventRaw::RawDataBuilder>(rawData_);
179 return builder_ != nullptr;
180 }
181
TansJsonStrToRawData(const std::string & jsonStr)182 std::shared_ptr<EventRaw::RawData> SysEvent::TansJsonStrToRawData(const std::string& jsonStr)
183 {
184 auto parser = std::make_unique<EventRaw::RawDataBuilderJsonParser>(jsonStr);
185 if (parser == nullptr) {
186 return nullptr;
187 }
188 auto rawDataBuilder = parser->Parse();
189 if (rawDataBuilder == nullptr) {
190 return nullptr;
191 }
192 return rawDataBuilder->Build();
193 }
194
SetTag(const std::string & tag)195 void SysEvent::SetTag(const std::string& tag)
196 {
197 tag_ = tag;
198 }
199
GetTag() const200 std::string SysEvent::GetTag() const
201 {
202 return tag_;
203 }
204
SetLevel(const std::string & level)205 void SysEvent::SetLevel(const std::string& level)
206 {
207 level_ = level;
208 }
209
GetLevel() const210 std::string SysEvent::GetLevel() const
211 {
212 return level_;
213 }
214
SetSeq(int64_t seq)215 void SysEvent::SetSeq(int64_t seq)
216 {
217 seq_ = seq;
218 }
219
GetSeq() const220 int64_t SysEvent::GetSeq() const
221 {
222 return seq_;
223 }
224
SetEventSeq(int64_t eventSeq)225 void SysEvent::SetEventSeq(int64_t eventSeq)
226 {
227 eventSeq_ = eventSeq;
228 }
229
GetEventSeq() const230 int64_t SysEvent::GetEventSeq() const
231 {
232 return eventSeq_;
233 }
234
GetPid() const235 int32_t SysEvent::GetPid() const
236 {
237 return pid_;
238 }
239
GetTid() const240 int32_t SysEvent::GetTid() const
241 {
242 return tid_;
243 }
244
GetUid() const245 int32_t SysEvent::GetUid() const
246 {
247 return uid_;
248 }
249
GetTz() const250 int16_t SysEvent::GetTz() const
251 {
252 return tz_;
253 }
254
GetEventType() const255 int SysEvent::GetEventType() const
256 {
257 return eventType_;
258 }
259
SetId(uint64_t id)260 void SysEvent::SetId(uint64_t id)
261 {
262 if (rawData_ != nullptr && rawData_->GetData() != nullptr) {
263 rawData_->Update(reinterpret_cast<uint8_t*>(&id), sizeof(uint64_t),
264 BLOCK_SIZE_OFFSET + EventRaw::POS_OF_ID_IN_HEADER);
265 }
266 if (builder_ != nullptr) {
267 builder_->AppendId(id);
268 }
269 }
270
SetLog(uint8_t log)271 void SysEvent::SetLog(uint8_t log)
272 {
273 log_ = log;
274 if (rawData_ != nullptr && rawData_->GetData() != nullptr) {
275 reinterpret_cast<struct HiSysEventHeader*>(
276 rawData_->GetData() + BLOCK_SIZE_OFFSET)->log = log;
277 }
278 if (builder_ != nullptr) {
279 builder_->AppendLog(log);
280 }
281 }
282
SetPrivacy(uint8_t privacy)283 void SysEvent::SetPrivacy(uint8_t privacy)
284 {
285 privacy_ = privacy;
286 }
287
GetPrivacy() const288 uint8_t SysEvent::GetPrivacy() const
289 {
290 return privacy_;
291 }
292
SetInvalidParams(PARAM_INFO_MAP_PTR invalidParams)293 void SysEvent::SetInvalidParams(PARAM_INFO_MAP_PTR invalidParams)
294 {
295 invalidParams_ = invalidParams;
296 }
297
GetInvalidParams()298 PARAM_INFO_MAP_PTR SysEvent::GetInvalidParams()
299 {
300 return invalidParams_;
301 }
302
RemoveParam(const std::string & paramName)303 bool SysEvent::RemoveParam(const std::string& paramName)
304 {
305 if (!InitBuilder()) {
306 return false;
307 }
308 builder_->RemoveParam(paramName);
309 isUpdated_ = true;
310 return true;
311 }
312
IsParamExist(const std::string & paramName)313 bool SysEvent::IsParamExist(const std::string& paramName)
314 {
315 if (!InitBuilder()) {
316 return false;
317 }
318 return builder_->GetValue(paramName) != nullptr;
319 }
320
GetEventValue(const std::string & key)321 std::string SysEvent::GetEventValue(const std::string& key)
322 {
323 if (!InitBuilder()) {
324 return "";
325 }
326 std::string dest;
327 builder_->ParseValueByKey(key, dest);
328 return dest;
329 }
330
GetEventIntValue(const std::string & key)331 int64_t SysEvent::GetEventIntValue(const std::string& key)
332 {
333 if (!InitBuilder()) {
334 return DEFAULT_INT_VALUE;
335 }
336 if (int64_t intDest = DEFAULT_INT_VALUE; builder_->ParseValueByKey(key, intDest)) {
337 return intDest;
338 }
339 if (uint64_t uIntDest = DEFAULT_UINT_VALUE; builder_->ParseValueByKey(key, uIntDest) &&
340 (uIntDest <= static_cast<uint64_t>(std::numeric_limits<int64_t>::max()))) {
341 return static_cast<int64_t>(uIntDest);
342 }
343 if (double dDest = DEFAULT_DOUBLE_VALUE; builder_->ParseValueByKey(key, dDest) &&
344 (dDest >= static_cast<double>(std::numeric_limits<int64_t>::min())) &&
345 (dDest <= static_cast<double>(std::numeric_limits<int64_t>::max()))) {
346 return static_cast<int64_t>(dDest);
347 }
348 return DEFAULT_INT_VALUE;
349 }
350
GetEventUintValue(const std::string & key)351 uint64_t SysEvent::GetEventUintValue(const std::string& key)
352 {
353 if (!InitBuilder()) {
354 return DEFAULT_UINT_VALUE;
355 }
356 if (uint64_t uIntDest = DEFAULT_UINT_VALUE; builder_->ParseValueByKey(key, uIntDest)) {
357 return uIntDest;
358 }
359 if (int64_t intDest = DEFAULT_INT_VALUE; builder_->ParseValueByKey(key, intDest) &&
360 (intDest >= DEFAULT_INT_VALUE)) {
361 return static_cast<uint64_t>(intDest);
362 }
363 if (double dDest = DEFAULT_DOUBLE_VALUE; builder_->ParseValueByKey(key, dDest) &&
364 (dDest >= static_cast<double>(std::numeric_limits<uint64_t>::min())) &&
365 (dDest <= static_cast<double>(std::numeric_limits<uint64_t>::max()))) {
366 return static_cast<uint64_t>(dDest);
367 }
368 return DEFAULT_UINT_VALUE;
369 }
370
GetEventDoubleValue(const std::string & key)371 double SysEvent::GetEventDoubleValue(const std::string& key)
372 {
373 if (!InitBuilder()) {
374 return DEFAULT_DOUBLE_VALUE;
375 }
376 if (double dDest = DEFAULT_DOUBLE_VALUE; builder_->ParseValueByKey(key, dDest)) {
377 return dDest;
378 }
379 if (int64_t intDest = DEFAULT_INT_VALUE; builder_->ParseValueByKey(key, intDest)) {
380 return static_cast<double>(intDest);
381 }
382 if (uint64_t uIntDest = DEFAULT_UINT_VALUE; builder_->ParseValueByKey(key, uIntDest)) {
383 return static_cast<double>(uIntDest);
384 }
385 return DEFAULT_DOUBLE_VALUE;
386 }
387
GetEventIntArrayValue(const std::string & key,std::vector<int64_t> & dest)388 bool SysEvent::GetEventIntArrayValue(const std::string& key, std::vector<int64_t>& dest)
389 {
390 dest.clear();
391 if (!InitBuilder()) {
392 return false;
393 }
394 auto intArrayItemHandler = [&dest] (int64_t& item) {
395 dest.emplace_back(item);
396 return true;
397 };
398 auto uIntArrayItemHandler = [&dest] (uint64_t& item) {
399 if (item <= static_cast<uint64_t>(std::numeric_limits<int64_t>::max())) {
400 dest.emplace_back(static_cast<int64_t>(item));
401 return true;
402 }
403 return false;
404 };
405 auto dArrayItemHandler = [&dest] (double& item) {
406 if ((item >= static_cast<double>(std::numeric_limits<int64_t>::min())) &&
407 (item <= static_cast<double>(std::numeric_limits<int64_t>::max()))) {
408 dest.emplace_back(static_cast<int64_t>(item));
409 return true;
410 }
411 return false;
412 };
413 auto strArrayItemHandler = [&dest] (std::string& item) {
414 return false;
415 };
416 if (ParseArrayValue<int64_t>(builder_, key, intArrayItemHandler) ||
417 ParseArrayValue<uint64_t>(builder_, key, uIntArrayItemHandler) ||
418 ParseArrayValue<double>(builder_, key, dArrayItemHandler) ||
419 ParseArrayValue<std::string>(builder_, key, strArrayItemHandler)) {
420 return true;
421 }
422 dest.clear();
423 return false;
424 }
425
GetEventUintArrayValue(const std::string & key,std::vector<uint64_t> & dest)426 bool SysEvent::GetEventUintArrayValue(const std::string& key, std::vector<uint64_t>& dest)
427 {
428 dest.clear();
429 if (!InitBuilder()) {
430 return false;
431 }
432 auto uIntArrayItemHandler = [&dest] (uint64_t& item) {
433 dest.emplace_back(item);
434 return true;
435 };
436 auto intArrayItemHandler = [&dest] (int64_t& item) {
437 if (item >= DEFAULT_INT_VALUE) {
438 dest.emplace_back(static_cast<uint64_t>(item));
439 return true;
440 }
441 return false;
442 };
443 auto dArrayItemHandler = [&dest] (double& item) {
444 if ((item >= static_cast<double>(std::numeric_limits<uint64_t>::min())) &&
445 (item <= static_cast<double>(std::numeric_limits<uint64_t>::max()))) {
446 dest.emplace_back(static_cast<uint64_t>(item));
447 return true;
448 }
449 return false;
450 };
451 auto strArrayItemHandler = [&dest] (std::string& item) {
452 return false;
453 };
454 if (ParseArrayValue<uint64_t>(builder_, key, uIntArrayItemHandler) ||
455 ParseArrayValue<int64_t>(builder_, key, intArrayItemHandler) ||
456 ParseArrayValue<double>(builder_, key, dArrayItemHandler) ||
457 ParseArrayValue<std::string>(builder_, key, strArrayItemHandler)) {
458 return true;
459 }
460 dest.clear();
461 return false;
462 }
463
GetEventDoubleArrayValue(const std::string & key,std::vector<double> & dest)464 bool SysEvent::GetEventDoubleArrayValue(const std::string& key, std::vector<double>& dest)
465 {
466 dest.clear();
467 if (!InitBuilder()) {
468 return false;
469 }
470 auto dArrayItemHandler = [&dest] (double& item) {
471 dest.emplace_back(item);
472 return true;
473 };
474 auto intArrayItemHandler = [&dest] (int64_t& item) {
475 dest.emplace_back(static_cast<double>(item));
476 return true;
477 };
478 auto uIntArrayItemHandler = [&dest] (uint64_t& item) {
479 dest.emplace_back(static_cast<double>(item));
480 return true;
481 };
482 auto strArrayItemHandler = [&dest] (std::string& item) {
483 return false;
484 };
485 if (ParseArrayValue<double>(builder_, key, dArrayItemHandler) ||
486 ParseArrayValue<int64_t>(builder_, key, intArrayItemHandler) ||
487 ParseArrayValue<uint64_t>(builder_, key, uIntArrayItemHandler) ||
488 ParseArrayValue<std::string>(builder_, key, strArrayItemHandler)) {
489 return true;
490 }
491 dest.clear();
492 return false;
493 }
494
GetEventStringArrayValue(const std::string & key,std::vector<std::string> & dest)495 bool SysEvent::GetEventStringArrayValue(const std::string& key, std::vector<std::string>& dest)
496 {
497 dest.clear();
498 if (!InitBuilder()) {
499 return false;
500 }
501 auto strArrayItemHandler = [&dest] (std::string& item) {
502 dest.emplace_back(item);
503 return true;
504 };
505 auto dArrayItemHandler = [&dest] (double& item) {
506 return false;
507 };
508 auto intArrayItemHandler = [&dest] (int64_t& item) {
509 return false;
510 };
511 auto uIntArrayItemHandler = [&dest] (uint64_t& item) {
512 return false;
513 };
514 if (ParseArrayValue<std::string>(builder_, key, strArrayItemHandler) ||
515 ParseArrayValue<uint64_t>(builder_, key, uIntArrayItemHandler) ||
516 ParseArrayValue<int64_t>(builder_, key, intArrayItemHandler) ||
517 ParseArrayValue<double>(builder_, key, dArrayItemHandler)) {
518 return true;
519 }
520 dest.clear();
521 return false;
522 }
523
TryToUpdateRawData()524 bool SysEvent::TryToUpdateRawData()
525 {
526 if (rawData_ == nullptr) {
527 return false;
528 }
529
530 // raw data does not need to be re-initialized
531 if (!isUpdated_) {
532 return true;
533 }
534
535 // raw data needs to be re-initialized
536 if (!InitBuilder()) {
537 return false;
538 }
539 builder_->AppendLog(log_);
540 rawData_ = builder_->Build();
541 if (rawData_ == nullptr) {
542 return false;
543 }
544 isUpdated_ = false;
545 return true;
546 }
547
AsJsonStr()548 std::string SysEvent::AsJsonStr()
549 {
550 if (!TryToUpdateRawData()) {
551 return "";
552 }
553
554 std::string jsonStr;
555 {
556 EventRaw::DecodedEvent event(rawData_->GetData(), rawData_->GetDataLength());
557 jsonStr = event.AsJsonStr();
558 }
559 if (!tag_.empty()) {
560 AppendJsonValue(jsonStr, EventStore::EventCol::TAG, tag_);
561 }
562 if (!level_.empty()) {
563 AppendJsonValue(jsonStr, EventStore::EventCol::LEVEL, level_);
564 }
565 if (eventSeq_ >= 0) {
566 AppendJsonValue(jsonStr, EventStore::EventCol::SEQ, eventSeq_);
567 }
568 if (reportInterval_ != UNINIT_REPORT_INTERVAL) {
569 AppendJsonValue(jsonStr, REPORT_INTERVAL_KEY, reportInterval_);
570 }
571 return jsonStr;
572 }
573
AsRawData()574 uint8_t* SysEvent::AsRawData()
575 {
576 if (!TryToUpdateRawData()) {
577 return nullptr;
578 }
579 return rawData_->GetData();
580 }
581
EscapeJsonStringValue(const std::string & src)582 std::string SysEvent::EscapeJsonStringValue(const std::string& src)
583 {
584 return StringUtil::EscapeJsonStringValue(src);
585 }
586
UnescapeJsonStringValue(const std::string & src)587 std::string SysEvent::UnescapeJsonStringValue(const std::string& src)
588 {
589 return StringUtil::UnescapeJsonStringValue(src);
590 }
591
GetSysVersion()592 std::string SysEvent::GetSysVersion()
593 {
594 return sysVersion_;
595 }
596
GetPatchVersion()597 std::string SysEvent::GetPatchVersion()
598 {
599 return patchVersion_;
600 }
601
SetEventPeriodSeqInfo(const EventPeriodSeqInfo & info)602 void SysEvent::SetEventPeriodSeqInfo(const EventPeriodSeqInfo& info)
603 {
604 std::string periodSeqStr = info.timeStamp;
605 // append export tag
606 periodSeqStr.append(" ").append(std::to_string(info.isNeedExport ? 1 : 0));
607 // append period sequence
608 periodSeqStr.append(" ").append(std::to_string(info.periodSeq));
609 SetValue("period_seq_", periodSeqStr);
610 }
611
GetEventPeriodSeqInfo()612 EventPeriodSeqInfo SysEvent::GetEventPeriodSeqInfo()
613 {
614 std::string periodSeqStr = GetValue("period_seq_");
615 if (periodSeqStr.empty()) {
616 periodSeqStr = GetEventValue("period_seq_");
617 }
618 std::vector<std::string> allInfo;
619 StringUtil::SplitStr(periodSeqStr, " ", allInfo);
620 EventPeriodSeqInfo eventPeriodSeqInfo;
621 if (allInfo.size() != PERIOD_SEQ_INFO_ITEM_CNT) {
622 return eventPeriodSeqInfo;
623 }
624 eventPeriodSeqInfo.timeStamp = allInfo[0]; // 0 is the index of time stamp
625 int isNeedExported = 0;
626 StringUtil::ConvertStringTo(allInfo[1], isNeedExported); // 1 is the index of tag whether to export
627 eventPeriodSeqInfo.isNeedExport = static_cast<bool>(isNeedExported);
628 uint64_t periodSeq = 0;
629 StringUtil::ConvertStringTo(allInfo[2], periodSeq); // 2 is the index of period sequence
630 eventPeriodSeqInfo.periodSeq = periodSeq;
631 return eventPeriodSeqInfo;
632 }
633
SetReportInterval(int16_t reportInterval)634 void SysEvent::SetReportInterval(int16_t reportInterval)
635 {
636 if (reportInterval_ == reportInterval) {
637 return;
638 }
639 reportInterval_ = reportInterval;
640 SetEventValue(REPORT_INTERVAL_KEY, reportInterval_);
641 }
642
GetReportInterval()643 int16_t SysEvent::GetReportInterval()
644 {
645 if (reportInterval_ == UNINIT_REPORT_INTERVAL) {
646 reportInterval_ = static_cast<int16_t>(GetEventIntValue(REPORT_INTERVAL_KEY));
647 }
648 return reportInterval_;
649 }
650
SysEventCreator(const std::string & domain,const std::string & eventName,SysEventCreator::EventType type)651 SysEventCreator::SysEventCreator(const std::string& domain, const std::string& eventName,
652 SysEventCreator::EventType type)
653 {
654 builder_ = std::make_shared<EventRaw::RawDataBuilder>();
655 if (builder_ != nullptr) {
656 builder_->AppendDomain(domain).AppendName(eventName).AppendType(static_cast<int>(type)).
657 AppendTimeStamp(TimeUtil::GetMilliseconds()).AppendTimeZone(TimeUtil::GetTimeZone()).
658 AppendPid(getpid()).AppendTid(gettid()).AppendUid(getuid());
659 }
660 }
661
GetRawData()662 std::shared_ptr<EventRaw::RawData> SysEventCreator::GetRawData()
663 {
664 if (builder_ == nullptr) {
665 return nullptr;
666 }
667 return builder_->Build();
668 }
669
EscapeJsonStringValue(const std::string & src)670 std::string SysEventCreator::EscapeJsonStringValue(const std::string& src)
671 {
672 return StringUtil::EscapeJsonStringValue(src);
673 }
674 } // namespace HiviewDFX
675 } // namespace OHOS
676