• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 #include "app_usage_event.h"
16 
17 #include "hiview_logger.h"
18 #include "usage_event_common.h"
19 
20 namespace OHOS {
21 namespace HiviewDFX {
22 DEFINE_LOG_TAG("AppUsageEvent");
23 using namespace AppUsageEventSpace;
24 
AppUsageEvent(const std::string & name,HiSysEvent::EventType type)25 AppUsageEvent::AppUsageEvent(const std::string &name, HiSysEvent::EventType type)
26     : LoggerEvent(name, type)
27 {
28     this->paramMap_ = {
29         {std::string(KEY_OF_PACKAGE), std::string(DEFAULT_STRING)},
30         {std::string(KEY_OF_VERSION), std::string(DEFAULT_STRING)},
31         {std::string(KEY_OF_USAGE), DEFAULT_UINT64},
32         {std::string(KEY_OF_DATE), std::string(DEFAULT_STRING)},
33         {std::string(KEY_OF_START_NUM), DEFAULT_UINT32}
34     };
35 }
36 
Report()37 void AppUsageEvent::Report()
38 {
39     ReportDFX();
40     ReportDFXUE();
41 }
42 
ReportDFX()43 void AppUsageEvent::ReportDFX()
44 {
45     auto ret = HiSysEventWrite(HiSysEvent::Domain::HIVIEWDFX, this->eventName_, this->eventType_,
46         KEY_OF_PACKAGE, this->paramMap_[KEY_OF_PACKAGE].GetString(),
47         KEY_OF_VERSION, this->paramMap_[KEY_OF_VERSION].GetString(),
48         KEY_OF_USAGE, this->paramMap_[KEY_OF_USAGE].GetUint64(),
49         KEY_OF_DATE, this->paramMap_[KEY_OF_DATE].GetString(),
50         KEY_OF_START_NUM, this->paramMap_[KEY_OF_START_NUM].GetUint32());
51     if (ret != 0) {
52         HIVIEW_LOGW("failed to report app usage event, ret=%{public}d", ret);
53     }
54 }
55 
ReportDFXUE()56 void AppUsageEvent::ReportDFXUE()
57 {
58     auto ret = HiSysEventWrite(DomainSpace::HIVIEWDFX_UE_DOMAIN, this->eventName_, this->eventType_,
59         KEY_OF_PACKAGE, this->paramMap_[KEY_OF_PACKAGE].GetString(),
60         KEY_OF_VERSION, this->paramMap_[KEY_OF_VERSION].GetString(),
61         KEY_OF_USAGE, this->paramMap_[KEY_OF_USAGE].GetUint64(),
62         KEY_OF_DATE, this->paramMap_[KEY_OF_DATE].GetString(),
63         KEY_OF_START_NUM, this->paramMap_[KEY_OF_START_NUM].GetUint32());
64     if (ret != 0) {
65         HIVIEW_LOGW("failed to report app usage event, ret=%{public}d", ret);
66     }
67 }
68 } // namespace HiviewDFX
69 } // namespace OHOS
70