Skip to content

Commit

Permalink
Features: use yaml-node as source of configurator and mass-rotate (#16)
Browse files Browse the repository at this point in the history
* feature: use yaml-node as source of configurator

Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>

* Feature: mass rotate (#36)

* feature: execute rotate on each sink

Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>

* update: bump version

Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>

---------

Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>

(cherry picked from commit 526cede)

---------

Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>
  • Loading branch information
xDimon authored Jan 13, 2025
1 parent c13d77d commit a564a5f
Show file tree
Hide file tree
Showing 4 changed files with 36 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
5 changes: 5 additions & 0 deletions include/soralog/logging_system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@ namespace soralog {
*/
bool resetLevelOfLogger(const std::string &logger_name);

/**
* Calls `Sink::rotate()` for all registered sinks
*/
void callRotateForAllSinks();

private:
/**
* @returns loggers (with creating that if it isn't exists yet) with
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
8 changes: 8 additions & 0 deletions src/logging_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -556,4 +556,12 @@ namespace soralog {
return false;
}

void LoggingSystem::callRotateForAllSinks() {
std::lock_guard guard(mutex_);
std::ranges::for_each(sinks_, [](const auto &it) {
const auto &sink = it.second;
sink->rotate();
});
}

} // namespace soralog

0 comments on commit a564a5f

Please sign in to comment.