• 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 "event_logger_config.h"
16 
17 #include <chrono>
18 #include <regex>
19 
20 #include "logger.h"
21 namespace OHOS {
22 namespace HiviewDFX {
23 namespace {
24     constexpr char EVENT_LOGGER_CONFIG_PATH[] = "/system/etc/hiview/event_logger_config";
25     constexpr int VERSION_FIELD = 1;
26     constexpr int ID_FIELD = 1;
27     constexpr int NAME_FIELD = 2;
28     constexpr int ACTION_FIELD = 3;
29     constexpr int INTERVAL_FIELD = 4;
30 }
31 
32 DEFINE_LOG_TAG("EventLogger-EventLoggerConfig");
33 
EventLoggerConfig()34 EventLoggerConfig::EventLoggerConfig()
35 {
36     configPath_ = EVENT_LOGGER_CONFIG_PATH;
37 }
38 
EventLoggerConfig(std::string configPath)39 EventLoggerConfig::EventLoggerConfig(std::string configPath)
40 {
41     configPath_ = configPath;
42 }
43 
OpenConfig()44 bool EventLoggerConfig::OpenConfig()
45 {
46     in_.open(configPath_);
47     if (!in_.is_open()) {
48         HIVIEW_LOGW("fail to open config file.\n");
49         return false;
50     }
51     return true;
52 }
53 
CloseConfig()54 void EventLoggerConfig::CloseConfig()
55 {
56     if (in_.is_open()) {
57         in_.close();
58     }
59 }
60 
FindConfigVersion()61 bool EventLoggerConfig::FindConfigVersion()
62 {
63     if (!OpenConfig()) {
64         return false;
65     }
66 
67     std::string buf = "";
68     if (!getline(in_, buf)) {
69         HIVIEW_LOGW("Configfile is none.\n");
70         CloseConfig();
71         return false;
72     }
73 
74     std::smatch result;
75     auto versionRegex = std::regex("version=\"([0-9\\.]+)\".*");
76     if (!regex_search(buf, result, versionRegex)) {
77         HIVIEW_LOGW("match version failed.\n");
78         CloseConfig();
79         return false;
80     }
81     version_ = result[VERSION_FIELD];
82     HIVIEW_LOGI("version: %{public}s\n", version_.c_str());
83     return true;
84 }
85 
ParseConfigData(std::function<bool (EventLoggerConfigData &)> func)86 bool EventLoggerConfig::ParseConfigData(std::function<bool(EventLoggerConfigData&)> func)
87 {
88     HIVIEW_LOGI("called\n");
89     if (!FindConfigVersion()) {
90         return false;
91     }
92 
93     std::string buf = "";
94     std::smatch result;
95     auto eventRegex = std::regex(
96         "event id=\"([0-9xX]*)\"\\s*name=\"([A-Z0-9_]+)\"\\s*action=\"(.*)\"\\s*interval=\"([0-9]*)\".*");
97 
98     while (getline(in_, buf)) {
99         if (!regex_search(buf, result, eventRegex)) {
100             HIVIEW_LOGW("match event failed, getline duf is %{public}s.\n", buf.c_str());
101             continue;
102         }
103 
104         EventLoggerConfigData tmpConfigDate;
105         std::string idString = result[ID_FIELD];
106         if (idString.empty()) {
107             tmpConfigDate.id = -1;
108         } else {
109             tmpConfigDate.id = std::stoi(idString, nullptr, 0);
110         }
111         tmpConfigDate.name = result[NAME_FIELD];
112         tmpConfigDate.action = result[ACTION_FIELD];
113         std::string intervalString = result[INTERVAL_FIELD];
114         if (intervalString.empty()) {
115             tmpConfigDate.interval = 0;
116         } else {
117             tmpConfigDate.interval = std::stoi(intervalString);
118         }
119         if (!func(tmpConfigDate)) {
120             break;
121         }
122     }
123     CloseConfig();
124     return true;
125 }
126 
FindConfigLine(int eventId,std::string eventName,EventLoggerConfigData & configOut)127 bool EventLoggerConfig::FindConfigLine(int eventId, std::string eventName, EventLoggerConfigData &configOut)
128 {
129     HIVIEW_LOGI("called\n");
130     bool ret = false;
131     ParseConfigData([&](EventLoggerConfigData& configDate)->bool {
132         if (eventName == configDate.name) {
133             ret = true;
134         }
135         if (eventId == configDate.id) {
136             ret = true;
137         }
138         if (ret) {
139             configOut.id = configDate.id;
140             configOut.name = configDate.name;
141             configOut.interval = configDate.interval;
142             configOut.action = configDate.action;
143             HIVIEW_LOGI("configDate-> id: 0x%{public}x, name: %{public}s, action: %{public}s, interval: %{public}d\n",
144                 configOut.id, configOut.name.c_str(), configOut.action.c_str(), configOut.interval);
145         return false;
146         }
147         return true;
148     });
149     return ret;
150 }
151 
GetConfig()152 std::unordered_map<std::string, EventLoggerConfig::EventLoggerConfigData> EventLoggerConfig::GetConfig()
153 {
154     std::unordered_map<std::string, EventLoggerConfigData> ret;
155     ParseConfigData([&](EventLoggerConfigData& data)->bool {
156         ret.insert({ data.name, data });
157         return true;
158     });
159     return ret;
160 }
161 } // namespace HiviewDFX
162 } // namespace OHOS
163