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