1 /*
2  * Copyright (c) 2021-2022 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 
16 #ifndef HISYSEVENT_MANAGER_H
17 #define HISYSEVENT_MANAGER_H
18 
19 #include <mutex>
20 #include <string>
21 #include <unordered_map>
22 #include <vector>
23 
24 #include "hisysevent_base_listener.h"
25 #include "hisysevent_listener.h"
26 #include "hisysevent_query_callback.h"
27 #include "hisysevent_rules.h"
28 
29 namespace OHOS {
30 namespace HiviewDFX {
31 class HiSysEventManager {
32 public:
33     HiSysEventManager() = default;
~HiSysEventManager()34     ~HiSysEventManager() {}
35 
36 public:
37     /**
38      * @brief Add a watcher on event writing.
39      * @param listener  event watcher.
40      * @param rules    rules for watcher.
41      * @return 0 means success, others means failure.
42      */
43     static int32_t AddListener(std::shared_ptr<HiSysEventListener> listener,
44         std::vector<ListenerRule>& rules);
45 
46     /**
47      * @brief Remove a watcher.
48      * @param listener event watcher.
49      * @return 0 means success, others means failure.
50      */
51     static int32_t RemoveListener(std::shared_ptr<HiSysEventListener> listener);
52 
53     /**
54      * @brief Query event.
55      * @param arg      arg of query.
56      * @param rules    rules of query.
57      * @param callback callback of query.
58      * @return 0 means success, others means failure.
59      */
60     static int32_t Query(struct QueryArg& arg, std::vector<QueryRule>& rules,
61         std::shared_ptr<HiSysEventQueryCallback> callback);
62 
63     /**
64      * @brief Set debug mode for event watcher.
65      * @param listener  event watcher.
66      * @param mode      debug mode.
67      * @return 0 means success, others means failure.
68      */
69     static int32_t SetDebugMode(std::shared_ptr<HiSysEventListener> listener, bool mode);
70 
71 private:
72     static std::unordered_map<std::shared_ptr<HiSysEventListener>,
73         std::shared_ptr<HiSysEventBaseListener>> listenerToBaseMap_;
74     static std::mutex listenersMutex_;
75 };
76 } // namespace HiviewDFX
77 } // namespace OHOS
78 
79 #endif // HISYSEVENT_MANAGER_H
80 
81