20 #include "pism/util/error_handling.hh"
29 : filename(NULL), line_number(0) {
34 : filename(name), line_number(line) {
41 : std::runtime_error(message), m_location(location) {
51 va_start(argp, format);
52 vsnprintf(buffer,
sizeof(buffer), format, argp);
74 va_start(argp, format);
75 vsnprintf(buffer,
sizeof(buffer), format, argp);
83 PetscErrorCode ierr = 0;
84 std::string error =
"PISM ERROR: ";
85 std::string message = this->what();
87 std::string padding = std::string(error.size(),
' ');
90 size_t k = message.find(
'\n', 0);
91 while (
k != std::string::npos) {
92 message.insert(
k+1, padding);
93 k = message.find(
'\n',
k+1);
97 ierr = PetscPrintf(com,
98 "%s%s\n", error.c_str(), message.c_str()); CHKERRCONTINUE(ierr);
101 std::string while_str = std::string(error.size(),
' ') +
"while ";
102 padding = std::string(while_str.size() + 1,
' ');
109 k = message.find(
'\n', 0);
110 while (
k != std::string::npos) {
111 message.insert(
k+1, padding);
112 k = message.find(
'\n',
k+1);
116 ierr = PetscPrintf(com,
117 "%s%s\n", while_str.c_str(), message.c_str()); CHKERRCONTINUE(ierr);
121 padding = std::string(error.size(),
' ');
122 ierr = PetscPrintf(com,
123 "%sError location: %s, line %d\n",
142 catch (std::exception &e) {
143 ierr = PetscPrintf(PETSC_COMM_SELF,
145 "PISM ERROR: Caught a C++ standard library exception: \"%s\".\n"
146 " This is probably a bug in PISM.\n"
147 " Please send a report to uaf-pism@alaska.edu\n"
149 e.what()); CHKERRCONTINUE(ierr);
151 ierr = PetscPrintf(PETSC_COMM_SELF,
153 "PISM ERROR: Caught an unexpected exception.\n"
154 " This is probably a bug in PISM.\n"
155 " Please send a report to uaf-pism@alaska.edu\n"
157 CHKERRCONTINUE(ierr);
162 const char* function_name,
const char *file,
int line) {
163 if (errcode != success) {
165 function_name, file, line);
170 const char* function_name,
const char *file,
int line) {
172 CHKERRCONTINUE(errcode);
177 : m_failed(false), m_com(com) {
187 MPI_Comm_rank(
m_com, &rank);
189 PetscFPrintf(MPI_COMM_SELF, stderr,
190 "PISM ERROR: Rank %d failed with the following message.\n", rank);
203 int success_flag =
m_failed ? 0 : 1;
204 int success_flag_global = 0;
206 MPI_Allreduce(&success_flag, &success_flag_global, 1, MPI_INT, MPI_LAND,
m_com);
208 if (success_flag_global == 0) {
ParallelSection(MPI_Comm com)
void failed()
Indicates a failure of a parallel section.
void(* Hook)(RuntimeError *)
RuntimeError(const ErrorLocation &location, const std::string &message)
static RuntimeError static void set_hook(Hook new_hook)
void add_context(const std::string &message)
Add a message providing some context. This way we can (sort of) get a stack trace even though C++ exc...
std::vector< std::string > m_context
void void print(MPI_Comm com)
static RuntimeError formatted(const ErrorLocation &location, const char format[],...) __attribute__((format(printf
build a RuntimeError with a formatted message
#define PISM_ERROR_LOCATION
void check_c_call(int errcode, int success, const char *function_name, const char *file, int line)
void handle_fatal_errors(MPI_Comm com)
void check_petsc_call(int errcode, const char *function_name, const char *file, int line)