1 /*
2 * Copyright (c) 2024 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 "export_config_manager.h"
17
18 #include <regex>
19
20 #include "file_util.h"
21 #include "hiview_logger.h"
22
23 namespace OHOS {
24 namespace HiviewDFX {
25 DEFINE_LOG_TAG("HiView-ExportConfigManager");
GetModuleNames(std::vector<std::string> & moduleNames) const26 void ExportConfigManager::GetModuleNames(std::vector<std::string>& moduleNames) const
27 {
28 if (exportConfigs_.empty()) {
29 HIVIEW_LOGW("no module name found.");
30 return;
31 }
32 for (auto& config : exportConfigs_) {
33 moduleNames.emplace_back(config.first);
34 }
35 }
36
GetExportConfigs(std::vector<std::shared_ptr<ExportConfig>> & exportConfigs) const37 void ExportConfigManager::GetExportConfigs(std::vector<std::shared_ptr<ExportConfig>>& exportConfigs) const
38 {
39 for (auto& config : exportConfigs_) {
40 exportConfigs.emplace_back(config.second);
41 }
42 }
43
GetExportConfig(const std::string & moduleName) const44 std::shared_ptr<ExportConfig> ExportConfigManager::GetExportConfig(const std::string& moduleName) const
45 {
46 auto iter = exportConfigs_.find(moduleName);
47 if (iter == exportConfigs_.end()) {
48 HIVIEW_LOGW("no export config found for module:%{public}s.", moduleName.c_str());
49 return nullptr;
50 }
51 return iter->second;
52 }
53
Init(const std::string & configDir)54 void ExportConfigManager::Init(const std::string& configDir)
55 {
56 HIVIEW_LOGD("configuration file directory is %{public}s.", configDir.c_str());
57 if (!FileUtil::IsLegalPath(configDir) || !FileUtil::FileExists(configDir)) {
58 HIVIEW_LOGW("configuration file directory is invalid, dir: %{public}s.", configDir.c_str());
59 return;
60 }
61 std::vector<std::string> configFiles;
62 FileUtil::GetDirFiles(configDir, configFiles);
63 if (configFiles.empty()) {
64 HIVIEW_LOGW("no event export configuration file found.");
65 return;
66 }
67 ParseConfigFiles(configFiles);
68 }
69
ParseConfigFiles(const std::vector<std::string> & configFiles)70 void ExportConfigManager::ParseConfigFiles(const std::vector<std::string>& configFiles)
71 {
72 for (auto& configFile : configFiles) {
73 ParseConfigFile(configFile);
74 }
75 }
76
ParseConfigFile(const std::string & configFile)77 void ExportConfigManager::ParseConfigFile(const std::string& configFile)
78 {
79 // module name consists of only lowercase letter and underline.
80 // eg. module name parsed from 'hiview_event_export_config.json' is 'hiview'
81 std::regex reg { ".*/([a-z_]+)_event_export_config.json$" };
82 std::smatch match;
83 if (!std::regex_match(configFile, match, reg)) {
84 HIVIEW_LOGW("config file name is invalid, file=%{public}s.", configFile.c_str());
85 return;
86 }
87 std::string moduleName = match[1].str();
88 auto config = GetExportConfig(moduleName);
89 if (config != nullptr) {
90 HIVIEW_LOGW("this config file of %{public}s module has been parsed", moduleName.c_str());
91 return;
92 }
93 ExportConfigParser parser(configFile);
94 config = parser.Parse();
95 if (config == nullptr) {
96 HIVIEW_LOGE("failed to parse config file, file=%{public}s.", configFile.c_str());
97 return;
98 }
99 config->moduleName = moduleName;
100 exportConfigs_.emplace(moduleName, config);
101 }
102 } // HiviewDFX
103 } // OHOS