Line data Source code
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 :
42 : /// \addtogroup ugbase_common
43 : /// \{
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 :
53 : ////////////////////////////////////////////////////////////////////////////////
54 : // UG Throw / Catch
55 : ////////////////////////////////////////////////////////////////////////////////
56 :
57 : #define UG_THROW(msg) {ug_throw_error(); std::stringstream __ss; __ss << msg; \
58 : throw(ug::UGError(__ss.str(),__FILE__,__LINE__));}
59 :
60 : /// UG_COND_THROW(cond, msg) : performs a UG_THROW(msg) if cond == true
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
88 : /// \}
89 :
90 : ////////////////////////////////////////////////////////////////////////////////
91 : // UG Error
92 : ////////////////////////////////////////////////////////////////////////////////
93 :
94 : namespace ug{
95 :
96 : std::string ErrorStringFromStdException(const std::exception *pex);
97 :
98 : /// \addtogroup ugbase_common
99 : /// \{
100 :
101 : /// Instances of this class or of derived classes are thrown if errors arise.
102 : /** By default the error-code is 0 and terminate returns false.*/
103 : class UGError
104 : {
105 : public:
106 0 : UGError(const char* msg,
107 : const char* file = " -- no file -- ", const unsigned long line = 0)
108 0 : {push_msg(msg, file, line);}
109 1 : UGError(const std::string& msg,
110 : const char* file = " -- no file -- ", const unsigned long line = 0)
111 1 : {push_msg(msg, file, line);}
112 :
113 : UGError(const std::string &msg, const std::exception &ex, const char *file, const unsigned long line);
114 :
115 : /// virtual destructor
116 1 : virtual ~UGError() {}
117 :
118 : /// adds a message to the message stack
119 1 : void push_msg(const std::string& msg, const char* file = " -- no file -- ",
120 : const unsigned long line = 0)
121 : {
122 1 : m_vMsg.push_back(msg);
123 1 : m_vFile.push_back(file);
124 1 : m_vLine.push_back(line);
125 1 : }
126 :
127 : /// adds a message to the message stack
128 0 : void push_msg(const char* msg, const char* file = " -- no file -- ",
129 : const unsigned long line = 0)
130 : {
131 0 : m_vMsg.push_back(msg);
132 0 : m_vFile.push_back(file);
133 0 : m_vLine.push_back(line);
134 0 : }
135 :
136 : /// returns the initial message
137 : const std::string& get_msg() const {return m_vMsg.at(0);}
138 :
139 : /// number of messages in message-stack
140 : size_t num_msg() const {return m_vMsg.size();}
141 :
142 : /// returns a message in the message-stack (innermost is first)
143 : const std::string& get_msg(size_t i) const {return m_vMsg.at(i);}
144 :
145 : /// returns the file where a message occured
146 : const std::string& get_file(size_t i) const {return m_vFile.at(i);}
147 :
148 : /// returns the line where a message occured
149 0 : unsigned long get_line(size_t i) const{return m_vLine.at(i);}
150 :
151 0 : std::string get_stacktrace() const
152 : {
153 0 : std::stringstream ss;
154 0 : for(size_t i = 0; i < num_msg(); ++i)
155 : {
156 0 : ss << get_file(i) << ':' << get_line(i) << " : " << get_msg(i) << '\n';
157 : }
158 0 : return ss.str();
159 0 : }
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 :
168 : ////////////////////////////////////////////////////////////////////////////////
169 : /// This special error is used to perform a soft-abort e.g. during script execution.
170 : /** Note that SoftAbort isn't an error per se. It is just a tool to use the error-handling
171 : * mechanism to exit through multiple layers of code-execution.*/
172 0 : class SoftAbort : public UGError
173 : {
174 : public:
175 0 : SoftAbort(std::string msg) : UGError(msg.c_str()) {}
176 : };
177 :
178 :
179 : ////////////////////////////////////////////////////////////////////////////////
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
191 : /// \}
192 :
193 : } // end namespace ug
194 :
195 : #endif /* __H__UG__COMMON__ERROR__ */
|