PolyFEM
Loading...
Searching...
No Matches
GeogramUtils.cpp
Go to the documentation of this file.
1#include "GeogramUtils.hpp"
2
3#include <geogram/basic/logger.h>
4#include <geogram/basic/command_line.h>
5#include <geogram/basic/command_line_args.h>
6
7namespace polyfem::utils
8{
9 namespace
10 {
11 class GeoLoggerForward : public GEO::LoggerClient
12 {
13 std::shared_ptr<spdlog::logger> logger_;
14
15 public:
16 template <typename T>
17 GeoLoggerForward(T logger) : logger_(logger) {}
18
19 private:
20 std::string truncate(const std::string &msg)
21 {
22 static size_t prefix_len = GEO::CmdLine::ui_feature(" ", false).size();
23 return msg.substr(prefix_len, msg.size() - 1 - prefix_len);
24 }
25
26 protected:
27 void div(const std::string &title) override
28 {
29 logger_->trace(title.substr(0, title.size() - 1));
30 }
31
32 void out(const std::string &str) override
33 {
34 logger_->info(truncate(str));
35 }
36
37 void warn(const std::string &str) override
38 {
39 logger_->warn(truncate(str));
40 }
41
42 void err(const std::string &str) override
43 {
44 logger_->error(truncate(str));
45 }
46
47 void status(const std::string &str) override
48 {
49 // Errors and warnings are also dispatched as status by geogram, but without
50 // the "feature" header. We thus forward them as trace, to avoid duplicated
51 // logger info...
52 logger_->trace(str.substr(0, str.size() - 1));
53 }
54 };
55 } // namespace
56
58 {
59 if (initialized)
60 return;
61
62 initialized = true;
63
64#ifndef WIN32
65 setenv("GEO_NO_SIGNAL_HANDLER", "1", 1);
66#endif
67
68 GEO::initialize();
69
70 // Import standard command line arguments, and custom ones
71 GEO::CmdLine::import_arg_group("standard");
72 GEO::CmdLine::import_arg_group("pre");
73 GEO::CmdLine::import_arg_group("algo");
74 }
75
76 void GeogramUtils::set_logger(spdlog::logger &logger)
77 {
78 GEO::Logger *geo_logger = GEO::Logger::instance();
79 geo_logger->unregister_all_clients();
80 geo_logger->register_client(new GeoLoggerForward(logger.clone("geogram")));
81 geo_logger->set_pretty(false);
82 }
83
84} // namespace polyfem::utils
std::shared_ptr< spdlog::logger > logger_
void set_logger(spdlog::logger &logger)
spdlog::logger & logger()
Retrieves the current logger.
Definition Logger.cpp:42