add method that allows suppressing printing error messages immediately
This commit is contained in:
@ -35,7 +35,7 @@ static std::string truncpath(const std::string &path)
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
Error::Error(LAMMPS *lmp)
|
||||
: Pointers(lmp), numwarn(0), maxwarn(100), allwarn(0)
|
||||
: Pointers(lmp), numwarn(0), maxwarn(100), allwarn(0), showerror(1)
|
||||
{
|
||||
last_error_message.clear();
|
||||
last_error_type = ERROR_NONE;
|
||||
@ -56,7 +56,7 @@ void Error::universe_all(const std::string &file, int line, const std::string &s
|
||||
} catch (fmt::format_error &) {
|
||||
; // do nothing
|
||||
}
|
||||
if (universe->me == 0) {
|
||||
if (showerror && (universe->me == 0)) {
|
||||
if (universe->uscreen) fputs(mesg.c_str(),universe->uscreen);
|
||||
if (universe->ulogfile) fputs(mesg.c_str(),universe->ulogfile);
|
||||
}
|
||||
@ -84,7 +84,10 @@ void Error::universe_one(const std::string &file, int line, const std::string &s
|
||||
{
|
||||
std::string mesg = fmt::format("ERROR on proc {}: {} ({}:{})\n",
|
||||
universe->me,str,truncpath(file),line);
|
||||
if (universe->uscreen) fputs(mesg.c_str(),universe->uscreen);
|
||||
if (showerror) {
|
||||
if (universe->uscreen) fputs(mesg.c_str(),universe->uscreen);
|
||||
if (universe->ulogfile) fputs(mesg.c_str(),universe->ulogfile);
|
||||
}
|
||||
utils::flush_buffers(lmp);
|
||||
|
||||
// allow commands if an exception was caught in a run
|
||||
@ -131,7 +134,7 @@ void Error::all(const std::string &file, int line, int failed, const std::string
|
||||
|
||||
if (failed > NOLASTLINE) mesg += utils::point_to_error(input, failed);
|
||||
if (failed == ARGZERO) mesg += utils::point_to_error(input, 0);
|
||||
if (me == 0) utils::logmesg(lmp,mesg);
|
||||
if (showerror && (me == 0)) utils::logmesg(lmp,mesg);
|
||||
utils::flush_buffers(lmp);
|
||||
|
||||
// allow commands if an exception was caught in a run
|
||||
@ -164,11 +167,12 @@ void Error::one(const std::string &file, int line, int failed, const std::string
|
||||
std::string mesg = fmt::format("ERROR on proc {}: {} ({}:{})\n", me, str, truncpath(file), line);
|
||||
if (failed > NOPOINTER) mesg += utils::point_to_error(input, failed);
|
||||
if (failed == ARGZERO) mesg += utils::point_to_error(input, 0);
|
||||
utils::logmesg(lmp,mesg);
|
||||
if (showerror) utils::logmesg(lmp,mesg);
|
||||
|
||||
if (universe->nworlds > 1)
|
||||
if (universe->uscreen)
|
||||
fputs(mesg.c_str(),universe->uscreen);
|
||||
if (showerror && (universe->nworlds > 1)) {
|
||||
if (universe->uscreen) fputs(mesg.c_str(),universe->uscreen);
|
||||
if (universe->ulogfile) fputs(mesg.c_str(),universe->ulogfile);
|
||||
}
|
||||
|
||||
utils::flush_buffers(lmp);
|
||||
// allow commands if an exception was caught in a run
|
||||
@ -213,8 +217,7 @@ void Error::warning(const std::string &file, int line, const std::string &str)
|
||||
{
|
||||
++numwarn;
|
||||
if ((maxwarn != 0) && ((numwarn > maxwarn) || (allwarn > maxwarn) || (maxwarn < 0))) return;
|
||||
std::string mesg = fmt::format("WARNING: {} ({}:{})\n",
|
||||
str,truncpath(file),line);
|
||||
std::string mesg = fmt::format("WARNING: {} ({}:{})\n", str,truncpath(file),line);
|
||||
if (screen) fputs(mesg.c_str(),screen);
|
||||
if (logfile) fputs(mesg.c_str(),logfile);
|
||||
}
|
||||
@ -308,3 +311,13 @@ void Error::set_last_error(const char *msg, ErrorType type)
|
||||
last_error_message = msg;
|
||||
last_error_type = type;
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
enable or disable printing error messages. for use with library interface.
|
||||
if flag = 0 only last error message and type are updated.
|
||||
------------------------------------------------------------------------- */
|
||||
|
||||
void Error::set_show_error(const int flag)
|
||||
{
|
||||
showerror = flag;
|
||||
}
|
||||
|
||||
@ -98,12 +98,13 @@ class Error : protected Pointers {
|
||||
std::string get_last_error() const;
|
||||
ErrorType get_last_error_type() const;
|
||||
void set_last_error(const char *msg, ErrorType type = ERROR_NORMAL);
|
||||
void set_show_error(const int flag);
|
||||
|
||||
private:
|
||||
std::string last_error_message;
|
||||
ErrorType last_error_type;
|
||||
|
||||
int numwarn, maxwarn, allwarn;
|
||||
int numwarn, maxwarn, allwarn, showerror;
|
||||
// internal versions that accept explicit fmtlib arguments
|
||||
[[noreturn]] void _all(const std::string &, int, int, fmt::string_view, fmt::format_args args);
|
||||
[[noreturn]] void _one(const std::string &, int, int, fmt::string_view, fmt::format_args args);
|
||||
|
||||
@ -89,7 +89,7 @@ static void ptr_argument_warning()
|
||||
|
||||
[[noreturn]] static void lammps_throw_error(const std::string &fname, const std::string &mesg)
|
||||
{
|
||||
throw LAMMPSException("ERROR in library function " + fname + "(): " + mesg + "\n");
|
||||
throw LAMMPSException("ERROR in " + fname + "(): " + mesg + "\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@ -130,7 +130,7 @@ template <typename... ARgs>
|
||||
END_CAPTURE
|
||||
------------------------------------------------------------------------- */
|
||||
|
||||
#define BEGIN_CAPTURE \
|
||||
#define BEGIN_CAPTURE \
|
||||
Error *error = lmp->error; \
|
||||
try
|
||||
|
||||
@ -327,7 +327,8 @@ multiple LAMMPS instances concurrently or sequentially. See
|
||||
void lammps_close(void *handle)
|
||||
{
|
||||
auto lmp = (LAMMPS *) handle;
|
||||
delete lmp;
|
||||
// only delete if not already deleted
|
||||
if (lmp && lmp->comm) delete lmp;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
@ -7200,6 +7201,35 @@ int lammps_has_error(void *handle)
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
/** Enable or disable direct printing of error messages
|
||||
|
||||
\verbatim embed:rst
|
||||
|
||||
.. versionadded:: TBD
|
||||
|
||||
This function can be used to stop LAMMPS from printing error messages
|
||||
*before* LAMMPS throws a :ref:`C++ exception <exceptions>`. This is so
|
||||
it may be left to the code calling the library interface whether to
|
||||
check for them, and retrieve and print error messages using the library
|
||||
interface functions :cpp:func:`lammps_has_error` and
|
||||
:cpp:func:`lammps_get_last_error_message`.
|
||||
|
||||
\endverbatim
|
||||
*
|
||||
* \param handle pointer to a previously created LAMMPS instance cast to ``void *`` or NULL
|
||||
* \param flag enable (not 0) or disable (0) printing error messages before throwing exception
|
||||
*/
|
||||
void lammps_set_show_error(void *handle, const int flag)
|
||||
{
|
||||
if (handle) {
|
||||
LAMMPS *lmp = (LAMMPS *) handle;
|
||||
Error *error = lmp->error;
|
||||
error->set_show_error(flag);
|
||||
}
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
/** Copy the last error message into the provided buffer
|
||||
|
||||
\verbatim embed:rst
|
||||
@ -7287,5 +7317,5 @@ int lammps_python_api_version() {
|
||||
}
|
||||
|
||||
// Local Variables:
|
||||
// fill-column: 80
|
||||
// fill-column: 99
|
||||
// End:
|
||||
|
||||
Reference in New Issue
Block a user