numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
toml_combined_log_sink_factory.h
Go to the documentation of this file.
1/*
2 * Copyright 2022 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
20#pragma once
21
22#include <cstddef>
23#include <string>
24#include <utility>
25#include <vector>
26
27#include <toml++/toml.h>
28
36
38
45public:
51 explicit toml_combined_log_sink_factory(const ::toml::table& table) {
52 const auto inner_sink_names_node = table.at_path("inner_sink_names");
53 if (!inner_sink_names_node) {
54 throw invalid_argument(
55 "Configuration inner_sink_names in num_collect.logging.sinks "
56 "element with type \"combined\" is required.");
57 }
58 const auto* inner_sink_names_array = inner_sink_names_node.as_array();
59 if (inner_sink_names_array == nullptr) {
60 throw invalid_argument(
61 "Configuration inner_sink_names in num_collect.logging.sinks "
62 "element with type \"combined\" must be an array of strings.");
63 }
64 inner_sink_names_.reserve(inner_sink_names_array->size());
65 for (const auto& inner_sink_name_node : *inner_sink_names_array) {
66 const auto inner_sink_name =
67 inner_sink_name_node.value<std::string>();
68 if (!inner_sink_name) {
69 throw invalid_argument(
70 "Configuration inner_sink_names in "
71 "num_collect.logging.sinks element with type \"combined\" "
72 "must be an array of strings.");
73 }
74 inner_sink_names_.push_back(*inner_sink_name);
75 }
76
77 const auto output_log_level_strs_node =
78 table.at_path("output_log_levels");
79 if (!output_log_level_strs_node) {
80 throw invalid_argument(
81 "Configuration output_log_levels in num_collect.logging.sinks "
82 "element with type \"combined\" is required.");
83 }
84 const auto* output_log_level_strs_array =
85 output_log_level_strs_node.as_array();
86 if (output_log_level_strs_array == nullptr) {
87 throw invalid_argument(
88 "Configuration output_log_levels in num_collect.logging.sinks "
89 "element with type \"combined\" must be an array of strings.");
90 }
91 output_log_levels_.reserve(output_log_level_strs_array->size());
92 for (const auto& output_log_level_str_node :
93 *output_log_level_strs_array) {
94 const auto output_log_level_str =
95 output_log_level_str_node.value<std::string>();
96 if (!output_log_level_str) {
97 throw invalid_argument(
98 "Configuration output_log_levels in "
99 "num_collect.logging.sinks element with type \"combined\" "
100 "must be an array of strings.");
101 }
102 output_log_levels_.push_back(
103 parse_output_log_level_str(*output_log_level_str));
104 }
105
106 if (inner_sink_names_.size() != output_log_levels_.size()) {
107 throw invalid_argument(
108 "inner_sink_names and output_log_levels in "
109 "num_collect.logging.sinks element with type \"combined\" must "
110 "have the same number of elements.");
111 }
112 }
113
115 [[nodiscard]] auto create(log_sink_factory_table& sinks)
116 -> sinks::log_sink override {
117 std::vector<std::pair<sinks::log_sink, log_level>> inner_sinks;
118 inner_sinks.reserve(inner_sink_names_.size());
119 for (std::size_t i = 0; i < inner_sink_names_.size(); ++i) {
120 inner_sinks.emplace_back(
121 sinks.get(inner_sink_names_[i]), output_log_levels_[i]);
122 }
123 return sinks::create_combined_log_sink(std::move(inner_sinks));
124 }
125
129 ~toml_combined_log_sink_factory() noexcept override = default;
130
132 const toml_combined_log_sink_factory&) = delete;
134 auto operator=(const toml_combined_log_sink_factory&)
136 auto operator=(toml_combined_log_sink_factory&&)
138
139private:
141 std::vector<std::string> inner_sink_names_;
142
145};
146
147} // namespace num_collect::logging::config::toml
Class of exception on invalid arguments.
Definition exception.h:85
Class to create log sinks to write logs to multiple log sinks.
std::vector< log_level > output_log_levels_
Output log levels of log sinks.
auto create(log_sink_factory_table &sinks) -> sinks::log_sink override
Create log sink.
~toml_combined_log_sink_factory() noexcept override=default
Destructor.
Declaration of functions to create combined log sinks.
Definition of exceptions.
Definition of log_level enumeration.
Definition of log_sink class.
Definition of log_sink_factory_base class.
Definition of log_sink_factory_table class.
Namespace of logging configuration in TOML files.
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.
auto create_combined_log_sink(std::vector< std::pair< log_sink, log_level > > sinks) -> log_sink
Create a log sink to write logs to multiple log sinks.
log_level
Enumeration of log levels.
Definition log_level.h:47
STL namespace.
Definition of parse_output_log_level_str function.