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 <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",
|
||||
universe->me,str,truncpath(file),line);
|
||||
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,11 +115,11 @@ 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",
|
||||
universe->me,str,truncpath(file),line);
|
||||
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",
|
||||
me,str,truncpath(file),line,lastcmd);
|
||||
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",
|
||||
str,truncpath(file),line);
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user