27#include <unordered_map>
30#include <fmt/format.h>
31#include <toml++/toml.h>
61 std::string_view config_name) ->
log_level {
76 if (table.contains(
"sink")) {
78 "sink in num_collect.logging.tag_configs element",
"a string");
79 config.
sink(sinks.
get(sink_name));
82 if (table.contains(
"output_log_level")) {
84 "output_log_level in num_collect.logging.tag_configs element");
88 if (table.contains(
"output_log_level_in_child_iterations")) {
91 "output_log_level_in_child_iterations in "
92 "num_collect.logging.tag_configs element");
96 if (table.contains(
"iteration_output_period")) {
99 "iteration_output_period in num_collect.logging.tag_configs "
105 if (table.contains(
"iteration_label_period")) {
108 "iteration_label_period in num_collect.logging.tag_configs "
129 "console", std::make_shared<toml_console_log_sink_config_parser>());
131 std::make_shared<toml_single_file_log_sink_config_parser>());
133 std::make_shared<toml_combined_log_sink_config_parser>());
138 if (!std::filesystem::exists(std::filesystem::path(filepath)) ||
139 !std::filesystem::is_regular_file(
140 std::filesystem::path(filepath))) {
142 "Invalid filepath to load configurations {}.", filepath));
145 const auto parse_result = ::toml::parse_file(filepath);
147 }
catch (
const std::exception& e) {
149 fmt::format(
"Failed to load {}: {}", filepath, e.what()));
156 const auto parse_result = ::toml::parse(text);
158 }
catch (
const std::exception& e) {
160 fmt::format(
"Failed to load from a text: {}\n"
175 const auto log_sink_configs_node =
176 table.at_path(
"num_collect.logging.sinks");
177 if (log_sink_configs_node) {
178 const auto* log_sink_configs_array =
179 log_sink_configs_node.as_array();
180 if (log_sink_configs_array ==
nullptr) {
182 "Configuration num_collect.logging.sinks must be an "
188 const auto log_tag_configs_node =
189 table.at_path(
"num_collect.logging.tag_configs");
190 if (log_tag_configs_node) {
191 const auto* log_tag_configs_array = log_tag_configs_node.as_array();
192 if (log_tag_configs_array ==
nullptr) {
194 "Configuration num_collect.logging.tag_configs must be an "
208 const std::shared_ptr<toml_log_sink_config_parser_base>& parser) {
211 "Duplicate name of parsers of configurations of log sinks {}.",
237 for (
const auto& elem_node : array) {
238 const auto* elem_table = elem_node.as_table();
239 if (elem_table ==
nullptr) {
241 "Each element in configuration num_collect.logging.sinks "
246 "name in num_collect.logging.sinks element",
"a string");
248 "type in num_collect.logging.sinks element",
"a string");
253 fmt::format(
"Invalid type of log sink {}.", type));
255 const auto sink_factory = parser_iter->second->parse(*elem_table);
257 sinks.append(name, sink_factory);
269 for (
const auto& elem_node : array) {
270 const auto* elem_table = elem_node.as_table();
271 if (elem_table ==
nullptr) {
273 "Each element in configuration "
274 "num_collect.logging.tag_configs "
279 "tag",
"tag in num_collect.logging.tag_configs element",
281 const auto tag =
log_tag(tag_string);
292 std::unordered_map<std::string,
293 std::shared_ptr<toml_log_sink_config_parser_base>>
Class of exception on invalid arguments.
Interface of parsers of logging configurations.
Class of tables of log sinks.
auto get(const std::string &name) -> sinks::log_sink
Get a log sink creating it if needed.
Class to parse log configuration from a TOML file.
toml_log_config_parser()
Constructor.
void parse_from_text(std::string_view text) override
Parse configuration from a file.
~toml_log_config_parser() noexcept override=default
Destructor.
static void parse_and_apply_log_tag_configs(const ::toml::array &array, log_sink_factory_table &sinks)
Parse and apply configurations of log tags.
std::unordered_map< std::string, std::shared_ptr< toml_log_sink_config_parser_base > > log_sink_config_parsers_
Parses of configurations of log sinks.
void parse_from_file(std::string_view filepath) override
Parse configuration from a file.
void parse_log_sinks(const ::toml::array &array, log_sink_factory_table &sinks)
Parse configurations of log sinks.
void append_log_sink_config_parser(const std::string &name, const std::shared_ptr< toml_log_sink_config_parser_base > &parser)
Append a parser of configurations of log sinks.
void parse_from_table(const ::toml::table &table)
Parse configuration from a TOML table.
Class to hold configurations for log tags.
auto iteration_output_period() const noexcept -> index_type
Get the period to write iteration logs.
auto sink() const noexcept -> const sinks::log_sink &
Get the log sink.
auto iteration_label_period() const noexcept -> index_type
Get the period to write labels of iteration logs.
auto output_log_level_in_child_iterations() const noexcept -> log_level
Get the minimum log level to output in child iterations.
auto output_log_level() const noexcept -> log_level
Get the minimum log level to output.
Definition of exceptions.
Definition of index_type type.
Definition of functions to get and set logging configurations.
Definition of log_config_parser_base class.
Definition of log_level enumeration.
Definition of log_sink_factory_table class.
Definition of log_tag class.
Definition of log_tag_config class.
auto require_log_level(const ::toml::table &table, std::string_view path, std::string_view config_name) -> log_level
Get a log level value or throw exception.
void parse_log_tag_config_to(log_tag_config &config, const ::toml::table &table, log_sink_factory_table &sinks)
Parse configurations of logging for a log tag.
Namespace of logging configuration in TOML files.
auto require_value(const ::toml::table &table, std::string_view path, std::string_view config_name, std::string_view type_name) -> T
Get a value or throw exceptions.
auto parse_output_log_level_str(std::string_view str) -> log_level
Parse a log level from a string to use in output_log_level of log_tag_config class.
NUM_COLLECT_EXPORT void set_config_of(log_tag_view tag, const log_tag_config &config)
Set the configuration of a tag.
log_level
Enumeration of log levels.
NUM_COLLECT_EXPORT auto get_config_of(log_tag_view tag) -> log_tag_config
Get the configuration of a tag.
Definition of parse_output_log_level_str function.
Definition of toml_combined_log_sink_config_parser class.
Definition of toml_console_log_sink_config_parser class.
Definition of helper functions for parsing TOML files.
Definition of toml_log_sink_config_parser_base class.
Definition of toml_single_file_log_sink_config_parser class.