1 /*
2 * Copyright (c) 2024-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 "command_reporter.h"
17
18 #if defined(is_ohos) && is_ohos
19 #include "hiperf_hilog.h"
20 #include "hisysevent.h"
21 #include "utilities.h"
22 #endif
23
24 namespace OHOS::Developtools::HiPerf {
25
26 #define FOR_ERROR_NAME(x) #x
27
CommandReporter(const std::string & fullArgument)28 CommandReporter::CommandReporter(const std::string& fullArgument) : subCommand_(fullArgument)
29 {
30 #if defined(is_ohos) && is_ohos
31 caller_ = GetProcessName(getppid());
32 #endif
33 }
34
~CommandReporter()35 CommandReporter::~CommandReporter()
36 {
37 ReportCommand();
38 }
39
ReportCommand()40 void CommandReporter::ReportCommand()
41 {
42 #if defined(is_ohos) && is_ohos
43 #ifndef FUZZER_TEST
44 if (isReported_) {
45 HIPERF_HILOGD(MODULE_DEFAULT, "command has been reported");
46 return;
47 }
48
49 int32_t errorCode = static_cast<int32_t>(errorCode_);
50 static const char* const ERROR_MESSAGE[] = {
51 MAKE_ERROR_ITEM(FOR_ERROR_NAME)
52 };
53 int32_t ret = HiSysEventWrite(
54 OHOS::HiviewDFX::HiSysEvent::Domain::PROFILER, "HIPERF_USAGE",
55 OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
56 "MAIN_CMD", mainCommand_,
57 "SUB_CMD", subCommand_,
58 "CALLER", caller_,
59 "TARGET_PROCESS", targetProcess_,
60 "ERROR_CODE", errorCode,
61 "ERROR_MESSAGE", ERROR_MESSAGE[errorCode]);
62 if (ret != 0) {
63 HIPERF_HILOGE(MODULE_DEFAULT, "hisysevent report failed, err:%{public}d", ret);
64 }
65 #endif
66 #endif
67 isReported_ = true;
68 }
69
70 } // namespace OHOS::Developtools::HiPerf
71