• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "monitor_config.h"
17 
18 #include <cstdint>
19 #include <fstream>
20 #include <iostream>
21 #include <regex>
22 #include <sstream>
23 
24 #include "logger.h"
25 #include "string_util.h"
26 
27 namespace OHOS {
28 namespace HiviewDFX {
29 DEFINE_LOG_TAG("HiView-MonitorCfg");
Parse()30 bool MonitorConfig::Parse()
31 {
32     std::ifstream in;
33     in.open(configPath_);
34     if (!in.is_open()) {
35         HIVIEW_LOGW("fail to open monitor config file.");
36         return false;
37     }
38 
39     std::string buf = "";
40     const int configItemField = 1;
41     const int configValueField = 2;
42     while (getline(in, buf)) {
43         std::string strTmp = StringUtil::TrimStr(buf);
44         if (strTmp.empty()) {
45             continue;
46         }
47         std::smatch result;
48         if (!regex_search(strTmp, result, std::regex("(collectPeriod|reportPeriod|totalSizeBenchMark|"
49             "realTimeBenchMark|processTimeBenchMark)\\s*=\\s*(\\d+)"))) {
50             HIVIEW_LOGW("match field failed %{public}s", strTmp.c_str());
51             continue;
52         }
53 
54         std::string configItem = StringUtil::TrimStr(result[configItemField]);
55         uint32_t configValue = static_cast<uint32_t>(atol(std::string(result[configValueField]).c_str()));
56         configs_[configItem] = configValue;
57         HIVIEW_LOGD("config=%{public}s value=%{public}u", configItem.c_str(), configValue);
58     }
59     in.close();
60     return true;
61 }
62 
ReadParam(const std::string & name,uint32_t & value)63 void MonitorConfig::ReadParam(const std::string &name, uint32_t &value)
64 {
65     auto it = configs_.find(name);
66     if (it == configs_.end()) {
67         return;
68     }
69     if (it->second == 0) {
70         return;
71     }
72     value = it->second;
73 }
74 } // namespace HiviewDFX
75 } // namespace OHOS