mirror of
https://github.com/XRPLF/clio.git
synced 2025-12-06 17:27:58 +00:00
feat: Use spdlog logger (#2372)
This commit is contained in:
@@ -21,44 +21,40 @@
|
||||
|
||||
#include "util/log/Logger.hpp"
|
||||
|
||||
#include <boost/log/core/core.hpp>
|
||||
#include <boost/log/expressions/predicates/channel_severity_filter.hpp>
|
||||
#include <boost/log/keywords/format.hpp>
|
||||
#include <boost/log/utility/setup/common_attributes.hpp>
|
||||
#include <boost/log/utility/setup/console.hpp>
|
||||
#include <boost/log/utility/setup/formatter_parser.hpp>
|
||||
#include <spdlog/common.h>
|
||||
#include <spdlog/sinks/ostream_sink.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <mutex>
|
||||
#include <memory>
|
||||
|
||||
LoggerFixture::LoggerFixture()
|
||||
{
|
||||
static std::once_flag kONCE;
|
||||
std::call_once(kONCE, [] {
|
||||
boost::log::add_common_attributes();
|
||||
boost::log::register_simple_formatter_factory<util::Severity, char>("Severity");
|
||||
// Clear any existing loggers
|
||||
spdlog::drop_all();
|
||||
|
||||
// Create ostream sink for testing
|
||||
auto ostreamSink = std::make_shared<spdlog::sinks::ostream_sink_mt>(stream_);
|
||||
|
||||
// Create loggers for each channel
|
||||
std::ranges::for_each(util::Logger::kCHANNELS, [&ostreamSink](char const* channel) {
|
||||
auto logger = std::make_shared<spdlog::logger>(channel, ostreamSink);
|
||||
logger->set_level(spdlog::level::trace);
|
||||
spdlog::register_logger(logger);
|
||||
});
|
||||
|
||||
namespace keywords = boost::log::keywords;
|
||||
namespace expr = boost::log::expressions;
|
||||
auto core = boost::log::core::get();
|
||||
spdlog::get("General")->set_level(spdlog::level::debug);
|
||||
|
||||
core->remove_all_sinks();
|
||||
boost::log::add_console_log(stream_, keywords::format = "%Channel%:%Severity% %Message%");
|
||||
auto minSeverity = expr::channel_severity_filter(util::LogChannel, util::LogSeverity);
|
||||
auto traceLogger = std::make_shared<spdlog::logger>("Trace", ostreamSink);
|
||||
traceLogger->set_level(spdlog::level::trace);
|
||||
spdlog::register_logger(traceLogger);
|
||||
|
||||
std::ranges::for_each(util::Logger::kCHANNELS, [&minSeverity](char const* channel) {
|
||||
minSeverity[channel] = util::Severity::TRC;
|
||||
});
|
||||
spdlog::set_default_logger(spdlog::get("General"));
|
||||
|
||||
minSeverity["General"] = util::Severity::DBG;
|
||||
minSeverity["Trace"] = util::Severity::TRC;
|
||||
|
||||
core->set_filter(minSeverity);
|
||||
core->set_logging_enabled(true);
|
||||
spdlog::set_pattern("%^%3!l:%n%$ - %v");
|
||||
}
|
||||
|
||||
NoLoggerFixture::NoLoggerFixture()
|
||||
{
|
||||
boost::log::core::get()->set_logging_enabled(false);
|
||||
spdlog::set_level(spdlog::level::off);
|
||||
}
|
||||
|
||||
@@ -19,56 +19,25 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "util/log/Logger.hpp"
|
||||
#include "util/StringBuffer.hpp"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <mutex>
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
/**
|
||||
* @brief Fixture with util::Logger support.
|
||||
* @brief A fixture for testing LogService and Logger.
|
||||
*/
|
||||
class LoggerFixture : virtual public ::testing::Test {
|
||||
/**
|
||||
* @brief A simple string buffer that can be used to mock std::cout for
|
||||
* console logging.
|
||||
*/
|
||||
class FakeBuffer final : public std::stringbuf {
|
||||
public:
|
||||
std::string
|
||||
getStrAndReset()
|
||||
{
|
||||
auto value = str();
|
||||
str("");
|
||||
return value;
|
||||
}
|
||||
};
|
||||
|
||||
FakeBuffer buffer_;
|
||||
StringBuffer buffer_;
|
||||
std::ostream stream_ = std::ostream{&buffer_};
|
||||
|
||||
public:
|
||||
// Simulates the `util::Logger::init(config)` call
|
||||
// Simulates the `util::LogService::init(config)` call
|
||||
LoggerFixture();
|
||||
|
||||
protected:
|
||||
void
|
||||
checkEqual(std::string expected)
|
||||
{
|
||||
auto value = buffer_.getStrAndReset();
|
||||
ASSERT_EQ(value, expected + '\n') << "Got: " << value;
|
||||
}
|
||||
|
||||
void
|
||||
checkEmpty()
|
||||
{
|
||||
ASSERT_TRUE(buffer_.getStrAndReset().empty());
|
||||
}
|
||||
|
||||
std::string
|
||||
getLoggerString()
|
||||
{
|
||||
|
||||
37
tests/common/util/StringBuffer.hpp
Normal file
37
tests/common/util/StringBuffer.hpp
Normal file
@@ -0,0 +1,37 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of clio: https://github.com/XRPLF/clio
|
||||
Copyright (c) 2025, the clio developers.
|
||||
|
||||
Permission to use, copy, modify, and distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
/**
|
||||
* @brief A simple string buffer that can be used to mock std::cout for console logging.
|
||||
*/
|
||||
class StringBuffer final : public std::stringbuf {
|
||||
public:
|
||||
std::string
|
||||
getStrAndReset()
|
||||
{
|
||||
auto value = str();
|
||||
str("");
|
||||
return value;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user