You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

343 lines
13 KiB

  1. /* -*- c++ -*- (enables emacs c++ mode) */
  2. /*===========================================================================
  3. Copyright (C) 2002-2012 Yves Renard
  4. This file is a part of GETFEM++
  5. Getfem++ is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU Lesser General Public License as published
  7. by the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version along with the GCC Runtime Library
  9. Exception either version 3.1 or (at your option) any later version.
  10. This program is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  13. License and GCC Runtime Library Exception for more details.
  14. You should have received a copy of the GNU Lesser General Public License
  15. along with this program; if not, write to the Free Software Foundation,
  16. Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  17. As a special exception, you may use this file as it is a part of a free
  18. software library without restriction. Specifically, if other files
  19. instantiate templates or use macros or inline functions from this file,
  20. or you compile this file and link it with other files to produce an
  21. executable, this file does not by itself cause the resulting executable
  22. to be covered by the GNU Lesser General Public License. This exception
  23. does not however invalidate any other reasons why the executable file
  24. might be covered by the GNU Lesser General Public License.
  25. ===========================================================================*/
  26. /** @file gmm_except.h
  27. @author Yves Renard <Yves.Renard@insa-lyon.fr>
  28. @author Julien Pommier <Julien.Pommier@insa-toulouse.fr>
  29. @date September 01, 2002.
  30. @brief Definition of basic exceptions.
  31. */
  32. #ifndef GMM_EXCEPT_H__
  33. #define GMM_EXCEPT_H__
  34. #include "gmm_std.h"
  35. namespace gmm {
  36. /* *********************************************************************** */
  37. /* Getfem++ generic errors. */
  38. /* *********************************************************************** */
  39. class gmm_error: public std::logic_error {
  40. public:
  41. gmm_error(const std::string& what_arg): std::logic_error (what_arg) {}
  42. };
  43. #ifdef GETFEM_HAVE_PRETTY_FUNCTION
  44. # define GMM_PRETTY_FUNCTION __PRETTY_FUNCTION__
  45. #else
  46. # define GMM_PRETTY_FUNCTION ""
  47. #endif
  48. // Errors : GMM_THROW should not be used on its own.
  49. // GMM_ASSERT1 : Non-maskable errors. Typically for in/ouput and
  50. // when the test do not significantly reduces the performance.
  51. // GMM_ASSERT2 : All tests which are potentially performance
  52. // consuming. Not hidden by default. Hidden when NDEBUG is
  53. // defined.
  54. // GMM_ASSERT3 : For internal checks. Hidden by default. Active
  55. // only when DEBUG_MODE is defined.
  56. // __EXCEPTIONS is defined by gcc, _CPPUNWIND is defined by visual c++
  57. #if defined(__EXCEPTIONS) || defined(_CPPUNWIND)
  58. inline void short_error_throw(const char *file, int line, const char *func,
  59. const char *errormsg) {
  60. std::stringstream msg;
  61. msg << "Error in " << file << ", line " << line << " " << func
  62. << ": \n" << errormsg << std::ends;
  63. throw gmm::gmm_error(msg.str());
  64. }
  65. # define GMM_THROW_(type, errormsg) { \
  66. std::stringstream msg; \
  67. msg << "Error in " << __FILE__ << ", line " \
  68. << __LINE__ << " " << GMM_PRETTY_FUNCTION << ": \n" \
  69. << errormsg << std::ends; \
  70. throw (type)(msg.str()); \
  71. }
  72. #else
  73. #ifndef _MSC_VER
  74. # define abort_no_return() ::abort()
  75. #else
  76. // apparently ::abort() on windows is not declared with __declspec(noreturn) so the compiler spits a lot of warnings when abort is used.
  77. # define abort_no_return() { assert("GMM ABORT"==0); throw "GMM ABORT"; }
  78. #endif
  79. inline void short_error_throw(const char *file, int line, const char *func,
  80. const char *errormsg) {
  81. std::stringstream msg;
  82. msg << "Error in " << file << ", line " << line << " " << func
  83. << ": \n" << errormsg << std::ends;
  84. std::cerr << msg.str() << std::endl;
  85. abort_no_return();
  86. }
  87. # define GMM_THROW_(type, errormsg) { \
  88. std::stringstream msg; \
  89. msg << "Error in " << __FILE__ << ", line " \
  90. << __LINE__ << " " << GMM_PRETTY_FUNCTION << ": \n" \
  91. << errormsg; \
  92. std::cerr << msg.str() << std::endl; \
  93. abort_no_return(); \
  94. }
  95. #endif
  96. # define GMM_ASSERT1(test, errormsg) \
  97. { if (!(test)) GMM_THROW_(gmm::gmm_error, errormsg); }
  98. // inline void GMM_THROW() IS_DEPRECATED;
  99. inline void GMM_THROW() {}
  100. #define GMM_THROW(a, b) { GMM_THROW_(a,b); gmm::GMM_THROW(); }
  101. #if defined(NDEBUG)
  102. # define GMM_ASSERT2(test, errormsg) {}
  103. # define GMM_ASSERT3(test, errormsg) {}
  104. #elif !defined(GMM_FULL_NDEBUG)
  105. # define GMM_ASSERT2(test, errormsg) \
  106. { if (!(test)) gmm::short_error_throw(__FILE__, __LINE__, \
  107. GMM_PRETTY_FUNCTION, errormsg); }
  108. # define GMM_ASSERT3(test, errormsg) \
  109. { if (!(test)) gmm::short_error_throw(__FILE__, __LINE__, \
  110. GMM_PRETTY_FUNCTION, errormsg); }
  111. #else
  112. # define GMM_ASSERT2(test, errormsg) \
  113. { if (!(test)) gmm::short_error_throw(__FILE__, __LINE__, \
  114. GMM_PRETTY_FUNCTION, errormsg); }
  115. # define GMM_ASSERT3(test, errormsg)
  116. #endif
  117. /* *********************************************************************** */
  118. /* Getfem++ warnings. */
  119. /* *********************************************************************** */
  120. // This allows to dynamically hide warnings
  121. struct warning_level {
  122. static int level(int l = -2)
  123. { static int level_ = 3; return (l != -2) ? (level_ = l) : level_; }
  124. };
  125. inline void set_warning_level(int l) { warning_level::level(std::max(0,l)); }
  126. inline int get_warning_level(void) { return warning_level::level(-2); }
  127. // This allow not too compile some Warnings
  128. #ifndef GMM_WARNING_LEVEL
  129. # define GMM_WARNING_LEVEL 4
  130. #endif
  131. // Warning levels : 0 always printed
  132. // 1 very important : specify a possible error in the code.
  133. // 2 important : specify a default of optimization for inst.
  134. // 3 remark
  135. // 4 ignored by default.
  136. #define GMM_WARNING_MSG(level_, thestr) { \
  137. std::stringstream msg; \
  138. msg << "Level " << level_ << " Warning in " << __FILE__ << ", line " \
  139. << __LINE__ << ": " << thestr; \
  140. std::cerr << msg.str() << std::endl; \
  141. }
  142. #define GMM_WARNING0(thestr) GMM_WARNING_MSG(0, thestr)
  143. #if GMM_WARNING_LEVEL > 0
  144. # define GMM_WARNING1(thestr) \
  145. { if (1 <= gmm::warning_level::level()) GMM_WARNING_MSG(1, thestr) }
  146. #else
  147. # define GMM_WARNING1(thestr) {}
  148. #endif
  149. #if GMM_WARNING_LEVEL > 1
  150. # define GMM_WARNING2(thestr) \
  151. { if (2 <= gmm::warning_level::level()) GMM_WARNING_MSG(2, thestr) }
  152. #else
  153. # define GMM_WARNING1(thestr) {}
  154. #endif
  155. #if GMM_WARNING_LEVEL > 2
  156. # define GMM_WARNING3(thestr) \
  157. { if (3 <= gmm::warning_level::level()) GMM_WARNING_MSG(3, thestr) }
  158. #else
  159. # define GMM_WARNING1(thestr) {}
  160. #endif
  161. #if GMM_WARNING_LEVEL > 3
  162. # define GMM_WARNING4(thestr) \
  163. { if (4 <= gmm::warning_level::level()) GMM_WARNING_MSG(4, thestr) }
  164. #else
  165. # define GMM_WARNING1(thestr) {}
  166. #endif
  167. /* *********************************************************************** */
  168. /* Getfem++ traces. */
  169. /* *********************************************************************** */
  170. // This allows to dynamically hide traces
  171. struct traces_level {
  172. static int level(int l = -2)
  173. { static int level_ = 3; return (l != -2) ? (level_ = l) : level_; }
  174. };
  175. inline void set_traces_level(int l) { traces_level::level(std::max(0,l)); }
  176. // This allow not too compile some Warnings
  177. #ifndef GMM_TRACES_LEVEL
  178. # define GMM_TRACES_LEVEL 3
  179. #endif
  180. // Traces levels : 0 always printed
  181. // 1 Susceptible to occur once in a program.
  182. // 2 Susceptible to occur occasionnaly in a program (10).
  183. // 3 Susceptible to occur often (100).
  184. // 4 Susceptible to occur very often (>1000).
  185. #define GMM_TRACE_MSG_MPI // for Parallelized version
  186. #define GMM_TRACE_MSG(level_, thestr) { \
  187. GMM_TRACE_MSG_MPI { \
  188. std::stringstream msg; \
  189. msg << "Trace " << level_ << " in " << __FILE__ << ", line " \
  190. << __LINE__ << ": " << thestr; \
  191. std::cout << msg.str() << std::endl; \
  192. } \
  193. }
  194. #define GMM_TRACE0(thestr) GMM_TRACE_MSG(0, thestr)
  195. #if GMM_TRACES_LEVEL > 0
  196. # define GMM_TRACE1(thestr) \
  197. { if (1 <= gmm::traces_level::level()) GMM_TRACE_MSG(1, thestr) }
  198. #else
  199. # define GMM_TRACE1(thestr) {}
  200. #endif
  201. #if GMM_TRACES_LEVEL > 1
  202. # define GMM_TRACE2(thestr) \
  203. { if (2 <= gmm::traces_level::level()) GMM_TRACE_MSG(2, thestr) }
  204. #else
  205. # define GMM_TRACE2(thestr) {}
  206. #endif
  207. #if GMM_TRACES_LEVEL > 2
  208. # define GMM_TRACE3(thestr) \
  209. { if (3 <= gmm::traces_level::level()) GMM_TRACE_MSG(3, thestr) }
  210. #else
  211. # define GMM_TRACE3(thestr) {}
  212. #endif
  213. #if GMM_TRACES_LEVEL > 3
  214. # define GMM_TRACE4(thestr) \
  215. { if (4 <= gmm::traces_level::level()) GMM_TRACE_MSG(4, thestr) }
  216. #else
  217. # define GMM_TRACE4(thestr) {}
  218. #endif
  219. /* ********************************************************************* */
  220. /* Definitions for compatibility with old versions. */
  221. /* ********************************************************************* */
  222. using std::invalid_argument;
  223. struct dimension_error : public std::logic_error
  224. { dimension_error(const std::string& w): std::logic_error(w) {} };
  225. struct file_not_found_error : public std::logic_error
  226. { file_not_found_error(const std::string& w): std::logic_error (w) {} };
  227. struct internal_error : public std::logic_error
  228. { internal_error(const std::string& w): std::logic_error(w) {} };
  229. struct failure_error : public std::logic_error
  230. { failure_error(const std::string& w): std::logic_error (w) {} };
  231. struct not_linear_error : public std::logic_error
  232. { not_linear_error(const std::string& w): std::logic_error (w) {} };
  233. struct to_be_done_error : public std::logic_error
  234. { to_be_done_error(const std::string& w): std::logic_error (w) {} };
  235. #define GMM_STANDARD_CATCH_ERROR catch(std::logic_error e) \
  236. { \
  237. std::cerr << "============================================\n"; \
  238. std::cerr << "| An error has been detected !!! |\n"; \
  239. std::cerr << "============================================\n"; \
  240. std::cerr << e.what() << std::endl << std::endl; \
  241. exit(1); \
  242. } \
  243. catch(std::runtime_error e) \
  244. { \
  245. std::cerr << "============================================\n"; \
  246. std::cerr << "| An error has been detected !!! |\n"; \
  247. std::cerr << "============================================\n"; \
  248. std::cerr << e.what() << std::endl << std::endl; \
  249. exit(1); \
  250. } \
  251. catch(std::bad_alloc) { \
  252. std::cerr << "============================================\n"; \
  253. std::cerr << "| A bad allocation has been detected !!! |\n"; \
  254. std::cerr << "============================================\n"; \
  255. exit(1); \
  256. } \
  257. catch(std::bad_typeid) { \
  258. std::cerr << "============================================\n"; \
  259. std::cerr << "| A bad typeid has been detected !!! |\n"; \
  260. std::cerr << "============================================\n"; \
  261. exit(1); \
  262. } \
  263. catch(std::bad_exception) { \
  264. std::cerr << "============================================\n"; \
  265. std::cerr << "| A bad exception has been detected !!! |\n"; \
  266. std::cerr << "============================================\n"; \
  267. exit(1); \
  268. } \
  269. catch(std::bad_cast) { \
  270. std::cerr << "============================================\n"; \
  271. std::cerr << "| A bad cast has been detected !!! |\n"; \
  272. std::cerr << "============================================\n"; \
  273. exit(1); \
  274. } \
  275. catch(...) { \
  276. std::cerr << "============================================\n"; \
  277. std::cerr << "| An unknown error has been detected !!! |\n"; \
  278. std::cerr << "============================================\n"; \
  279. exit(1); \
  280. }
  281. // catch(ios_base::failure) {
  282. // std::cerr << "============================================\n";
  283. // std::cerr << "| A ios_base::failure has been detected !!!|\n";
  284. // std::cerr << "============================================\n";
  285. // exit(1);
  286. // }
  287. #if defined(__GNUC__) && (__GNUC__ > 3)
  288. # define GMM_SET_EXCEPTION_DEBUG \
  289. std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
  290. #else
  291. # define GMM_SET_EXCEPTION_DEBUG
  292. #endif
  293. }
  294. #endif /* GMM_EXCEPT_H__ */