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 "export_event_list_parser.h"
17 
18 #include <set>
19 
20 #include "cjson_util.h"
21 #include "hiview_logger.h"
22 
23 namespace OHOS {
24 namespace HiviewDFX {
25 DEFINE_LOG_TAG("HiView-EventExportList");
26 namespace {
27 constexpr char CONFIGURATION_INFO[] = "configurationInfo";
28 constexpr char CONFIGURATION_VERSION[] = "configurationVersion";
29 constexpr char EVENTS[] = "events";
30 constexpr char DOMAIN[] = "domain";
31 constexpr char NAME[] = "name";
32 constexpr char NAMES[] = "names";
33 constexpr int64_t INVALID_INT_VAL = -1;
34 
AddDomainNames(const std::string & domain,const std::vector<std::string> & names,ExportEventList & eventList)35 void AddDomainNames(const std::string& domain, const std::vector<std::string>& names,
36     ExportEventList& eventList)
37 {
38     if (names.empty()) {
39         return;
40     }
41     auto iter = eventList.find(domain);
42     if (iter == eventList.end()) {
43         eventList.emplace(domain, names);
44         return;
45     }
46     iter->second.insert(iter->second.end(), names.begin(), names.end());
47     std::set<std::string> uniqueNames(iter->second.begin(), iter->second.end());
48     iter->second.assign(uniqueNames.begin(), uniqueNames.end());
49 }
50 
ParseExportEventList(cJSON * eventItem,ExportEventList & eventList)51 void ParseExportEventList(cJSON* eventItem, ExportEventList& eventList)
52 {
53     auto domain = CJsonUtil::GetStringValue(eventItem, DOMAIN);
54     if (domain.empty()) {
55         return;
56     }
57     std::vector<std::string> allEventNames;
58     CJsonUtil::GetStringArray(eventItem, NAMES, allEventNames);
59     if (allEventNames.size() > 0) {
60         AddDomainNames(domain, allEventNames, eventList);
61         return;   // names configured in events has a higher priority than name to parse
62     }
63     auto eventName = CJsonUtil::GetStringValue(eventItem, NAME);
64     if (eventName.empty()) {
65         return;
66     }
67     allEventNames.emplace_back(eventName);
68     AddDomainNames(domain, allEventNames, eventList);
69 }
70 }
71 
ExportEventListParser(const std::string & configFile)72 ExportEventListParser::ExportEventListParser(const std::string& configFile)
73 {
74     HIVIEW_LOGD("event list config file path is %{public}s.", configFile.c_str());
75     jsonRoot_ = CJsonUtil::ParseJsonRoot(configFile);
76     ParseConfiguration();
77 }
78 
~ExportEventListParser()79 ExportEventListParser::~ExportEventListParser()
80 {
81     if (jsonRoot_ == nullptr) {
82         return;
83     }
84     cJSON_Delete(jsonRoot_);
85 }
86 
87 
GetExportEventList(ExportEventList & eventList) const88 void ExportEventListParser::GetExportEventList(ExportEventList& eventList) const
89 {
90     if (jsonRoot_ == nullptr) {
91         return;
92     }
93     cJSON* eventArray = cJSON_GetObjectItem(jsonRoot_, EVENTS);
94     if (eventArray == nullptr || !cJSON_IsArray(eventArray)) {
95         HIVIEW_LOGW("events configured is invalid.");
96         return;
97     }
98     int size = cJSON_GetArraySize(eventArray);
99     if (size <= 0) {
100         HIVIEW_LOGW("events configured is empty.");
101         return;
102     }
103     for (int index = 0; index < size; ++index) {
104         cJSON* eventItem = cJSON_GetArrayItem(eventArray, index);
105         if (eventItem == nullptr || !cJSON_IsObject(eventItem)) {
106             HIVIEW_LOGW("event configured is invalid.");
107             continue;
108         }
109         ParseExportEventList(eventItem, eventList);
110     }
111 }
112 
GetConfigurationVersion() const113 int64_t ExportEventListParser::GetConfigurationVersion() const
114 {
115     return configurationVersion_;
116 }
117 
ParseConfiguration()118 void ExportEventListParser::ParseConfiguration()
119 {
120     cJSON* jsonObj = cJSON_GetObjectItem(jsonRoot_, CONFIGURATION_INFO);
121     if (jsonObj == nullptr || !cJSON_IsObject(jsonObj)) {
122         HIVIEW_LOGW("configurationInfo configured is invalid.");
123         return;
124     }
125     configurationVersion_ = CJsonUtil::GetIntValue(jsonObj, CONFIGURATION_VERSION, INVALID_INT_VAL);
126     if (configurationVersion_ == INVALID_INT_VAL) {
127         HIVIEW_LOGW("failed to parse integer value of configurationVersion.");
128         return;
129     }
130 }
131 } // HiviewDFX
132 } // OHOS