• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "event.h"
17 
18 #include <charconv>
19 
20 #include "hiview_global.h"
21 #include "time_util.h"
22 
23 namespace OHOS {
24 namespace HiviewDFX {
SetValue(const std::string & name,const std::string & value)25 void Event::SetValue(const std::string &name, const std::string &value)
26 {
27     if (name.length() > 0) {
28         // force update original value
29         bundle_[name] = value;
30     }
31 }
32 
GetValue(const std::string & name) const33 const std::string Event::GetValue(const std::string &name) const
34 {
35     auto it = bundle_.find(name);
36     if (it != bundle_.end()) {
37         return it->second;
38     }
39     return std::string("");
40 }
41 
SetValue(const std::string & name,int32_t value)42 void Event::SetValue(const std::string &name, int32_t value)
43 {
44     std::string str = std::to_string(value);
45     if (name.length() > 0) {
46         // force update original value
47         bundle_[name] = str;
48     }
49 }
50 
GetIntValue(const std::string & name) const51 int32_t Event::GetIntValue(const std::string &name) const
52 {
53     auto it = bundle_.find(name);
54     if (it != bundle_.end()) {
55         const int decimal = 10;
56         int32_t ret = static_cast<int32_t>(std::strtol(it->second.c_str(), nullptr, decimal));
57         return (errno == ERANGE) ? -1 : ret;
58     }
59     return -1;
60 }
61 
GetInt64Value(const std::string & name) const62 int64_t Event::GetInt64Value(const std::string &name) const
63 {
64     int64_t dateNum = -1;
65     if (auto it = bundle_.find(name); it != bundle_.end()) {
66         auto valueStr = it->second;
67         auto result = std::from_chars(valueStr.c_str(), valueStr.c_str() + valueStr.size(), dateNum);
68         if (result.ec != std::errc()) {
69             return -1;
70         }
71     }
72     return dateNum;
73 }
74 
ResetTimestamp()75 void Event::ResetTimestamp()
76 {
77     createTime_ = TimeUtil::GenerateTimestamp();
78 }
79 
SetKeyValuePairs(std::map<std::string,std::string> keyValuePairs)80 void Event::SetKeyValuePairs(std::map<std::string, std::string> keyValuePairs)
81 {
82     bundle_.insert(keyValuePairs.begin(), keyValuePairs.end());
83 }
84 
GetKeyValuePairs() const85 std::map<std::string, std::string> Event::GetKeyValuePairs() const
86 {
87     return bundle_;
88 }
89 
AddListenerInfo(uint32_t type,const std::map<std::string,DomainRule> & domainRulesMap)90 void EventListener::AddListenerInfo(uint32_t type, const std::map<std::string, DomainRule>& domainRulesMap)
91 {
92     auto& context = HiviewGlobal::GetInstance();
93     if (context == nullptr) {
94         return;
95     }
96     std::set<std::string> eventNames;
97     context->AddListenerInfo(type, GetListenerName(), eventNames, domainRulesMap);
98 }
99 
AddListenerInfo(uint32_t type,const std::set<std::string> & eventNames,const std::map<std::string,DomainRule> & domainRulesMap)100 void EventListener::AddListenerInfo(uint32_t type, const std::set<std::string> &eventNames,
101     const std::map<std::string, DomainRule>& domainRulesMap)
102 {
103     auto& context = HiviewGlobal::GetInstance();
104     if (context == nullptr) {
105         return;
106     }
107     context->AddListenerInfo(type, GetListenerName(), eventNames, domainRulesMap);
108 }
109 
AddListenerInfo(uint32_t type)110 void EventListener::AddListenerInfo(uint32_t type)
111 {
112     auto& context = HiviewGlobal::GetInstance();
113     if (context == nullptr) {
114         return;
115     }
116     context->AddListenerInfo(type, GetListenerName());
117 }
118 }
119 }
120