numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
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 <algorithm>
23#include <iterator>
24#include <string>
25#include <string_view>
26#include <utility>
27
28#include <fmt/base.h>
29#include <fmt/format.h>
30
40
41namespace num_collect::logging {
42
50public:
60 logging_proxy(std::string_view tag, log_level level,
61 util::source_info_view source, const sinks::log_sink* sink,
62 bool write_log) noexcept
63 : tag_(tag),
64 level_(level),
65 source_(source),
66 sink_(sink),
67 write_log_(write_log) {}
68
74 void operator()(std::string_view body) const {
75 if (!write_log_) {
76 return;
77 }
78
80 }
81
89 template <typename... Args>
90 requires(sizeof...(Args) > 0)
91 void operator()(fmt::format_string<Args...> format, Args&&... args) const {
92 if (!write_log_) {
93 return;
94 }
95
96 fmt::memory_buffer buffer;
97 fmt::format_to(
98 std::back_inserter(buffer), format, std::forward<Args>(args)...);
100 std::string_view{buffer.data(), buffer.size()});
101 }
102
103private:
105 std::string_view tag_;
106
109
112
115
118};
119
123constexpr auto default_tag = log_tag_view("");
124
145class logger {
146public:
151
158
167
175 : tag_(std::move(tag)),
176 config_(std::move(config)),
177 always_output_log_level_(std::max(config_.output_log_level(),
178 config_.output_log_level_in_child_iterations())),
179 lowest_output_log_level_(std::min(config_.output_log_level(),
180 config_.output_log_level_in_child_iterations())) {}
181
187 [[nodiscard]] auto tag() const noexcept -> const log_tag& { return tag_; }
188
194 [[nodiscard]] auto config() const noexcept -> const log_tag_config& {
195 return config_;
196 }
197
201 void set_iterative() const noexcept {
203 }
204
214 child.iteration_layer_handler_);
215 }
216
224 [[nodiscard]] auto should_log(log_level level) const noexcept -> bool {
225 if (level < lowest_output_log_level_) {
226 return false;
227 }
228 if (level >= always_output_log_level_) {
229 return true;
230 }
233 }
234 return level >= config_.output_log_level();
235 }
236
247 [[nodiscard]] auto log(log_level level,
248 util::source_info_view source = util::source_info_view()) const noexcept
249 -> logging_proxy {
250 return logging_proxy(
251 tag_.name(), level, source, &config_.sink(), should_log(level));
252 }
253
263 [[nodiscard]] auto trace(
264 util::source_info_view source = util::source_info_view()) const noexcept
265 -> logging_proxy {
266 return log(log_level::trace, source);
267 }
268
278 [[nodiscard]] auto debug(
279 util::source_info_view source = util::source_info_view()) const noexcept
280 -> logging_proxy {
281 return log(log_level::debug, source);
282 }
283
294 [[nodiscard]] auto iteration(
295 util::source_info_view source = util::source_info_view()) const noexcept
296 -> logging_proxy {
297 return log(log_level::iteration, source);
298 }
299
310 [[nodiscard]] auto iteration_label(
311 util::source_info_view source = util::source_info_view()) const noexcept
312 -> logging_proxy {
313 return log(log_level::iteration_label, source);
314 }
315
326 [[nodiscard]] auto summary(
327 util::source_info_view source = util::source_info_view()) const noexcept
328 -> logging_proxy {
329 return log(log_level::summary, source);
330 }
331
341 [[nodiscard]] auto info(
342 util::source_info_view source = util::source_info_view()) const noexcept
343 -> logging_proxy {
344 return log(log_level::info, source);
345 }
346
356 [[nodiscard]] auto warning(
357 util::source_info_view source = util::source_info_view()) const noexcept
358 -> logging_proxy {
359 return log(log_level::warning, source);
360 }
361
371 [[nodiscard]] auto error(
372 util::source_info_view source = util::source_info_view()) const noexcept
373 -> logging_proxy {
374 return log(log_level::error, source);
375 }
376
386 [[nodiscard]] auto critical(
387 util::source_info_view source = util::source_info_view()) const noexcept
388 -> logging_proxy {
389 return log(log_level::critical, source);
390 }
391
402 log_level level, std::string_view body) const {
403 config_.sink().write(
404 time_stamp::now(), tag_.name(), level, source, body);
405 }
406
418 template <typename... Args>
419 requires(sizeof...(Args) > 0)
421 log_level level, fmt::format_string<Args...> format,
422 Args&&... args) const {
423 fmt::memory_buffer buffer;
424 fmt::format_to(
425 std::back_inserter(buffer), format, std::forward<Args>(args)...);
426 config_.sink().write(time_stamp::now(), tag_.name(), level, source,
427 std::string_view(buffer.data(), buffer.size()));
428 }
429
430private:
433
436
439
442
445};
446
447} // namespace num_collect::logging
void set_iterative() const noexcept
Set this node to an iterative algorithm.
void initialize_lower_layer(iteration_layer_handler &lower_layer) noexcept
Initialize the lower layer.
auto is_upper_layer_iterative() const noexcept -> bool
Check whether one of upper layers is iterative.
Class to hold configurations for log tags.
auto sink() const noexcept -> const sinks::log_sink &
Get the log sink.
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.
Class of tags of logs without memory management.
Class of tags of logs.
Definition log_tag.h:33
auto name() const noexcept -> const std::string &
Get the name of this tag.
Definition log_tag.h:47
Class of loggers.
Definition logger.h:145
void log_without_condition_check(util::source_info_view source, log_level level, std::string_view body) const
Write a log without check of the condition to write logs.
Definition logger.h:401
logger(log_tag tag, log_tag_config config)
Constructor.
Definition logger.h:174
impl::iteration_layer_handler iteration_layer_handler_
Handler of layers of iterations.
Definition logger.h:444
log_tag_config config_
Configuration.
Definition logger.h:435
log_level lowest_output_log_level_
Lowest log level to output.
Definition logger.h:441
auto iteration_label(util::source_info_view source=util::source_info_view()) const noexcept -> logging_proxy
Write a label of iteration logs.
Definition logger.h:310
auto iteration(util::source_info_view source=util::source_info_view()) const noexcept -> logging_proxy
Write a iteration log.
Definition logger.h:294
auto trace(util::source_info_view source=util::source_info_view()) const noexcept -> logging_proxy
Write a trace log.
Definition logger.h:263
auto warning(util::source_info_view source=util::source_info_view()) const noexcept -> logging_proxy
Write a warning log.
Definition logger.h:356
auto summary(util::source_info_view source=util::source_info_view()) const noexcept -> logging_proxy
Write a summary log.
Definition logger.h:326
auto log(log_level level, util::source_info_view source=util::source_info_view()) const noexcept -> logging_proxy
Write a log.
Definition logger.h:247
void set_iterative() const noexcept
Set this node to an iterative algorithm.
Definition logger.h:201
void log_without_condition_check(util::source_info_view source, log_level level, fmt::format_string< Args... > format, Args &&... args) const
Write a log without check of the condition to write logs.
Definition logger.h:420
logger(log_tag_view tag)
Constructor.
Definition logger.h:157
log_level always_output_log_level_
Minimum log level to output always.
Definition logger.h:438
logger(log_tag_view tag, log_tag_config config)
Constructor.
Definition logger.h:165
auto info(util::source_info_view source=util::source_info_view()) const noexcept -> logging_proxy
Write a information log.
Definition logger.h:341
auto debug(util::source_info_view source=util::source_info_view()) const noexcept -> logging_proxy
Write a debug log.
Definition logger.h:278
auto critical(util::source_info_view source=util::source_info_view()) const noexcept -> logging_proxy
Write a critical log.
Definition logger.h:386
auto should_log(log_level level) const noexcept -> bool
Check whether to write logs with a log level.
Definition logger.h:224
auto error(util::source_info_view source=util::source_info_view()) const noexcept -> logging_proxy
Write a error log.
Definition logger.h:371
auto tag() const noexcept -> const log_tag &
Get the log tag.
Definition logger.h:187
auto config() const noexcept -> const log_tag_config &
Get the configuration.
Definition logger.h:194
void initialize_child_algorithm_logger(logger &child) noexcept
Initialize a logger as the logger of the algorithm called from the algorithm of this logger.
Definition logger.h:212
Proxy class to write logs.
Definition logger.h:49
bool write_log_
Whether to write log.
Definition logger.h:117
logging_proxy(std::string_view tag, log_level level, util::source_info_view source, const sinks::log_sink *sink, bool write_log) noexcept
Constructor.
Definition logger.h:60
util::source_info_view source_
Information of the source code.
Definition logger.h:111
log_level level_
Log level.
Definition logger.h:108
std::string_view tag_
Tag.
Definition logger.h:105
void operator()(std::string_view body) const
Write a log.
Definition logger.h:74
const sinks::log_sink * sink_
Log sink.
Definition logger.h:114
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 hold information of source codes.
Definition of iteration_layer_handler class.
Definition of functions to get and set logging configurations.
Definition of log_level enumeration.
Definition of log_sink class.
Definition of log_tag class.
Definition of log_tag_config class.
Definition of log_tag_view class.
log_level
Enumeration of log levels.
Definition log_level.h:47
@ iteration_label
Label of iterations.
NUM_COLLECT_EXPORT auto get_config_of(log_tag_view tag) -> log_tag_config
Get the configuration of a tag.
constexpr auto default_tag
Default log tag.
Definition logger.h:123
STL namespace.
Definition of source_info_view class.
Definition of time_stamp class.