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