1 /*
2 * Copyright (c) 2024 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 "dfx/radar_reporter.h"
17 #include "device_manager_adapter.h"
18 #include "hisysevent.h"
19
20 namespace OHOS {
21 namespace DistributedDataDfx {
22 using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter;
RadarReporter(const char * eventName,int32_t scene,const char * bundleName,const char * funcName)23 RadarReporter::RadarReporter(const char *eventName, int32_t scene, const char *bundleName, const char *funcName)
24 {
25 eventName_ = eventName != nullptr ? eventName : "";
26 funcName_ = funcName != nullptr ? funcName : "";
27 radarParam_.scene_ = scene;
28 radarParam_.bundleName_ = bundleName != nullptr ? bundleName : "";
29 radarParam_.res_ = RES_IDLE;
30 Report(radarParam_, funcName_, BEGIN, eventName_);
31 }
32
~RadarReporter()33 RadarReporter::~RadarReporter()
34 {
35 Report(radarParam_, funcName_, END, eventName_);
36 }
37
operator =(int32_t errCode)38 RadarReporter &RadarReporter::operator=(int32_t errCode)
39 {
40 radarParam_.errCode_ = errCode;
41 return *this;
42 }
43
Report(const RadarParam & param,const char * funcName,int32_t state,const char * eventName)44 void RadarReporter::Report(const RadarParam ¶m, const char *funcName, int32_t state, const char *eventName)
45 {
46 int32_t res = state == BEGIN ? param.res_ : (param.errCode_ != NO_ERROR ? RES_FAILED : RES_SUCCESS);
47 if (state != 0) {
48 HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::DISTRIBUTED_DATAMGR, eventName,
49 OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR, ORG_PKG_LABEL, ORG_PKG, FUNC_LABEL, funcName,
50 BIZ_SCENE_LABEL, param.scene_, BIZ_STAGE_LABEL, param.stage_, BIZ_STATE_LABEL, state, STAGE_RES_LABEL, res,
51 ERROR_CODE_LABEL, param.errCode_, HOST_PKG, param.bundleName_, LOCAL_UUID_LABEL,
52 AnonymousUuid(DmAdapter::GetInstance().GetLocalDevice().uuid), CONCURRENT_ID,
53 std::to_string(param.syncId_), TRIGGER_MODE, param.triggerMode_, WATER_VERSION, param.changeCount);
54 } else {
55 HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::DISTRIBUTED_DATAMGR, eventName,
56 OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR, ORG_PKG_LABEL, ORG_PKG, FUNC_LABEL, funcName,
57 BIZ_SCENE_LABEL, param.scene_, BIZ_STAGE_LABEL, param.stage_, STAGE_RES_LABEL, res, ERROR_CODE_LABEL,
58 param.errCode_, HOST_PKG, param.bundleName_, LOCAL_UUID_LABEL,
59 AnonymousUuid(DmAdapter::GetInstance().GetLocalDevice().uuid), CONCURRENT_ID,
60 std::to_string(param.syncId_), TRIGGER_MODE, param.triggerMode_, WATER_VERSION, param.changeCount);
61 }
62 return;
63 }
64
AnonymousUuid(const std::string & uuid)65 std::string RadarReporter::AnonymousUuid(const std::string &uuid)
66 {
67 if (uuid.length() < BASE_SIZE) {
68 return DEFAULT_ANONYMOUS;
69 }
70 return (uuid.substr(0, HEAD_SIZE) + REPLACE_CHAIN + uuid.substr(uuid.length() - END_SIZE, END_SIZE));
71 }
72 } // namespace DistributedDataDfx
73 } // namespace OHOS