• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "base/log/event_report.h"
17 
18 #include <ctime>
19 #include <string>
20 #include <unistd.h>
21 
22 #include "hisysevent.h"
23 
24 #include "base/json/json_util.h"
25 #include "core/common/ace_application_info.h"
26 #include "core/common/ace_engine.h"
27 
28 namespace OHOS::Ace {
29 namespace {
30 
31 constexpr char EVENT_KEY_ERROR_TYPE[] = "ERROR_TYPE";
32 constexpr char EVENT_KEY_UID[] = "UID";
33 constexpr char EVENT_KEY_PID[] = "PID";
34 constexpr char EVENT_KEY_SESSIONID[] = "SESSION_ID";
35 constexpr char EVENT_KEY_PACKAGE_NAME[] = "PACKAGE_NAME";
36 constexpr char EVENT_KEY_PROCESS_NAME[] = "PROCESS_NAME";
37 constexpr char EVENT_KEY_MESSAGE[] = "MSG";
38 constexpr char EVENT_KEY_CMD[] = "CMD";
39 constexpr char EVENT_KEY_REASON[] = "REASON";
40 constexpr char EVENT_KEY_SUMMARY[] = "SUMMARY";
41 constexpr char EVENT_NAME_JS_ERROR[] = "JS_ERROR";
42 constexpr char STATISTIC_DURATION[] = "DURATION";
43 
44 constexpr int32_t MAX_PACKAGE_NAME_LENGTH = 128;
45 
46 constexpr char DUMP_LOG_COMMAND[] = "B";
47 
StrTrim(std::string & str)48 void StrTrim(std::string& str)
49 {
50     if (str.size() > MAX_PACKAGE_NAME_LENGTH) {
51         str = str.substr(0, MAX_PACKAGE_NAME_LENGTH);
52     }
53 }
54 
55 } // namespace
56 
SendEvent(const EventInfo & eventInfo)57 void EventReport::SendEvent(const EventInfo& eventInfo)
58 {
59     auto packageName = AceApplicationInfo::GetInstance().GetPackageName();
60     if (packageName.size() > MAX_PACKAGE_NAME_LENGTH) {
61         StrTrim(packageName);
62     }
63     OHOS::HiviewDFX::HiSysEvent::Write(OHOS::HiviewDFX::HiSysEvent::Domain::ACE, eventInfo.eventType,
64         OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
65         EVENT_KEY_ERROR_TYPE, eventInfo.errorType,
66         EVENT_KEY_PACKAGE_NAME, packageName);
67 }
68 
SendJsCardRenderTimeEvent(const std::string & sessionID,const std::string & timeType,uint64_t timeDelay)69 void EventReport::SendJsCardRenderTimeEvent(
70     const std::string& sessionID,
71     const std::string& timeType,
72     uint64_t timeDelay)
73 {
74     OHOS::HiviewDFX::HiSysEvent::Write(OHOS::HiviewDFX::HiSysEvent::Domain::ACE, timeType,
75         OHOS::HiviewDFX::HiSysEvent::EventType::STATISTIC,
76         EVENT_KEY_SESSIONID, sessionID,
77         STATISTIC_DURATION, timeDelay);
78 }
79 
SendAppStartException(AppStartExcepType type)80 void EventReport::SendAppStartException(AppStartExcepType type)
81 {
82     EventInfo eventInfo = {
83         .eventType = EXCEPTION_FRAMEWORK_APP_START,
84         .errorType = static_cast<int32_t>(type),
85     };
86 
87     SendEventInner(eventInfo);
88 }
89 
SendPageRouterException(PageRouterExcepType type,const std::string & pageUrl)90 void EventReport::SendPageRouterException(PageRouterExcepType type, const std::string& pageUrl)
91 {
92     EventInfo eventInfo = {
93         .eventType = EXCEPTION_FRAMEWORK_PAGE_ROUTER,
94         .errorType = static_cast<int32_t>(type),
95         .pageUrl = pageUrl,
96     };
97 
98     SendEventInner(eventInfo);
99 }
100 
SendComponentException(ComponentExcepType type)101 void EventReport::SendComponentException(ComponentExcepType type)
102 {
103     EventInfo eventInfo = {
104         .eventType = EXCEPTION_COMPONENT,
105         .errorType = static_cast<int32_t>(type),
106     };
107 
108     SendEventInner(eventInfo);
109 }
110 
SendAPIChannelException(APIChannelExcepType type)111 void EventReport::SendAPIChannelException(APIChannelExcepType type)
112 {
113     EventInfo eventInfo = {
114         .eventType = EXCEPTION_API_CHANNEL,
115         .errorType = static_cast<int32_t>(type),
116     };
117 
118     SendEventInner(eventInfo);
119 }
120 
SendRenderException(RenderExcepType type)121 void EventReport::SendRenderException(RenderExcepType type)
122 {
123     EventInfo eventInfo = {
124         .eventType = EXCEPTION_RENDER,
125         .errorType = static_cast<int32_t>(type),
126     };
127 
128     SendEventInner(eventInfo);
129 }
130 
SendJsException(JsExcepType type)131 void EventReport::SendJsException(JsExcepType type)
132 {
133     EventInfo eventInfo = {
134         .eventType = EXCEPTION_JS,
135         .errorType = static_cast<int32_t>(type),
136     };
137 
138     SendEventInner(eventInfo);
139 }
140 
SendAnimationException(AnimationExcepType type)141 void EventReport::SendAnimationException(AnimationExcepType type)
142 {
143     EventInfo eventInfo = {
144         .eventType = EXCEPTION_ANIMATION,
145         .errorType = static_cast<int32_t>(type),
146     };
147 
148     SendEventInner(eventInfo);
149 }
150 
SendEventException(EventExcepType type)151 void EventReport::SendEventException(EventExcepType type)
152 {
153     EventInfo eventInfo = {
154         .eventType = EXCEPTION_EVENT,
155         .errorType = static_cast<int32_t>(type),
156     };
157 
158     SendEventInner(eventInfo);
159 }
160 
SendInternalException(InternalExcepType type)161 void EventReport::SendInternalException(InternalExcepType type)
162 {
163     EventInfo eventInfo = {
164         .eventType = EXCEPTION_INTERNATIONALIZATION,
165         .errorType = static_cast<int32_t>(type),
166     };
167 
168     SendEventInner(eventInfo);
169 }
170 
SendAccessibilityException(AccessibilityExcepType type)171 void EventReport::SendAccessibilityException(AccessibilityExcepType type)
172 {
173     EventInfo eventInfo = {
174         .eventType = EXCEPTION_ACCESSIBILITY,
175         .errorType = static_cast<int32_t>(type),
176     };
177 
178     SendEventInner(eventInfo);
179 }
180 
SendFormException(FormExcepType type)181 void EventReport::SendFormException(FormExcepType type)
182 {
183     EventInfo eventInfo = {
184         .eventType = EXCEPTION_FORM,
185         .errorType = static_cast<int32_t>(type),
186     };
187 
188     SendEventInner(eventInfo);
189 }
190 
JsEventReport(int32_t eventType,const std::string & jsonStr)191 void EventReport::JsEventReport(int32_t eventType, const std::string& jsonStr)
192 {
193     if (!JsonUtil::ParseJsonString(jsonStr)) {
194         LOGE("jsonStr is not a JsonArray.");
195         return;
196     }
197 }
198 
JsErrReport(const std::string & packageName,const std::string & reason,const std::string & summary)199 void EventReport::JsErrReport(
200     const std::string& packageName, const std::string& reason, const std::string& summary)
201 {
202     OHOS::HiviewDFX::HiSysEvent::Write(OHOS::HiviewDFX::HiSysEvent::Domain::ACE, EVENT_NAME_JS_ERROR,
203         OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
204         EVENT_KEY_PACKAGE_NAME, packageName,
205         EVENT_KEY_REASON, reason,
206         EVENT_KEY_SUMMARY, summary);
207 }
208 
ANRRawReport(RawEventType type,int32_t uid,const std::string & packageName,const std::string & processName,const std::string & msg)209 void EventReport::ANRRawReport(RawEventType type, int32_t uid, const std::string& packageName,
210     const std::string& processName, const std::string& msg)
211 {
212     int32_t pid = getpid();
213     std::string cmd = " ";
214     std::string eventName = "";
215     if (type == RawEventType::WARNING) {
216         eventName = "UI_BLOCK_3S";
217         cmd = "p=" + std::to_string(pid);
218     } else if (type == RawEventType::FREEZE) {
219         eventName = "UI_BLOCK_6S";
220         cmd = DUMP_LOG_COMMAND;
221     } else {
222         eventName = "UI_BLOCK_RECOVERED";
223     }
224     std::string uidStr = std::to_string(uid);
225     OHOS::HiviewDFX::HiSysEvent::Write(OHOS::HiviewDFX::HiSysEvent::Domain::ACE, eventName,
226         OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
227         EVENT_KEY_UID, uidStr,
228         EVENT_KEY_PACKAGE_NAME, packageName,
229         EVENT_KEY_PROCESS_NAME, processName,
230         EVENT_KEY_MESSAGE, msg,
231         EVENT_KEY_CMD, cmd);
232 }
233 
ANRShowDialog(int32_t uid,const std::string & packageName,const std::string & processName,const std::string & msg)234 void EventReport::ANRShowDialog(int32_t uid, const std::string& packageName,
235     const std::string& processName, const std::string& msg)
236 {
237     int32_t pid = getpid();
238     std::string eventName = "UI_BLOCK_DIALOG";
239     std::string uidStr = std::to_string(uid);
240     std::string pidStr = std::to_string(pid);
241     OHOS::HiviewDFX::HiSysEvent::Write(OHOS::HiviewDFX::HiSysEvent::Domain::ACE, eventName,
242         OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
243         EVENT_KEY_UID, uidStr,
244         EVENT_KEY_PID, pidStr,
245         EVENT_KEY_PACKAGE_NAME, packageName,
246         EVENT_KEY_PROCESS_NAME, processName,
247         EVENT_KEY_MESSAGE, msg);
248 }
249 
SendEventInner(const EventInfo & eventInfo)250 void EventReport::SendEventInner(const EventInfo& eventInfo)
251 {
252     auto packageName = AceApplicationInfo::GetInstance().GetPackageName();
253     StrTrim(packageName);
254     OHOS::HiviewDFX::HiSysEvent::Write(OHOS::HiviewDFX::HiSysEvent::Domain::ACE, eventInfo.eventType,
255             OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
256             EVENT_KEY_ERROR_TYPE, eventInfo.errorType,
257             EVENT_KEY_PACKAGE_NAME, packageName);
258 }
259 
260 } // namespace OHOS::Ace
261