• 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 #ifndef HIVIEW_BASE_EVENT_H
16 #define HIVIEW_BASE_EVENT_H
17 #include <iostream>
18 #include <list>
19 #include <map>
20 #include <memory>
21 #include <set>
22 #include <string>
23 #include <string>
24 #include <sys/types.h>
25 #include "defines.h"
26 
27 #include "base/raw_data.h"
28 
29 namespace OHOS {
30 namespace HiviewDFX {
31 class DllExport Event : public std::enable_shared_from_this<Event> {
32 public:
Event(const std::string & sender)33     explicit Event(const std::string &sender)
34         : messageType_(MessageType::NONE),
35           processType_(ManageType::UNORDERED),
36           what_(0),
37           eventId_(0),
38           happenTime_(0),
39           targetDispatchTime_(0),
40           createTime_(0),
41           realtime_(0),
42           processTime_(0),
43           sender_(sender),
44           domain_(""),
45           eventName_(""),
46           isPipeline_(false),
47           hasFinish_(false),
48           hasPending_(false),
49           traceId_(""),
50           spanId_(""),
51           parentSpanId_(""),
52           traceFlag_(""),
53           normalExtraInfo_(""),
54           rawData_(nullptr)
55     {
56         ResetTimestamp();
57     };
58 
Event(const std::string & sender,const std::string & name)59     explicit Event(const std::string &sender, const std::string &name)
60         : messageType_(MessageType::NONE),
61           processType_(ManageType::UNORDERED),
62           what_(0),
63           eventId_(0),
64           happenTime_(0),
65           targetDispatchTime_(0),
66           createTime_(0),
67           realtime_(0),
68           processTime_(0),
69           sender_(sender),
70           domain_(""),
71           eventName_(name),
72           isPipeline_(false),
73           hasFinish_(false),
74           hasPending_(false),
75           traceId_(""),
76           spanId_(""),
77           parentSpanId_(""),
78           traceFlag_(""),
79           normalExtraInfo_(""),
80           rawData_(nullptr)
81     {
82         ResetTimestamp();
83     };
84 
~Event()85     virtual ~Event() {};
86 
OnContinue()87     virtual bool OnContinue()
88     {
89         return true;
90     };
91 
OnFinish()92     virtual bool OnFinish()
93     {
94         hasFinish_ = true;
95         return true;
96     };
97 
OnRepack()98     virtual void OnRepack() {};
99 
OnPending()100     virtual void OnPending() {};
101 
GetPendingProcessorSize()102     virtual uint32_t GetPendingProcessorSize()
103     {
104         return 0;
105     }
106 
107     // call from audit module
108     // if you want to override this function
109     // you should call parent function first and append to your result
GetEventInfo()110     virtual std::string GetEventInfo()
111     {
112         return std::to_string(eventId_);
113     };
114 
115     // use for broadcasting events
116     enum MessageType {
117         NONE = 0,
118         PLUGIN_MAINTENANCE, // Reserved
119         FAULT_EVENT,
120         STATISTICS_EVENT,
121         RAW_EVENT,
122         SYS_EVENT,
123         UE_EVENT,
124         EXTERNAL_EVENT,
125         EXTERNAL_REMOTE_EVENT,
126         CROSS_PLATFORM,
127         PRIVATE_MESSAGE_TYPE, // Expand macro from defines.h
128         EVENT_EXPORT_TYPE
129     };
130 
131     enum ManageType {
132         ORDERED,
133         UNORDERED,
134         PROCESS_TYPE_NUM,
135     };
136 
137     enum EventId {
138         PLUGIN_LOADED,
139     };
140 
141     MessageType messageType_;
142     ManageType processType_;
143     uint16_t what_;
144     uint32_t eventId_;
145     uint64_t happenTime_;
146     uint64_t targetDispatchTime_;
147     uint64_t createTime_;
148     uint64_t realtime_;
149     uint64_t processTime_;
150     std::string sender_;
151     std::string domain_;
152     std::string eventName_;
153     bool isPipeline_;
154     bool hasFinish_;
155     bool hasPending_;
156     // dft fault listener params
157     std::string traceId_;
158     std::string spanId_;
159     std::string parentSpanId_;
160     std::string traceFlag_;
161     std::string normalExtraInfo_;
162     std::shared_ptr<EventRaw::RawData> rawData_ = nullptr;
163     void SetValue(const std::string &name, const std::string &value);
164     void SetValue(const std::string &name, int32_t value);
165     void SetKeyValuePairs(std::map<std::string, std::string> keyValuePairs);
166     const std::string GetValue(const std::string &name) const;
167     int32_t GetIntValue(const std::string &name) const;
168     int64_t GetInt64Value(const std::string &name) const;
169     std::map<std::string, std::string> GetKeyValuePairs() const;
170     void ResetTimestamp();
ResetPendingStatus()171     void ResetPendingStatus()
172     {
173         hasPending_ = false;
174     };
175 
IsPipelineEvent()176     bool IsPipelineEvent() const
177     {
178         return isPipeline_;
179     };
180 
HasFinish()181     bool HasFinish() const
182     {
183         return hasFinish_;
184     };
185 
HasPending()186     bool HasPending() const
187     {
188         return hasPending_;
189     };
190 
GetEventName()191     std::string GetEventName() const
192     {
193         return eventName_;
194     }
195 
SetEventName(const std::string & name)196     void SetEventName(const std::string &name)
197     {
198         eventName_ = name;
199     }
200 
201     template <typename Derived>
DownCastTo(std::shared_ptr<Event> event)202     static std::shared_ptr<Derived> DownCastTo(std::shared_ptr<Event> event)
203     {
204         return std::static_pointer_cast<Derived>(event);
205     };
206 
207     // Ensure the copy constructor of Base and Derived classes are carefully arranged
208     template <typename Base, typename Derived>
209     static std::shared_ptr<Derived> Repack(std::shared_ptr<Event>& event, bool replace = true)
210     {
211         auto base = std::static_pointer_cast<Base>(event);
212         if (replace) {
213             auto derived = new Derived(*(base.get()));
214             event.reset(derived);
215             return std::static_pointer_cast<Derived>(event);
216         }
217 
218         auto newEvent = std::make_shared<Derived>(*(base.get()));
219         newEvent->OnRepack();
220         return newEvent;
221     };
222 
223 protected:
224     std::map<std::string, std::string> bundle_;
225 };
226 class DllExport EventHandler {
227 public:
~EventHandler()228     virtual ~EventHandler(){};
229     virtual bool OnEvent(std::shared_ptr<Event>& event) = 0;
OnEventProxy(std::shared_ptr<Event> event)230     virtual bool OnEventProxy(std::shared_ptr<Event> event)
231     {
232         return OnEvent(event);
233     };
234 
CanProcessEvent(std::shared_ptr<Event> event __UNUSED)235     virtual bool CanProcessEvent(std::shared_ptr<Event> event __UNUSED)
236     {
237         return true;
238     };
239 
IsInterestedPipelineEvent(std::shared_ptr<Event> event __UNUSED)240     virtual bool IsInterestedPipelineEvent(std::shared_ptr<Event> event __UNUSED)
241     {
242         return true;
243     };
244 
CanProcessMoreEvents()245     virtual bool CanProcessMoreEvents()
246     {
247         return true;
248     };
249 
GetHandlerInfo()250     virtual std::string GetHandlerInfo()
251     {
252         return "";
253     };
254 };
255 class EventListener {
256 public:
EventListener()257     EventListener() {};
~EventListener()258     virtual ~EventListener(){};
259 
OnOrderedEvent(const Event & msg)260     virtual bool OnOrderedEvent(const Event &msg)
261     {
262         return false;
263     }
264     virtual void OnUnorderedEvent(const Event &msg) = 0;
265     virtual std::string GetListenerName() = 0;
266     void AddListenerInfo(uint32_t type);
267     void AddListenerInfo(uint32_t type, const std::map<std::string, DomainRule>& domainRulesMap);
268     void AddListenerInfo(uint32_t type, const std::set<std::string> &eventNames,
269         const std::map<std::string, DomainRule>& domainRulesMap = {});
270 };
271 } // namespace HiviewDFX
272 } // namespace OHOS
273 #endif // HIVIEW_BASE_EVENT_H
274