mirror of
https://github.com/XRPLF/clio.git
synced 2025-11-04 11:55:51 +00:00
fix: Drop dynamic loggers to fix memory leak (#2686)
This commit is contained in:
@@ -35,7 +35,7 @@ LoggerFixture::init()
|
||||
{
|
||||
util::LogServiceState::init(false, util::Severity::FTL, {});
|
||||
|
||||
std::ranges::for_each(util::Logger::kCHANNELS, [](char const* channel) {
|
||||
std::ranges::for_each(util::Logger::kCHANNELS, [](std::string_view const channel) {
|
||||
util::LogService::registerLogger(channel);
|
||||
});
|
||||
|
||||
|
||||
@@ -31,6 +31,7 @@
|
||||
#include <optional>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
using namespace util::config;
|
||||
|
||||
@@ -164,7 +165,7 @@ TEST_F(ConstraintTest, SetValuesOnPortConstraint)
|
||||
|
||||
TEST_F(ConstraintTest, OneOfConstraintOneValue)
|
||||
{
|
||||
std::array<char const*, 1> const arr = {"tracer"};
|
||||
std::array<std::string_view, 1> const arr = {"tracer"};
|
||||
auto const databaseConstraint{OneOf{"database.type", arr}};
|
||||
EXPECT_FALSE(databaseConstraint.checkConstraint("tracer").has_value());
|
||||
|
||||
@@ -180,7 +181,7 @@ TEST_F(ConstraintTest, OneOfConstraintOneValue)
|
||||
|
||||
TEST_F(ConstraintTest, OneOfConstraint)
|
||||
{
|
||||
std::array<char const*, 3> const arr = {"123", "trace", "haha"};
|
||||
std::array<std::string_view, 3> const arr = {"123", "trace", "haha"};
|
||||
auto const oneOfCons{OneOf{"log.level", arr}};
|
||||
|
||||
EXPECT_FALSE(oneOfCons.checkConstraint("trace").has_value());
|
||||
|
||||
@@ -101,7 +101,7 @@ TEST_F(LogServiceInitTests, DefaultLogLevel)
|
||||
EXPECT_TRUE(LogService::init(config_));
|
||||
|
||||
std::string const logString = "some log";
|
||||
for (auto const& channel : Logger::kCHANNELS) {
|
||||
for (std::string_view const channel : Logger::kCHANNELS) {
|
||||
Logger const log{channel};
|
||||
log.trace() << logString;
|
||||
auto loggerStr = getLoggerString();
|
||||
|
||||
@@ -21,11 +21,23 @@
|
||||
#include "util/log/Logger.hpp"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <spdlog/logger.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
using namespace util;
|
||||
|
||||
namespace {
|
||||
size_t
|
||||
loggersNum()
|
||||
{
|
||||
size_t counter = 0;
|
||||
spdlog::apply_all([&counter](std::shared_ptr<spdlog::logger>) { ++counter; });
|
||||
return counter;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
// Used as a fixture for tests with enabled logging
|
||||
class LoggerTest : public LoggerFixture {};
|
||||
|
||||
@@ -71,3 +83,24 @@ TEST_F(LoggerTest, LOGMacro)
|
||||
EXPECT_TRUE(computeCalled);
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST_F(LoggerTest, ManyDynamicLoggers)
|
||||
{
|
||||
static constexpr size_t kNUM_LOGGERS = 10'000;
|
||||
|
||||
auto initialLoggers = loggersNum();
|
||||
|
||||
for (size_t i = 0; i < kNUM_LOGGERS; ++i) {
|
||||
std::string const loggerName = "DynamicLogger" + std::to_string(i);
|
||||
|
||||
Logger log{loggerName};
|
||||
log.info() << "Logger number " << i;
|
||||
ASSERT_EQ(getLoggerString(), "inf:" + loggerName + " - Logger number " + std::to_string(i) + "\n");
|
||||
|
||||
Logger copy = log;
|
||||
copy.info() << "Copy of logger number " << i;
|
||||
ASSERT_EQ(getLoggerString(), "inf:" + loggerName + " - Copy of logger number " + std::to_string(i) + "\n");
|
||||
}
|
||||
|
||||
ASSERT_EQ(loggersNum(), initialLoggers);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user