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