1 /*
2  * Copyright (c) 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 FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_EVENT_REPORT_H
17 #define FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_EVENT_REPORT_H
18 
19 #include <unordered_map>
20 #include <string>
21 
22 #ifdef HAS_HISYSEVENT_PART
23 #include "hisysevent.h"
24 #endif
25 
26 namespace OHOS {
27 namespace EventFwk {
28 namespace {
29 // event name
30 constexpr char ORDERED_EVENT_PROC_TIMEOUT[] = "ORDERED_EVENT_PROC_TIMEOUT";
31 constexpr char STATIC_EVENT_PROC_ERROR[] = "STATIC_EVENT_PROC_ERROR";
32 constexpr char SUBSCRIBER_EXCEED_MAXIMUM[] = "SUBSCRIBER_EXCEED_MAXIMUM";
33 constexpr char PUBLISH_ERROR[] = "PUBLISH_ERROR";
34 constexpr char SUBSCRIBE[] = "SUBSCRIBE";
35 constexpr char UNSUBSCRIBE[] = "UNSUBSCRIBE";
36 constexpr char PUBLISH[] = "PUBLISH";
37 constexpr char STATIC_SUBSCRIBER_START[] = "STATIC_SUBSCRIBER_START";
38 constexpr char STATIC_SUBSCRIBER_RUNTIME[] = "STATIC_SUBSCRIBER_RUNTIME";
39 } // namespace
40 
41 struct EventInfo {
42     int32_t userId;
43     int32_t pid;
44     int32_t uid;
45     int32_t resultCode;
46     uint32_t subscriberNum;
47     std::string publisherName;
48     std::string subscriberName;
49     std::string eventName;
50 
EventInfoEventInfo51     EventInfo() : userId(-1), pid(0), uid(0), resultCode(0), subscriberNum(0) {}
52 };
53 
54 class EventReport {
55 public:
56     /**
57      * @brief send hisysevent
58      *
59      * @param eventName event name, corresponding to the document 'hisysevent.yaml'
60      * @param eventInfo event info
61      */
62     static void SendHiSysEvent(const std::string &eventName, const EventInfo &eventInfo);
63 
64 private:
65 #ifdef HAS_HISYSEVENT_PART
66     // fault event
67     static void InnerSendOrderedEventProcTimeoutEvent(const EventInfo &eventInfo);
68     static void InnerSendStaticEventProcErrorEvent(const EventInfo &eventInfo);
69     static void InnerSendSubscriberExceedMaximumEvent(const EventInfo &eventInfo);
70     static void InnerSendPublishErrorEvent(const EventInfo &eventInfo);
71 
72     // statistic event
73     static void InnerSendSubscribeEvent(const EventInfo &eventInfo);
74     static void InnerSendUnSubscribeEvent(const EventInfo &eventInfo);
75     static void InnerSendPublishEvent(const EventInfo &eventInfo);
76     static void InnerSendStaticSubStartEvent(const EventInfo &eventInfo);
77     static void InnerSendStaticSubRunEvent(const EventInfo &eventInfo);
78 
79     template<typename... Types>
80     static void InnerEventWrite(const std::string &eventName,
81         HiviewDFX::HiSysEvent::EventType type, Types... keyValues);
82 
83     static std::unordered_map<std::string, void (*)(const EventInfo &eventInfo)> cesSysEventFuncMap_;
84 #endif
85 };
86 } // namespace EventFwk
87 } // namespace OHOS
88 #endif  // FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_EVENT_REPORT_H