add option to suppress printing the last command

This commit is contained in:
Axel Kohlmeyer
2025-01-16 21:11:27 -05:00
parent da5a12fcd1
commit fa54fd1097
5 changed files with 40 additions and 25 deletions

View File

@ -118,36 +118,30 @@ void Error::all(const std::string &file, int line, int failed, const std::string
{ {
MPI_Barrier(world); MPI_Barrier(world);
int me;
std::string lastcmd = "(unknown)"; std::string lastcmd = "(unknown)";
std::string mesg = "ERROR: " + str + fmt::format(" ({}:{})\n", truncpath(file), line);
MPI_Comm_rank(world,&me); if (input && input->line) lastcmd = input->line;
if (me == 0) { if (failed > NOLASTLINE) {
std::string mesg = "ERROR: " + str;
if (input && input->line) lastcmd = input->line;
try { try {
mesg += fmt::format(" ({}:{})\nLast command: {}\n", truncpath(file),line,lastcmd); mesg += fmt::format("Last input line: {}\n", lastcmd);
if (failed > NOPOINTER) mesg += utils::point_to_error(input, failed); if (failed > NOPOINTER) mesg += utils::point_to_error(input, failed);
} catch (fmt::format_error &) { } catch (fmt::format_error &) {
; // do nothing ; // do nothing
} }
utils::logmesg(lmp,mesg);
} }
if (comm->me == 0) utils::logmesg(lmp,mesg);
// allow commands if an exception was caught in a run // allow commands if an exception was caught in a run
// update may be a null pointer when catching command-line errors // update may be a null pointer when catching command-line errors
if (update) update->whichflag = 0; if (update) update->whichflag = 0;
std::string msg = fmt::format("ERROR: {} ({}:{})\n", str, truncpath(file), line);
if (failed > NOPOINTER) msg += utils::point_to_error(input, failed);
if (universe->nworlds > 1) if (universe->nworlds > 1)
throw LAMMPSAbortException(msg, universe->uworld); throw LAMMPSAbortException(mesg, universe->uworld);
else else
throw LAMMPSException(msg); throw LAMMPSException(mesg);
} }
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
@ -166,8 +160,12 @@ void Error::one(const std::string &file, int line, int failed, const std::string
if (input && input->line) lastcmd = input->line; if (input && input->line) lastcmd = input->line;
std::string mesg; std::string mesg;
try { try {
mesg = fmt::format("ERROR on proc {}: {} ({}:{})\nLast command: {}\n", if (failed == NOLASTLINE) {
me,str,truncpath(file),line,lastcmd); mesg = fmt::format("ERROR on proc {}: {} ({}:{})\n", me, str, truncpath(file), line);
} else {
mesg = fmt::format("ERROR on proc {}: {} ({}:{})\nLast input line: {}\n",
me, str, truncpath(file), line, lastcmd);
}
if (failed > NOPOINTER) mesg += utils::point_to_error(input, failed); if (failed > NOPOINTER) mesg += utils::point_to_error(input, failed);
} catch (fmt::format_error &) { } catch (fmt::format_error &) {
; // do nothing ; // do nothing

View File

@ -28,6 +28,7 @@ class Error : protected Pointers {
void universe_warn(const std::string &, int, const std::string &); void universe_warn(const std::string &, int, const std::string &);
static constexpr int NOPOINTER = -2; static constexpr int NOPOINTER = -2;
static constexpr int NOLASTLINE = -3;
// regular error calls // regular error calls

View File

@ -38,7 +38,7 @@ FixPrint::FixPrint(LAMMPS *lmp, int narg, char **arg) :
nevery = 1; nevery = 1;
} else { } else {
nevery = utils::inumeric(FLERR, arg[3], false, lmp); nevery = utils::inumeric(FLERR, arg[3], false, lmp);
if (nevery <= 0) error->all(FLERR, "Illegal fix print nevery value {}; must be > 0", nevery); if (nevery <= 0) error->all(FLERR, 3, "Illegal fix print nevery value {}; must be > 0", nevery);
} }
text = utils::strdup(arg[4]); text = utils::strdup(arg[4]);
@ -121,12 +121,15 @@ void FixPrint::init()
if (var_print) { if (var_print) {
ivar_print = input->variable->find(var_print); ivar_print = input->variable->find(var_print);
if (ivar_print < 0) if (ivar_print < 0)
error->all(FLERR, "Variable {} for fix print timestep does not exist", var_print); error->all(FLERR, Error::NOLASTLINE, "Variable {} for fix print timestep does not exist",
var_print);
if (!input->variable->equalstyle(ivar_print)) if (!input->variable->equalstyle(ivar_print))
error->all(FLERR, "Variable {} for fix print timestep is invalid style", var_print); error->all(FLERR, Error::NOLASTLINE, "Variable {} for fix print timestep is invalid style",
var_print);
next_print = static_cast<bigint>(input->variable->compute_equal(ivar_print)); next_print = static_cast<bigint>(input->variable->compute_equal(ivar_print));
if (next_print <= update->ntimestep) if (next_print <= update->ntimestep)
error->all(FLERR, "Fix print timestep variable {} returned a bad timestep: {}", var_print, error->all(FLERR, Error::NOLASTLINE,
"Fix print timestep variable {} returned a bad timestep: {}", var_print,
next_print); next_print);
} else { } else {
if (update->ntimestep % nevery) if (update->ntimestep % nevery)

View File

@ -135,15 +135,28 @@ void utils::missing_cmd_args(const std::string &file, int line, const std::strin
std::string utils::point_to_error(Input *input, int failed) std::string utils::point_to_error(Input *input, int failed)
{ {
if (input) { if (input) {
std::string cmdline = "Preprocessed: "; std::string cmdline = "--> parsed line: ";
int indicator = cmdline.size(); // error indicator points to command by default int indicator = cmdline.size(); // error indicator points to command by default
cmdline += input->command; cmdline += input->command;
cmdline += ' '; cmdline += ' ';
// assemble pre-processed command line and update error indicator position, if needed. // assemble pre-processed command line and update error indicator position, if needed.
for (int i = 0; i < input->narg; ++i) { for (int i = 0; i < input->narg; ++i) {
std::string inputarg = input->arg[i];
if (i == failed) indicator = cmdline.size(); if (i == failed) indicator = cmdline.size();
cmdline += input->arg[i];
// argument contains whitespace. add quotes. check which type of quotes, too
if (inputarg.find_first_of(" \t\n") != std::string::npos) {
if (inputarg.find_first_of('"') != std::string::npos) {
cmdline += "'";
cmdline += inputarg;
cmdline += "'";
} else {
cmdline += '"';
cmdline += inputarg;
cmdline += '"';
}
} else cmdline += inputarg;
cmdline += ' '; cmdline += ' ';
} }
// construct and append error indicator line // construct and append error indicator line

View File

@ -5227,14 +5227,14 @@ void Variable::print_var_error(const std::string &srcfile, const int lineno,
if ((ivar >= 0) && (ivar < nvar)) { if ((ivar >= 0) && (ivar < nvar)) {
std::string msg = fmt::format("Variable {}: ",names[ivar]) + errmsg; std::string msg = fmt::format("Variable {}: ",names[ivar]) + errmsg;
if (global) if (global)
error->all(srcfile,lineno,msg); error->all(srcfile, lineno, Error::NOLASTLINE, msg);
else else
error->one(srcfile,lineno,msg); error->one(srcfile, lineno, Error::NOLASTLINE, msg);
} else { } else {
if (global) if (global)
error->all(srcfile,lineno,errmsg); error->all(srcfile,lineno, Error::NOLASTLINE, errmsg);
else else
error->one(srcfile,lineno,errmsg); error->one(srcfile,lineno, Error::NOLASTLINE, errmsg);
} }
} }