Skip to content

Commit

Permalink
feature: use yaml-node as source of configurator
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>
  • Loading branch information
xDimon committed Jan 13, 2025
1 parent 08dad9b commit 8694e79
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
24 changes: 17 additions & 7 deletions include/soralog/impl/configurator_from_yaml.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ namespace soralog {

/**
* @class ConfiguratorFromYAML
* @brief This configurator for set up Logging System in according with
* config using YAML format.
* @brief This configurator for set up Logging System
* in according to config using YAML format.
*/
class ConfiguratorFromYAML : public Configurator {
public:
Expand Down Expand Up @@ -53,22 +53,32 @@ namespace soralog {
std::string config_content)
: previous_(std::move(previous)), config_(std::move(config_content)) {};

/**
* Uses YAML-node {@param config_yaml_node} as source of config
* Firstly applies provided underlying configurator {@param previous}.
*/
explicit ConfiguratorFromYAML(std::shared_ptr<Configurator> previous,
YAML::Node config_yaml_node)
: previous_(std::move(previous)),
config_(std::move(config_yaml_node)) {};

~ConfiguratorFromYAML() override = default;

Result applyOn(LoggingSystem &system) const override;

private:
std::shared_ptr<Configurator> previous_;
std::variant<std::filesystem::path, std::string> config_;
std::variant<std::filesystem::path, std::string, YAML::Node> config_;

/**
* Helper-class to parse config and create sinks and groups during that
*/
class Applicator {
public:
Applicator(LoggingSystem &system,
std::variant<std::filesystem::path, std::string> config,
std::shared_ptr<Configurator> previous = {})
Applicator(
LoggingSystem &system,
std::variant<std::filesystem::path, std::string, YAML::Node> config,
std::shared_ptr<Configurator> previous = {})
: system_(system),
previous_(std::move(previous)),
config_(std::move(config)) {}
Expand Down Expand Up @@ -106,7 +116,7 @@ namespace soralog {
// NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
LoggingSystem &system_;
std::shared_ptr<Configurator> previous_ = nullptr;
std::variant<std::filesystem::path, std::string> config_;
std::variant<std::filesystem::path, std::string, YAML::Node> config_;
bool has_warning_ = false;
bool has_error_ = false;
std::ostringstream errors_;
Expand Down
6 changes: 6 additions & 0 deletions src/impl/configurator_from_yaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ namespace soralog {
[&](auto &&arg) {
using T = std::decay_t<decltype(arg)>;

// Provided path - trying to read and parse like yaml-file
if constexpr (std::is_same_v<T, std::filesystem::path>) {
try {
node = YAML::LoadFile(arg);
Expand All @@ -75,6 +76,7 @@ namespace soralog {
has_error_ = true;
}

// Provided string - trying to parse like yaml-content
} else if constexpr (std::is_same_v<T, std::string>) {
try {
node = YAML::Load(arg);
Expand All @@ -83,6 +85,10 @@ namespace soralog {
has_error_ = true;
}

// Provided yaml-node - using directly
} else if constexpr (std::is_same_v<T, YAML::Node>) {
node = arg;

} else {
static_assert(always_false_v<T>, "non-exhaustive visitor!");
}
Expand Down

0 comments on commit 8694e79

Please sign in to comment.