Skip to content

Commit

Permalink
Merge
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Agüero <[email protected]>
  • Loading branch information
caguero committed Jul 3, 2024
2 parents 578ede7 + 53e9436 commit 55fa79d
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 32 deletions.
43 changes: 37 additions & 6 deletions include/gz/common/ConsoleNew.hh
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
#include <gz/common/Export.hh>
#include <gz/common/Util.hh>
#include <gz/utils/ImplPtr.hh>
#include "gz/common/Filesystem.hh"

#include <spdlog/logger.h>
#include <spdlog/spdlog.h>

namespace gz::common
{
Expand All @@ -44,13 +46,17 @@ namespace gz::common
///\param[in] _mode Color mode.
public: void SetColorMode(spdlog::color_mode _mode);

/// \brief Access the underlying spdlog logger.
/// \return The spdlog logger.
public: [[nodiscard]] spdlog::logger &Logger() const;
/// \brief Set the log destnation filename
public: void SetLogDestination(const std::string &_filename);

/// \brief Access the global gz console logger.
/// \return The global console logger.
public: static ConsoleNew &Root();
/// \brief Access the underlying spdlog logger
public: [[nodiscard]] spdlog::logger& Logger() const;

/// \brief Access the underlying spdlog logger, with ownership
public: [[nodiscard]] std::shared_ptr<spdlog::logger> LoggerPtr() const;

/// \brief Access the global gz console logger
public: static ConsoleNew& Root();

/// \brief Implementation Pointer.
GZ_UTILS_UNIQUE_IMPL_PTR(dataPtr)
Expand Down Expand Up @@ -100,4 +106,29 @@ namespace gz::common
#define gztrace gz::common::LogMessage( \
__FILE__, __LINE__, spdlog::level::trace).stream()

void gzLogInit(const std::string &directory, const std::string &filename)
{
auto &root = gz::common::ConsoleNew::Root();

std::string logPath;
if (!directory.empty())
{
logPath = directory;
} else if(!gz::common::env(GZ_HOMEDIR, logPath)) {
root.Logger().error("Missing HOME environment variable. No log file will be generated.");
return;
}

if(!gz::common::createDirectories(logPath))
{
root.Logger().error("Failed to create output log directory {}", logPath.c_str());
return;
}

logPath = gz::common::joinPaths(logPath, filename);
root.Logger().info("Setting log file output destination to {}", logPath.c_str());
gz::common::ConsoleNew::Root().SetLogDestination(logPath);
spdlog::set_default_logger(root.LoggerPtr());
}

#endif // GZ_COMMON_CONSOLENEW_HH_
60 changes: 43 additions & 17 deletions src/ConsoleNew.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@
#include <sstream>
#include <string>

#include <gz/common/ConsoleNew.hh>
#include <gz/common/config.hh>
#include <gz/common/ConsoleNew.hh>
#include <gz/common/Util.hh>
#include <gz/utils/NeverDestroyed.hh>

#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/dist_sink.h>

namespace {
/// \brief ToDo.
/// \brief Custom log sink that routes to stdout/stderr in Gazebo conventions
class GzSplitSink : public spdlog::sinks::sink
{
/// \brief Class destructor.
Expand Down Expand Up @@ -112,32 +114,38 @@ std::ostream& LogMessage::stream()
/// \brief Private data for the ConsoleNew class.
class ConsoleNew::Implementation
{
public:
explicit Implementation(const std::string &_loggerName):
consoleSink(std::make_shared<GzSplitSink>()),
sinks(std::make_shared<spdlog::sinks::dist_sink_mt>()),
logger(std::make_shared<spdlog::logger>(_loggerName, sinks))
{
}

/// \brief .
public: std::shared_ptr<GzSplitSink> consoleSink {nullptr};
std::shared_ptr<GzSplitSink> consoleSink;

/// \brief .
public: std::shared_ptr<spdlog::sinks::basic_file_sink_mt> fileSink {nullptr};
std::shared_ptr<spdlog::sinks::basic_file_sink_mt> fileSink {nullptr};

/// \brief .
public: std::shared_ptr<spdlog::logger> logger {nullptr};
std::shared_ptr<spdlog::sinks::dist_sink_mt> sinks {nullptr};

/// \brief .
std::shared_ptr<spdlog::logger> logger {nullptr};
};

/////////////////////////////////////////////////
ConsoleNew::ConsoleNew(const std::string &loggerName):
dataPtr(gz::utils::MakeUniqueImpl<Implementation>())
ConsoleNew::ConsoleNew(const std::string &_loggerName):
dataPtr(gz::utils::MakeUniqueImpl<Implementation>(_loggerName))
{
this->dataPtr->consoleSink = std::make_shared<GzSplitSink>();
this->dataPtr->fileSink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(
"/tmp/filename.txt", true);
this->dataPtr->logger = std::make_shared<spdlog::logger>(
spdlog::logger(loggerName,
{
this->dataPtr->fileSink, this->dataPtr->consoleSink
}));
// Add the console sink as a destination
this->dataPtr->sinks->add_sink(this->dataPtr->consoleSink);

// Configure the logger
this->dataPtr->logger->set_level(spdlog::level::trace);
this->dataPtr->logger->set_level(spdlog::level::info);
this->dataPtr->logger->flush_on(spdlog::level::err);

spdlog::flush_every(std::chrono::seconds(5));
spdlog::register_logger(this->dataPtr->logger);
}
Expand All @@ -148,12 +156,30 @@ void ConsoleNew::SetColorMode(spdlog::color_mode _mode)
this->dataPtr->consoleSink->set_color_mode(_mode);
}

/////////////////////////////////////////////////
void ConsoleNew::SetLogDestination(const std::string &_filename)
{
if (this->dataPtr->fileSink != nullptr)
{
this->dataPtr->sinks->remove_sink(this->dataPtr->fileSink);
}
this->dataPtr->fileSink =
std::make_shared<spdlog::sinks::basic_file_sink_mt>(_filename, true);
this->dataPtr->sinks->add_sink(this->dataPtr->fileSink);
}

/////////////////////////////////////////////////
spdlog::logger& ConsoleNew::Logger() const
{
return *this->dataPtr->logger;
}

/////////////////////////////////////////////////
std::shared_ptr<spdlog::logger> ConsoleNew::LoggerPtr() const
{
return this->dataPtr->logger;
}

/////////////////////////////////////////////////
ConsoleNew& ConsoleNew::Root()
{
Expand Down
38 changes: 29 additions & 9 deletions src/ConsoleNew_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@
#include <spdlog/spdlog.h>

class ConsoleNew_TEST : public ::testing::Test {
protected: virtual void SetUp()
protected: void SetUp() override
{
this->temp = std::make_unique<gz::common::TempDirectory>(
"test", "gz_common", true);
"test", "gz_common", false);
ASSERT_TRUE(this->temp->Valid());
gz::common::setenv(GZ_HOMEDIR, this->temp->Path());
}

/// \brief Clear out all the directories we produced during this test.
public: virtual void TearDown()
public: void TearDown() override
{
EXPECT_TRUE(gz::common::unsetenv(GZ_HOMEDIR));
}
Expand Down Expand Up @@ -89,10 +89,30 @@ TEST_F(ConsoleNew_TEST, RootLoggerNoColor)

TEST_F(ConsoleNew_TEST, RootLoggerMacros)
{
gztrace << "This is a trace message";
gzdbg << "This is a debug message";
gzmsg << "This is an info message";
gzwarn << "This is a warning message";
gzerr << "This is an error message";
gzcrit << "This is a critical message";
gztrace << "This is a trace message";
gzdbg << "This is a debug message";
gzmsg << "This is an info message";
gzwarn << "This is a warning message";
gzerr << "This is an error message";
gzcrit << "This is a critical message";
}

TEST_F(ConsoleNew_TEST, LogToFile)
{
gzLogInit("", "test.log");

gztrace << "This is a trace message";
gzdbg << "This is a debug message";
gzmsg << "This is an info message";
gzwarn << "This is a warning message";
gzerr << "This is an error message";
gzcrit << "This is a critical message";

// gzLogInit installs a global handler
spdlog::trace("This is a trace message");
spdlog::debug("This is a debug message");
spdlog::info("This is an info message");
spdlog::warn("This is a warning message");
spdlog::error("This is an error message");
spdlog::critical("This is a critical message");
}

0 comments on commit 55fa79d

Please sign in to comment.