From 27253f0c29448c2040c2e1b69c306bf3f3c915a5 Mon Sep 17 00:00:00 2001 From: Chad Malmrose Date: Mon, 19 Jan 2026 17:45:33 -0500 Subject: [PATCH 1/2] #20: allow string args in log template function --- include/pressio-log/logger/logger_impl.hpp | 4 ++-- tests/logging/CMakeLists.txt | 5 +++++ tests/logging/test_logger_string.cc | 25 ++++++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 tests/logging/test_logger_string.cc diff --git a/include/pressio-log/logger/logger_impl.hpp b/include/pressio-log/logger/logger_impl.hpp index 9243e7d..f1d4f1c 100644 --- a/include/pressio-log/logger/logger_impl.hpp +++ b/include/pressio-log/logger/logger_impl.hpp @@ -119,7 +119,7 @@ inline void Logger::log(LogLevel level, const std::string& message) { if (current_rank_ == logging_rank_) { switch (level) { case LogLevel::none: return; - case LogLevel::sparse: sparse_(message); break; + case LogLevel::sparse: sparse_(message); break; case LogLevel::info: info_(message); break; case LogLevel::debug: debug_(message); break; case LogLevel::warning: warning_(message); break; @@ -136,7 +136,7 @@ inline void Logger::log(LogLevel level, const std::string& fmt_str, Args&&... ar } catch(const fmt::v11::format_error&) { std::ostringstream oss; oss << "fmt could not format given string: " << fmt_str << " with args:\n"; - ((oss << " " << std::to_string(args) << "\n"), ...); + ((oss << " " << args << "\n"), ...); throw std::runtime_error(oss.str()); } } diff --git a/tests/logging/CMakeLists.txt b/tests/logging/CMakeLists.txt index 6bb9a15..1e7338f 100644 --- a/tests/logging/CMakeLists.txt +++ b/tests/logging/CMakeLists.txt @@ -13,6 +13,11 @@ add_utest_serial( ${CMAKE_CURRENT_SOURCE_DIR}/test_logger_write_serial.cc ) +add_utest_serial( + test_logger_string + ${CMAKE_CURRENT_SOURCE_DIR}/test_logger_string.cc +) + if (PRESSIO_ENABLE_TPL_MPI) add_utest_mpi( test_logger_mpi gTestMain_mpi 3 diff --git a/tests/logging/test_logger_string.cc b/tests/logging/test_logger_string.cc new file mode 100644 index 0000000..ca1924a --- /dev/null +++ b/tests/logging/test_logger_string.cc @@ -0,0 +1,25 @@ +#include + +#include "helpers.hpp" +#include "LoggerTest.hpp" +#include "pressio-log/core.hpp" + +TEST_F(LoggerTest, Logger_String_Templated) { + PRESSIOLOG_SET_LEVEL(pressiolog::LogLevel::debug); + + CoutRedirector redirect; + + PRESSIOLOG_SPARSE("{}", "hey there"); + PRESSIOLOG_INFO("{} {}", "two", "strings"); + PRESSIOLOG_DEBUG("{}", "Debug"); + PRESSIOLOG_WARNING("{}: {}", 2, "number"); + PRESSIOLOG_ERROR("{}", "Error"); + + std::string output = redirect.str(); + + EXPECT_TRUE(check_output(output, "hey there", true)); + EXPECT_TRUE(check_output(output, "two strings", true)); + EXPECT_TRUE(check_output(output, "Debug", true)); + EXPECT_TRUE(check_output(output, "2: number", true)); + EXPECT_TRUE(check_output(output, "Error", true)); +} From 80457120fc81a183e2121a452bdafedae633f6b1 Mon Sep 17 00:00:00 2001 From: Chad Malmrose Date: Thu, 5 Feb 2026 09:22:33 -0500 Subject: [PATCH 2/2] #20: Logger string unit tests use same level, added new expect to ensure good output --- tests/logging/test_logger_string.cc | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/tests/logging/test_logger_string.cc b/tests/logging/test_logger_string.cc index ca1924a..ad896c5 100644 --- a/tests/logging/test_logger_string.cc +++ b/tests/logging/test_logger_string.cc @@ -4,22 +4,23 @@ #include "LoggerTest.hpp" #include "pressio-log/core.hpp" -TEST_F(LoggerTest, Logger_String_Templated) { +TEST_F(LoggerTest, Logger_String_Formatting) { PRESSIOLOG_SET_LEVEL(pressiolog::LogLevel::debug); CoutRedirector redirect; PRESSIOLOG_SPARSE("{}", "hey there"); - PRESSIOLOG_INFO("{} {}", "two", "strings"); - PRESSIOLOG_DEBUG("{}", "Debug"); - PRESSIOLOG_WARNING("{}: {}", 2, "number"); - PRESSIOLOG_ERROR("{}", "Error"); + PRESSIOLOG_SPARSE("{} {}", "two", "strings"); + PRESSIOLOG_SPARSE("{}", "Debug"); + PRESSIOLOG_SPARSE("{}: {}", 2.1, "number"); + PRESSIOLOG_SPARSE("{}", "Error"); std::string output = redirect.str(); EXPECT_TRUE(check_output(output, "hey there", true)); EXPECT_TRUE(check_output(output, "two strings", true)); EXPECT_TRUE(check_output(output, "Debug", true)); - EXPECT_TRUE(check_output(output, "2: number", true)); + EXPECT_TRUE(check_output(output, "2.1: number", true)); EXPECT_TRUE(check_output(output, "Error", true)); + EXPECT_TRUE(check_output(output, "could not format given string", false)); }