convert Error class to accept 'const std::string &' instead of 'const char *'

This commit is contained in:
Axel Kohlmeyer
2020-06-03 15:43:05 -04:00
parent 9e8ce240a0
commit 9f3f53cc6b
4 changed files with 73 additions and 91 deletions

View File

@ -15,10 +15,13 @@
#include <mpi.h>
#include <cstdlib>
#include <cstring>
#include <string>
#include "universe.h"
#include "output.h"
#include "input.h"
#include "accelerator_kokkos.h"
#include "utils.h"
#include "fmt/format.h"
#if defined(LAMMPS_EXCEPTIONS)
#include "update.h"
@ -28,23 +31,19 @@ using namespace LAMMPS_NS;
// helper function to truncate a string to a segment starting with "src/";
static const char *truncpath(const char *path)
static std::string truncpath(const std::string &path)
{
if (path) {
int len = strlen(path);
for (int i = len-4; i > 0; --i) {
if (strncmp("src/",path+i,4) == 0)
return path+i;
}
}
return path;
std::size_t found = path.find("src/");
if (found != std::string::npos)
return path.substr(found);
else return path;
}
/* ---------------------------------------------------------------------- */
Error::Error(LAMMPS *lmp) : Pointers(lmp) {
#ifdef LAMMPS_EXCEPTIONS
last_error_message = NULL;
last_error_message.clear();
last_error_type = ERROR_NONE;
#endif
}
@ -55,15 +54,14 @@ Error::Error(LAMMPS *lmp) : Pointers(lmp) {
no abort, so insure all procs in universe call, else will hang
------------------------------------------------------------------------- */
void Error::universe_all(const char *file, int line, const char *str)
void Error::universe_all(const std::string &file, int line, const std::string &str)
{
MPI_Barrier(universe->uworld);
std::string mesg = fmt::format("ERROR: {} ({}:{})\n",
str,truncpath(file),line);
if (universe->me == 0) {
if (universe->uscreen) fprintf(universe->uscreen,
"ERROR: %s (%s:%d)\n",str,truncpath(file),line);
if (universe->ulogfile) fprintf(universe->ulogfile,
"ERROR: %s (%s:%d)\n",str,truncpath(file),line);
if (universe->uscreen) fputs(mesg.c_str(),universe->uscreen);
if (universe->ulogfile) fputs(mesg.c_str(),universe->ulogfile);
}
if (output) delete output;
@ -80,9 +78,7 @@ void Error::universe_all(const char *file, int line, const char *str)
if (update) update->whichflag = 0;
char msg[100];
snprintf(msg, 100, "ERROR: %s (%s:%d)\n", str, truncpath(file), line);
throw LAMMPSException(msg);
throw LAMMPSException(mesg);
#else
if (lmp->kokkos) Kokkos::finalize();
MPI_Finalize();
@ -95,11 +91,11 @@ void Error::universe_all(const char *file, int line, const char *str)
forces abort of entire universe if any proc in universe calls
------------------------------------------------------------------------- */
void Error::universe_one(const char *file, int line, const char *str)
void Error::universe_one(const std::string &file, int line, const std::string &str)
{
if (universe->uscreen)
fprintf(universe->uscreen,"ERROR on proc %d: %s (%s:%d)\n",
std::string mesg = fmt::format("ERROR on proc {}: {} ({}:{})\n",
universe->me,str,truncpath(file),line);
if (universe->uscreen) fputs(mesg.c_str(),universe->uscreen);
#ifdef LAMMPS_EXCEPTIONS
@ -108,9 +104,7 @@ void Error::universe_one(const char *file, int line, const char *str)
if (update) update->whichflag = 0;
char msg[100];
snprintf(msg, 100, "ERROR: %s (%s:%d)\n", str, truncpath(file), line);
throw LAMMPSAbortException(msg, universe->uworld);
throw LAMMPSAbortException(mesg, universe->uworld);
#else
MPI_Abort(universe->uworld,1);
#endif
@ -121,10 +115,10 @@ void Error::universe_one(const char *file, int line, const char *str)
prints a warning message to the screen
------------------------------------------------------------------------- */
void Error::universe_warn(const char *file, int line, const char *str)
void Error::universe_warn(const std::string &file, int line, const std::string &str)
{
if (universe->uscreen)
fprintf(universe->uscreen,"WARNING on proc %d: %s (%s:%d)\n",
fmt::print(universe->uscreen,"WARNING on proc {}: {} ({}:{})\n",
universe->me,str,truncpath(file),line);
}
@ -135,23 +129,19 @@ void Error::universe_warn(const char *file, int line, const char *str)
force MPI_Abort if running in multi-partition mode
------------------------------------------------------------------------- */
void Error::all(const char *file, int line, const char *str)
void Error::all(const std::string &file, int line, const std::string &str)
{
MPI_Barrier(world);
int me;
const char *lastcmd = (const char*)"(unknown)";
std::string lastcmd = "(unknown)";
MPI_Comm_rank(world,&me);
if (me == 0) {
if (input && input->line) lastcmd = input->line;
if (screen) fprintf(screen,"ERROR: %s (%s:%d)\n"
"Last command: %s\n",
str,truncpath(file),line,lastcmd);
if (logfile) fprintf(logfile,"ERROR: %s (%s:%d)\n"
"Last command: %s\n",
str,truncpath(file),line,lastcmd);
utils::logmesg(lmp,fmt::format("ERROR: {} ({}:{})\nLast command: {}\n",
str,truncpath(file),line,lastcmd));
}
#ifdef LAMMPS_EXCEPTIONS
@ -161,8 +151,8 @@ void Error::all(const char *file, int line, const char *str)
if (update) update->whichflag = 0;
char msg[100];
snprintf(msg, 100, "ERROR: %s (%s:%d)\n", str, truncpath(file), line);
std::string msg = fmt::format("ERROR: {} ({}:{})\n",
str, truncpath(file), line);
if (universe->nworlds > 1) {
throw LAMMPSAbortException(msg, universe->uworld);
@ -188,24 +178,20 @@ void Error::all(const char *file, int line, const char *str)
forces abort of entire world (and universe) if any proc in world calls
------------------------------------------------------------------------- */
void Error::one(const char *file, int line, const char *str)
void Error::one(const std::string &file, int line, const std::string &str)
{
int me;
const char *lastcmd = (const char*)"(unknown)";
std::string lastcmd = "(unknown)";
MPI_Comm_rank(world,&me);
if (input && input->line) lastcmd = input->line;
if (screen) fprintf(screen,"ERROR on proc %d: %s (%s:%d)\n"
"Last command: %s\n",
me,str,truncpath(file),line,lastcmd);
if (logfile) fprintf(logfile,"ERROR on proc %d: %s (%s:%d)\n"
"Last command: %s\n",
std::string mesg = fmt::format("ERROR on proc {}: {} ({}:{})\n",
me,str,truncpath(file),line,lastcmd);
utils::logmesg(lmp,mesg);
if (universe->nworlds > 1)
if (universe->uscreen)
fprintf(universe->uscreen,"ERROR on proc %d: %s (%s:%d)\n",
universe->me,str,truncpath(file),line);
fputs(mesg.c_str(),universe->uscreen);
#ifdef LAMMPS_EXCEPTIONS
@ -214,9 +200,7 @@ void Error::one(const char *file, int line, const char *str)
if (update) update->whichflag = 0;
char msg[100];
snprintf(msg, 100, "ERROR on proc %d: %s (%s:%d)\n", me, str, truncpath(file), line);
throw LAMMPSAbortException(msg, world);
throw LAMMPSAbortException(mesg, world);
#else
if (screen) fflush(screen);
if (logfile) fflush(logfile);
@ -229,11 +213,12 @@ void Error::one(const char *file, int line, const char *str)
only write to screen if non-NULL on this proc since could be file
------------------------------------------------------------------------- */
void Error::warning(const char *file, int line, const char *str, int logflag)
void Error::warning(const std::string &file, int line, const std::string &str, int logflag)
{
if (screen) fprintf(screen,"WARNING: %s (%s:%d)\n",str,truncpath(file),line);
if (logflag && logfile) fprintf(logfile,"WARNING: %s (%s:%d)\n",
std::string mesg = fmt::format("WARNING: {} ({}:{})\n",
str,truncpath(file),line);
if (screen) fputs(mesg.c_str(),screen);
if (logflag && logfile) fputs(mesg.c_str(),logfile);
}
/* ----------------------------------------------------------------------
@ -241,10 +226,13 @@ void Error::warning(const char *file, int line, const char *str, int logflag)
write message to screen and logfile (if logflag is set)
------------------------------------------------------------------------- */
void Error::message(const char *file, int line, const char *str, int logflag)
void Error::message(const std::string &file, int line, const std::string &str, int logflag)
{
if (screen) fprintf(screen,"%s (%s:%d)\n",str,truncpath(file),line);
if (logflag && logfile) fprintf(logfile,"%s (%s:%d)\n",str,truncpath(file),line);
std::string mesg = fmt::format("{} ({}:{})\n",
str,truncpath(file),line);
if (screen) fputs(mesg.c_str(),screen);
if (logflag && logfile) fputs(mesg.c_str(),logfile);
}
/* ----------------------------------------------------------------------
@ -273,7 +261,7 @@ void Error::done(int status)
compiled with -DLAMMPS_EXCEPTIONS)
------------------------------------------------------------------------- */
char * Error::get_last_error() const
std::string Error::get_last_error() const
{
return last_error_message;
}
@ -293,16 +281,9 @@ ErrorType Error::get_last_error_type() const
(only used if compiled with -DLAMMPS_EXCEPTIONS)
------------------------------------------------------------------------- */
void Error::set_last_error(const char * msg, ErrorType type)
void Error::set_last_error(const std::string &msg, ErrorType type)
{
delete [] last_error_message;
if(msg) {
last_error_message = new char[strlen(msg)+1];
strcpy(last_error_message, msg);
} else {
last_error_message = NULL;
}
last_error_message = msg;
last_error_type = type;
}
#endif

View File

@ -15,6 +15,7 @@
#define LMP_ERROR_H
#include "pointers.h"
#include <string>
#ifdef LAMMPS_EXCEPTIONS
#include "exceptions.h"
@ -26,23 +27,23 @@ class Error : protected Pointers {
public:
Error(class LAMMPS *);
void universe_all(const char *, int, const char *);
void universe_one(const char *, int, const char *);
void universe_warn(const char *, int, const char *);
void universe_all(const std::string &, int, const std::string &);
void universe_one(const std::string &, int, const std::string &);
void universe_warn(const std::string &, int, const std::string &);
void all(const char *, int, const char *);
void one(const char *, int, const char *);
void warning(const char *, int, const char *, int = 1);
void message(const char *, int, const char *, int = 1);
void all(const std::string &, int, const std::string &);
void one(const std::string &, int, const std::string &);
void warning(const std::string &, int, const std::string &, int = 1);
void message(const std::string &, int, const std::string &, int = 1);
void done(int = 0); // 1 would be fully backwards compatible
#ifdef LAMMPS_EXCEPTIONS
char * get_last_error() const;
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_last_error(const std::string &msg, ErrorType type = ERROR_NORMAL);
private:
char * last_error_message;
std::string last_error_message;
ErrorType last_error_type;
#endif
};

View File

@ -25,7 +25,7 @@ class LAMMPSException : public std::exception
public:
std::string message;
LAMMPSException(std::string msg) : message(msg) {
LAMMPSException(const std::string &msg) : message(msg) {
}
~LAMMPSException() throw() {
@ -40,7 +40,7 @@ class LAMMPSAbortException : public LAMMPSException {
public:
MPI_Comm universe;
LAMMPSAbortException(std::string msg, MPI_Comm universe) :
LAMMPSAbortException(const std::string &msg, MPI_Comm universe) :
LAMMPSException(msg),
universe(universe)
{

View File

@ -78,12 +78,12 @@ using namespace LAMMPS_NS;
MPI_Comm_size(ae.universe, &nprocs ); \
\
if (nprocs > 1) { \
error->set_last_error(ae.message.c_str(), ERROR_ABORT); \
error->set_last_error(ae.message, ERROR_ABORT); \
} else { \
error->set_last_error(ae.message.c_str(), ERROR_NORMAL); \
error->set_last_error(ae.message, ERROR_NORMAL); \
} \
} catch(LAMMPSException & e) { \
error->set_last_error(e.message.c_str(), ERROR_NORMAL); \
error->set_last_error(e.message, ERROR_NORMAL); \
}
#else
#define BEGIN_CAPTURE
@ -1716,7 +1716,7 @@ int lammps_config_has_exceptions() {
int lammps_has_error(void *ptr) {
LAMMPS *lmp = (LAMMPS *)ptr;
Error *error = lmp->error;
return error->get_last_error() ? 1 : 0;
return (error->get_last_error() != "") ? 1 : 0;
}
/* ----------------------------------------------------------------------
@ -1730,9 +1730,9 @@ int lammps_get_last_error_message(void *ptr, char * buffer, int buffer_size) {
LAMMPS *lmp = (LAMMPS *)ptr;
Error *error = lmp->error;
if(error->get_last_error()) {
if(error->get_last_error() != "") {
int error_type = error->get_last_error_type();
strncpy(buffer, error->get_last_error(), buffer_size-1);
strncpy(buffer, error->get_last_error().c_str(), buffer_size-1);
error->set_last_error(NULL, ERROR_NONE);
return error_type;
}