PolyFEM
Loading...
Searching...
No Matches
Logger.cpp
Go to the documentation of this file.
1#include "Logger.hpp"
3#include <spdlog/sinks/stdout_color_sinks.h>
5
6namespace polyfem
7{
8 namespace
9 {
10
11 // Custom logger instance defined by the user, if any
12 std::shared_ptr<spdlog::logger> &get_shared_logger()
13 {
14 static std::shared_ptr<spdlog::logger> logger;
15 return logger;
16 }
17
18 // Custom logger instance defined by the user, if any
19 std::shared_ptr<spdlog::logger> &get_shared_adjoint_logger()
20 {
21 static std::shared_ptr<spdlog::logger> logger;
22 return logger;
23 }
24
25 } // namespace
26
27 // Retrieve current logger
28 spdlog::logger &adjoint_logger()
29 {
30 if (get_shared_adjoint_logger())
31 {
32 return *get_shared_adjoint_logger();
33 }
34 else
35 {
36 static std::shared_ptr<spdlog::logger> default_logger = spdlog::stdout_color_mt("adjoint-polyfem");
37 return *default_logger;
38 }
39 }
40
41 // Retrieve current logger
42 spdlog::logger &logger()
43 {
44 if (get_shared_logger())
45 {
46 return *get_shared_logger();
47 }
48 else
49 {
50 // When using factory methods provided by spdlog (_st and _mt functions),
51 // names must be unique, since the logger is registered globally.
52 // Otherwise, you will need to create the logger manually. See
53 // https://github.com/gabime/spdlog/wiki/2.-Creating-loggers
54 static std::shared_ptr<spdlog::logger> default_logger = spdlog::stdout_color_mt("polyfem");
55 return *default_logger;
56 }
57 }
58
59 // Use a custom logger
60 void set_logger(std::shared_ptr<spdlog::logger> p_logger)
61 {
62 get_shared_logger() = std::move(p_logger);
63 }
64
65 // Use a custom logger
66 void set_adjoint_logger(std::shared_ptr<spdlog::logger> p_logger)
67 {
68 get_shared_adjoint_logger() = std::move(p_logger);
69 }
70
71 void log_and_throw_error(const std::string &msg)
72 {
73 logger().error(msg);
74 throw std::runtime_error(msg);
75 }
76
77 void log_and_throw_adjoint_error(const std::string &msg)
78 {
79 adjoint_logger().error(msg);
80 throw std::runtime_error(msg);
81 }
82} // namespace polyfem
spdlog::logger & logger()
Retrieves the current logger.
Definition Logger.cpp:42
spdlog::logger & adjoint_logger()
Retrieves the current logger for adjoint.
Definition Logger.cpp:28
void set_adjoint_logger(std::shared_ptr< spdlog::logger > p_logger)
Setup a logger object to be used by adjoint Polyfem.
Definition Logger.cpp:66
void log_and_throw_adjoint_error(const std::string &msg)
Definition Logger.cpp:77
void set_logger(std::shared_ptr< spdlog::logger > p_logger)
Setup a logger object to be used by Polyfem.
Definition Logger.cpp:60
void log_and_throw_error(const std::string &msg)
Definition Logger.cpp:71