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 }; 129 130 enum ManageType { 131 ORDERED, 132 UNORDERED, 133 PROCESS_TYPE_NUM, 134 }; 135 136 enum EventId { 137 PLUGIN_LOADED, 138 }; 139 140 MessageType messageType_; 141 ManageType processType_; 142 uint16_t what_; 143 uint32_t eventId_; 144 uint64_t happenTime_; 145 uint64_t targetDispatchTime_; 146 uint64_t createTime_; 147 uint64_t realtime_; 148 uint64_t processTime_; 149 std::string sender_; 150 std::string domain_; 151 std::string eventName_; 152 bool isPipeline_; 153 bool hasFinish_; 154 bool hasPending_; 155 // dft fault listener params 156 std::string traceId_; 157 std::string spanId_; 158 std::string parentSpanId_; 159 std::string traceFlag_; 160 std::string normalExtraInfo_; 161 std::shared_ptr<EventRaw::RawData> rawData_ = nullptr; 162 void SetValue(const std::string &name, const std::string &value); 163 void SetValue(const std::string &name, int32_t value); 164 void SetKeyValuePairs(std::map<std::string, std::string> keyValuePairs); 165 const std::string GetValue(const std::string &name) const; 166 int32_t GetIntValue(const std::string &name) const; 167 std::map<std::string, std::string> GetKeyValuePairs() const; 168 void ResetTimestamp(); ResetPendingStatus()169 void ResetPendingStatus() 170 { 171 hasPending_ = false; 172 }; 173 IsPipelineEvent()174 bool IsPipelineEvent() const 175 { 176 return isPipeline_; 177 }; 178 HasFinish()179 bool HasFinish() const 180 { 181 return hasFinish_; 182 }; 183 HasPending()184 bool HasPending() const 185 { 186 return hasPending_; 187 }; 188 GetEventName()189 std::string GetEventName() const 190 { 191 return eventName_; 192 } 193 SetEventName(const std::string & name)194 void SetEventName(const std::string &name) 195 { 196 eventName_ = name; 197 } 198 199 template <typename Derived> DownCastTo(std::shared_ptr<Event> event)200 static std::shared_ptr<Derived> DownCastTo(std::shared_ptr<Event> event) 201 { 202 return std::static_pointer_cast<Derived>(event); 203 }; 204 205 // Ensure the copy constructor of Base and Derived classes are carefully arranged 206 template <typename Base, typename Derived> 207 static std::shared_ptr<Derived> Repack(std::shared_ptr<Event>& event, bool replace = true) 208 { 209 auto base = std::static_pointer_cast<Base>(event); 210 if (replace) { 211 auto derived = new Derived(*(base.get())); 212 event.reset(derived); 213 return std::static_pointer_cast<Derived>(event); 214 } 215 216 auto newEvent = std::make_shared<Derived>(*(base.get())); 217 newEvent->OnRepack(); 218 return newEvent; 219 }; 220 221 protected: 222 std::map<std::string, std::string> bundle_; 223 }; 224 class DllExport EventHandler { 225 public: ~EventHandler()226 virtual ~EventHandler(){}; 227 virtual bool OnEvent(std::shared_ptr<Event>& event) = 0; OnEventProxy(std::shared_ptr<Event> event)228 virtual bool OnEventProxy(std::shared_ptr<Event> event) 229 { 230 return OnEvent(event); 231 }; 232 CanProcessEvent(std::shared_ptr<Event> event __UNUSED)233 virtual bool CanProcessEvent(std::shared_ptr<Event> event __UNUSED) 234 { 235 return true; 236 }; 237 IsInterestedPipelineEvent(std::shared_ptr<Event> event __UNUSED)238 virtual bool IsInterestedPipelineEvent(std::shared_ptr<Event> event __UNUSED) 239 { 240 return true; 241 }; 242 CanProcessMoreEvents()243 virtual bool CanProcessMoreEvents() 244 { 245 return true; 246 }; 247 GetHandlerInfo()248 virtual std::string GetHandlerInfo() 249 { 250 return ""; 251 }; 252 }; 253 class EventListener { 254 public: EventListener()255 EventListener() {}; ~EventListener()256 virtual ~EventListener(){}; 257 OnOrderedEvent(const Event & msg)258 virtual bool OnOrderedEvent(const Event &msg) 259 { 260 return false; 261 } 262 virtual void OnUnorderedEvent(const Event &msg) = 0; 263 virtual std::string GetListenerName() = 0; 264 void AddListenerInfo(uint32_t type); 265 void AddListenerInfo(uint32_t type, const std::map<std::string, DomainRule>& domainRulesMap); 266 void AddListenerInfo(uint32_t type, const std::set<std::string> &eventNames, 267 const std::map<std::string, DomainRule>& domainRulesMap = {}); 268 }; 269 } // namespace HiviewDFX 270 } // namespace OHOS 271 #endif // HIVIEW_BASE_EVENT_H 272