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