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