MFEM  v4.0
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
error.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-443211. All Rights
3 // reserved. See file COPYRIGHT for details.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability see http://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the GNU Lesser General Public License (as published by the Free
10 // Software Foundation) version 2.1 dated February 1999.
11 
12 #ifndef MFEM_ERROR_HPP
13 #define MFEM_ERROR_HPP
14 
15 #include "../config/config.hpp"
16 #include <iomanip>
17 #include <sstream>
18 
19 namespace mfem
20 {
21 
22 /// Action to take when MFEM encounters an error.
24 {
25  MFEM_ERROR_ABORT = 0, /**<
26  Abort execution using abort() or MPI_Abort(). This is the default error
27  action when the build option MFEM_USE_EXCEPTIONS is set to NO. */
29  Throw an ErrorException. Requires the build option MFEM_USE_EXCEPTIONS=YES
30  in which case it is also the default error action. */
31 };
32 
33 /// Set the action MFEM takes when an error is encountered.
34 void set_error_action(ErrorAction action);
35 /// Get the action MFEM takes when an error is encountered.
37 
38 #ifdef MFEM_USE_EXCEPTIONS
39 /** @brief Exception class thrown when MFEM encounters an error and the current
40  ErrorAction is set to MFEM_ERROR_THROW. */
41 class ErrorException: public std::exception
42 {
43 private:
44  std::string msg;
45 public:
46  explicit ErrorException(const std::string & in_msg) : msg(in_msg) { }
47  virtual ~ErrorException() throw() { }
48  virtual const char* what() const throw();
49 };
50 #endif
51 
52 void mfem_backtrace(int mode = 0, int depth = -1);
53 
54 /** @brief Function called when an error is encountered. Used by the macros
55  MFEM_ABORT, MFEM_ASSERT, MFEM_VERIFY. */
56 void mfem_error(const char *msg = NULL);
57 
58 /// Function called by the macro MFEM_WARNING.
59 void mfem_warning(const char *msg = NULL);
60 
61 }
62 
63 #ifndef _MFEM_FUNC_NAME
64 #ifndef _MSC_VER
65 // This is nice because it shows the class and method name
66 #define _MFEM_FUNC_NAME __PRETTY_FUNCTION__
67 // This one is C99 standard.
68 //#define _MFEM_FUNC_NAME __func__
69 #else
70 // for Visual Studio C++
71 #define _MFEM_FUNC_NAME __FUNCSIG__
72 #endif
73 #endif
74 
75 #define MFEM_LOCATION \
76  "\n ... in function: " << _MFEM_FUNC_NAME << \
77  "\n ... in file: " << __FILE__ << ':' << __LINE__ << '\n'
78 
79 // Common error message and abort macro
80 #define _MFEM_MESSAGE(msg, warn) \
81  { \
82  std::ostringstream mfemMsgStream; \
83  mfemMsgStream << std::setprecision(16); \
84  mfemMsgStream << std::setiosflags(std::ios_base::scientific); \
85  mfemMsgStream << msg << MFEM_LOCATION; \
86  if (!(warn)) \
87  mfem::mfem_error(mfemMsgStream.str().c_str()); \
88  else \
89  mfem::mfem_warning(mfemMsgStream.str().c_str()); \
90  }
91 
92 // Outputs lots of useful information and aborts.
93 // For all of these functions, "msg" is pushed to an ostream, so you can
94 // write useful (if complicated) error messages instead of writing
95 // out to the screen first, then calling abort. For example:
96 // MFEM_ABORT( "Unknown geometry type: " << type );
97 #define MFEM_ABORT(msg) _MFEM_MESSAGE("MFEM abort: " << msg, 0)
98 
99 // Does a check, and then outputs lots of useful information if the test fails
100 #define MFEM_VERIFY(x, msg) \
101  if (!(x)) \
102  { \
103  _MFEM_MESSAGE("Verification failed: (" \
104  << #x << ") is false:\n --> " << msg, 0); \
105  }
106 
107 // Use this if the only place your variable is used is in ASSERTs
108 // For example, this code snippet:
109 // int err = MPI_Reduce(ldata, maxdata, 5, MPI_INT, MPI_MAX, 0, MyComm);
110 // MFEM_CONTRACT_VAR(err);
111 // MFEM_ASSERT( err == 0, "MPI_Reduce gave an error with length "
112 // << ldata );
113 #define MFEM_CONTRACT_VAR(x) if (0 && &x == &x){}
114 
115 // Now set up some optional checks, but only if the right flags are on
116 #ifdef MFEM_DEBUG
117 
118 #define MFEM_ASSERT(x, msg) \
119  if (!(x)) \
120  { \
121  _MFEM_MESSAGE("Assertion failed: (" \
122  << #x << ") is false:\n --> " << msg, 0); \
123  }
124 
125 // A macro that exposes its argument in debug mode only.
126 #define MFEM_DEBUG_DO(x) x
127 
128 #else
129 
130 // Get rid of all this code, since we're not checking.
131 #define MFEM_ASSERT(x, msg)
132 
133 // A macro that exposes its argument in debug mode only.
134 #define MFEM_DEBUG_DO(x)
135 
136 #endif
137 
138 // Generate a warning message - always generated, regardless of MFEM_DEBUG.
139 #define MFEM_WARNING(msg) _MFEM_MESSAGE("MFEM Warning: " << msg, 1)
140 
141 #endif
virtual const char * what() const
Definition: error.cpp:39
ErrorAction get_error_action()
Get the action MFEM takes when an error is encountered.
Definition: error.cpp:72
Exception class thrown when MFEM encounters an error and the current ErrorAction is set to MFEM_ERROR...
Definition: error.hpp:41
void mfem_error(const char *msg)
Function called when an error is encountered. Used by the macros MFEM_ABORT, MFEM_ASSERT, MFEM_VERIFY.
Definition: error.cpp:146
virtual ~ErrorException()
Definition: error.hpp:47
void set_error_action(ErrorAction action)
Set the action MFEM takes when an error is encountered.
Definition: error.cpp:49
ErrorAction
Action to take when MFEM encounters an error.
Definition: error.hpp:23
ErrorException(const std::string &in_msg)
Definition: error.hpp:46
void mfem_warning(const char *msg)
Function called by the macro MFEM_WARNING.
Definition: error.cpp:178
void mfem_backtrace(int mode, int depth)
Definition: error.cpp:77