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_PLUGIN_EXAMPLES_EVENT_SOURCE_EXAMPLE 16 #define HIVIEW_PLUGIN_EXAMPLES_EVENT_SOURCE_EXAMPLE 17 #include "event_source.h" 18 namespace OHOS { 19 namespace HiviewDFX { 20 class EventSourceExampleEvent : public PipelineEvent { 21 public: EventSourceExampleEvent(const std::string & sender,PipelineEventProducer * handler)22 EventSourceExampleEvent(const std::string& sender, PipelineEventProducer* handler) 23 : PipelineEvent(sender, handler), data_(nullptr), addon_(""){}; 24 EventSourceExampleEvent(const EventSourceExampleEvent & obj)25 EventSourceExampleEvent(const EventSourceExampleEvent& obj) : PipelineEvent(obj) 26 { 27 data_ = nullptr; 28 addon_ = obj.addon_; 29 }; 30 31 EventSourceExampleEvent& operator=(const EventSourceExampleEvent& obj) 32 { 33 if (&obj == this) { 34 return *this; 35 } 36 37 PipelineEvent::operator=(obj); 38 data_ = nullptr; 39 addon_ = obj.addon_; 40 return *this; 41 }; 42 ~EventSourceExampleEvent()43 ~EventSourceExampleEvent() 44 { 45 if (data_ != nullptr) { 46 free(data_); 47 data_ = nullptr; 48 } 49 } 50 51 // example for add more metrics 52 char* data_; 53 std::string addon_; 54 }; 55 56 class EventSourceExample : public FileDescriptorEventCallback, public EventSource { 57 public: 58 EventSourceExample(); 59 ~EventSourceExample(); 60 61 static std::set<std::string> count; 62 63 void OnLoad() override; 64 void OnUnload() override; 65 void StartEventSource() override; 66 67 bool OnFileDescriptorEvent(int fd, int type) override; 68 int32_t GetPollFd() override; 69 int32_t GetPollType() override; 70 71 void Recycle(PipelineEvent* event) override; 72 void PauseDispatch(std::weak_ptr<Plugin> plugin) override; 73 const static inline int PIPELINE_EVENT_ID_AAA = 901000000; 74 const static inline int PIPELINE_EVENT_ID_BBB = 901000001; 75 const static inline int PIPELINE_EVENT_ID_CCC = 901000002; 76 const static inline int PIPELINE_EVENT_ID_TAA = 901000010; 77 78 private: 79 void CreateWatchFile(const std::string& path); 80 void CreateAndPublishEvent(const std::string& file); 81 const static inline std::string SYSTEM_FAULT_LOG_PATH = "/data/test/faultlog"; 82 int inotifyFd_; 83 std::map<std::string, int> fileMap_; 84 }; 85 } // namespace HiviewDFX 86 } // namespace OHOS 87 #endif // HIVIEW_PLUGIN_EXAMPLES_EVENT_SOURCE_EXAMPLE