Skip to content

Commit

Permalink
Additional local changes
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Carroll <[email protected]>
  • Loading branch information
mjcarroll committed Jul 3, 2024
1 parent 74cb85b commit 53e9436
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 22 deletions.
38 changes: 34 additions & 4 deletions include/gz/common/ConsoleNew.hh
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
#include <gz/utils/SuppressWarning.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 @@ -42,9 +44,15 @@ namespace gz::common
/// \brief Set the console output color mode
public: void set_color_mode(spdlog::color_mode mode);

/// \brief Set the log destnation filename
public: void set_log_destination(const std::string &filename);

/// \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();

Expand Down Expand Up @@ -73,17 +81,39 @@ namespace gz::common
/// \brief Underlying stream
private: std::ostringstream ss;
};



} // namespace gz::common

#define gzcrit (gz::common::LogMessage(__FILE__, __LINE__, spdlog::level::critical).stream())
#define gzcrit gz::common::LogMessage(__FILE__, __LINE__, spdlog::level::critical).stream()
#define gzerr gz::common::LogMessage(__FILE__, __LINE__, spdlog::level::err).stream()
#define gzwarn gz::common::LogMessage(__FILE__, __LINE__, spdlog::level::warn).stream()
#define gzlog gz::common::LogMessage(__FILE__, __LINE__, spdlog::level::info).stream()
#define gzmsg gz::common::LogMessage(__FILE__, __LINE__, spdlog::level::info).stream()
#define gzdbg gz::common::LogMessage(__FILE__, __LINE__, spdlog::level::debug).stream()
#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().set_log_destination(logPath);
spdlog::set_default_logger(root.LoggerPtr());
}

#endif // GZ_COMMON_CONSOLENEW_HH_
41 changes: 32 additions & 9 deletions src/ConsoleNew.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
#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 Custom log sink that routes to stdout/stderr in Gazebo conventions
class gz_split_sink : public spdlog::sinks::sink {
public:
~gz_split_sink() override = default;
Expand Down Expand Up @@ -94,26 +96,32 @@ std::ostream& LogMessage::stream()
class ConsoleNew::Implementation
{
public:
std::shared_ptr<gz_split_sink> console_sink {nullptr};
explicit Implementation(const std::string &logger_name):
console_sink(std::make_shared<gz_split_sink>()),
sinks(std::make_shared<spdlog::sinks::dist_sink_mt>()),
logger(std::make_shared<spdlog::logger>(logger_name, sinks))
{
}

std::shared_ptr<gz_split_sink> console_sink;

std::shared_ptr<spdlog::sinks::basic_file_sink_mt> file_sink {nullptr};

std::shared_ptr<spdlog::sinks::dist_sink_mt> sinks {nullptr};

std::shared_ptr<spdlog::logger> logger {nullptr};
};

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

// 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 @@ -123,11 +131,26 @@ void ConsoleNew::set_color_mode(spdlog::color_mode mode)
this->dataPtr->console_sink->set_color_mode(mode);
}

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

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

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

ConsoleNew& ConsoleNew::Root()
{
static gz::utils::NeverDestroyed<ConsoleNew> root{"gz"};
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 53e9436

Please sign in to comment.