www.mooseframework.org
MooseError.h
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 #ifndef MOOSEERROR_H
16 #define MOOSEERROR_H
17 
18 #include "Moose.h"
19 #include "MooseException.h"
20 #include "libmesh/threads.h"
21 
22 #include "libmesh/print_trace.h"
23 #include "libmesh/libmesh_common.h"
24 
25 // C++ includes
26 #include <cstdlib>
27 
29 #if defined(LIBMESH_HAVE_MPI)
30 #define MOOSE_ABORT \
31  do \
32  { \
33  MPI_Abort(libMesh::GLOBAL_COMM_WORLD, 1); \
34  std::abort(); \
35  } while (0)
36 #else
37 #define MOOSE_ABORT \
38  do \
39  { \
40  std::abort(); \
41  } while (0)
42 #endif
43 
44 #define mooseDoOnce(do_this) \
45  do \
46  { \
47  static bool did_this_already = false; \
48  if (!did_this_already) \
49  { \
50  did_this_already = true; \
51  do_this; \
52  } \
53  } while (0)
54 
55 #define mooseCheckMPIErr(err) \
56  do \
57  { \
58  if (err != MPI_SUCCESS) \
59  { \
60  if (libMesh::global_n_processors() == 1) \
61  print_trace(); \
62  libmesh_here(); \
63  MOOSE_ABORT; \
64  } \
65  } while (0)
66 
67 #define mooseException(msg) \
68  do \
69  { \
70  std::ostringstream _exception_oss_; \
71  _exception_oss_ << msg; \
72  \
73  throw MooseException(_exception_oss_.str()); \
74  } while (0)
75 
76 #ifdef NDEBUG
77 #define mooseAssert(asserted, msg)
78 #else
79 #define mooseAssert(asserted, msg) \
80  do \
81  { \
82  if (!(asserted)) \
83  { \
84  std::ostringstream _assert_oss_; \
85  _assert_oss_ << COLOR_RED << "\n\nAssertion `" #asserted "' failed\n" \
86  << msg << "\nat " << __FILE__ << ", line " << __LINE__ << COLOR_DEFAULT \
87  << std::endl; \
88  if (Moose::_throw_on_error) \
89  throw std::runtime_error(_assert_oss_.str()); \
90  else \
91  { \
92  Moose::err << _assert_oss_.str() << std::flush; \
93  if (libMesh::global_n_processors() == 1) \
94  print_trace(); \
95  else \
96  libMesh::write_traceout(); \
97  libmesh_here(); \
98  MOOSE_ABORT; \
99  } \
100  } \
101  } while (0)
102 #endif
103 
104 template <typename... Args>
105 [[noreturn]] void mooseError(Args &&... args);
106 
107 namespace moose
108 {
109 namespace internal
110 {
111 
112 std::string
113 mooseMsgFmt(const std::string & msg, const std::string & title, const std::string & color);
114 
115 [[noreturn]] void mooseErrorRaw(std::string msg, const std::string prefix = "");
116 
117 void mooseStreamAll(std::ostringstream & ss);
118 
119 template <typename T, typename... Args>
120 void
121 mooseStreamAll(std::ostringstream & ss, T && val, Args &&... args)
122 {
123  ss << val;
124  mooseStreamAll(ss, std::forward<Args>(args)...);
125 }
126 
127 template <typename S, typename... Args>
128 void
129 mooseWarningStream(S & oss, Args &&... args)
130 {
132  mooseError(std::forward<Args>(args)...);
133 
134  std::ostringstream ss;
135  mooseStreamAll(ss, args...);
136  std::string msg = mooseMsgFmt(ss.str(), "*** Warning ***", COLOR_YELLOW);
138  throw std::runtime_error(msg);
139 
140  oss << msg << std::flush;
141 }
142 
143 template <typename S, typename... Args>
144 void
145 mooseInfoStream(S & oss, Args &&... args)
146 {
147  mooseDoOnce({
148  std::ostringstream ss;
149  mooseStreamAll(ss, args...);
150  std::string msg = mooseMsgFmt(ss.str(), "*** Info ***", COLOR_CYAN);
151  oss << msg << std::flush;
152  });
153 }
154 
155 template <typename S, typename... Args>
156 void
157 mooseDeprecatedStream(S & oss, Args &&... args)
158 {
160  mooseError("\n\nDeprecated code:\n", std::forward<Args>(args)...);
161 
162  mooseDoOnce(std::ostringstream ss; mooseStreamAll(ss, args...);
163  std::string msg = mooseMsgFmt(
164  ss.str(),
165  "*** Warning, This code is deprecated and will be removed in future versions!",
166  COLOR_YELLOW);
167  oss << msg;
168  ss.str("");
169  if (libMesh::global_n_processors() == 1) print_trace(ss);
170  else libMesh::write_traceout();
171  oss << ss.str() << std::endl;);
172 }
173 
174 } // namespace internal
175 } // namespace moose
176 
180 template <typename... Args>
181 [[noreturn]] void
182 mooseError(Args &&... args)
183 {
184  std::ostringstream oss;
185  moose::internal::mooseStreamAll(oss, std::forward<Args>(args)...);
187 }
188 
192 template <typename... Args>
193 void
194 mooseWarning(Args &&... args)
195 {
196  moose::internal::mooseWarningStream(Moose::out, std::forward<Args>(args)...);
197 }
198 
200 template <typename... Args>
201 void
202 mooseDeprecated(Args &&... args)
203 {
204  moose::internal::mooseDeprecatedStream(Moose::out, std::forward<Args>(args)...);
205 }
206 
208 template <typename... Args>
209 void
210 mooseInfo(Args &&... args)
211 {
212  moose::internal::mooseInfoStream(Moose::out, std::forward<Args>(args)...);
213 }
214 
215 #endif /* MOOSEERRORS_H */
void mooseDeprecatedStream(S &oss, Args &&...args)
Definition: MooseError.h:157
void mooseStreamAll(std::ostringstream &ss)
Definition: MooseError.C:78
bool _warnings_are_errors
Variable to toggle any warning into an error (includes deprecated code warnings)
Definition: Moose.C:1282
void mooseError(Args &&...args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:182
void mooseWarningStream(S &oss, Args &&...args)
Definition: MooseError.h:129
void mooseDeprecated(Args &&...args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:202
void mooseInfo(Args &&...args)
Emit an informational message with the given stringified, concatenated args.
Definition: MooseError.h:210
bool _deprecated_is_error
Variable to toggle only deprecated warnings as errors.
Definition: Moose.C:1284
void mooseInfoStream(S &oss, Args &&...args)
Definition: MooseError.h:145
void mooseErrorRaw(std::string msg, const std::string prefix="")
Definition: MooseError.C:34
bool _throw_on_error
Variable to turn on exceptions during mooseError() and mooseWarning(), should only be used with MOOSE...
Definition: Moose.C:1286
std::string mooseMsgFmt(const std::string &msg, const std::string &title, const std::string &color)
Definition: MooseError.C:24
void mooseWarning(Args &&...args)
Emit a warning message with the given stringified, concatenated args.
Definition: MooseError.h:194