add overload to utils::logmesg() that handles format strings and variable arguments

this reduces utils::logmesg(lmp,fmt::format(...)) to utils::logmesg(lmp,...)
while still allowing just a single string argument.
This commit is contained in:
Axel Kohlmeyer
2021-04-21 22:52:32 -04:00
parent 7e2d40c5fa
commit 0cf1252f1f
2 changed files with 33 additions and 9 deletions

View File

@ -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. */

View File

@ -17,6 +17,8 @@
/*! \file utils.h */
#include "lmptype.h"
#include "fmt/format.h"
#include <string>
#include <vector>
#include <cstdio>
@ -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 <typename S, typename... Args>
void logmesg(LAMMPS *lmp, const S &format, Args&&... args) {
_internal_logmesg(lmp, format,
fmt::make_args_checked<Args...>(format, args...));
}
/** \overload */
void logmesg(LAMMPS *lmp, const std::string &mesg);