LehrFEM++ 1.0.0
A simple Finite Element Library for teaching
spdlog_utils.h
1
9#ifndef __6f06a5790b0b46cf94fb3cc3cc0cc2d3
10#define __6f06a5790b0b46cf94fb3cc3cc0cc2d3
11
12#include <fmt/format.h>
13#include <fmt/ranges.h>
14#include <spdlog/formatter.h>
15#include <spdlog/logger.h>
16
17#include <Eigen/Core>
18#include <memory>
19
20namespace lf::base {
21
47std::shared_ptr<spdlog::logger> InitLogger(const std::string& name);
48
49// clang-format off
74// clang-format on
75class LineFeedFormatter final : public spdlog::formatter {
76 public:
77 explicit LineFeedFormatter(
78 std::unique_ptr<spdlog::formatter> wrapped_formatter);
79
84 ~LineFeedFormatter() override = default;
85
86 void format(const spdlog::details::log_msg& msg,
87 spdlog::memory_buf_t& dest) override;
88
89 [[nodiscard]] std::unique_ptr<formatter> clone() const override;
90
91 private:
92 std::unique_ptr<spdlog::formatter> wrapped_formatter_;
93};
94
95namespace internal {
96template <class MATRIX, typename = std::enable_if_t<std::is_base_of_v<
97 Eigen::DenseBase<MATRIX>, MATRIX>>>
98using enable_if_eigen = MATRIX;
99} // namespace internal
100
101} // namespace lf::base
102
104
105// The following is needed to prohibit fmt to treat Eigen matrices/arrays as
106// ranges.
107template <class MATRIX>
108struct fmt::is_range<lf::base::internal::enable_if_eigen<MATRIX>, char> {
109 static FMT_CONSTEXPR_DECL const bool value = false;
110};
111
116template <class MATRIX>
117struct fmt::formatter<lf::base::internal::enable_if_eigen<MATRIX>> {
118 constexpr auto parse(const format_parse_context& ctx) {
119 const auto* it = ctx.begin();
120 const auto* end = ctx.end();
121
122 if (it != end && *it != '}') {
123 throw format_error("invalid format");
124 }
125
126 return it;
127 }
128
129 template <typename FormatContext>
130 auto format(const MATRIX& m, FormatContext& ctx) {
131 std::stringstream ss;
132 ss << m.format(clean_fmt);
133
134 auto it = ctx.out();
135 auto str = ss.str();
136 std::copy(str.begin(), str.end(), it);
137 return it;
138 }
139
140 private:
141 static inline const Eigen::IOFormat clean_fmt =
142 Eigen::IOFormat(4, 0, ", ", "\n", "[", "]");
143};
145
146#endif // __6f06a5790b0b46cf94fb3cc3cc0cc2d3
A spdlog formatter which wraps another formatter and makes sure that if there are new lines (\n) in t...
Definition: spdlog_utils.h:75
LineFeedFormatter(const LineFeedFormatter &)=delete
LineFeedFormatter & operator=(const LineFeedFormatter &)=delete
LineFeedFormatter(LineFeedFormatter &&)=default
~LineFeedFormatter() override=default
LineFeedFormatter(std::unique_ptr< spdlog::formatter > wrapped_formatter)
Definition: spdlog_utils.cc:23
std::unique_ptr< spdlog::formatter > wrapped_formatter_
Definition: spdlog_utils.h:92
LineFeedFormatter & operator=(LineFeedFormatter &&)=default
void format(const spdlog::details::log_msg &msg, spdlog::memory_buf_t &dest) override
Definition: spdlog_utils.cc:27
std::unique_ptr< formatter > clone() const override
Definition: spdlog_utils.cc:66
Contains basic functionality that is used by other parts of LehrFEM++.
Definition: base.h:15
std::shared_ptr< spdlog::logger > InitLogger(const std::string &name)
Create a spdlog logger, register it in the spdlog registry and initialize it with LehrFEM++ specific ...
Definition: spdlog_utils.cc:16
Definition: assemble.h:30