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 #include "event_log_wrapper.h"
17 #include "event_report.h"
18
19 namespace OHOS {
20 namespace EventFwk {
21 namespace {
22 // event params
23 const std::string EVENT_PARAM_USER_ID = "USER_ID";
24 const std::string EVENT_PARAM_PID = "PID";
25 const std::string EVENT_PARAM_UID = "UID";
26 const std::string EVENT_PARAM_SUBSCRIBER_NUM = "SUBSCRIBER_NUM";
27 const std::string EVENT_PARAM_BUNDLE_NAME_OF_PUBLISHER = "BUNDLE_NAME_OF_PUBLISHER";
28 const std::string EVENT_PARAM_BUNDLE_NAME_OF_SUBSCRIBER = "BUNDLE_NAME_OF_SUBSCRIBER";
29 const std::string EVENT_PARAM_EVENT_NAME = "EVENT_NAME";
30 } // namespace
31
SendHiSysEvent(const std::string & eventName,const EventInfo & eventInfo)32 void EventReport::SendHiSysEvent(const std::string &eventName, const EventInfo &eventInfo)
33 {
34 #ifndef HAS_HISYSEVENT_PART
35 EVENT_LOGD("Hisysevent is disabled");
36 #else
37 auto iter = cesSysEventFuncMap_.find(eventName);
38 if (iter == cesSysEventFuncMap_.end()) {
39 return;
40 }
41
42 iter->second(eventInfo);
43 #endif
44 }
45
46 #ifdef HAS_HISYSEVENT_PART
47 std::unordered_map<std::string, void (*)(const EventInfo& eventInfo)> EventReport::cesSysEventFuncMap_ = {
__anon79e3b76a0202() 48 {ORDERED_EVENT_PROC_TIMEOUT, [](const EventInfo& eventInfo) {
49 InnerSendOrderedEventProcTimeoutEvent(eventInfo);
50 }},
__anon79e3b76a0302() 51 {STATIC_EVENT_PROC_ERROR, [](const EventInfo& eventInfo) {
52 InnerSendStaticEventProcErrorEvent(eventInfo);
53 }},
__anon79e3b76a0402() 54 {SUBSCRIBER_EXCEED_MAXIMUM, [](const EventInfo& eventInfo) {
55 InnerSendSubscriberExceedMaximumEvent(eventInfo);
56 }},
__anon79e3b76a0502() 57 {PUBLISH_ERROR, [](const EventInfo& eventInfo) {
58 InnerSendPublishErrorEvent(eventInfo);
59 }},
__anon79e3b76a0602() 60 {SUBSCRIBE, [](const EventInfo& eventInfo) {
61 InnerSendSubscribeEvent(eventInfo);
62 }},
__anon79e3b76a0702() 63 {UNSUBSCRIBE, [](const EventInfo& eventInfo) {
64 InnerSendUnSubscribeEvent(eventInfo);
65 }},
__anon79e3b76a0802() 66 {PUBLISH, [](const EventInfo& eventInfo) {
67 InnerSendPublishEvent(eventInfo);
68 }},
69 };
70
InnerSendOrderedEventProcTimeoutEvent(const EventInfo & eventInfo)71 void EventReport::InnerSendOrderedEventProcTimeoutEvent(const EventInfo &eventInfo)
72 {
73 InnerEventWrite(
74 ORDERED_EVENT_PROC_TIMEOUT,
75 HiviewDFX::HiSysEvent::EventType::FAULT,
76 EVENT_PARAM_USER_ID, eventInfo.userId,
77 EVENT_PARAM_BUNDLE_NAME_OF_SUBSCRIBER, eventInfo.subscriberName,
78 EVENT_PARAM_PID, eventInfo.pid,
79 EVENT_PARAM_UID, eventInfo.uid,
80 EVENT_PARAM_EVENT_NAME, eventInfo.eventName);
81 }
82
InnerSendStaticEventProcErrorEvent(const EventInfo & eventInfo)83 void EventReport::InnerSendStaticEventProcErrorEvent(const EventInfo &eventInfo)
84 {
85 InnerEventWrite(
86 STATIC_EVENT_PROC_ERROR,
87 HiviewDFX::HiSysEvent::EventType::FAULT,
88 EVENT_PARAM_USER_ID, eventInfo.userId,
89 EVENT_PARAM_BUNDLE_NAME_OF_PUBLISHER, eventInfo.publisherName,
90 EVENT_PARAM_BUNDLE_NAME_OF_SUBSCRIBER, eventInfo.subscriberName,
91 EVENT_PARAM_EVENT_NAME, eventInfo.eventName);
92 }
93
InnerSendSubscriberExceedMaximumEvent(const EventInfo & eventInfo)94 void EventReport::InnerSendSubscriberExceedMaximumEvent(const EventInfo &eventInfo)
95 {
96 InnerEventWrite(
97 SUBSCRIBER_EXCEED_MAXIMUM,
98 HiviewDFX::HiSysEvent::EventType::FAULT,
99 EVENT_PARAM_USER_ID, eventInfo.userId,
100 EVENT_PARAM_EVENT_NAME, eventInfo.eventName,
101 EVENT_PARAM_SUBSCRIBER_NUM, eventInfo.subscriberNum);
102 }
103
InnerSendPublishErrorEvent(const EventInfo & eventInfo)104 void EventReport::InnerSendPublishErrorEvent(const EventInfo &eventInfo)
105 {
106 InnerEventWrite(
107 PUBLISH_ERROR,
108 HiviewDFX::HiSysEvent::EventType::FAULT,
109 EVENT_PARAM_USER_ID, eventInfo.userId,
110 EVENT_PARAM_BUNDLE_NAME_OF_PUBLISHER, eventInfo.publisherName,
111 EVENT_PARAM_PID, eventInfo.pid,
112 EVENT_PARAM_UID, eventInfo.uid,
113 EVENT_PARAM_EVENT_NAME, eventInfo.eventName);
114 }
115
InnerSendSubscribeEvent(const EventInfo & eventInfo)116 void EventReport::InnerSendSubscribeEvent(const EventInfo &eventInfo)
117 {
118 InnerEventWrite(
119 SUBSCRIBE,
120 HiviewDFX::HiSysEvent::EventType::STATISTIC,
121 EVENT_PARAM_USER_ID, eventInfo.userId,
122 EVENT_PARAM_BUNDLE_NAME_OF_SUBSCRIBER, eventInfo.subscriberName,
123 EVENT_PARAM_PID, eventInfo.pid,
124 EVENT_PARAM_UID, eventInfo.uid,
125 EVENT_PARAM_EVENT_NAME, eventInfo.eventName);
126 }
127
InnerSendUnSubscribeEvent(const EventInfo & eventInfo)128 void EventReport::InnerSendUnSubscribeEvent(const EventInfo &eventInfo)
129 {
130 InnerEventWrite(
131 UNSUBSCRIBE,
132 HiviewDFX::HiSysEvent::EventType::STATISTIC,
133 EVENT_PARAM_USER_ID, eventInfo.userId,
134 EVENT_PARAM_BUNDLE_NAME_OF_SUBSCRIBER, eventInfo.subscriberName,
135 EVENT_PARAM_PID, eventInfo.pid,
136 EVENT_PARAM_UID, eventInfo.uid,
137 EVENT_PARAM_EVENT_NAME, eventInfo.eventName);
138 }
139
InnerSendPublishEvent(const EventInfo & eventInfo)140 void EventReport::InnerSendPublishEvent(const EventInfo &eventInfo)
141 {
142 InnerEventWrite(
143 PUBLISH,
144 HiviewDFX::HiSysEvent::EventType::STATISTIC,
145 EVENT_PARAM_USER_ID, eventInfo.userId,
146 EVENT_PARAM_BUNDLE_NAME_OF_PUBLISHER, eventInfo.publisherName,
147 EVENT_PARAM_PID, eventInfo.pid,
148 EVENT_PARAM_UID, eventInfo.uid,
149 EVENT_PARAM_EVENT_NAME, eventInfo.eventName);
150 }
151
152 template<typename... Types>
InnerEventWrite(const std::string & eventName,HiviewDFX::HiSysEvent::EventType type,Types...keyValues)153 void EventReport::InnerEventWrite(const std::string &eventName,
154 HiviewDFX::HiSysEvent::EventType type, Types... keyValues)
155 {
156 HiSysEventWrite(
157 HiviewDFX::HiSysEvent::Domain::COMMONEVENT,
158 eventName,
159 static_cast<HiviewDFX::HiSysEvent::EventType>(type),
160 keyValues...);
161 }
162 #endif
163 } // namespace EventFwk
164 } // namespace OHOS