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.

328 lines
12 KiB

  1. /* -*- c++ -*- (enables emacs c++ mode) */
  2. /*===========================================================================
  3. Copyright (C) 2002-2017 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. //provides external implementation of gmm_exception and logging.
  36. #ifndef EXTERNAL_EXCEPT_
  37. namespace gmm {
  38. /* *********************************************************************** */
  39. /* GetFEM++ generic errors. */
  40. /* *********************************************************************** */
  41. class gmm_error: public std::logic_error {
  42. public:
  43. gmm_error(const std::string& what_arg): std::logic_error (what_arg) {}
  44. };
  45. #ifdef GETFEM_HAVE_PRETTY_FUNCTION
  46. # define GMM_PRETTY_FUNCTION __PRETTY_FUNCTION__
  47. #else
  48. # define GMM_PRETTY_FUNCTION ""
  49. #endif
  50. // Errors : GMM_THROW should not be used on its own.
  51. // GMM_ASSERT1 : Non-maskable errors. Typically for in/ouput and
  52. // when the test do not significantly reduces the performance.
  53. // GMM_ASSERT2 : All tests which are potentially performance
  54. // consuming. Not hidden by default. Hidden when NDEBUG is
  55. // defined.
  56. // GMM_ASSERT3 : For internal checks. Hidden by default. Active
  57. // only when DEBUG_MODE is defined.
  58. // __EXCEPTIONS is defined by gcc, _CPPUNWIND is defined by visual c++
  59. #if defined(__EXCEPTIONS) || defined(_CPPUNWIND)
  60. inline void short_error_throw(const char *file, int line, const char *func,
  61. const char *errormsg) {
  62. std::stringstream msg__;
  63. msg__ << "Error in " << file << ", line " << line << " " << func
  64. << ": \n" << errormsg << std::ends;
  65. throw gmm::gmm_error(msg__.str());
  66. }
  67. # define GMM_THROW_(type, errormsg) { \
  68. std::stringstream msg__; \
  69. msg__ << "Error in " << __FILE__ << ", line " \
  70. << __LINE__ << " " << GMM_PRETTY_FUNCTION << ": \n" \
  71. << errormsg << std::ends; \
  72. throw (type)(msg__.str()); \
  73. }
  74. #else
  75. #ifndef _MSC_VER
  76. # define abort_no_return() ::abort()
  77. #else
  78. // apparently ::abort() on windows is not declared with __declspec(noreturn) so the compiler spits a lot of warnings when abort is used.
  79. # define abort_no_return() { assert("GMM ABORT"==0); throw "GMM ABORT"; }
  80. #endif
  81. inline void short_error_throw(const char *file, int line, const char *func,
  82. const char *errormsg) {
  83. std::stringstream msg__;
  84. msg__ << "Error in " << file << ", line " << line << " " << func
  85. << ": \n" << errormsg << std::ends;
  86. std::cerr << msg__.str() << std::endl;
  87. abort_no_return();
  88. }
  89. # define GMM_THROW_(type, errormsg) { \
  90. std::stringstream msg__; \
  91. msg__ << "Error in " << __FILE__ << ", line " \
  92. << __LINE__ << " " << GMM_PRETTY_FUNCTION << ": \n" \
  93. << errormsg; \
  94. std::cerr << msg__.str() << std::endl; \
  95. abort_no_return(); \
  96. }
  97. #endif
  98. # define GMM_ASSERT1(test, errormsg) \
  99. { if (!(test)) GMM_THROW_(gmm::gmm_error, errormsg); }
  100. inline void GMM_THROW() {}
  101. #define GMM_THROW(a, b) { GMM_THROW_(a,b); gmm::GMM_THROW(); }
  102. #if defined(NDEBUG)
  103. # define GMM_ASSERT2(test, errormsg) {}
  104. # define GMM_ASSERT3(test, errormsg) {}
  105. #elif !defined(GMM_FULL_NDEBUG)
  106. # define GMM_ASSERT2(test, errormsg) \
  107. { if (!(test)) GMM_THROW_(gmm::gmm_error, errormsg); }
  108. # define GMM_ASSERT3(test, errormsg) \
  109. { if (!(test)) GMM_THROW_(gmm::gmm_error, errormsg); }
  110. #else
  111. # define GMM_ASSERT2(test, errormsg) \
  112. { if (!(test)) GMM_THROW_(gmm::gmm_error, errormsg); }
  113. # define GMM_ASSERT3(test, errormsg)
  114. #endif
  115. /* *********************************************************************** */
  116. /* GetFEM++ warnings. */
  117. /* *********************************************************************** */
  118. // This allows to dynamically hide warnings
  119. struct warning_level {
  120. static int level(int l = -2)
  121. { static int level_ = 3; return (l != -2) ? (level_ = l) : level_; }
  122. };
  123. inline void set_warning_level(int l) { warning_level::level(std::max(0,l)); }
  124. inline int get_warning_level(void) { return warning_level::level(-2); }
  125. // This allows not to compile some Warnings
  126. #ifndef GMM_WARNING_LEVEL
  127. # define GMM_WARNING_LEVEL 4
  128. #endif
  129. // Warning levels : 0 always printed
  130. // 1 very important : specify a possible error in the code.
  131. // 2 important : specify a default of optimization for inst.
  132. // 3 remark
  133. // 4 ignored by default.
  134. #define GMM_WARNING_MSG(level_, thestr) { \
  135. std::stringstream msg__; \
  136. msg__ << "Level " << level_ << " Warning in " << __FILE__ << ", line " \
  137. << __LINE__ << ": " << thestr; \
  138. std::cerr << msg__.str() << std::endl; \
  139. }
  140. #define GMM_WARNING0(thestr) GMM_WARNING_MSG(0, thestr)
  141. #if GMM_WARNING_LEVEL > 0
  142. # define GMM_WARNING1(thestr) \
  143. { if (1 <= gmm::warning_level::level()) GMM_WARNING_MSG(1, thestr) }
  144. #else
  145. # define GMM_WARNING1(thestr) {}
  146. #endif
  147. #if GMM_WARNING_LEVEL > 1
  148. # define GMM_WARNING2(thestr) \
  149. { if (2 <= gmm::warning_level::level()) GMM_WARNING_MSG(2, thestr) }
  150. #else
  151. # define GMM_WARNING2(thestr) {}
  152. #endif
  153. #if GMM_WARNING_LEVEL > 2
  154. # define GMM_WARNING3(thestr) \
  155. { if (3 <= gmm::warning_level::level()) GMM_WARNING_MSG(3, thestr) }
  156. #else
  157. # define GMM_WARNING3(thestr) {}
  158. #endif
  159. #if GMM_WARNING_LEVEL > 3
  160. # define GMM_WARNING4(thestr) \
  161. { if (4 <= gmm::warning_level::level()) GMM_WARNING_MSG(4, thestr) }
  162. #else
  163. # define GMM_WARNING4(thestr) {}
  164. #endif
  165. /* *********************************************************************** */
  166. /* GetFEM++ traces. */
  167. /* *********************************************************************** */
  168. // This allows to dynamically hide traces
  169. struct traces_level {
  170. static int level(int l = -2)
  171. { static int level_ = 3; return (l != -2) ? (level_ = l) : level_; }
  172. };
  173. inline void set_traces_level(int l) { traces_level::level(std::max(0,l)); }
  174. // This allow not too compile some Warnings
  175. #ifndef GMM_TRACES_LEVEL
  176. # define GMM_TRACES_LEVEL 4
  177. #endif
  178. // Traces levels : 0 always printed
  179. // 1 Susceptible to occur once in a program.
  180. // 2 Susceptible to occur occasionnaly in a program (10).
  181. // 3 Susceptible to occur often (100).
  182. // 4 Susceptible to occur very often (>1000).
  183. #define GMM_TRACE_MSG_MPI // for Parallelized version
  184. #define GMM_TRACE_MSG(level_, thestr) { \
  185. GMM_TRACE_MSG_MPI { \
  186. std::stringstream msg__; \
  187. msg__ << "Trace " << level_ << " in " << __FILE__ << ", line " \
  188. << __LINE__ << ": " << thestr; \
  189. std::cout << msg__.str() << std::endl; \
  190. } \
  191. }
  192. #define GMM_TRACE0(thestr) GMM_TRACE_MSG(0, thestr)
  193. #if GMM_TRACES_LEVEL > 0
  194. # define GMM_TRACE1(thestr) \
  195. { if (1 <= gmm::traces_level::level()) GMM_TRACE_MSG(1, thestr) }
  196. #else
  197. # define GMM_TRACE1(thestr) {}
  198. #endif
  199. #if GMM_TRACES_LEVEL > 1
  200. # define GMM_TRACE2(thestr) \
  201. { if (2 <= gmm::traces_level::level()) GMM_TRACE_MSG(2, thestr) }
  202. #else
  203. # define GMM_TRACE2(thestr) {}
  204. #endif
  205. #if GMM_TRACES_LEVEL > 2
  206. # define GMM_TRACE3(thestr) \
  207. { if (3 <= gmm::traces_level::level()) GMM_TRACE_MSG(3, thestr) }
  208. #else
  209. # define GMM_TRACE3(thestr) {}
  210. #endif
  211. #if GMM_TRACES_LEVEL > 3
  212. # define GMM_TRACE4(thestr) \
  213. { if (4 <= gmm::traces_level::level()) GMM_TRACE_MSG(4, thestr) }
  214. #else
  215. # define GMM_TRACE4(thestr) {}
  216. #endif
  217. /* ********************************************************************* */
  218. /* Definitions for compatibility with old versions. */
  219. /* ********************************************************************* */
  220. #define GMM_STANDARD_CATCH_ERROR catch(std::logic_error e) \
  221. { \
  222. std::cerr << "============================================\n"; \
  223. std::cerr << "| An error has been detected !!! |\n"; \
  224. std::cerr << "============================================\n"; \
  225. std::cerr << e.what() << std::endl << std::endl; \
  226. exit(1); \
  227. } \
  228. catch(const std::runtime_error &e) \
  229. { \
  230. std::cerr << "============================================\n"; \
  231. std::cerr << "| An error has been detected !!! |\n"; \
  232. std::cerr << "============================================\n"; \
  233. std::cerr << e.what() << std::endl << std::endl; \
  234. exit(1); \
  235. } \
  236. catch(const std::bad_alloc &) { \
  237. std::cerr << "============================================\n"; \
  238. std::cerr << "| A bad allocation has been detected !!! |\n"; \
  239. std::cerr << "============================================\n"; \
  240. exit(1); \
  241. } \
  242. catch(const std::bad_typeid &) { \
  243. std::cerr << "============================================\n"; \
  244. std::cerr << "| A bad typeid has been detected !!! |\n"; \
  245. std::cerr << "============================================\n"; \
  246. exit(1); \
  247. } \
  248. catch(const std::bad_exception &) { \
  249. std::cerr << "============================================\n"; \
  250. std::cerr << "| A bad exception has been detected !!! |\n"; \
  251. std::cerr << "============================================\n"; \
  252. exit(1); \
  253. } \
  254. catch(const std::bad_cast &) { \
  255. std::cerr << "============================================\n"; \
  256. std::cerr << "| A bad cast has been detected !!! |\n"; \
  257. std::cerr << "============================================\n"; \
  258. exit(1); \
  259. } \
  260. catch(...) { \
  261. std::cerr << "============================================\n"; \
  262. std::cerr << "| An unknown error has been detected !!! |\n"; \
  263. std::cerr << "============================================\n"; \
  264. exit(1); \
  265. }
  266. // catch(ios_base::failure) {
  267. // std::cerr << "============================================\n";
  268. // std::cerr << "| A ios_base::failure has been detected !!!|\n";
  269. // std::cerr << "============================================\n";
  270. // exit(1);
  271. // }
  272. #if defined(__GNUC__) && (__GNUC__ > 3)
  273. # define GMM_SET_EXCEPTION_DEBUG \
  274. std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
  275. #else
  276. # define GMM_SET_EXCEPTION_DEBUG
  277. #endif
  278. }
  279. #else
  280. #include <external_except.h>
  281. #endif /* EXTERNAL_EXCEPT_*/
  282. #endif /* GMM_EXCEPT_H__ */