Skip to content

Commit

Permalink
Fix logger singleton (#736)
Browse files Browse the repository at this point in the history
* Fix logger singleton
* Fix includes without G2O_HAVE_LOGGING

Fix #732
  • Loading branch information
RainerKuemmerle authored Nov 12, 2023
1 parent 41b9d9f commit 7023b80
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
13 changes: 11 additions & 2 deletions g2o/stuff/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,31 @@

#include "logger.h"

#include <string_view>

#include "g2o/config.h"

#ifdef G2O_HAVE_LOGGING
#include <spdlog/cfg/env.h>
#include <spdlog/common.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h>

#include <cassert>

namespace g2o::internal {

constexpr std::string_view kLoggerName = "g2o";

LoggerInterface::LoggerInterface() {
spdlog::cfg::load_env_levels();
console_ = spdlog::stdout_color_mt("g2o");
console_ = spdlog::get(std::string(kLoggerName));
if (console_) return;
console_ = spdlog::stdout_color_mt(std::string(kLoggerName));
console_->set_pattern("%+");
}

LoggerInterface::~LoggerInterface() { spdlog::drop("g2o"); }
LoggerInterface::~LoggerInterface() { spdlog::drop(std::string(kLoggerName)); }

} // namespace g2o::internal
#endif
Expand All @@ -54,6 +61,8 @@ void setLevel(Level level) {
#ifdef G2O_HAVE_LOGGING
auto toSpdLogLevel = [](Level level) {
switch (level) {
case Level::kTrace:
return spdlog::level::trace;
case Level::kDebug:
return spdlog::level::debug;
case Level::kInfo:
Expand Down
17 changes: 11 additions & 6 deletions g2o/stuff/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ using Logger = internal::Singleton<internal::LoggerInterface>;
} // namespace g2o

// TODO(Rainer): Switch to std::source_location with c++20
#define G2O_TRACE(...) \
g2o::Logger::get().console().log( \
spdlog::source_loc(__FILE__, __LINE__, __FUNCTION__), \
spdlog::level::trace, __VA_ARGS__)
#define G2O_DEBUG(...) \
g2o::Logger::get().console().log( \
spdlog::source_loc(__FILE__, __LINE__, __FUNCTION__), \
Expand All @@ -102,16 +106,17 @@ using Logger = internal::Singleton<internal::LoggerInterface>;

#else

#define G2O_DEBUG(...)
#define G2O_INFO(...)
#define G2O_WARN(...)
#define G2O_ERROR(...)
#define G2O_CRITICAL(...)
#define G2O_TRACE(...) (void)0
#define G2O_DEBUG(...) (void)0
#define G2O_INFO(...) (void)0
#define G2O_WARN(...) (void)0
#define G2O_ERROR(...) (void)0
#define G2O_CRITICAL(...) (void)0

#endif // G2O_HAVE_LOGGING

namespace g2o::logging {
enum class Level { kDebug, kInfo, kWarn, kError, kOff };
enum class Level { kTrace, kDebug, kInfo, kWarn, kError, kOff };
/**
* @brief Set the Log Level
*
Expand Down

0 comments on commit 7023b80

Please sign in to comment.