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