From 59b14724edd8a7ac6b2bd9d99dff82ed8cd4ffe6 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Mon, 7 Sep 2015 00:47:37 -0400 Subject: [PATCH] Allow writelog to do string formatting If multiple arguments are supplied, the string is treated as a Python-style format string and the extra arguments are used to populate the format string. --- include/cantera/base/global.h | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/include/cantera/base/global.h b/include/cantera/base/global.h index 1ff32cbbe..65f5ea625 100644 --- a/include/cantera/base/global.h +++ b/include/cantera/base/global.h @@ -144,13 +144,26 @@ std::string canteraRoot(); */ //! @copydoc Application::Messages::writelog(const std::string&) -void writelog(const std::string& msg); +void writelog_direct(const std::string& msg); //! Write a message to the log only if loglevel > 0 inline void debuglog(const std::string& msg, int loglevel) { if (loglevel > 0) { - writelog(msg); + writelog_direct(msg); + } +} + +//! @copydoc Application::Messages::writelog(const std::string&) +//! This function passes its arguments to the cppformat 'format' function to +//! generate a formatted string from a Python-style (curly braces) format +//! string. +template +void writelog(const std::string& fmt, const Args&... args) { + if (sizeof...(args) == 0) { + writelog_direct(fmt); + } else { + writelog_direct(fmt::format(fmt, args...)); } } @@ -167,7 +180,7 @@ inline void debuglog(const std::string& msg, int loglevel) */ template void writelogf(const char* fmt, const Args& ... args) { - writelog(fmt::sprintf(fmt, args...)); + writelog_direct(fmt::sprintf(fmt, args...)); } //! Write an end of line character to the screen and flush output