1/* 2 * Copyright (c) 2021 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 "compose_rule.h" 16 17#include <algorithm> 18#include <fstream> 19#include <memory> 20 21#include "file_util.h" 22#include "hiview_logger.h" 23#include "string_util.h" 24 25using namespace std; 26namespace OHOS { 27namespace HiviewDFX { 28DEFINE_LOG_TAG("ComposeRule"); 29 30void ComposeRule::ParseComposeRule(const string& config, const string& type, vector<string> featureIds) 31{ 32 std::ifstream fin(config, std::ifstream::binary); 33#ifdef JSONCPP_VERSION_STRING 34 Json::CharReaderBuilder builder; 35 Json::CharReaderBuilder::strictMode(&builder.settings_); 36 JSONCPP_STRING errs; 37#else 38 Json::Reader reader(Json::Features::strictMode()); 39#endif 40 41 Json::Value root; 42#ifdef JSONCPP_VERSION_STRING 43 bool ret = parseFromStream(builder, fin, &root, &errs); 44 if (!ret || !errs.empty() || !root.isMember(type)) { 45 HIVIEW_LOGE("Json parse fail, err is %{public}s or %{public}s don't exist in %{public}s.", 46 errs.c_str(), type.c_str(), config.c_str()); 47 return; 48 } 49#else 50 if (!reader.parse(fin, root) || !root.isMember(type)) { 51 HIVIEW_LOGE("Json parse fail in %{public}s.", config.c_str()); 52 return; 53 } 54#endif 55 56 ParseRule(root[type], type, featureIds); 57 HIVIEW_LOGI("ComposeRule ParseFile end."); 58 return; 59} 60 61std::list<std::pair<std::string, std::map<std::string, std::string>>> ComposeRule::GetComposeRule() const 62{ 63 return composeRules_; 64} 65 66void ComposeRule::ParseRule(const Json::Value& json, const string& type, vector<string>& featureIds) 67{ 68 sort(featureIds.begin(), featureIds.end(), ComparePrio); 69 for (const auto& featureId : featureIds) { 70 map<string, string> composeParams = GetMapFromJson(json, featureId); 71 composeRules_.emplace_back(pair<string, map<string, string>>(featureId, composeParams)); 72 } 73} 74 75bool ComposeRule::ComparePrio(const string& featureIdOne, const string& featureIdTwo) 76{ 77 return StringUtil::GetRightSubstr(featureIdOne, "_") < StringUtil::GetRightSubstr(featureIdTwo, "_"); 78} 79 80std::map<std::string, std::string> ComposeRule::GetMapFromJson(const Json::Value& json, const string& featureId) 81{ 82 if (!json.isMember(featureId)) { 83 HIVIEW_LOGE("ComposeRule don't have %{public}s featureId.", featureId.c_str()); 84 return {}; 85 } 86 std::map<std::string, std::string> result; 87 auto value = json[featureId]; 88 for (auto iter = value.begin(); iter != value.end(); iter++) { 89 result.emplace(pair<std::string, std::string>(iter.key().asString(), (*iter).asString())); 90 } 91 return result; 92} 93} // namespace HiviewDFX 94} // namespace OHOS 95