numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
iteration_logger.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 <memory>
23#include <string>
24#include <string_view>
25#include <type_traits>
26#include <utility>
27#include <variant>
28#include <vector>
29
30#include <fmt/format.h>
31
49
51
58template <typename Algorithm = std::monostate>
60public:
67 : tag_(logger.tag()),
68 sink_(logger.config().sink()),
70 logger.config().iteration_output_period()),
72 logger.config().iteration_label_period()) {
74 }
75
87
93 void append(
94 std::shared_ptr<iteration_parameter_base<Algorithm>> parameter) {
95 parameters_.push_back(std::move(parameter));
96 }
97
110 auto append(std::string label, ParameterValue value)
112 auto parameter = std::make_shared<
114 std::move(label), std::move(value));
115 auto* raw_ptr = parameter.get();
116 append(std::move(parameter));
117 return raw_ptr;
118 }
119
128 template <concepts::formattable_iteration_parameter_value Value>
129 auto append(std::string label, const Value& value)
130 -> iteration_parameter<Algorithm, Value,
132 return append<Value>(std::move(label),
134 }
135
147 auto append(std::string label, Function&& function)
148 -> iteration_parameter<Algorithm, Value,
149 function_iteration_parameter_value<Algorithm, Value,
150 std::decay_t<Function>>>* {
151 return append<Value>(std::move(label),
152 function_iteration_parameter_value<Algorithm, Value,
153 std::decay_t<Function>>(std::forward<Function>(function)));
154 }
155
164 template <concepts::formattable_iteration_parameter_value Value>
165 auto append(std::string label, const Value Algorithm::* ptr)
166 -> iteration_parameter<Algorithm, Value,
168 return append<Value>(std::move(label),
170 }
171
183 auto append(std::string label, Function&& function)
184 -> iteration_parameter<Algorithm, Value,
186 std::decay_t<Function>>>* {
187 return append<Value>(std::move(label),
189 std::decay_t<Function>>(std::forward<Function>(function)));
190 }
191
201 if (!should_write_iteration()) [[likely]] {
202 return;
203 }
204
205 write_label_if_needed(source);
206
207 buffer_.clear();
210 source, std::string_view(buffer_.data(), buffer_.size()));
211
213 }
214
223 void write_iteration(Algorithm* algorithm,
225 if (!should_write_iteration()) [[likely]] {
226 return;
227 }
228
229 write_label_if_needed(source);
230
231 buffer_.clear();
232 format_values_to(buffer_, algorithm);
234 source, std::string_view(buffer_.data(), buffer_.size()));
235
237 }
238
246 if (!write_summaries_) [[likely]] {
247 return;
248 }
249
250 buffer_.clear();
253 std::string_view(buffer_.data(), buffer_.size()));
254 }
255
262 void write_summary(Algorithm* algorithm,
264 if (!write_summaries_) [[likely]] {
265 return;
266 }
267
268 buffer_.clear();
269 format_summary_to(buffer_, algorithm);
271 std::string_view(buffer_.data(), buffer_.size()));
272 }
273
274private:
280 [[nodiscard]] auto should_write_iteration() -> bool {
281 if (!write_iterations_) [[likely]] {
282 return false;
283 }
284
285 if (!iteration_output_period_checker_) [[likely]] {
287 return false;
288 }
289
290 return true;
291 }
292
300 buffer_.clear();
304 std::string_view(buffer_.data(), buffer_.size()));
305 }
307 }
308
314 void format_labels_to(fmt::memory_buffer& buffer) const {
315 for (const auto& param : parameters_) {
316 buffer.push_back(' ');
317 param->format_label_to(buffer);
318 }
319 }
320
326 void format_values_to(fmt::memory_buffer& buffer) const {
327 for (const auto& param : parameters_) {
328 buffer.push_back(' ');
329 param->format_value_to(buffer);
330 }
331 }
332
340 fmt::memory_buffer& buffer, Algorithm* algorithm) const {
341 for (const auto& param : parameters_) {
342 buffer.push_back(' ');
343 param->format_value_to(buffer, algorithm);
344 }
345 }
346
352 void format_summary_to(fmt::memory_buffer& buffer) const {
353 buffer.append(std::string_view("Finished iterations: "));
354 bool is_first = true;
355 for (const auto& param : parameters_) {
356 if (is_first) {
357 is_first = false;
358 } else {
359 buffer.push_back(',');
360 buffer.push_back(' ');
361 }
362 param->format_summary_to(buffer);
363 }
364 }
365
373 fmt::memory_buffer& buffer, Algorithm* algorithm) const {
374 buffer.append(std::string_view("Finished iterations: "));
375 bool is_first = true;
376 for (const auto& param : parameters_) {
377 if (is_first) {
378 is_first = false;
379 } else {
380 buffer.push_back(',');
381 buffer.push_back(' ');
382 }
383 param->format_summary_to(buffer, algorithm);
384 }
385 }
386
389
391 bool write_iterations_{false};
392
394 bool write_summaries_{false};
395
398
401
404
406 std::vector<std::shared_ptr<iteration_parameter_base<Algorithm>>>
408
410 fmt::memory_buffer buffer_{};
411};
412
413} // namespace num_collect::logging::iterations
Class of parameters values in iterations specified by functions.
void format_summary_to(fmt::memory_buffer &buffer) const
Format a line of summary.
auto should_write_iteration() -> bool
Determine whether to write an iteration to the logger.
void format_values_to(fmt::memory_buffer &buffer, Algorithm *algorithm) const
Format a line of values.
auto append(std::string label, Function &&function) -> iteration_parameter< Algorithm, Value, function_iteration_parameter_value< Algorithm, Value, std::decay_t< Function > > > *
Append a parameter specified by functions.
void write_iteration(util::source_info_view source=util::source_info_view())
Write an iteration to the logger.
fmt::memory_buffer buffer_
Buffer of logging data.
void write_iteration(Algorithm *algorithm, util::source_info_view source=util::source_info_view())
Write an iteration to the logger.
void write_label_if_needed(util::source_info_view source)
Write labels if needed.
bool write_iterations_
Whether to write iteration logs.
auto append(std::string label, const Value &value) -> iteration_parameter< Algorithm, Value, variable_iteration_parameter_value< Algorithm, Value > > *
Append a parameter specified by variables.
std::vector< std::shared_ptr< iteration_parameter_base< Algorithm > > > parameters_
Parameters.
void format_labels_to(fmt::memory_buffer &buffer) const
Format a line of labels.
void write_summary(Algorithm *algorithm, util::source_info_view source=util::source_info_view())
Write a summary to a logger.
auto append(std::string label, const Value Algorithm::*ptr) -> iteration_parameter< Algorithm, Value, member_variable_iteration_parameter_value< Algorithm, Value > > *
Append a parameter specified by member variables.
bool write_summaries_
Whether to write summary logs.
util::iteration_period_checker iteration_label_period_checker_
Checker of periods to write labels of iteration logs.
auto append(std::string label, Function &&function) -> iteration_parameter< Algorithm, Value, member_function_iteration_parameter_value< Algorithm, Value, std::decay_t< Function > > > *
Append a parameter specified by member functions.
void write_summary(util::source_info_view source=util::source_info_view())
Write a summary to a logger.
auto append(std::string label, ParameterValue value) -> iteration_parameter< Algorithm, Value, ParameterValue > *
Append a parameter.
void append(std::shared_ptr< iteration_parameter_base< Algorithm > > parameter)
Append a parameter.
util::iteration_period_checker iteration_output_period_checker_
Checker of periods to write iteration logs.
void format_summary_to(fmt::memory_buffer &buffer, Algorithm *algorithm) const
Format a line of summary.
void format_values_to(fmt::memory_buffer &buffer) const
Format a line of values.
void start(logger &logger)
Start iterations.
Class of parameters values in iterations specified by member functions.
Class of parameters values in iterations specified by member variables.
Class of parameters values in iterations specified by variables.
Class of tags of logs without memory management.
constexpr auto name() const noexcept -> std::string_view
Get the name of this tag.
Class of loggers.
Definition logger.h:145
auto should_log(log_level level) const noexcept -> bool
Check whether to write logs with a log level.
Definition logger.h:224
void write(time_stamp time, std::string_view tag, log_level level, util::source_info_view source, std::string_view body) const noexcept
Write a log.
static auto now() noexcept -> time_stamp
Get the current time stamp.
Class to check periods of iterations.
Class to hold information of source codes.
Concept of getter functions.
Definition getter_of.h:35
Definition of formattable_iteration_parameter_value concept.
Definition of function_iteration_parameter_value class.
Definition of getter_of class.
Definition of iteration_parameter class.
Definition of iteration_parameter_value concept.
Definition of iteration_period_checker class.
Definition of log_level enumeration.
Definition of log_sink class.
Definition of log_tag_config class.
Definition of log_tag_view class.
Definition of logger class.
Definition of member_function_iteration_parameter_value class.
Definition of member_getter_of class.
Definition of member_variable_iteration_parameter_value class.
@ iteration_label
Label of iterations.
Definition of source_info_view class.
Definition of time_stamp class.
Definition of variable_iteration_parameter_value class.