diff --git a/src/utils.cpp b/src/utils.cpp index e733d7eaae..4071b7b61b 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -123,12 +123,7 @@ std::string utils::strfind(const std::string &text, const std::string &pattern) return ""; } -/** This function simplifies the repetitive task of outputting some - * message to both the screen and/or the log file. In combination - * with using fmt::format(), which returns the formatted text - * in a std::string() instance, this can be used to reduce - * operations previously requiring several lines of code to - * a single statement. */ +/* specialization for the case of just a single string argument */ void utils::logmesg(LAMMPS *lmp, const std::string &mesg) { @@ -136,6 +131,13 @@ void utils::logmesg(LAMMPS *lmp, const std::string &mesg) if (lmp->logfile) fputs(mesg.c_str(), lmp->logfile); } +void utils::_internal_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); +} + /* define this here, so we won't have to include the headers everywhere and utils.h will more likely be included anyway. */ diff --git a/src/utils.h b/src/utils.h index ec4dd6ae85..6eed8fd3bc 100644 --- a/src/utils.h +++ b/src/utils.h @@ -17,6 +17,8 @@ /*! \file utils.h */ #include "lmptype.h" +#include "fmt/format.h" + #include #include #include @@ -45,10 +47,30 @@ namespace LAMMPS_NS { std::string strfind(const std::string &text, const std::string &pattern); - /** Send message to screen and logfile, if available + /* Internal function handling the argument list for logmesg(). */ + + void _internal_logmesg(LAMMPS *lmp, fmt::string_view format, + fmt::format_args args); + + /** Send formatted message to screen and logfile, if available * - * \param lmp pointer to LAMMPS class instance - * \param mesg message to be printed */ + * This function simplifies the repetitive task of outputting some + * message to both the screen and/or the log file. The template + * wrapper with fmtlib format and argument processing allows + * this function to work similar to fmt::print(). + * The specialization overload handles the case of no arguments. + * + * \param lmp pointer to LAMMPS class instance + * \param format format string of message to be printed + * \param args arguments to format string */ + + template + void logmesg(LAMMPS *lmp, const S &format, Args&&... args) { + _internal_logmesg(lmp, format, + fmt::make_args_checked(format, args...)); + } + + /** \overload */ void logmesg(LAMMPS *lmp, const std::string &mesg);