| #include "tlbmc/configs/proto_config_parser.h" |
| |
| #include <stdint.h> |
| |
| #include <fstream> |
| #include <memory> |
| |
| #include "absl/log/log.h" |
| #include "absl/strings/str_cat.h" |
| #include "absl/strings/string_view.h" |
| #include "resource.pb.h" |
| #include "google/protobuf/io/zero_copy_stream_impl.h" |
| #include "google/protobuf/text_format.h" |
| |
| namespace milotic_tlbmc { |
| |
| using ::google::protobuf::TextFormat; |
| using ::google::protobuf::io::IstreamInputStream; |
| |
| template <typename T> |
| static T GetConfigFromProto(absl::string_view config_path) { |
| std::ifstream config_file(config_path.data()); |
| if (!config_file.is_open()) { |
| LOG(WARNING) << "Failed to open config file at '" << config_path |
| << "'. Using default config."; |
| return T(); |
| } |
| T config; |
| IstreamInputStream istream_source(&config_file); |
| if (!TextFormat::Parse(&istream_source, &config)) { |
| LOG(WARNING) << "Failed to parse config from file: '" << config_path |
| << "'. Using default config."; |
| return T(); |
| } |
| return config; |
| } |
| |
| void ProtoConfigParser::LoadProtoConfigs() { |
| data_store_.software_metrics_configs = |
| GetConfigFromProto<SoftwareMetricsConfig>( |
| absl::StrCat(config_path_, "software_metrics_config.textproto")); |
| } |
| |
| SoftwareMetricsConfig ProtoConfigParser::GetSoftwareMetricsConfig() const { |
| return data_store_.software_metrics_configs; |
| } |
| |
| std::unique_ptr<ProtoConfigParser> ProtoConfigParser::Create( |
| absl::string_view config_path) { |
| return std::make_unique<ProtoConfigParser>(config_path); |
| } |
| } // namespace milotic_tlbmc |