SynAPSE  0.6.0
Synergistic Analysis of Phase Space Evolution
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
Exception.hh
1 #ifndef EXCEPTION_HH
2 #define EXCEPTION_HH
3 
4 // C++ includes
5 #include <cstdio>
6 #include <cstring>
7 #include <exception>
8 #include <string>
9 #include <vector>
10 #include <ostream>
11 #include <sstream>
12 
13 // Other includes
14 #include "ExceptionLevel.hh"
15 
18 namespace Exceptions {
19 
30 class Exception : public std::exception {
31  public:
32 
34  Exception() throw();
35 
43  std::string errorMessage,
44  std::string location) throw();
45 
47  ~Exception() throw() {}
48 
52  const char* what() const throw() { return &_what[0]; }
53 
59  void Print();
60 
62  inline exceptionLevel GetErrorLevel() const { return _level; }
63 
65  inline std::string GetMessage() const { return _message; }
66 
68  inline void SetMessage(std::string new_message);
69 
71  std::string GetLocation() const { return _location; }
72 
74  std::string GetStackTrace() const { return _stacktrace; }
75 
82  static std::string MakeStackTrace(size_t skipTrace);
83 
85  static void SetWillDoStackTrace(bool willDoStackTrace);
86 
88  static bool GetWillDoStackTrace();
89 
90  private:
92  inline void SetWhat(std::string what_str);
93 
94  static const size_t _maxStackSize;
95  static bool _willDoStackTrace;
96  std::string _message;
97  std::string _location;
98  std::string _stacktrace;
99  std::vector<char> _what;
101 };
102 
103 void Exception::SetMessage(std::string new_message) {
104  _message = new_message;
105  SetWhat("\n["+_location+"] "+_message);
106 }
107 
108 void Exception::SetWhat(std::string what_str) {
109  _what = std::vector<char>(what_str.size()+1);
110  snprintf(&_what[0], what_str.size()+1, "%s", what_str.c_str());
111 }
112 
113 } // namespace Exceptions
114 
115 #endif
const char * what() const
Return char buffer of message+" at "+location, memory remains owned by Exception. ...
Definition: Exception.hh:52
static void SetWillDoStackTrace(bool willDoStackTrace)
Set that it makes a stack trace on exception.
Definition: Exception.cc:40
void SetMessage(std::string new_message)
Set the error message.
Definition: Exception.hh:103
exceptionLevel GetErrorLevel() const
Get the severity of the exception.
Definition: Exception.hh:62
void SetWhat(std::string what_str)
Sets the reason for the exception.
Definition: Exception.hh:108
void Print()
Print the error message and location.
Definition: Exception.cc:33
std::string _message
Message to be thrown.
Definition: Exception.hh:96
Global exception object for all non-standard exceptions thrown by the code.
Definition: Exception.hh:30
static bool _willDoStackTrace
Returns the stack trace if true.
Definition: Exception.hh:95
std::string GetMessage() const
Get the error message (as defined by constructor)
Definition: Exception.hh:65
static bool GetWillDoStackTrace()
Return whether makes a stack trace on exception.
Definition: Exception.cc:44
~Exception()
Destructor.
Definition: Exception.hh:47
static const size_t _maxStackSize
Maximum memory size of the stack.
Definition: Exception.hh:94
std::string _location
Name of the function that threw.
Definition: Exception.hh:97
exceptionLevel
ExceptionLevel enumerates the severity of the exception.
Definition: ExceptionLevel.hh:16
std::string GetStackTrace() const
Return the stack trace if it was stored.
Definition: Exception.hh:74
std::string GetLocation() const
Get the location (as defined by Exception constructor) of the error.
Definition: Exception.hh:71
std::string _stacktrace
Stack trace as an std::string.
Definition: Exception.hh:98
std::vector< char > _what
Reason for throwing.
Definition: Exception.hh:99
exceptionLevel _level
Severity of the exception.
Definition: Exception.hh:100
static std::string MakeStackTrace(size_t skipTrace)
Gcc-specific code to recover the stack trace as a string.
Definition: Exception.cc:49
Exception()
Default constructor, initilizes everything to 0.
Definition: Exception.cc:14