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