6 #include <knowrob/Logger.h>
7 #include <spdlog/sinks/stdout_color_sinks.h>
8 #include <spdlog/sinks/rotating_file_sink.h>
9 #include "knowrob/integration/python/utils.h"
10 #include "knowrob/URI.h"
15 std::shared_ptr<spdlog::sinks::stdout_color_sink_mt> console_sink;
16 std::shared_ptr<spdlog::sinks::rotating_file_sink_mt> file_sink;
17 std::shared_ptr<spdlog::logger> logger;
20 : console_sink(std::make_shared<spdlog::sinks::stdout_color_sink_mt>()) {}
24 : pimpl_{std::make_unique<impl>()} {
30 pimpl_->logger = std::make_shared<spdlog::logger>(
31 "multi_sink", spdlog::sinks_init_list({
pimpl_->console_sink,
pimpl_->file_sink}));
33 pimpl_->logger = std::make_shared<spdlog::logger>(
34 "multi_sink", spdlog::sinks_init_list({
pimpl_->console_sink}));
35 spdlog::set_default_logger(
pimpl_->logger);
36 spdlog::set_level(spdlog::level::trace);
37 spdlog::flush_every(std::chrono::seconds(2));
50 static std::filesystem::path getLoggingPath(
const boost::optional<std::string> &logDir) {
51 if (logDir.has_value()) {
52 return logDir.value();
54 std::vector<std::filesystem::path> consideredPaths;
57 if (homePath.has_value()) {
58 consideredPaths.push_back(std::filesystem::path(homePath.value()) /
".knowrob" /
"logs");
60 consideredPaths.push_back(std::filesystem::temp_directory_path() /
"knowrob" /
"logs");
61 consideredPaths.push_back(std::filesystem::current_path());
63 for (
const auto &path : consideredPaths) {
64 if (!std::filesystem::exists(path)) {
65 if (!std::filesystem::create_directories(path)) {
69 if (!std::filesystem::is_directory(path))
continue;
72 return std::filesystem::current_path();
77 auto consoleConfig = config.get_child_optional(
"console-sink");
79 auto level = consoleConfig.value().get_optional<
std::string>(
"level");
81 if (level.has_value()) {
89 auto fileConfig = config.get_child_optional(
"file-sink");
91 auto &fileConfig0 = fileConfig.value();
92 auto path = getLoggingPath(fileConfig0.get_optional<
std::string>(
"path"));
93 auto basename = fileConfig0.get<
std::string>(
"basename",
"knowrob.log");
95 KB_INFO(
"[KnowRob] Writing logs to {}", path.native());
98 fileConfig0.get<
bool>(
"rotate",
true),
99 fileConfig0.get<uint32_t>(
"max_size", 1048576),
100 fileConfig0.get<uint32_t>(
"max_files", 4));
102 auto level = fileConfig0.get_optional<
std::string>(
"level");
104 if (level.has_value()) {
112 auto flushInterval = config.get_optional<int64_t>(
"flush-interval");
113 if (flushInterval.has_value()) {
114 spdlog::flush_every(std::chrono::seconds(flushInterval.value()));
121 uint32_t max_files) {
123 self.pimpl_->file_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(
124 basename, max_size, max_files, rotate);
133 get().
pimpl_->console_sink->set_level(log_level);
137 get().
pimpl_->file_sink->set_level(log_level);
156 return fmt::format(R
"(Failed to execute action of type "{}")", type);
158 return fmt::format(R
"(Component "{}" failed to execute action of type "{}")", name, type);
static void setupFileSink(const std::string &basename="knowrob.log", bool rotate=true, uint32_t max_size=1048576, uint32_t max_files=4)
static void setSinkLevel(SinkType sinkType, spdlog::level::level_enum log_level)
static std::string formatGenericFailure(const std::string &name, const std::string &type)
static void loadConfiguration(boost::property_tree::ptree &config)
static void setSinkPattern(SinkType sinkType, const std::string &pattern)
std::unique_ptr< impl > pimpl_
static std::optional< std::string > getHomePath(void)
GraphTermRule & pattern()
void createType< Logger >()