Ice Sheet System Model  4.18
Code documentation
Exceptions.cpp
Go to the documentation of this file.
1 /* \file Exceptions.cpp
2  * \brief: implementation of the exceptions.
3  */
4 
5 #ifdef HAVE_CONFIG_H
6 #include <config.h>
7 #else
8 #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
9 #endif
10 
11 #include <cstring>
12 #include <cstdio>
13 #include <string>
14 #include <iostream>
15 #include <iomanip>
16 #include "./exceptions.h"
17 #include "../io/Print/Print.h"
18 #include "../io/Comm/IssmComm.h"
19 #include "../MemOps/MemOps.h"
20 
21 ErrorException::ErrorException(const string & what_arg){/*{{{*/
22 
23  int len;
24  len = strlen(what_arg.c_str())+1;
25  what_str = new char[len];
26  memcpy(what_str,what_arg.c_str(),len);
27 
28  file_name = NULL;
29  function_name = NULL;
30  file_line = 0;
31 
32 }/*}}}*/
33 ErrorException::ErrorException(const string& what_file, const string& what_function,int what_line, const string& what_arg){/*{{{*/
34 
35  int len;
36 
37  len = strlen(what_arg.c_str())+1;
38  what_str = new char[len];
39  memcpy(what_str,what_arg.c_str(),len);
40 
41  len = strlen(what_file.c_str())+1;
42  file_name = new char[len];
43  memcpy(file_name,what_file.c_str(),len);
44 
45  len = strlen(what_function.c_str())+1;
46  function_name = new char[len];
47  memcpy(function_name,what_function.c_str(),len);
48 
49  file_line= what_line;
50  /*When error messages are not shown properly, uncomment the following line*/
51  //this->Report();
52 
53 }/*}}}*/
55  delete [] what_str;
56  delete [] file_name;
57  delete [] function_name;
58 }/*}}}*/
59 const char* ErrorException::what() const throw(){/*{{{*/
60  //this->Report();
61  return what_str;
62 }/*}}}*/
63 void ErrorException::Report() const{/*{{{*/
64 
65  /*WINDOWS*/
66  if(!function_name || file_line==0){
67  _printf_("Error message: " << what());
68  return;
69  }
70 
71  /*recover my_rank and num_procs:*/
72  int my_rank = IssmComm::GetRank();
73  int num_procs = IssmComm::GetSize();
74 
75  if(num_procs==1){
76  _printf_("\n??? Error in ==> " << file_name << ":" << file_line << "\n");
77  _printf_(function_name << " error message: " << what() << "\n\n");
78  }
79  else{
80  _printf_("\n[" << my_rank<< "] ??? Error using ==> " << file_name << ":" << file_line << "\n");
81  _printf_( "[" << my_rank << "] " << function_name << " error message: " << what() << "\n\n");
82  }
83 
84  return;
85 }/*}}}*/
86 const char* ErrorException::WrapperReport() const{/*{{{*/
87 
88  /*Output*/
89  std::ostringstream buffer;
90  char *message = NULL;
91 
92  /*WINDOWS*/
93  if(!function_name || file_line==0){
94  buffer << " error message: " << this->what_str;
95  }
96  else{
97  buffer << "\nError in ==> " << this->file_name << ":" << file_line << "\n";
98  buffer << this->function_name << " error message: " << this->what_str;
99  }
100 
101  /*Convert std::ostringstream to std::string and then create char* */
102  std::string buffer2 = buffer.str();
103  message = xNew<char>(strlen(buffer2.c_str())+1); sprintf(message,"%s",buffer2.c_str());
104  return message;
105 }/*}}}*/
ErrorException::file_name
char * file_name
Definition: exceptions.h:84
exceptions.h
: two types of exceptions are handled for now. Errors, and warnings. Those exceptions are trapped pro...
ErrorException::function_name
char * function_name
Definition: exceptions.h:83
_printf_
#define _printf_(StreamArgs)
Definition: Print.h:22
ErrorException::~ErrorException
~ErrorException()
Definition: Exceptions.cpp:54
ErrorException::WrapperReport
const char * WrapperReport() const
Definition: Exceptions.cpp:86
ErrorException::Report
void Report() const
Definition: Exceptions.cpp:63
ErrorException::what
virtual const char * what() const
Definition: Exceptions.cpp:59
IssmComm::GetSize
static int GetSize(void)
Definition: IssmComm.cpp:46
ErrorException::ErrorException
ErrorException(const string &what_arg)
Definition: Exceptions.cpp:21
ErrorException::what_str
char * what_str
Definition: exceptions.h:82
IssmComm::GetRank
static int GetRank(void)
Definition: IssmComm.cpp:34
ErrorException::file_line
int file_line
Definition: exceptions.h:85