diff --git a/src/utils.cpp b/src/utils.cpp index 809e625a29..2dcf77eacd 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -134,8 +134,11 @@ void utils::logmesg(LAMMPS *lmp, const std::string &mesg) void utils::fmtargs_logmesg(LAMMPS *lmp, fmt::string_view format, fmt::format_args args) { - if (lmp->screen) fmt::vprint(lmp->screen, format, args); - if (lmp->logfile) fmt::vprint(lmp->logfile, format, args); + try { + logmesg(lmp, fmt::vformat(format, args)); + } catch (fmt::format_error &e) { + logmesg(lmp, std::string(e.what())+"\n"); + } } /* define this here, so we won't have to include the headers diff --git a/unittest/formats/test_file_operations.cpp b/unittest/formats/test_file_operations.cpp index fe2bf2d2f6..392ca04656 100644 --- a/unittest/formats/test_file_operations.cpp +++ b/unittest/formats/test_file_operations.cpp @@ -126,7 +126,7 @@ TEST_F(FileOperationsTest, safe_fread) TEST_F(FileOperationsTest, logmesg) { - char buf[16]; + char buf[64]; BEGIN_HIDE_OUTPUT(); command("echo none"); END_HIDE_OUTPUT(); @@ -135,14 +135,16 @@ TEST_F(FileOperationsTest, logmesg) command("log test_logmesg.log"); utils::logmesg(lmp, "two\n"); utils::logmesg(lmp, "three={}\n",3); + utils::logmesg(lmp, "four {}\n"); + utils::logmesg(lmp, "five\n",5); command("log none"); std::string out = END_CAPTURE_OUTPUT(); - memset(buf, 0, 16); + memset(buf, 0, 64); FILE *fp = fopen("test_logmesg.log", "r"); - fread(buf, 1, 16, fp); + fread(buf, 1, 64, fp); fclose(fp); - ASSERT_THAT(out, StrEq("one\ntwo\nthree=3\n")); - ASSERT_THAT(buf, StrEq("two\nthree=3\n")); + ASSERT_THAT(out, StrEq("one\ntwo\nthree=3\nargument not found\nfive\n")); + ASSERT_THAT(buf, StrEq("two\nthree=3\nargument not found\nfive\n")); remove("test_logmesg.log"); }