From cf763e2477720df7847efc45d4c914a0efbbfcf5 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Sun, 2 Aug 2020 22:10:59 -0400 Subject: [PATCH] Add templated write_message for formatting messages This new function lives inside include/openmc/error.h and checks if a message will be written by checking the current verbosity level prior to formatting the message. This works very similar to python logging modules and the spdlog project, where the call to the logging function accepts a formattable message string and the format arguments separately, e.g. write_message(1, "Writing data for statepoint {}", point) The function accepts any number and type of argument after the format message and passes directly to fmt::format and in-turn to the non-templated write_message. While this later function does similar checking with the level and MPI rank, the real goal is the output function in src/error.cpp which is not currently exposed through the error header file Thanks to @paulromano for feedback and suggestions --- include/openmc/error.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/include/openmc/error.h b/include/openmc/error.h index 32ed36c8b8..4c4efe6a73 100644 --- a/include/openmc/error.h +++ b/include/openmc/error.h @@ -5,7 +5,10 @@ #include #include +#include + #include "openmc/capi.h" +#include "openmc/settings.h" #ifdef __GNUC__ #define UNREACHABLE() __builtin_unreachable() @@ -63,6 +66,21 @@ void write_message(const std::stringstream& message, int level) write_message(message.str(), level); } +template +void write_message( + int level, const std::string& message, const Params&... fmt_args) +{ + if (settings::verbosity >= level) { + write_message(fmt::format(message, fmt_args...)); + } +} + +template +void write_message(const std::string& message, const Params&... fmt_args) +{ + write_message(fmt::format(message, fmt_args...)); +} + #ifdef OPENMC_MPI extern "C" void abort_mpi(int code); #endif