From 698718a02a5c449b9f40f8d79da34826de71c152 Mon Sep 17 00:00:00 2001 From: Peter Chen <34582813+PeterChen13579@users.noreply.github.com> Date: Mon, 6 Jan 2025 12:37:40 -0500 Subject: [PATCH] fix: Incorrect log values in newconfig (#1807) Additional fixes for #1627 --- src/app/ClioApplication.hpp | 1 + src/util/BytesConverter.hpp | 37 +++++++++++++++++++++++ src/util/log/Logger.cpp | 7 +++-- tests/unit/CMakeLists.txt | 1 + tests/unit/util/BytesConverterTests.cpp | 40 +++++++++++++++++++++++++ 5 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 src/util/BytesConverter.hpp create mode 100644 tests/unit/util/BytesConverterTests.cpp diff --git a/src/app/ClioApplication.hpp b/src/app/ClioApplication.hpp index 95263f8bf..65b8f5f81 100644 --- a/src/app/ClioApplication.hpp +++ b/src/app/ClioApplication.hpp @@ -18,6 +18,7 @@ //============================================================================== #pragma once + #include "util/SignalsHandler.hpp" #include "util/newconfig/ConfigDefinition.hpp" diff --git a/src/util/BytesConverter.hpp b/src/util/BytesConverter.hpp new file mode 100644 index 000000000..6bb9e971b --- /dev/null +++ b/src/util/BytesConverter.hpp @@ -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 + +namespace util { + +/** + * @brief Convert megabytes to bytes + * @param mb Number of megabytes to convert + * @return The equivalent number of bytes + */ +constexpr std::uint64_t +mbToBytes(std::uint32_t mb) +{ + return mb * 1024ul * 1024ul; +} + +}; // namespace util diff --git a/src/util/log/Logger.cpp b/src/util/log/Logger.cpp index 2a1dc7a11..dfd1086f0 100644 --- a/src/util/log/Logger.cpp +++ b/src/util/log/Logger.cpp @@ -20,6 +20,7 @@ #include "util/log/Logger.hpp" #include "util/Assert.hpp" +#include "util/BytesConverter.hpp" #include "util/SourceLocation.hpp" #include "util/newconfig/ArrayView.hpp" #include "util/newconfig/ConfigDefinition.hpp" @@ -134,9 +135,11 @@ LogService::init(config::ClioConfigDefinition const& config) boost::filesystem::path dirPath{logDir.value()}; if (!boost::filesystem::exists(dirPath)) boost::filesystem::create_directories(dirPath); - auto const rotationSize = config.get("log_rotation_size"); auto const rotationPeriod = config.get("log_rotation_hour_interval"); - auto const dirSize = config.get("log_directory_max_size"); + + // the below are taken from user in MB, but boost::log::add_file_log needs it to be in bytes + auto const rotationSize = mbToBytes(config.get("log_rotation_size")); + auto const dirSize = mbToBytes(config.get("log_directory_max_size")); auto fileSink = boost::log::add_file_log( keywords::file_name = dirPath / "clio.log", keywords::target_file_name = dirPath / "clio_%Y-%m-%d_%H-%M-%S.log", diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index cf835fbf9..589c78019 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -37,6 +37,7 @@ target_sources( etlng/GrpcSourceTests.cpp etlng/RegistryTests.cpp # Feed + util/BytesConverterTests.cpp feed/BookChangesFeedTests.cpp feed/ForwardFeedTests.cpp feed/LedgerFeedTests.cpp diff --git a/tests/unit/util/BytesConverterTests.cpp b/tests/unit/util/BytesConverterTests.cpp new file mode 100644 index 000000000..5396e4583 --- /dev/null +++ b/tests/unit/util/BytesConverterTests.cpp @@ -0,0 +1,40 @@ +//------------------------------------------------------------------------------ +/* + 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/BytesConverter.hpp" + +#include +#include +#include + +using namespace util; + +TEST(MBToBytesTest, SimpleValues) +{ + EXPECT_EQ(mbToBytes(0), 0); + EXPECT_EQ(mbToBytes(1), 1024 * 1024); + EXPECT_EQ(mbToBytes(2), 2 * 1024 * 1024); +} + +TEST(MBToBytesTest, LimitValues) +{ + auto const maxNum = std::numeric_limits::max(); + EXPECT_NE(mbToBytes(maxNum), maxNum * 1024 * 1024); + EXPECT_EQ(mbToBytes(maxNum), maxNum * 1024ul * 1024ul); +}