Loading [MathJax]/extensions/tex2jax.js
PolyFEM
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Logger.cpp
Go to the documentation of this file.
1#include "Logger.hpp"
3#include <spdlog/sinks/stdout_color_sinks.h>
5
6#include <sstream>
7
8namespace polyfem
9{
10 namespace
11 {
12
13 // Custom logger instance defined by the user, if any
14 std::shared_ptr<spdlog::logger> &get_shared_logger()
15 {
16 static std::shared_ptr<spdlog::logger> logger;
17 return logger;
18 }
19
20 // Custom logger instance defined by the user, if any
21 std::shared_ptr<spdlog::logger> &get_shared_adjoint_logger()
22 {
23 static std::shared_ptr<spdlog::logger> logger;
24 return logger;
25 }
26
27 } // namespace
28
29 // Retrieve current logger
30 spdlog::logger &adjoint_logger()
31 {
32 if (get_shared_adjoint_logger())
33 {
34 return *get_shared_adjoint_logger();
35 }
36 else
37 {
38 static std::shared_ptr<spdlog::logger> default_logger = spdlog::stdout_color_mt("adjoint-polyfem");
39 return *default_logger;
40 }
41 }
42
43 // Retrieve current logger
44 spdlog::logger &logger()
45 {
46 if (get_shared_logger())
47 {
48 return *get_shared_logger();
49 }
50 else
51 {
52 // When using factory methods provided by spdlog (_st and _mt functions),
53 // names must be unique, since the logger is registered globally.
54 // Otherwise, you will need to create the logger manually. See
55 // https://github.com/gabime/spdlog/wiki/2.-Creating-loggers
56 static std::shared_ptr<spdlog::logger> default_logger = spdlog::stdout_color_mt("polyfem");
57 return *default_logger;
58 }
59 }
60
61 // Use a custom logger
62 void set_logger(std::shared_ptr<spdlog::logger> p_logger)
63 {
64 get_shared_logger() = std::move(p_logger);
65 }
66
67 // Use a custom logger
68 void set_adjoint_logger(std::shared_ptr<spdlog::logger> p_logger)
69 {
70 get_shared_adjoint_logger() = std::move(p_logger);
71 }
72
73 void log_and_throw_error(const std::string &msg)
74 {
75 logger().error(msg);
76 throw std::runtime_error(msg);
77 }
78
79 void log_and_throw_adjoint_error(const std::string &msg)
80 {
81 adjoint_logger().error(msg);
82 throw std::runtime_error(msg);
83 }
84} // namespace polyfem
85
86fmt::format_context::iterator fmt::formatter<polyfem::StiffnessMatrix>::format(polyfem::StiffnessMatrix const &mat, fmt::format_context &ctx) const
87{
88 std::stringstream ss;
89 ss << mat;
90 return formatter<fmt::string_view>::format(ss.str(), ctx);
91}
92
93fmt::format_context::iterator fmt::formatter<Eigen::MatrixXd>::format(const Eigen::MatrixXd &mat, fmt::format_context &ctx) const
94{
95 std::stringstream ss;
96 ss << mat;
97 return formatter<fmt::string_view>::format(ss.str(), ctx);
98}
spdlog::logger & logger()
Retrieves the current logger.
Definition Logger.cpp:44
spdlog::logger & adjoint_logger()
Retrieves the current logger for adjoint.
Definition Logger.cpp:30
void set_adjoint_logger(std::shared_ptr< spdlog::logger > p_logger)
Setup a logger object to be used by adjoint Polyfem.
Definition Logger.cpp:68
void log_and_throw_adjoint_error(const std::string &msg)
Definition Logger.cpp:79
void set_logger(std::shared_ptr< spdlog::logger > p_logger)
Setup a logger object to be used by Polyfem.
Definition Logger.cpp:62
void log_and_throw_error(const std::string &msg)
Definition Logger.cpp:73
Eigen::SparseMatrix< double, Eigen::ColMajor > StiffnessMatrix
Definition Types.hpp:22