scaffold some common services
This commit is contained in:
35
common/config/ConfigService.cpp
Normal file
35
common/config/ConfigService.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
|
||||
#include "ConfigService.hpp"
|
||||
|
||||
#include <exception>
|
||||
|
||||
ConfigService::ConfigService(const std::string& filePath) {
|
||||
|
||||
if(!loadFromFile(filePath)) {
|
||||
std::cout << "Error loading file " << filePath << std::endl;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool ConfigService::loadFromFile(const std::string& filePath) {
|
||||
try {
|
||||
config_.clear();
|
||||
config_.readFile(filePath.c_str());
|
||||
lastError_.clear();
|
||||
return true;
|
||||
} catch (const libconfig::FileIOException&) {
|
||||
lastError_ = "Unable to read config file: " + filePath;
|
||||
} catch (const libconfig::ParseException& error) {
|
||||
lastError_ = std::string("Parse error in ") + error.getFile() + ":" +
|
||||
std::to_string(error.getLine()) + " - " + error.getError();
|
||||
} catch (const std::exception& error) {
|
||||
lastError_ = error.what();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
const std::string& ConfigService::lastError() const {
|
||||
return lastError_;
|
||||
}
|
||||
|
||||
56
common/config/ConfigService.hpp
Normal file
56
common/config/ConfigService.hpp
Normal file
@@ -0,0 +1,56 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <libconfig.h++>
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "common/config/IConfig.hpp"
|
||||
|
||||
// TODO: would be cool for the config file to include other config files so theyre a bit more compartmentalized
|
||||
// then the main file will be like a hub and would be more like each component having its own config file
|
||||
class ConfigService {
|
||||
|
||||
public:
|
||||
|
||||
ConfigService(const std::string& filePath);
|
||||
~ConfigService() = default;
|
||||
|
||||
bool loadFromFile(const std::string& filePath);
|
||||
|
||||
template<typename Config>
|
||||
bool getConfig(const std::string& type, const std::string& id, typename Config::Params* params) const {
|
||||
|
||||
Config config { params };
|
||||
|
||||
try {
|
||||
const libconfig::Setting& configs = config_.lookup(type);
|
||||
for (int index = 0; index < configs.getLength(); ++index) {
|
||||
const libconfig::Setting& configSetting = configs[index];
|
||||
|
||||
std::string configId;
|
||||
if (!configSetting.lookupValue("Id", configId) || configId != id) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return config.parseConfig(configSetting);
|
||||
}
|
||||
} catch (const libconfig::SettingException& ex) {
|
||||
std::cout << "libconfig setting exception: " << ex.what() << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
const std::string& lastError() const;
|
||||
|
||||
private:
|
||||
|
||||
libconfig::Config config_;
|
||||
std::string lastError_;
|
||||
};
|
||||
22
common/config/IConfig.hpp
Normal file
22
common/config/IConfig.hpp
Normal file
@@ -0,0 +1,22 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <libconfig.h++>
|
||||
|
||||
template <typename T>
|
||||
class IConfig {
|
||||
|
||||
public:
|
||||
|
||||
using Params = T;
|
||||
|
||||
IConfig(Params* params) : params_(params) { }
|
||||
~IConfig() = default;
|
||||
|
||||
virtual bool parseConfig(const libconfig::Setting& setting) = 0;
|
||||
Params* params() { return params_; }
|
||||
|
||||
protected:
|
||||
Params* params_;
|
||||
|
||||
};
|
||||
49
common/config/LoggerConfig.hpp
Normal file
49
common/config/LoggerConfig.hpp
Normal file
@@ -0,0 +1,49 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "IConfig.hpp"
|
||||
|
||||
struct LoggerParams {
|
||||
std::string id;
|
||||
std::vector<std::string> flagsEnabled;
|
||||
bool showTime = false;
|
||||
bool showSourceTrace = false;
|
||||
bool coutEnabled = false;
|
||||
bool fileEnabled = false;
|
||||
std::string filePath;
|
||||
std::string fileName;
|
||||
};
|
||||
|
||||
class LoggerConfig : public IConfig<LoggerParams> {
|
||||
|
||||
public:
|
||||
|
||||
using IConfig<LoggerParams>::IConfig;
|
||||
|
||||
bool parseConfig(const libconfig::Setting& setting) override {
|
||||
|
||||
if (!setting.lookupValue("Id", params_->id)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
const libconfig::Setting& flags = setting.lookup("FlagsEnabled");
|
||||
for (int index = 0; index < flags.getLength(); ++index) {
|
||||
params_->flagsEnabled.push_back(static_cast<const char*>(flags[index]));
|
||||
}
|
||||
} catch (const libconfig::SettingException&) {
|
||||
params_->flagsEnabled.clear();
|
||||
return false;
|
||||
}
|
||||
|
||||
setting.lookupValue("ShowTime", params_->showTime);
|
||||
setting.lookupValue("ShowSourceTrace", params_->showSourceTrace);
|
||||
setting.lookupValue("CoutEnabled", params_->coutEnabled);
|
||||
setting.lookupValue("FileEnabled", params_->fileEnabled);
|
||||
setting.lookupValue("FilePath", params_->filePath);
|
||||
setting.lookupValue("FileName", params_->fileName);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user