• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #include "app_event_util.h"
16 #include "event_json_util.h"
17 #include "hilog/log.h"
18 #include "hisysevent_c.h"
19 #include "parameters.h"
20 
21 #ifdef LOG_MAIN
22 #undef LOG_MAIN
23 #define LOG_MAIN 0xD002D07
24 #endif
25 
26 #ifdef LOG_TAG
27 #undef LOG_TAG
28 #define LOG_TAG "EventUtil"
29 #endif
30 
31 namespace OHOS {
32 namespace HiviewDFX {
33 namespace AppEventUtil {
34 
IsBetaVersion()35 bool IsBetaVersion()
36 {
37     constexpr char keyVersionType[] = "const.logsystem.versiontype";
38     static bool isBetaVersion = OHOS::system::GetParameter(keyVersionType, "unknown").find("beta") !=
39                                 std::string::npos;
40     return isBetaVersion;
41 }
42 
ReportAppEventReceive(const std::vector<std::shared_ptr<AppEventPack>> & appEventInfos,const std::string & watcherName,const std::string & callback)43 void ReportAppEventReceive(const std::vector<std::shared_ptr<AppEventPack>>& appEventInfos,
44                            const std::string& watcherName, const std::string& callback)
45 {
46     if (!IsBetaVersion()) {
47         HILOG_DEBUG(LOG_CORE, "no need to report APP_EVENT_RECEIVE event");
48         return;
49     }
50 
51     HiSysEventParam receiveParams[] = {
52         { .name = "BUNDLENAME",       .t = HISYSEVENT_STRING,    .arraySize = 0, },
53         { .name = "BUNDLEVERSION",    .t = HISYSEVENT_STRING,    .arraySize = 0, },
54         { .name = "CALLBACK",         .t = HISYSEVENT_STRING,    .arraySize = 0, },
55         { .name = "EVENTTYPE",        .t = HISYSEVENT_UINT8,     .arraySize = 0, },
56         { .name = "CRASHTYPE",        .t = HISYSEVENT_STRING,    .arraySize = 0, },
57         { .name = "WATCHERNAME",      .t = HISYSEVENT_STRING,    .arraySize = 0, },
58         { .name = "EXTERNALLOG",      .t = HISYSEVENT_BOOL,      .arraySize = 0, }
59     };
60     for (const auto& appEvent : appEventInfos) {
61         std::string eventName = appEvent->GetName();
62         if (eventName != "APP_FREEZE" && eventName != "APP_CRASH") {
63             HILOG_DEBUG(LOG_CORE, "only report APP_EVENT_SEND event for APP_FREEZE and APP_CRASH");
64             continue;
65         }
66         Json::Value eventJson;
67         std::string paramString = appEvent->GetParamStr();
68         if (!EventJsonUtil::GetJsonObjectFromJsonString(eventJson, paramString) ||
69             !eventJson.isMember("external_log") || !eventJson["external_log"].isArray()) {
70             HILOG_WARN(LOG_CORE, "parse event detail info failed, please check the style of json");
71             return;
72         }
73         std::string bundleName = EventJsonUtil::ParseString(eventJson, "bundle_name");
74         std::string bundleVersion = EventJsonUtil::ParseString(eventJson, "bundle_version");
75         std::string crashType = EventJsonUtil::ParseString(eventJson, "crash_type");
76         uint64_t index = 0;
77         receiveParams[index++].v = { .s = const_cast<char *>(bundleName.c_str()) };
78         receiveParams[index++].v = { .s = const_cast<char *>(bundleVersion.c_str()) };
79         receiveParams[index++].v = { .s = const_cast<char *>(callback.c_str()) };
80         receiveParams[index++].v = { .ui8 = eventName == "APP_CRASH" ? 0 : 1 };
81         receiveParams[index++].v = { .s = const_cast<char *>(crashType.c_str()) };
82         receiveParams[index++].v = { .s = const_cast<char *>(watcherName.c_str()) };
83         receiveParams[index++].v = { .b = eventJson["external_log"].size() > 0 };
84         int ret = OH_HiSysEvent_Write("HIVIEWDFX", "APP_EVENT_RECEIVE", HISYSEVENT_STATISTIC, receiveParams,
85                                       sizeof(receiveParams) / sizeof(receiveParams[0]));
86         if (ret != 0) {
87             HILOG_WARN(LOG_CORE, "fail to report APP_EVENT_RECEIVE event, ret =%{public}d", ret);
88         }
89     }
90 }
91 } // namespace AppEventUtil
92 } // namespace HiviewDFX
93 } // namespace OHOS
94