• 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 "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 EventListener::EventIdRange & range)76 void EventListener::AddListenerInfo(uint32_t type, const EventListener::EventIdRange &range)
77 {
78     auto& context = HiviewGlobal::GetInstance();
79     if (context == nullptr) {
80         return;
81     }
82     std::set<std::string> eventNames;
83     std::set<EventListener::EventIdRange> listenerInfo;
84     listenerInfo.insert(range);
85     context->AddListenerInfo(type, GetListenerName(), eventNames, listenerInfo);
86 }
87 
AddListenerInfo(uint32_t type,const std::set<EventListener::EventIdRange> & listenerInfo)88 void EventListener::AddListenerInfo(uint32_t type, const std::set<EventListener::EventIdRange> &listenerInfo)
89 {
90     auto& context = HiviewGlobal::GetInstance();
91     if (context == nullptr) {
92         return;
93     }
94     std::set<std::string> eventNames;
95     context->AddListenerInfo(type, GetListenerName(), eventNames, listenerInfo);
96 }
97 
GetListenerInfo(uint32_t type,std::set<EventListener::EventIdRange> & listenerInfo)98 bool EventListener::GetListenerInfo(uint32_t type, std::set<EventListener::EventIdRange> &listenerInfo)
99 {
100     auto& context = HiviewGlobal::GetInstance();
101     if (context == nullptr) {
102         return false;
103     }
104     return context->GetListenerInfo(type, GetListenerName(), listenerInfo);
105 }
106 
AddListenerInfo(uint32_t type,const std::string & eventName)107 void EventListener::AddListenerInfo(uint32_t type, const std::string& eventName)
108 {
109     auto& context = HiviewGlobal::GetInstance();
110     if (context == nullptr) {
111         return;
112     }
113     std::set<std::string> eventNames;
114     std::set<EventListener::EventIdRange> listenerInfo;
115     eventNames.insert(eventName);
116     context->AddListenerInfo(type, GetListenerName(), eventNames, listenerInfo);
117 }
118 
AddListenerInfo(uint32_t type,const std::set<std::string> & eventNames)119 void EventListener::AddListenerInfo(uint32_t type, const std::set<std::string> &eventNames)
120 {
121     auto& context = HiviewGlobal::GetInstance();
122     if (context == nullptr) {
123         return;
124     }
125     std::set<EventListener::EventIdRange> listenerInfo;
126     context->AddListenerInfo(type, GetListenerName(), eventNames, listenerInfo);
127 }
128 
GetListenerInfo(uint32_t type,std::set<std::string> & eventNames)129 bool EventListener::GetListenerInfo(uint32_t type, std::set<std::string> &eventNames)
130 {
131     auto& context = HiviewGlobal::GetInstance();
132     if (context == nullptr) {
133         return false;
134     }
135     return context->GetListenerInfo(type, GetListenerName(), eventNames);
136 }
137 }
138 }
139