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 "audit.h"
19 #include "hiview_global.h"
20 #include "time_util.h"
21
22 namespace OHOS {
23 namespace HiviewDFX {
SetValue(const std::string & name,const std::string & value)24 void Event::SetValue(const std::string &name, const std::string &value)
25 {
26 if (name.length() > 0) {
27 // force update original value
28 bundle_[name] = value;
29 }
30 }
31
GetValue(const std::string & name) const32 const std::string Event::GetValue(const std::string &name) const
33 {
34 auto it = bundle_.find(name);
35 if (it != bundle_.end()) {
36 return it->second;
37 }
38 return std::string("");
39 }
40
SetValue(const std::string & name,int32_t value)41 void Event::SetValue(const std::string &name, int32_t value)
42 {
43 std::string str = std::to_string(value);
44 if (name.length() > 0) {
45 // force update original value
46 bundle_[name] = str;
47 }
48 }
49
GetIntValue(const std::string & name) const50 int32_t Event::GetIntValue(const std::string &name) const
51 {
52 auto it = bundle_.find(name);
53 if (it != bundle_.end()) {
54 const int decimal = 10;
55 int32_t ret = static_cast<int32_t>(std::strtol(it->second.c_str(), nullptr, decimal));
56 return (errno == ERANGE) ? -1 : ret;
57 }
58 return -1;
59 }
60
ResetTimestamp()61 void Event::ResetTimestamp()
62 {
63 createTime_ = TimeUtil::GenerateTimestamp();
64 }
65
SetKeyValuePairs(std::map<std::string,std::string> keyValuePairs)66 void Event::SetKeyValuePairs(std::map<std::string, std::string> keyValuePairs)
67 {
68 bundle_.insert(keyValuePairs.begin(), keyValuePairs.end());
69 }
70
GetKeyValuePairs() const71 std::map<std::string, std::string> Event::GetKeyValuePairs() const
72 {
73 return bundle_;
74 }
75
AddListenerInfo(uint32_t type,const std::map<std::string,DomainRule> & domainRulesMap)76 void EventListener::AddListenerInfo(uint32_t type, const std::map<std::string, DomainRule>& domainRulesMap)
77 {
78 auto& context = HiviewGlobal::GetInstance();
79 if (context == nullptr) {
80 return;
81 }
82 std::set<std::string> eventNames;
83 context->AddListenerInfo(type, GetListenerName(), eventNames, domainRulesMap);
84 }
85
AddListenerInfo(uint32_t type,const std::set<std::string> & eventNames,const std::map<std::string,DomainRule> & domainRulesMap)86 void EventListener::AddListenerInfo(uint32_t type, const std::set<std::string> &eventNames,
87 const std::map<std::string, DomainRule>& domainRulesMap)
88 {
89 auto& context = HiviewGlobal::GetInstance();
90 if (context == nullptr) {
91 return;
92 }
93 context->AddListenerInfo(type, GetListenerName(), eventNames, domainRulesMap);
94 }
95
AddListenerInfo(uint32_t type)96 void EventListener::AddListenerInfo(uint32_t type)
97 {
98 auto& context = HiviewGlobal::GetInstance();
99 if (context == nullptr) {
100 return;
101 }
102 context->AddListenerInfo(type, GetListenerName());
103 }
104 }
105 }
106