LehrFEM++ 1.0.0
A simple Finite Element Library for teaching
Loading...
Searching...
No Matches
spdlog_utils.h
1
8
9#ifndef INCG6f06a5790b0b46cf94fb3cc3cc0cc2d3
10#define INCG6f06a5790b0b46cf94fb3cc3cc0cc2d3
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
58std::shared_ptr<spdlog::logger> InitLogger(const std::string& name);
59
60// clang-format off
85// clang-format on
86class LineFeedFormatter final : public spdlog::formatter {
87 public:
88 explicit LineFeedFormatter(
89 std::unique_ptr<spdlog::formatter> wrapped_formatter);
90
95 ~LineFeedFormatter() override = default;
96
97 void format(const spdlog::details::log_msg& msg,
98 spdlog::memory_buf_t& dest) override;
99
100 [[nodiscard]] std::unique_ptr<formatter> clone() const override;
101
102 private:
103 std::unique_ptr<spdlog::formatter> wrapped_formatter_;
104};
105
106namespace internal {
107template <class MATRIX, typename = std::enable_if_t<std::is_base_of_v<
108 Eigen::DenseBase<MATRIX>, MATRIX>>>
109using enable_if_eigen = MATRIX;
110} // namespace internal
111
112} // namespace lf::base
113
115
120template <class MATRIX>
121struct fmt::formatter<lf::base::internal::enable_if_eigen<MATRIX>> {
122 constexpr auto parse(const format_parse_context& ctx) {
123 const auto* it = ctx.begin();
124 const auto* end = ctx.end();
125
126 if (it != end && *it != '}') {
127 throw format_error("invalid format");
128 }
129
130 return it;
131 }
132
133 template <typename FormatContext>
134 auto format(const MATRIX& matrix, FormatContext& ctx) {
135 std::stringstream ss; // NOLINT(misc-const-correctness)
136 ss << matrix.format(clean_fmt);
137
138 auto it = ctx.out();
139 auto str = ss.str();
140 std::copy(str.begin(), str.end(), it);
141 return it;
142 }
143
144 private:
145 static inline const Eigen::IOFormat clean_fmt =
146 Eigen::IOFormat(4, 0, ", ", "\n", "[", "]");
147};
149
150#endif // INCG6f06a5790b0b46cf94fb3cc3cc0cc2d3
LineFeedFormatter(const LineFeedFormatter &)=delete
LineFeedFormatter & operator=(const LineFeedFormatter &)=delete
LineFeedFormatter(LineFeedFormatter &&)=default
~LineFeedFormatter() override=default
LineFeedFormatter(std::unique_ptr< spdlog::formatter > wrapped_formatter)
std::unique_ptr< spdlog::formatter > wrapped_formatter_
LineFeedFormatter & operator=(LineFeedFormatter &&)=default
void format(const spdlog::details::log_msg &msg, spdlog::memory_buf_t &dest) override
std::unique_ptr< formatter > clone() const override
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 assemble.h:34