convert Error class to accept 'const std::string &' instead of 'const char *'
This commit is contained in:
119
src/error.cpp
119
src/error.cpp
@ -15,10 +15,13 @@
|
|||||||
#include <mpi.h>
|
#include <mpi.h>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <string>
|
||||||
#include "universe.h"
|
#include "universe.h"
|
||||||
#include "output.h"
|
#include "output.h"
|
||||||
#include "input.h"
|
#include "input.h"
|
||||||
#include "accelerator_kokkos.h"
|
#include "accelerator_kokkos.h"
|
||||||
|
#include "utils.h"
|
||||||
|
#include "fmt/format.h"
|
||||||
|
|
||||||
#if defined(LAMMPS_EXCEPTIONS)
|
#if defined(LAMMPS_EXCEPTIONS)
|
||||||
#include "update.h"
|
#include "update.h"
|
||||||
@ -28,23 +31,19 @@ using namespace LAMMPS_NS;
|
|||||||
|
|
||||||
// helper function to truncate a string to a segment starting with "src/";
|
// 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) {
|
std::size_t found = path.find("src/");
|
||||||
int len = strlen(path);
|
if (found != std::string::npos)
|
||||||
for (int i = len-4; i > 0; --i) {
|
return path.substr(found);
|
||||||
if (strncmp("src/",path+i,4) == 0)
|
else return path;
|
||||||
return path+i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return path;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ---------------------------------------------------------------------- */
|
/* ---------------------------------------------------------------------- */
|
||||||
|
|
||||||
Error::Error(LAMMPS *lmp) : Pointers(lmp) {
|
Error::Error(LAMMPS *lmp) : Pointers(lmp) {
|
||||||
#ifdef LAMMPS_EXCEPTIONS
|
#ifdef LAMMPS_EXCEPTIONS
|
||||||
last_error_message = NULL;
|
last_error_message.clear();
|
||||||
last_error_type = ERROR_NONE;
|
last_error_type = ERROR_NONE;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@ -55,15 +54,14 @@ Error::Error(LAMMPS *lmp) : Pointers(lmp) {
|
|||||||
no abort, so insure all procs in universe call, else will hang
|
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);
|
MPI_Barrier(universe->uworld);
|
||||||
|
std::string mesg = fmt::format("ERROR: {} ({}:{})\n",
|
||||||
|
str,truncpath(file),line);
|
||||||
if (universe->me == 0) {
|
if (universe->me == 0) {
|
||||||
if (universe->uscreen) fprintf(universe->uscreen,
|
if (universe->uscreen) fputs(mesg.c_str(),universe->uscreen);
|
||||||
"ERROR: %s (%s:%d)\n",str,truncpath(file),line);
|
if (universe->ulogfile) fputs(mesg.c_str(),universe->ulogfile);
|
||||||
if (universe->ulogfile) fprintf(universe->ulogfile,
|
|
||||||
"ERROR: %s (%s:%d)\n",str,truncpath(file),line);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (output) delete output;
|
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;
|
if (update) update->whichflag = 0;
|
||||||
|
|
||||||
char msg[100];
|
throw LAMMPSException(mesg);
|
||||||
snprintf(msg, 100, "ERROR: %s (%s:%d)\n", str, truncpath(file), line);
|
|
||||||
throw LAMMPSException(msg);
|
|
||||||
#else
|
#else
|
||||||
if (lmp->kokkos) Kokkos::finalize();
|
if (lmp->kokkos) Kokkos::finalize();
|
||||||
MPI_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
|
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)
|
std::string mesg = fmt::format("ERROR on proc {}: {} ({}:{})\n",
|
||||||
fprintf(universe->uscreen,"ERROR on proc %d: %s (%s:%d)\n",
|
universe->me,str,truncpath(file),line);
|
||||||
universe->me,str,truncpath(file),line);
|
if (universe->uscreen) fputs(mesg.c_str(),universe->uscreen);
|
||||||
|
|
||||||
#ifdef LAMMPS_EXCEPTIONS
|
#ifdef LAMMPS_EXCEPTIONS
|
||||||
|
|
||||||
@ -108,9 +104,7 @@ void Error::universe_one(const char *file, int line, const char *str)
|
|||||||
|
|
||||||
if (update) update->whichflag = 0;
|
if (update) update->whichflag = 0;
|
||||||
|
|
||||||
char msg[100];
|
throw LAMMPSAbortException(mesg, universe->uworld);
|
||||||
snprintf(msg, 100, "ERROR: %s (%s:%d)\n", str, truncpath(file), line);
|
|
||||||
throw LAMMPSAbortException(msg, universe->uworld);
|
|
||||||
#else
|
#else
|
||||||
MPI_Abort(universe->uworld,1);
|
MPI_Abort(universe->uworld,1);
|
||||||
#endif
|
#endif
|
||||||
@ -121,11 +115,11 @@ void Error::universe_one(const char *file, int line, const char *str)
|
|||||||
prints a warning message to the screen
|
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)
|
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);
|
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
|
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);
|
MPI_Barrier(world);
|
||||||
|
|
||||||
int me;
|
int me;
|
||||||
const char *lastcmd = (const char*)"(unknown)";
|
std::string lastcmd = "(unknown)";
|
||||||
|
|
||||||
MPI_Comm_rank(world,&me);
|
MPI_Comm_rank(world,&me);
|
||||||
|
|
||||||
if (me == 0) {
|
if (me == 0) {
|
||||||
if (input && input->line) lastcmd = input->line;
|
if (input && input->line) lastcmd = input->line;
|
||||||
if (screen) fprintf(screen,"ERROR: %s (%s:%d)\n"
|
utils::logmesg(lmp,fmt::format("ERROR: {} ({}:{})\nLast command: {}\n",
|
||||||
"Last command: %s\n",
|
str,truncpath(file),line,lastcmd));
|
||||||
str,truncpath(file),line,lastcmd);
|
|
||||||
if (logfile) fprintf(logfile,"ERROR: %s (%s:%d)\n"
|
|
||||||
"Last command: %s\n",
|
|
||||||
str,truncpath(file),line,lastcmd);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef LAMMPS_EXCEPTIONS
|
#ifdef LAMMPS_EXCEPTIONS
|
||||||
@ -161,8 +151,8 @@ void Error::all(const char *file, int line, const char *str)
|
|||||||
|
|
||||||
if (update) update->whichflag = 0;
|
if (update) update->whichflag = 0;
|
||||||
|
|
||||||
char msg[100];
|
std::string msg = fmt::format("ERROR: {} ({}:{})\n",
|
||||||
snprintf(msg, 100, "ERROR: %s (%s:%d)\n", str, truncpath(file), line);
|
str, truncpath(file), line);
|
||||||
|
|
||||||
if (universe->nworlds > 1) {
|
if (universe->nworlds > 1) {
|
||||||
throw LAMMPSAbortException(msg, universe->uworld);
|
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
|
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;
|
int me;
|
||||||
const char *lastcmd = (const char*)"(unknown)";
|
std::string lastcmd = "(unknown)";
|
||||||
MPI_Comm_rank(world,&me);
|
MPI_Comm_rank(world,&me);
|
||||||
|
|
||||||
if (input && input->line) lastcmd = input->line;
|
if (input && input->line) lastcmd = input->line;
|
||||||
if (screen) fprintf(screen,"ERROR on proc %d: %s (%s:%d)\n"
|
std::string mesg = fmt::format("ERROR on proc {}: {} ({}:{})\n",
|
||||||
"Last command: %s\n",
|
me,str,truncpath(file),line,lastcmd);
|
||||||
me,str,truncpath(file),line,lastcmd);
|
utils::logmesg(lmp,mesg);
|
||||||
if (logfile) fprintf(logfile,"ERROR on proc %d: %s (%s:%d)\n"
|
|
||||||
"Last command: %s\n",
|
|
||||||
me,str,truncpath(file),line,lastcmd);
|
|
||||||
|
|
||||||
if (universe->nworlds > 1)
|
if (universe->nworlds > 1)
|
||||||
if (universe->uscreen)
|
if (universe->uscreen)
|
||||||
fprintf(universe->uscreen,"ERROR on proc %d: %s (%s:%d)\n",
|
fputs(mesg.c_str(),universe->uscreen);
|
||||||
universe->me,str,truncpath(file),line);
|
|
||||||
|
|
||||||
#ifdef LAMMPS_EXCEPTIONS
|
#ifdef LAMMPS_EXCEPTIONS
|
||||||
|
|
||||||
@ -214,9 +200,7 @@ void Error::one(const char *file, int line, const char *str)
|
|||||||
|
|
||||||
if (update) update->whichflag = 0;
|
if (update) update->whichflag = 0;
|
||||||
|
|
||||||
char msg[100];
|
throw LAMMPSAbortException(mesg, world);
|
||||||
snprintf(msg, 100, "ERROR on proc %d: %s (%s:%d)\n", me, str, truncpath(file), line);
|
|
||||||
throw LAMMPSAbortException(msg, world);
|
|
||||||
#else
|
#else
|
||||||
if (screen) fflush(screen);
|
if (screen) fflush(screen);
|
||||||
if (logfile) fflush(logfile);
|
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
|
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);
|
std::string mesg = fmt::format("WARNING: {} ({}:{})\n",
|
||||||
if (logflag && logfile) fprintf(logfile,"WARNING: %s (%s:%d)\n",
|
str,truncpath(file),line);
|
||||||
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)
|
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);
|
std::string mesg = fmt::format("{} ({}:{})\n",
|
||||||
if (logflag && logfile) fprintf(logfile,"%s (%s:%d)\n",str,truncpath(file),line);
|
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)
|
compiled with -DLAMMPS_EXCEPTIONS)
|
||||||
------------------------------------------------------------------------- */
|
------------------------------------------------------------------------- */
|
||||||
|
|
||||||
char * Error::get_last_error() const
|
std::string Error::get_last_error() const
|
||||||
{
|
{
|
||||||
return last_error_message;
|
return last_error_message;
|
||||||
}
|
}
|
||||||
@ -293,16 +281,9 @@ ErrorType Error::get_last_error_type() const
|
|||||||
(only used if compiled with -DLAMMPS_EXCEPTIONS)
|
(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;
|
last_error_message = msg;
|
||||||
|
|
||||||
if(msg) {
|
|
||||||
last_error_message = new char[strlen(msg)+1];
|
|
||||||
strcpy(last_error_message, msg);
|
|
||||||
} else {
|
|
||||||
last_error_message = NULL;
|
|
||||||
}
|
|
||||||
last_error_type = type;
|
last_error_type = type;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
21
src/error.h
21
src/error.h
@ -15,6 +15,7 @@
|
|||||||
#define LMP_ERROR_H
|
#define LMP_ERROR_H
|
||||||
|
|
||||||
#include "pointers.h"
|
#include "pointers.h"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#ifdef LAMMPS_EXCEPTIONS
|
#ifdef LAMMPS_EXCEPTIONS
|
||||||
#include "exceptions.h"
|
#include "exceptions.h"
|
||||||
@ -26,23 +27,23 @@ class Error : protected Pointers {
|
|||||||
public:
|
public:
|
||||||
Error(class LAMMPS *);
|
Error(class LAMMPS *);
|
||||||
|
|
||||||
void universe_all(const char *, int, const char *);
|
void universe_all(const std::string &, int, const std::string &);
|
||||||
void universe_one(const char *, int, const char *);
|
void universe_one(const std::string &, int, const std::string &);
|
||||||
void universe_warn(const char *, int, const char *);
|
void universe_warn(const std::string &, int, const std::string &);
|
||||||
|
|
||||||
void all(const char *, int, const char *);
|
void all(const std::string &, int, const std::string &);
|
||||||
void one(const char *, int, const char *);
|
void one(const std::string &, int, const std::string &);
|
||||||
void warning(const char *, int, const char *, int = 1);
|
void warning(const std::string &, int, const std::string &, int = 1);
|
||||||
void message(const char *, int, const char *, int = 1);
|
void message(const std::string &, int, const std::string &, int = 1);
|
||||||
void done(int = 0); // 1 would be fully backwards compatible
|
void done(int = 0); // 1 would be fully backwards compatible
|
||||||
|
|
||||||
#ifdef LAMMPS_EXCEPTIONS
|
#ifdef LAMMPS_EXCEPTIONS
|
||||||
char * get_last_error() const;
|
std::string get_last_error() const;
|
||||||
ErrorType get_last_error_type() 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:
|
private:
|
||||||
char * last_error_message;
|
std::string last_error_message;
|
||||||
ErrorType last_error_type;
|
ErrorType last_error_type;
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|||||||
@ -25,7 +25,7 @@ class LAMMPSException : public std::exception
|
|||||||
public:
|
public:
|
||||||
std::string message;
|
std::string message;
|
||||||
|
|
||||||
LAMMPSException(std::string msg) : message(msg) {
|
LAMMPSException(const std::string &msg) : message(msg) {
|
||||||
}
|
}
|
||||||
|
|
||||||
~LAMMPSException() throw() {
|
~LAMMPSException() throw() {
|
||||||
@ -40,7 +40,7 @@ class LAMMPSAbortException : public LAMMPSException {
|
|||||||
public:
|
public:
|
||||||
MPI_Comm universe;
|
MPI_Comm universe;
|
||||||
|
|
||||||
LAMMPSAbortException(std::string msg, MPI_Comm universe) :
|
LAMMPSAbortException(const std::string &msg, MPI_Comm universe) :
|
||||||
LAMMPSException(msg),
|
LAMMPSException(msg),
|
||||||
universe(universe)
|
universe(universe)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -78,12 +78,12 @@ using namespace LAMMPS_NS;
|
|||||||
MPI_Comm_size(ae.universe, &nprocs ); \
|
MPI_Comm_size(ae.universe, &nprocs ); \
|
||||||
\
|
\
|
||||||
if (nprocs > 1) { \
|
if (nprocs > 1) { \
|
||||||
error->set_last_error(ae.message.c_str(), ERROR_ABORT); \
|
error->set_last_error(ae.message, ERROR_ABORT); \
|
||||||
} else { \
|
} else { \
|
||||||
error->set_last_error(ae.message.c_str(), ERROR_NORMAL); \
|
error->set_last_error(ae.message, ERROR_NORMAL); \
|
||||||
} \
|
} \
|
||||||
} catch(LAMMPSException & e) { \
|
} catch(LAMMPSException & e) { \
|
||||||
error->set_last_error(e.message.c_str(), ERROR_NORMAL); \
|
error->set_last_error(e.message, ERROR_NORMAL); \
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
#define BEGIN_CAPTURE
|
#define BEGIN_CAPTURE
|
||||||
@ -1714,9 +1714,9 @@ int lammps_config_has_exceptions() {
|
|||||||
------------------------------------------------------------------------- */
|
------------------------------------------------------------------------- */
|
||||||
|
|
||||||
int lammps_has_error(void *ptr) {
|
int lammps_has_error(void *ptr) {
|
||||||
LAMMPS * lmp = (LAMMPS *) ptr;
|
LAMMPS *lmp = (LAMMPS *)ptr;
|
||||||
Error * error = lmp->error;
|
Error *error = lmp->error;
|
||||||
return error->get_last_error() ? 1 : 0;
|
return (error->get_last_error() != "") ? 1 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ----------------------------------------------------------------------
|
/* ----------------------------------------------------------------------
|
||||||
@ -1727,12 +1727,12 @@ int lammps_has_error(void *ptr) {
|
|||||||
------------------------------------------------------------------------- */
|
------------------------------------------------------------------------- */
|
||||||
|
|
||||||
int lammps_get_last_error_message(void *ptr, char * buffer, int buffer_size) {
|
int lammps_get_last_error_message(void *ptr, char * buffer, int buffer_size) {
|
||||||
LAMMPS * lmp = (LAMMPS *) ptr;
|
LAMMPS *lmp = (LAMMPS *)ptr;
|
||||||
Error * error = lmp->error;
|
Error *error = lmp->error;
|
||||||
|
|
||||||
if(error->get_last_error()) {
|
if(error->get_last_error() != "") {
|
||||||
int error_type = error->get_last_error_type();
|
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);
|
error->set_last_error(NULL, ERROR_NONE);
|
||||||
return error_type;
|
return error_type;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user