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.

344 lines
13 KiB

  1. /* -*- c++ -*- (enables emacs c++ mode) */
  2. /*===========================================================================
  3. Copyright (C) 2002-2015 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. //provides external implementation of gmm_exception and logging.
  35. #ifndef EXTERNAL_EXCEPT_
  36. #include "gmm_std.h"
  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() IS_DEPRECATED;
  101. inline void GMM_THROW() {}
  102. #define GMM_THROW(a, b) { GMM_THROW_(a,b); gmm::GMM_THROW(); }
  103. #if defined(NDEBUG)
  104. # define GMM_ASSERT2(test, errormsg) {}
  105. # define GMM_ASSERT3(test, errormsg) {}
  106. #elif !defined(GMM_FULL_NDEBUG)
  107. # define GMM_ASSERT2(test, errormsg) \
  108. { if (!(test)) GMM_THROW_(gmm::gmm_error, errormsg); }
  109. # define GMM_ASSERT3(test, errormsg) \
  110. { if (!(test)) GMM_THROW_(gmm::gmm_error, errormsg); }
  111. #else
  112. # define GMM_ASSERT2(test, errormsg) \
  113. { if (!(test)) GMM_THROW_(gmm::gmm_error, errormsg); }
  114. # define GMM_ASSERT3(test, errormsg)
  115. #endif
  116. /* *********************************************************************** */
  117. /* Getfem++ warnings. */
  118. /* *********************************************************************** */
  119. // This allows to dynamically hide warnings
  120. struct warning_level {
  121. static int level(int l = -2)
  122. { static int level_ = 3; return (l != -2) ? (level_ = l) : level_; }
  123. };
  124. inline void set_warning_level(int l) { warning_level::level(std::max(0,l)); }
  125. inline int get_warning_level(void) { return warning_level::level(-2); }
  126. // This allows not to compile some Warnings
  127. #ifndef GMM_WARNING_LEVEL
  128. # define GMM_WARNING_LEVEL 4
  129. #endif
  130. // Warning levels : 0 always printed
  131. // 1 very important : specify a possible error in the code.
  132. // 2 important : specify a default of optimization for inst.
  133. // 3 remark
  134. // 4 ignored by default.
  135. #define GMM_WARNING_MSG(level_, thestr) { \
  136. std::stringstream msg__; \
  137. msg__ << "Level " << level_ << " Warning in " << __FILE__ << ", line " \
  138. << __LINE__ << ": " << thestr; \
  139. std::cerr << msg__.str() << std::endl; \
  140. }
  141. #define GMM_WARNING0(thestr) GMM_WARNING_MSG(0, thestr)
  142. #if GMM_WARNING_LEVEL > 0
  143. # define GMM_WARNING1(thestr) \
  144. { if (1 <= gmm::warning_level::level()) GMM_WARNING_MSG(1, thestr) }
  145. #else
  146. # define GMM_WARNING1(thestr) {}
  147. #endif
  148. #if GMM_WARNING_LEVEL > 1
  149. # define GMM_WARNING2(thestr) \
  150. { if (2 <= gmm::warning_level::level()) GMM_WARNING_MSG(2, thestr) }
  151. #else
  152. # define GMM_WARNING2(thestr) {}
  153. #endif
  154. #if GMM_WARNING_LEVEL > 2
  155. # define GMM_WARNING3(thestr) \
  156. { if (3 <= gmm::warning_level::level()) GMM_WARNING_MSG(3, thestr) }
  157. #else
  158. # define GMM_WARNING3(thestr) {}
  159. #endif
  160. #if GMM_WARNING_LEVEL > 3
  161. # define GMM_WARNING4(thestr) \
  162. { if (4 <= gmm::warning_level::level()) GMM_WARNING_MSG(4, thestr) }
  163. #else
  164. # define GMM_WARNING4(thestr) {}
  165. #endif
  166. /* *********************************************************************** */
  167. /* Getfem++ traces. */
  168. /* *********************************************************************** */
  169. // This allows to dynamically hide traces
  170. struct traces_level {
  171. static int level(int l = -2)
  172. { static int level_ = 3; return (l != -2) ? (level_ = l) : level_; }
  173. };
  174. inline void set_traces_level(int l) { traces_level::level(std::max(0,l)); }
  175. // This allow not too compile some Warnings
  176. #ifndef GMM_TRACES_LEVEL
  177. # define GMM_TRACES_LEVEL 4
  178. #endif
  179. // Traces levels : 0 always printed
  180. // 1 Susceptible to occur once in a program.
  181. // 2 Susceptible to occur occasionnaly in a program (10).
  182. // 3 Susceptible to occur often (100).
  183. // 4 Susceptible to occur very often (>1000).
  184. #define GMM_TRACE_MSG_MPI // for Parallelized version
  185. #define GMM_TRACE_MSG(level_, thestr) { \
  186. GMM_TRACE_MSG_MPI { \
  187. std::stringstream msg__; \
  188. msg__ << "Trace " << level_ << " in " << __FILE__ << ", line " \
  189. << __LINE__ << ": " << thestr; \
  190. std::cout << msg__.str() << std::endl; \
  191. } \
  192. }
  193. #define GMM_TRACE0(thestr) GMM_TRACE_MSG(0, thestr)
  194. #if GMM_TRACES_LEVEL > 0
  195. # define GMM_TRACE1(thestr) \
  196. { if (1 <= gmm::traces_level::level()) GMM_TRACE_MSG(1, thestr) }
  197. #else
  198. # define GMM_TRACE1(thestr) {}
  199. #endif
  200. #if GMM_TRACES_LEVEL > 1
  201. # define GMM_TRACE2(thestr) \
  202. { if (2 <= gmm::traces_level::level()) GMM_TRACE_MSG(2, thestr) }
  203. #else
  204. # define GMM_TRACE2(thestr) {}
  205. #endif
  206. #if GMM_TRACES_LEVEL > 2
  207. # define GMM_TRACE3(thestr) \
  208. { if (3 <= gmm::traces_level::level()) GMM_TRACE_MSG(3, thestr) }
  209. #else
  210. # define GMM_TRACE3(thestr) {}
  211. #endif
  212. #if GMM_TRACES_LEVEL > 3
  213. # define GMM_TRACE4(thestr) \
  214. { if (4 <= gmm::traces_level::level()) GMM_TRACE_MSG(4, thestr) }
  215. #else
  216. # define GMM_TRACE4(thestr) {}
  217. #endif
  218. /* ********************************************************************* */
  219. /* Definitions for compatibility with old versions. */
  220. /* ********************************************************************* */
  221. using std::invalid_argument;
  222. struct dimension_error : public std::logic_error
  223. { dimension_error(const std::string& w): std::logic_error(w) {} };
  224. struct file_not_found_error : public std::logic_error
  225. { file_not_found_error(const std::string& w): std::logic_error (w) {} };
  226. struct internal_error : public std::logic_error
  227. { internal_error(const std::string& w): std::logic_error(w) {} };
  228. struct failure_error : public std::logic_error
  229. { failure_error(const std::string& w): std::logic_error (w) {} };
  230. struct not_linear_error : public std::logic_error
  231. { not_linear_error(const std::string& w): std::logic_error (w) {} };
  232. struct to_be_done_error : public std::logic_error
  233. { to_be_done_error(const std::string& w): std::logic_error (w) {} };
  234. #define GMM_STANDARD_CATCH_ERROR catch(std::logic_error e) \
  235. { \
  236. std::cerr << "============================================\n"; \
  237. std::cerr << "| An error has been detected !!! |\n"; \
  238. std::cerr << "============================================\n"; \
  239. std::cerr << e.what() << std::endl << std::endl; \
  240. exit(1); \
  241. } \
  242. catch(std::runtime_error e) \
  243. { \
  244. std::cerr << "============================================\n"; \
  245. std::cerr << "| An error has been detected !!! |\n"; \
  246. std::cerr << "============================================\n"; \
  247. std::cerr << e.what() << std::endl << std::endl; \
  248. exit(1); \
  249. } \
  250. catch(std::bad_alloc) { \
  251. std::cerr << "============================================\n"; \
  252. std::cerr << "| A bad allocation has been detected !!! |\n"; \
  253. std::cerr << "============================================\n"; \
  254. exit(1); \
  255. } \
  256. catch(std::bad_typeid) { \
  257. std::cerr << "============================================\n"; \
  258. std::cerr << "| A bad typeid has been detected !!! |\n"; \
  259. std::cerr << "============================================\n"; \
  260. exit(1); \
  261. } \
  262. catch(std::bad_exception) { \
  263. std::cerr << "============================================\n"; \
  264. std::cerr << "| A bad exception has been detected !!! |\n"; \
  265. std::cerr << "============================================\n"; \
  266. exit(1); \
  267. } \
  268. catch(std::bad_cast) { \
  269. std::cerr << "============================================\n"; \
  270. std::cerr << "| A bad cast has been detected !!! |\n"; \
  271. std::cerr << "============================================\n"; \
  272. exit(1); \
  273. } \
  274. catch(...) { \
  275. std::cerr << "============================================\n"; \
  276. std::cerr << "| An unknown error has been detected !!! |\n"; \
  277. std::cerr << "============================================\n"; \
  278. exit(1); \
  279. }
  280. // catch(ios_base::failure) {
  281. // std::cerr << "============================================\n";
  282. // std::cerr << "| A ios_base::failure has been detected !!!|\n";
  283. // std::cerr << "============================================\n";
  284. // exit(1);
  285. // }
  286. #if defined(__GNUC__) && (__GNUC__ > 3)
  287. # define GMM_SET_EXCEPTION_DEBUG \
  288. std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
  289. #else
  290. # define GMM_SET_EXCEPTION_DEBUG
  291. #endif
  292. }
  293. #else
  294. #include <external_except.h>
  295. #endif /* EXTERNAL_EXCEPT_*/
  296. #endif /* GMM_EXCEPT_H__ */