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
16 #include "pasteboard_app_event_dfx.h"
17
18 #include "app_event.h"
19 #include "app_event_processor_mgr.h"
20
21 namespace OHOS {
22 namespace MiscServices {
23 constexpr int NOT_APP_PROCESSORID = -200;
24 constexpr const char *HIAPP_SDK_NAME = "BasicServicesKit";
25 int64_t DfxAppEvent::processorId_ = -1;
26 std::mutex DfxAppEvent::mutex_;
DfxAppEvent()27 DfxAppEvent::DfxAppEvent()
28 {
29 std::lock_guard<std::mutex> lock(mutex_);
30 if (DfxAppEvent::processorId_ == -1) {
31 DfxAppEvent::processorId_ = DfxAppEvent::AddProcessor();
32 }
33 beginTime_ = static_cast<int64_t>(time(nullptr));
34 }
35
~DfxAppEvent()36 DfxAppEvent::~DfxAppEvent()
37 {
38 std::lock_guard<std::mutex> lock(mutex_);
39 if (DfxAppEvent::processorId_ == NOT_APP_PROCESSORID) {
40 return;
41 }
42 OHOS::HiviewDFX::HiAppEvent::Event event("api_diagnostic", "api_exec_end", OHOS::HiviewDFX::HiAppEvent::BEHAVIOR);
43 event.AddParam("trans_id", transId_);
44 event.AddParam("api_name", apiName_);
45 event.AddParam("sdk_name", std::string(HIAPP_SDK_NAME));
46 event.AddParam("begin_time", beginTime_);
47 event.AddParam("end_time", static_cast<int64_t>(time(nullptr)));
48 event.AddParam("result", result_);
49 event.AddParam("error_code", errCode_);
50 Write(event);
51 }
52
AddProcessor()53 int64_t DfxAppEvent::AddProcessor()
54 {
55 HiviewDFX::HiAppEvent::ReportConfig config;
56 config.name = "ha_app_event";
57 config.configName = "SDK_OCG";
58 return HiviewDFX::HiAppEvent::AppEventProcessorMgr::AddProcessor(config);
59 }
60
SetEvent(const std::string & apiName,int32_t errCode,int32_t result)61 void DfxAppEvent::SetEvent(const std::string &apiName, int32_t errCode, int32_t result)
62 {
63 result_ = result;
64 apiName_ = apiName;
65 errCode_ = errCode;
66 transId_ = std::string("transId_") + std::to_string(std::rand());
67 }
68 } // namespace MiscServices
69 } // namespace OHOS