mirror of
				https://github.com/XRPLF/clio.git
				synced 2025-11-04 11:55:51 +00:00 
			
		
		
		
	refactor: Refactor Logger.hpp and LoggerFixtures.hpp (#2356)
This commit is contained in:
		@@ -96,7 +96,6 @@ public:
 | 
			
		||||
}  // namespace
 | 
			
		||||
 | 
			
		||||
Logger LogService::generalLog = Logger{"General"};
 | 
			
		||||
Logger LogService::alertLog = Logger{"Alert"};
 | 
			
		||||
boost::log::filter LogService::filter{};
 | 
			
		||||
 | 
			
		||||
std::ostream&
 | 
			
		||||
@@ -202,7 +201,6 @@ LogService::init(config::ClioConfigDefinition const& config)
 | 
			
		||||
    std::unordered_map<std::string, Severity> minSeverity;
 | 
			
		||||
    for (auto const& channel : Logger::kCHANNELS)
 | 
			
		||||
        minSeverity[channel] = defaultSeverity;
 | 
			
		||||
    minSeverity["Alert"] = Severity::WRN;  // Channel for alerts, always warning severity
 | 
			
		||||
 | 
			
		||||
    auto const overrides = config.getArray("log_channels");
 | 
			
		||||
 | 
			
		||||
@@ -243,32 +241,32 @@ Logger::Pump
 | 
			
		||||
Logger::trace(SourceLocationType const& loc) const
 | 
			
		||||
{
 | 
			
		||||
    return {logger_, Severity::TRC, loc};
 | 
			
		||||
};
 | 
			
		||||
}
 | 
			
		||||
Logger::Pump
 | 
			
		||||
Logger::debug(SourceLocationType const& loc) const
 | 
			
		||||
{
 | 
			
		||||
    return {logger_, Severity::DBG, loc};
 | 
			
		||||
};
 | 
			
		||||
}
 | 
			
		||||
Logger::Pump
 | 
			
		||||
Logger::info(SourceLocationType const& loc) const
 | 
			
		||||
{
 | 
			
		||||
    return {logger_, Severity::NFO, loc};
 | 
			
		||||
};
 | 
			
		||||
}
 | 
			
		||||
Logger::Pump
 | 
			
		||||
Logger::warn(SourceLocationType const& loc) const
 | 
			
		||||
{
 | 
			
		||||
    return {logger_, Severity::WRN, loc};
 | 
			
		||||
};
 | 
			
		||||
}
 | 
			
		||||
Logger::Pump
 | 
			
		||||
Logger::error(SourceLocationType const& loc) const
 | 
			
		||||
{
 | 
			
		||||
    return {logger_, Severity::ERR, loc};
 | 
			
		||||
};
 | 
			
		||||
}
 | 
			
		||||
Logger::Pump
 | 
			
		||||
Logger::fatal(SourceLocationType const& loc) const
 | 
			
		||||
{
 | 
			
		||||
    return {logger_, Severity::FTL, loc};
 | 
			
		||||
};
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
std::string
 | 
			
		||||
Logger::Pump::prettyPath(SourceLocationType const& loc, size_t maxDepth)
 | 
			
		||||
 
 | 
			
		||||
@@ -269,7 +269,6 @@ public:
 | 
			
		||||
 */
 | 
			
		||||
class LogService {
 | 
			
		||||
    static Logger generalLog; /*< Global logger for General channel */
 | 
			
		||||
    static Logger alertLog;   /*< Global logger for Alerts channel */
 | 
			
		||||
    static boost::log::filter filter;
 | 
			
		||||
 | 
			
		||||
public:
 | 
			
		||||
@@ -356,18 +355,6 @@ public:
 | 
			
		||||
        return generalLog.fatal(loc);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @brief Globally accessible Alert logger
 | 
			
		||||
     *
 | 
			
		||||
     * @param loc The source location of the log message
 | 
			
		||||
     * @return The pump to use for logging
 | 
			
		||||
     */
 | 
			
		||||
    [[nodiscard]] static Logger::Pump
 | 
			
		||||
    alert(SourceLocationType const& loc = CURRENT_SRC_LOCATION)
 | 
			
		||||
    {
 | 
			
		||||
        return alertLog.warn(loc);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @brief Whether the LogService is enabled or not
 | 
			
		||||
     *
 | 
			
		||||
 
 | 
			
		||||
@@ -2,14 +2,15 @@ add_library(clio_testing_common)
 | 
			
		||||
 | 
			
		||||
target_sources(
 | 
			
		||||
  clio_testing_common
 | 
			
		||||
  PRIVATE util/MockAssert.cpp
 | 
			
		||||
          util/AssignRandomPort.cpp
 | 
			
		||||
  PRIVATE util/AssignRandomPort.cpp
 | 
			
		||||
          util/BinaryTestObject.cpp
 | 
			
		||||
          util/CallWithTimeout.cpp
 | 
			
		||||
          util/LoggerFixtures.cpp
 | 
			
		||||
          util/MockAssert.cpp
 | 
			
		||||
          util/StringUtils.cpp
 | 
			
		||||
          util/TestHttpClient.cpp
 | 
			
		||||
          util/TestHttpServer.cpp
 | 
			
		||||
          util/TestObject.cpp
 | 
			
		||||
          util/BinaryTestObject.cpp
 | 
			
		||||
          util/TestWebSocketClient.cpp
 | 
			
		||||
          util/TestWsServer.cpp
 | 
			
		||||
)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										59
									
								
								tests/common/util/LoggerFixtures.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										59
									
								
								tests/common/util/LoggerFixtures.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,59 @@
 | 
			
		||||
//------------------------------------------------------------------------------
 | 
			
		||||
/*
 | 
			
		||||
    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.
 | 
			
		||||
*/
 | 
			
		||||
//==============================================================================
 | 
			
		||||
 | 
			
		||||
#include "util/LoggerFixtures.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>
 | 
			
		||||
 | 
			
		||||
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");
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    namespace keywords = boost::log::keywords;
 | 
			
		||||
    namespace expr = boost::log::expressions;
 | 
			
		||||
    auto core = boost::log::core::get();
 | 
			
		||||
 | 
			
		||||
    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);
 | 
			
		||||
 | 
			
		||||
    std::ranges::for_each(util::Logger::kCHANNELS, [&minSeverity](char const* channel) {
 | 
			
		||||
        minSeverity[channel] = util::Severity::TRC;
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    minSeverity["General"] = util::Severity::DBG;
 | 
			
		||||
    minSeverity["Trace"] = util::Severity::TRC;
 | 
			
		||||
 | 
			
		||||
    core->set_filter(minSeverity);
 | 
			
		||||
    core->set_logging_enabled(true);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
NoLoggerFixture::NoLoggerFixture()
 | 
			
		||||
{
 | 
			
		||||
    boost::log::core::get()->set_logging_enabled(false);
 | 
			
		||||
}
 | 
			
		||||
@@ -21,12 +21,6 @@
 | 
			
		||||
 | 
			
		||||
#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 <gtest/gtest.h>
 | 
			
		||||
 | 
			
		||||
#include <algorithm>
 | 
			
		||||
@@ -59,32 +53,7 @@ class LoggerFixture : virtual public ::testing::Test {
 | 
			
		||||
 | 
			
		||||
public:
 | 
			
		||||
    // Simulates the `util::Logger::init(config)` call
 | 
			
		||||
    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");
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        namespace keywords = boost::log::keywords;
 | 
			
		||||
        namespace expr = boost::log::expressions;
 | 
			
		||||
        auto core = boost::log::core::get();
 | 
			
		||||
 | 
			
		||||
        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);
 | 
			
		||||
 | 
			
		||||
        std::ranges::for_each(util::Logger::kCHANNELS, [&minSeverity](char const* channel) {
 | 
			
		||||
            minSeverity[channel] = util::Severity::TRC;
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        minSeverity["General"] = util::Severity::DBG;
 | 
			
		||||
        minSeverity["Trace"] = util::Severity::TRC;
 | 
			
		||||
 | 
			
		||||
        core->set_filter(minSeverity);
 | 
			
		||||
        core->set_logging_enabled(true);
 | 
			
		||||
    }
 | 
			
		||||
    LoggerFixture();
 | 
			
		||||
 | 
			
		||||
protected:
 | 
			
		||||
    void
 | 
			
		||||
@@ -113,8 +82,5 @@ protected:
 | 
			
		||||
 * This is meant to be used as a base for other fixtures.
 | 
			
		||||
 */
 | 
			
		||||
struct NoLoggerFixture : virtual LoggerFixture {
 | 
			
		||||
    NoLoggerFixture()
 | 
			
		||||
    {
 | 
			
		||||
        boost::log::core::get()->set_logging_enabled(false);
 | 
			
		||||
    }
 | 
			
		||||
    NoLoggerFixture();
 | 
			
		||||
};
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user