ug4
error.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2015: G-CSC, Goethe University Frankfurt
3  * Authors: Sebastian Reiter, Andreas Vogel
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 #ifndef __H__UG__COMMON__ERROR__
34 #define __H__UG__COMMON__ERROR__
35 
36 #include <string>
37 #include <vector>
38 #include <sstream>
39 
40 #include <exception>
41 
44 
45 void ug_throw_error();
46 
47 #ifdef __GNUC__
48 #define PRETTY_FUNCTION __PRETTY_FUNCTION__
49 #else
50 #define PRETTY_FUNCTION __FUNCTION__
51 #endif
52 
54 // UG Throw / Catch
56 
57 #define UG_THROW(msg) {ug_throw_error(); std::stringstream __ss; __ss << msg; \
58  throw(ug::UGError(__ss.str(),__FILE__,__LINE__));}
59 
61 #define UG_COND_THROW(cond, msg) { if(cond) { UG_THROW(msg); } }
62 
63 
64 #define UG_CATCH_THROW(msg) catch(ug::UGError& err){std::stringstream __ss; __ss << msg;\
65  err.push_msg(__ss.str(),__FILE__,__LINE__); throw(err);} \
66  catch(const std::exception& ex) { std::stringstream __ss; __ss << msg;\
67  throw ug::UGError(__ss.str(), ex,__FILE__,__LINE__); }
68 
69 #define UG_CATCH_PRINT(msg) \
70  catch (ug::UGError& err)\
71  {\
72  std::stringstream __ss;\
73  __ss << msg;\
74  err.push_msg(__ss.str(), __FILE__, __LINE__);\
75  UG_LOG(err.get_stacktrace());\
76  }\
77  catch (const std::exception& ex)\
78  {\
79  std::stringstream __ss;\
80  __ss << msg;\
81  ug::UGError err(__ss.str(), ex, __FILE__, __LINE__);\
82  UG_LOG(err.get_stacktrace());\
83  }
84 
85 #define UG_CATCH_THROW_FUNC() UG_CATCH_THROW(PRETTY_FUNCTION << "failed. ")
86 
87 // end group ugbase_common
89 
91 // UG Error
93 
94 namespace ug{
95 
96 std::string ErrorStringFromStdException(const std::exception *pex);
97 
100 
102 
103 class UGError
104 {
105  public:
106  UGError(const char* msg,
107  const char* file = " -- no file -- ", const unsigned long line = 0)
108  {push_msg(msg, file, line);}
109  UGError(const std::string& msg,
110  const char* file = " -- no file -- ", const unsigned long line = 0)
111  {push_msg(msg, file, line);}
112 
113  UGError(const std::string &msg, const std::exception &ex, const char *file, const unsigned long line);
114 
116  virtual ~UGError() {}
117 
119  void push_msg(const std::string& msg, const char* file = " -- no file -- ",
120  const unsigned long line = 0)
121  {
122  m_vMsg.push_back(msg);
123  m_vFile.push_back(file);
124  m_vLine.push_back(line);
125  }
126 
128  void push_msg(const char* msg, const char* file = " -- no file -- ",
129  const unsigned long line = 0)
130  {
131  m_vMsg.push_back(msg);
132  m_vFile.push_back(file);
133  m_vLine.push_back(line);
134  }
135 
137  const std::string& get_msg() const {return m_vMsg.at(0);}
138 
140  size_t num_msg() const {return m_vMsg.size();}
141 
143  const std::string& get_msg(size_t i) const {return m_vMsg.at(i);}
144 
146  const std::string& get_file(size_t i) const {return m_vFile.at(i);}
147 
149  unsigned long get_line(size_t i) const{return m_vLine.at(i);}
150 
151  std::string get_stacktrace() const
152  {
153  std::stringstream ss;
154  for(size_t i = 0; i < num_msg(); ++i)
155  {
156  ss << get_file(i) << ':' << get_line(i) << " : " << get_msg(i) << '\n';
157  }
158  return ss.str();
159  }
160 
161  protected:
162  std::vector<std::string> m_vMsg; //< Message stack
163  std::vector<std::string> m_vFile; //< File stack
164  std::vector<unsigned long> m_vLine; //< Line stack
165 };
166 
167 
170 
172 class SoftAbort : public UGError
173 {
174  public:
175  SoftAbort(std::string msg) : UGError(msg.c_str()) {}
176 };
177 
178 
180 // some basic assertions
181 #define THROW_IF_NOT_EQUAL(s1, s2) { UG_COND_THROW(s1 != s2, "mismatch: " << UG_TO_STRING(s1) << " = " << s1 << " != " << UG_TO_STRING(s2) << " = " << s2 << "."); }
182 #define THROW_IF_NOT_EQUAL_3(s1, s2, s3) { THROW_IF_NOT_EQUAL(s1, s2); THROW_IF_NOT_EQUAL(s1, s3); }
183 #define THROW_IF_NOT_EQUAL_4(s1, s2, s3, s4) { THROW_IF_NOT_EQUAL(s1, s2); THROW_IF_NOT_EQUAL(s1, s3); THROW_IF_NOT_EQUAL(s1, s4); }
184 
185 #define ASSERT_EQUAL(s1, s2) { UG_COND_THROW(s1 != s2, "mismatch: " << UG_TO_STRING(s1) << " = " << s1 << " != " << UG_TO_STRING(s2) << " = " << s2 << "."); }
186 #define ASSERT_EQUAL_3(s1, s2, s3) { ASSERT_EQUAL(s1, s2); ASSERT_EQUAL(s1, s3); }
187 #define ASSERT_EQUAL_4(s1, s2, s3, s4) { ASSERT_EQUAL(s1, s2); ASSERT_EQUAL(s1, s3); ASSERT_EQUAL(s1, s4); }
188 
189 
190 // end group ugbase_common
192 
193 } // end namespace ug
194 
195 #endif /* __H__UG__COMMON__ERROR__ */
This special error is used to perform a soft-abort e.g. during script execution.
Definition: error.h:173
SoftAbort(std::string msg)
Definition: error.h:175
Instances of this class or of derived classes are thrown if errors arise.
Definition: error.h:104
std::vector< unsigned long > m_vLine
Definition: error.h:164
void push_msg(const std::string &msg, const char *file=" -- no file -- ", const unsigned long line=0)
adds a message to the message stack
Definition: error.h:119
const std::string & get_msg(size_t i) const
returns a message in the message-stack (innermost is first)
Definition: error.h:143
void push_msg(const char *msg, const char *file=" -- no file -- ", const unsigned long line=0)
adds a message to the message stack
Definition: error.h:128
std::string get_stacktrace() const
Definition: error.h:151
const std::string & get_file(size_t i) const
returns the file where a message occured
Definition: error.h:146
virtual ~UGError()
virtual destructor
Definition: error.h:116
std::vector< std::string > m_vMsg
Definition: error.h:162
unsigned long get_line(size_t i) const
returns the line where a message occured
Definition: error.h:149
std::vector< std::string > m_vFile
Definition: error.h:163
const std::string & get_msg() const
returns the initial message
Definition: error.h:137
UGError(const std::string &msg, const char *file=" -- no file -- ", const unsigned long line=0)
Definition: error.h:109
UGError(const char *msg, const char *file=" -- no file -- ", const unsigned long line=0)
Definition: error.h:106
size_t num_msg() const
number of messages in message-stack
Definition: error.h:140
parameterString ex
Executes the specified script.
Definition: command_line_util.lua:350
void ug_throw_error()
called whenever UG_THROW or UG_THROW_REGISTRY_ERROR is called.
Definition: assert.cpp:164
the ug namespace
std::string ErrorStringFromStdException(const std::exception *pex)
Definition: error.cpp:39