From 50a7d4e7fc5e025ac50a7a2eded66cc385bbc5ed Mon Sep 17 00:00:00 2001 From: Richard Berger Date: Fri, 4 Feb 2022 17:53:36 -0500 Subject: [PATCH] Add utils::flush_buffers() --- src/library.cpp | 6 +----- src/utils.cpp | 9 +++++++++ src/utils.h | 8 ++++++++ 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/library.cpp b/src/library.cpp index 7f80e6066c..8c6ee5e774 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -5449,11 +5449,7 @@ to simplify capturing output from LAMMPS library calls. * \param handle pointer to a previously created LAMMPS instance cast to ``void *``. */ void lammps_flush_buffers(void *handle) { - LAMMPS *lmp = (LAMMPS *) handle; - if (lmp->screen) fflush(lmp->screen); - if (lmp->logfile) fflush(lmp->logfile); - if (lmp->universe->uscreen) fflush(lmp->universe->uscreen); - if (lmp->universe->ulogfile) fflush(lmp->universe->ulogfile); + utils::flush_buffers((LAMMPS *) handle); } /* ---------------------------------------------------------------------- */ diff --git a/src/utils.cpp b/src/utils.cpp index ca2a0c4f5b..c60908a2f2 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -24,6 +24,7 @@ #include "text_file_reader.h" #include "tokenizer.h" #include "update.h" +#include "universe.h" #include #include @@ -138,6 +139,14 @@ void utils::fmtargs_logmesg(LAMMPS *lmp, fmt::string_view format, fmt::format_ar } } +void utils::flush_buffers(LAMMPS *lmp) +{ + if (lmp->screen) fflush(lmp->screen); + if (lmp->logfile) fflush(lmp->logfile); + if (lmp->universe->uscreen) fflush(lmp->universe->uscreen); + if (lmp->universe->ulogfile) fflush(lmp->universe->ulogfile); +} + /* 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 47a4ace5f9..425fbfe0c1 100644 --- a/src/utils.h +++ b/src/utils.h @@ -74,6 +74,14 @@ namespace utils { void logmesg(LAMMPS *lmp, const std::string &mesg); + /*! Flush output buffers + * + * This function calls fflush on screen and logfile FILE pointers + * if available + */ + + void flush_buffers(LAMMPS *lmp); + /*! Return a string representing the current system error status * * This is a wrapper around calling strerror(errno).