numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
logging_macros.h
Go to the documentation of this file.
1/*
2 * Copyright 2024 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// IWYU pragma: no_include "num_collect/base/exception.h"
23
24#include <concepts>
25#include <iterator>
26#include <string_view>
27#include <utility>
28
29#include <fmt/base.h>
30#include <fmt/format.h>
31
35
36namespace num_collect::logging {
37
38namespace impl {
39
49 const logger& l, log_level level, std::string_view body) {
50 l.log_without_condition_check(source, level, body);
51}
52
63template <typename... Args>
64 requires(sizeof...(Args) > 0)
66 const logger& l, log_level level, fmt::format_string<Args...> format,
67 Args&&... args) {
69 source, level, format, std::forward<Args>(args)...);
70}
71
72} // namespace impl
73
82template <
83 std::constructible_from<std::string_view, util::source_info_view> Exception>
84[[noreturn]] void log_and_throw(
85 util::source_info_view source, const logger& l, std::string_view message) {
86 l.error(source)(message);
87 throw Exception(message, source);
88}
89
100template <
101 std::constructible_from<std::string_view, util::source_info_view> Exception,
102 typename... MessageArgs>
103 requires(sizeof...(MessageArgs) > 0)
104[[noreturn]] void log_and_throw(util::source_info_view source, const logger& l,
105 fmt::format_string<MessageArgs...> message_format,
106 MessageArgs&&... message_args) {
107 fmt::memory_buffer buffer;
108 fmt::format_to(std::back_inserter(buffer), message_format,
109 std::forward<MessageArgs>(message_args)...);
111 source, l, std::string_view(buffer.data(), buffer.size()));
112}
113
121template <
122 std::constructible_from<std::string_view, util::source_info_view> Exception>
123[[noreturn]] void log_and_throw(
124 util::source_info_view source, std::string_view message) {
125 logger l;
126 log_and_throw<Exception>(source, l, message);
127}
128
138template <
139 std::constructible_from<std::string_view, util::source_info_view> Exception,
140 typename... MessageArgs>
141 requires(sizeof...(MessageArgs) > 0)
142[[noreturn]] void log_and_throw(util::source_info_view source,
143 fmt::format_string<MessageArgs...> message_format,
144 MessageArgs&&... message_args) {
145 logger l;
147 source, l, message_format, std::forward<MessageArgs>(message_args)...);
148}
149
150} // namespace num_collect::logging
151
163#define INTERNAL_NUM_COLLECT_LOG_IMPL(LOGGER, LEVEL, ...) /*NOLINT*/ \
164 do { \
165 if (LOGGER.should_log(LEVEL)) { \
166 ::num_collect::logging::impl::log_without_condition_check( \
167 ::num_collect::util::source_info_view(), LOGGER, LEVEL, \
168 __VA_ARGS__); \
169 } \
170 } while (false)
171
184#define NUM_COLLECT_LOG_TRACE(LOGGER, ...) /*NOLINT*/ \
185 INTERNAL_NUM_COLLECT_LOG_IMPL( \
186 LOGGER, ::num_collect::logging::log_level::trace, __VA_ARGS__)
187
200#define NUM_COLLECT_LOG_DEBUG(LOGGER, ...) /*NOLINT*/ \
201 INTERNAL_NUM_COLLECT_LOG_IMPL( \
202 LOGGER, ::num_collect::logging::log_level::debug, __VA_ARGS__)
203
216#define NUM_COLLECT_LOG_ITERATION(LOGGER, ...) /*NOLINT*/ \
217 INTERNAL_NUM_COLLECT_LOG_IMPL( \
218 LOGGER, ::num_collect::logging::log_level::iteration, __VA_ARGS__)
219
232#define NUM_COLLECT_LOG_ITERATION_LABEL(LOGGER, ...) /*NOLINT*/ \
233 INTERNAL_NUM_COLLECT_LOG_IMPL(LOGGER, \
234 ::num_collect::logging::log_level::iteration_label, __VA_ARGS__)
235
248#define NUM_COLLECT_LOG_SUMMARY(LOGGER, ...) /*NOLINT*/ \
249 INTERNAL_NUM_COLLECT_LOG_IMPL( \
250 LOGGER, ::num_collect::logging::log_level::summary, __VA_ARGS__)
251
264#define NUM_COLLECT_LOG_INFO(LOGGER, ...) /*NOLINT*/ \
265 INTERNAL_NUM_COLLECT_LOG_IMPL( \
266 LOGGER, ::num_collect::logging::log_level::info, __VA_ARGS__)
267
280#define NUM_COLLECT_LOG_WARNING(LOGGER, ...) /*NOLINT*/ \
281 INTERNAL_NUM_COLLECT_LOG_IMPL( \
282 LOGGER, ::num_collect::logging::log_level::warning, __VA_ARGS__)
283
296#define NUM_COLLECT_LOG_ERROR(LOGGER, ...) /*NOLINT*/ \
297 INTERNAL_NUM_COLLECT_LOG_IMPL( \
298 LOGGER, ::num_collect::logging::log_level::error, __VA_ARGS__)
299
312#define NUM_COLLECT_LOG_CRITICAL(LOGGER, ...) /*NOLINT*/ \
313 INTERNAL_NUM_COLLECT_LOG_IMPL( \
314 LOGGER, ::num_collect::logging::log_level::critical, __VA_ARGS__)
315
333#define NUM_COLLECT_LOG_AND_THROW(EXCEPTION_TYPE, ...) /*NOLINT*/ \
334 ::num_collect::logging::log_and_throw<EXCEPTION_TYPE>( \
335 ::num_collect::util::source_info_view(), __VA_ARGS__)
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
auto error(util::source_info_view source=util::source_info_view()) const noexcept -> logging_proxy
Write a error log.
Definition logger.h:371
Class to hold information of source codes.
Definition of log_level enumeration.
Definition of logger class.
void log_without_condition_check(util::source_info_view source, const logger &l, log_level level, std::string_view body)
Write a log without check of the condition to write logs.
void log_and_throw(util::source_info_view source, const logger &l, std::string_view message)
Write an error log and throw an exception for an error.
log_level
Enumeration of log levels.
Definition log_level.h:47
Definition of source_info_view class.