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_config_parser.h"
17
18 #include <fstream>
19
20 #include "cjson_util.h"
21 #include "hiview_logger.h"
22
23 namespace OHOS {
24 namespace HiviewDFX {
25 DEFINE_LOG_TAG("HiView-EventConfigParser");
26 namespace {
27 constexpr char EXPORT_SWITCH_PARAM_KEY[] = "exportSwitchParam";
28 constexpr char SYS_UPGRADE_PARAM_KEY[] = "sysUpgradeParam";
29 constexpr char SETTING_PARAM_NAME_KEY[] = "name";
30 constexpr char SETTING_PARAM_ENABLED_KEY[] = "enabledValue";
31 constexpr char EXPORT_DIR[] = "exportDir";
32 constexpr char EXPORT_DIR_MAX_CAPACITY[] = "exportDirMaxCapacity";
33 constexpr char EXPORT_SINGLE_FILE_MAX_SIZE[] = "exportSingleFileMaxSize";
34 constexpr char TASK_EXECUTING_CYCLE[] = "taskExecutingCycle";
35 constexpr char EXPORT_EVENT_LIST_CONFIG_PATHS[] = "exportEventListConfigPaths";
36 constexpr char FILE_STORED_MAX_DAY_CNT[] = "fileStoredMaxDayCnt";
37 constexpr int32_t INVALID_INT_VAL = -1;
38 constexpr double INVALID_DOUBLE_VAL = -1.0;
39 }
40
ExportConfigParser(const std::string & configFile)41 ExportConfigParser::ExportConfigParser(const std::string& configFile)
42 {
43 jsonRoot_ = CJsonUtil::ParseJsonRoot(configFile);
44 }
45
~ExportConfigParser()46 ExportConfigParser::~ExportConfigParser()
47 {
48 if (jsonRoot_ == nullptr) {
49 return;
50 }
51 cJSON_Delete(jsonRoot_);
52 }
53
Parse()54 std::shared_ptr<ExportConfig> ExportConfigParser::Parse()
55 {
56 auto exportConfig = std::make_shared<ExportConfig>();
57 if (jsonRoot_ == nullptr || !cJSON_IsObject(jsonRoot_)) {
58 HIVIEW_LOGE("the file format of export config file is not json.");
59 return nullptr;
60 }
61 // read event export config files
62 CJsonUtil::GetStringArray(jsonRoot_, EXPORT_EVENT_LIST_CONFIG_PATHS, exportConfig->eventsConfigFiles);
63 // parse export switch setting parameter
64 if (!ParseSettingDbParam(exportConfig->exportSwitchParam, EXPORT_SWITCH_PARAM_KEY)) {
65 HIVIEW_LOGE("failed to parse export switch parameter.");
66 return nullptr;
67 }
68 // parse system upgrade setting parameter
69 if (!ParseSettingDbParam(exportConfig->sysUpgradeParam, SYS_UPGRADE_PARAM_KEY)) {
70 HIVIEW_LOGI("failed to parse system upgrade parameter.");
71 }
72 // parse residual content of the config file
73 if (!ParseResidualContent(exportConfig)) {
74 HIVIEW_LOGE("failed to parse residual content.");
75 return nullptr;
76 }
77 return exportConfig;
78 }
79
ParseSettingDbParam(SettingDbParam & settingDbParam,const std::string & paramKey)80 bool ExportConfigParser::ParseSettingDbParam(SettingDbParam& settingDbParam, const std::string& paramKey)
81 {
82 cJSON* settingDbParamJson = cJSON_GetObjectItem(jsonRoot_, paramKey.c_str());
83 if (settingDbParamJson == nullptr || !cJSON_IsObject(settingDbParamJson)) {
84 HIVIEW_LOGW("settingDbParam configured is invalid.");
85 return false;
86 }
87 settingDbParam.name = CJsonUtil::GetStringValue(settingDbParamJson, SETTING_PARAM_NAME_KEY);
88 if (settingDbParam.name.empty()) {
89 HIVIEW_LOGW("name of setting db parameter configured is invalid.");
90 return false;
91 }
92 settingDbParam.enabledVal = CJsonUtil::GetStringValue(settingDbParamJson, SETTING_PARAM_ENABLED_KEY);
93 if (settingDbParam.enabledVal.empty()) {
94 HIVIEW_LOGW("enabled value of setting db parameter configured is invalid.");
95 return false;
96 }
97 return true;
98 }
99
ParseResidualContent(std::shared_ptr<ExportConfig> config)100 bool ExportConfigParser::ParseResidualContent(std::shared_ptr<ExportConfig> config)
101 {
102 // read export diectory
103 config->exportDir = CJsonUtil::GetStringValue(jsonRoot_, EXPORT_DIR);
104 if (config->exportDir.empty()) {
105 HIVIEW_LOGW("exportDirectory configured is invalid.");
106 return false;
107 }
108 // read maximum capacity of the export diectory
109 config->maxCapcity = CJsonUtil::GetIntValue(jsonRoot_, EXPORT_DIR_MAX_CAPACITY, INVALID_INT_VAL);
110 if (config->maxCapcity == INVALID_INT_VAL) {
111 HIVIEW_LOGW("exportDirMaxCapacity configured is invalid.");
112 return false;
113 }
114 // read maximum size of the export single event file
115 config->maxSize = CJsonUtil::GetIntValue(jsonRoot_, EXPORT_SINGLE_FILE_MAX_SIZE, INVALID_INT_VAL);
116 if (config->maxSize == INVALID_INT_VAL) {
117 HIVIEW_LOGW("exportSingleFileMaxSize configured is invalid.");
118 return false;
119 }
120 // read task executing cycle
121 config->taskCycle = CJsonUtil::GetIntValue(jsonRoot_, TASK_EXECUTING_CYCLE, INVALID_DOUBLE_VAL);
122 if (config->taskCycle == INVALID_INT_VAL) {
123 HIVIEW_LOGW("taskExecutingCycle configured is invalid.");
124 return false;
125 }
126 // read day count for event file to store
127 config->dayCnt = CJsonUtil::GetIntValue(jsonRoot_, FILE_STORED_MAX_DAY_CNT, INVALID_DOUBLE_VAL);
128 if (config->dayCnt == INVALID_INT_VAL) {
129 HIVIEW_LOGW("fileStoredMaxDayCnt configured is invalid.");
130 return false;
131 }
132 return true;
133 }
134 } // HiviewDFX
135 } // OHOS