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