Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use common formatter for console and file sinks #141

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions log/include/gz/utils/log/Logger.hh
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ class GZ_UTILS_LOG_VISIBLE Logger
/// \return The spdlog logger.
public: [[nodiscard]] std::shared_ptr<spdlog::logger> RawLoggerPtr() const;

/// \brief Set the severity level of the Console sink
/// \param [in] _level Severity level
public: void SetConsoleSinkLevel(spdlog::level::level_enum _level);

/// \brief Implementation Pointer.
GZ_UTILS_UNIQUE_IMPL_PTR(dataPtr)
};
Expand Down
29 changes: 24 additions & 5 deletions log/src/Logger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@

namespace gz::utils::log
{
namespace {
/// \brief Default log format
/// Example output
constexpr std::string_view kDefaultLogFormat{"%^(%Y-%m-%d %T.%e) [%l] %v%$"};
}
/// \brief Private data for the Logger class.
class Logger::Implementation
{
Expand All @@ -33,6 +38,9 @@ class Logger::Implementation
public: explicit Implementation(const std::string &_loggerName)
: consoleSink(std::make_shared<SplitConsoleSink>()),
sinks(std::make_shared<spdlog::sinks::dist_sink_mt>()),
formatter(std::make_unique<spdlog::pattern_formatter>(
kDefaultLogFormat.data(),
spdlog::pattern_time_type::local, std::string(""))),
logger(std::make_shared<spdlog::logger>(_loggerName, sinks))
{
}
Expand All @@ -46,6 +54,9 @@ class Logger::Implementation
/// \brief A sink distribution storing multiple sinks.
std::shared_ptr<spdlog::sinks::dist_sink_mt> sinks {nullptr};

/// \brief Common formatter for both all sinks
std::unique_ptr<spdlog::pattern_formatter> formatter;

/// \brief The underlying spdlog logger.
std::shared_ptr<spdlog::logger> logger {nullptr};
};
Expand All @@ -58,13 +69,10 @@ Logger::Logger(const std::string &_loggerName)
this->dataPtr->sinks->add_sink(this->dataPtr->consoleSink);

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

// Disable eol.
auto f = std::make_unique<spdlog::pattern_formatter>(
"%+", spdlog::pattern_time_type::local, std::string(""));
this->dataPtr->logger->set_formatter(std::move(f));
this->dataPtr->logger->set_formatter(this->dataPtr->formatter->clone());
}

/////////////////////////////////////////////////
Expand All @@ -77,6 +85,8 @@ void Logger::SetLogDestination(const std::string &_filename)
{
this->dataPtr->fileSink =
std::make_shared<spdlog::sinks::basic_file_sink_mt>(_filename, true);
this->dataPtr->fileSink->set_formatter(this->dataPtr->formatter->clone());
this->dataPtr->fileSink->set_level(spdlog::level::trace);
this->dataPtr->sinks->add_sink(this->dataPtr->fileSink);
}
}
Expand All @@ -103,4 +113,13 @@ std::shared_ptr<spdlog::logger> Logger::RawLoggerPtr() const
return this->dataPtr->logger;
}

/////////////////////////////////////////////////
void Logger::SetConsoleSinkLevel(spdlog::level::level_enum _level)
{
if (this->dataPtr->consoleSink)
{
this->dataPtr->consoleSink->set_level(_level);
}
}

} // namespace gz::utils::log
Loading