Wildmeshing Toolkit
Logger.cpp
Go to the documentation of this file.
1 #include <wmtk/utils/Logger.hpp>
2 
3 #include <spdlog/sinks/stdout_color_sinks.h>
4 #include <spdlog/spdlog.h>
5 
6 #include <sstream>
7 
8 namespace wmtk {
10  const char* val = std::getenv("WMTK_LOGGER_LEVEL");
11  if(val == nullptr) {
12  return false;
13  }
14  std::string env_val = val;
15  return !env_val.empty();
16 }
17 
18 namespace {
19 inline void load_env_levels(spdlog::logger& logger)
20 {
21  const char* val = std::getenv("WMTK_LOGGER_LEVEL");
22  if (val == nullptr) {
23  return;
24  }
25  std::string env_val = val;
26  if (!env_val.empty()) {
27  auto level = spdlog::level::from_str(env_val);
28  if (level == spdlog::level::off) {
29  if (env_val != "off") {
30  // we cannot call our logger here because it could be off!
31  spdlog::warn(
32  "Unknown logger level due to env value WMTK_LOGGER_LEVEL={}!",
33  env_val);
34  }
35  }
36  logger.set_level(level);
37  logger.flush_on(level);
38  }
39 }
40 
41 // Custom logger instance defined by the user, if any
42 std::shared_ptr<spdlog::logger>& get_shared_logger()
43 {
44  static std::shared_ptr<spdlog::logger> logger;
45  return logger;
46 }
47 
48 // Custom logger instance defined by the user, if any
49 std::shared_ptr<spdlog::logger>& get_shared_opt_logger()
50 {
51  static std::shared_ptr<spdlog::logger> logger;
52  return logger;
53 }
54 
55 } // namespace
56 
57 // Retrieve current logger
59 {
60  if (auto l = get_shared_logger(); bool(l)) {
61  return *l;
62  } else {
63  // When using factory methods provided by spdlog (_st and _mt functions),
64  // names must be unique, since the logger is registered globally.
65  // Otherwise, you will need to create the logger manually. See
66  // https://github.com/gabime/spdlog/wiki/2.-Creating-loggers
67  static auto default_logger = spdlog::stdout_color_mt("wmtk");
68  load_env_levels(*default_logger);
69  set_logger(default_logger);
70  return *default_logger;
71  }
72 }
73 
74 // Retrieve current logger
76 {
77  if (get_shared_opt_logger()) {
78  return *get_shared_opt_logger();
79  } else {
80  // When using factory methods provided by spdlog (_st and _mt functions),
81  // names must be unique, since the logger is registered globally.
82  // Otherwise, you will need to create the logger manually. See
83  // https://github.com/gabime/spdlog/wiki/2.-Creating-loggers
84  static auto default_logger = spdlog::stdout_color_mt("wmtk-opt");
85  return *default_logger;
86  }
87 }
88 
89 // Use a custom logger
90 void set_logger(std::shared_ptr<spdlog::logger> x)
91 {
92  get_shared_logger() = std::move(x);
93 }
94 
95 // Use a custom logger
96 void set_opt_logger(std::shared_ptr<spdlog::logger> x)
97 {
98  get_shared_opt_logger() = std::move(x);
99 }
100 
101 void log_and_throw_error(const std::string& msg)
102 {
103  logger().error(msg);
104  throw std::runtime_error(msg);
105 }
106 
107 } // namespace wmtk
Definition: Accessor.hpp:6
void set_opt_logger(std::shared_ptr< spdlog::logger > x)
Setup a logger object to be used by WildmeshToolkit optimization.
Definition: Logger.cpp:96
bool has_user_overloaded_logger_level()
Definition: Logger.cpp:9
void set_logger(std::shared_ptr< spdlog::logger > x)
Setup a logger object to be used by WildmeshToolkit.
Definition: Logger.cpp:90
void log_and_throw_error(const std::string &msg)
Definition: Logger.cpp:101
spdlog::logger & logger()
Retrieves the current logger.
Definition: Logger.cpp:58
spdlog::logger & opt_logger()
Retrieves the logger for the optimization.
Definition: Logger.cpp:75