LehrFEM++ 1.0.0
A simple Finite Element Library for teaching
Loading...
Searching...
No Matches
spdlog_utils.h
1
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
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
109template <class MATRIX>
110struct fmt::formatter<lf::base::internal::enable_if_eigen<MATRIX>> {
111 constexpr auto parse(const format_parse_context& ctx) {
112 const auto* it = ctx.begin();
113 const auto* end = ctx.end();
114
115 if (it != end && *it != '}') {
116 throw format_error("invalid format");
117 }
118
119 return it;
120 }
121
122 template <typename FormatContext>
123 auto format(const MATRIX& matrix, FormatContext& ctx) {
124 std::stringstream ss; // NOLINT(misc-const-correctness)
125 ss << matrix.format(clean_fmt);
126
127 auto it = ctx.out();
128 auto str = ss.str();
129 std::copy(str.begin(), str.end(), it);
130 return it;
131 }
132
133 private:
134 static inline const Eigen::IOFormat clean_fmt =
135 Eigen::IOFormat(4, 0, ", ", "\n", "[", "]");
136};
138
139#endif // INCG6f06a5790b0b46cf94fb3cc3cc0cc2d3
A spdlog formatter which wraps another formatter and makes sure that if there are new lines (\n) in t...
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:31