https://mooseframework.inl.gov
MooseError.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #pragma once
11 
12 #include "Moose.h"
13 #include "MooseException.h"
14 #include "libmesh/threads.h"
15 
16 #include "libmesh/print_trace.h"
17 #include "libmesh/libmesh_common.h"
18 
19 // C++ includes
20 #include <cstdlib>
21 #include <tuple>
22 #include <type_traits>
23 
24 // Used in numerous downstream classes without 'libMesh::' prefix
25 using libMesh::demangle;
26 
27 namespace MetaPhysicL
28 {
29 class LogicError;
30 }
31 
32 namespace hit
33 {
34 class Node;
35 }
36 
37 // this function allows streaming tuples to ostreams
38 template <size_t n, typename... T>
39 void
40 print_tuple(std::ostream & os, const std::tuple<T...> & tup)
41 {
42  if constexpr (n < sizeof...(T))
43  {
44  if (n != 0)
45  os << ", ";
46  os << std::get<n>(tup);
47  print_tuple<n + 1>(os, tup);
48  }
49 }
50 template <typename... T>
51 std::ostream &
52 operator<<(std::ostream & os, const std::tuple<T...> & tup)
53 {
54  os << "[";
55  print_tuple<0>(os, tup);
56  return os << "]";
57 }
58 
60 #if defined(LIBMESH_HAVE_MPI)
61 #define MOOSE_ABORT \
62  do \
63  { \
64  MPI_Abort(libMesh::GLOBAL_COMM_WORLD, 1); \
65  std::abort(); \
66  } while (0)
67 #else
68 #define MOOSE_ABORT \
69  do \
70  { \
71  std::abort(); \
72  } while (0)
73 #endif
74 
75 #define mooseDoOnce(do_this) \
76  do \
77  { \
78  static bool did_this_already = false; \
79  if (Moose::show_multiple || !did_this_already) \
80  { \
81  did_this_already = true; \
82  do_this; \
83  } \
84  } while (0)
85 
86 #define mooseCheckMPIErr(err) \
87  do \
88  { \
89  if (err != MPI_SUCCESS) \
90  { \
91  if (libMesh::global_n_processors() == 1) \
92  libMesh::print_trace(); \
93  libmesh_here(); \
94  MOOSE_ABORT; \
95  } \
96  } while (0)
97 
98 #define mooseException(...) \
99  do \
100  { \
101  throw MooseException(__VA_ARGS__); \
102  } while (0)
103 
104 #ifdef NDEBUG
105 #define mooseAssert(asserted, msg) ((void)0)
106 #else
107 #define mooseAssert(asserted, msg) \
108  do \
109  { \
110  if (!(asserted)) \
111  { \
112  std::ostringstream _assert_oss_; \
113  _assert_oss_ << COLOR_RED << "\n\nAssertion `" #asserted "' failed\n" \
114  << msg << "\nat " << __FILE__ << ", line " << __LINE__ << COLOR_DEFAULT \
115  << std::endl; \
116  if (Moose::_throw_on_error) \
117  throw std::runtime_error(_assert_oss_.str()); \
118  else \
119  { \
120  Moose::err << _assert_oss_.str() << std::flush; \
121  if (libMesh::global_n_processors() == 1) \
122  libMesh::print_trace(); \
123  else \
124  libMesh::write_traceout(); \
125  libmesh_here(); \
126  MOOSE_ABORT; \
127  } \
128  } \
129  } while (0)
130 #endif
131 
132 template <typename... Args>
133 [[noreturn]] void mooseError(Args &&... args);
134 
143 class MooseRuntimeError : public std::runtime_error
144 {
145 public:
146  MooseRuntimeError(const std::string & message, const hit::Node * const node)
147  : runtime_error(message), _node(node)
148  {
149  }
150 
152  const hit::Node * getNode() const { return _node; }
153 
154 private:
156  const hit::Node * const _node;
157 };
158 
160 
161 namespace moose
162 {
163 
164 namespace internal
165 {
167 
175 
183 std::string
184 mooseMsgFmt(const std::string & msg, const std::string & title, const std::string & color);
185 
192 std::string mooseMsgFmt(const std::string & msg, const std::string & color);
193 
200 [[noreturn]] void
201 mooseErrorRaw(std::string msg, const std::string & prefix = "", const hit::Node * node = nullptr);
202 
208 void mooseStreamAll(std::ostringstream & ss);
209 
210 template <typename T, typename... Args>
211 void
212 mooseStreamAll(std::ostringstream & ss, T && val, Args &&... args)
213 {
214  ss << val;
215  mooseStreamAll(ss, std::forward<Args>(args)...);
216 }
217 
218 template <typename S, typename... Args>
219 void
220 mooseWarningStream(S & oss, Args &&... args)
221 {
223  mooseError(std::forward<Args>(args)...);
224 
225  std::ostringstream ss;
226  mooseStreamAll(ss, args...);
227  std::string msg = mooseMsgFmt(ss.str(), "*** Warning ***", COLOR_YELLOW);
229  throw std::runtime_error(msg);
230 
231  {
233  oss << msg << std::flush;
234  }
235 }
236 
237 template <typename S, typename... Args>
238 void
239 mooseUnusedStream(S & oss, Args &&... args)
240 {
241  std::ostringstream ss;
242  mooseStreamAll(ss, args...);
243  std::string msg = mooseMsgFmt(ss.str(), "*** Warning ***", COLOR_YELLOW);
245  throw std::runtime_error(msg);
246 
247  {
249  oss << msg << std::flush;
250  }
251 }
252 
253 template <typename S, typename... Args>
254 void
255 mooseInfoStreamRepeated(S & oss, Args &&... args)
256 {
257  std::ostringstream ss;
258  mooseStreamAll(ss, args...);
259  std::string msg = mooseMsgFmt(ss.str(), "*** Info ***", COLOR_CYAN);
260  {
262  oss << msg << std::flush;
263  }
264 }
265 
266 template <typename S, typename... Args>
267 void
268 mooseInfoStream(S & oss, Args &&... args)
269 {
270  mooseDoOnce(mooseInfoStreamRepeated(oss, args...););
271 }
272 
273 template <typename S, typename... Args>
274 void
275 mooseDeprecatedStream(S & oss, const bool expired, const bool print_title, Args &&... args)
276 {
278  mooseError("\n\nDeprecated code:\n", std::forward<Args>(args)...);
279 
280  std::ostringstream ss;
281  mooseStreamAll(ss, args...);
282 
283  const auto color = expired ? COLOR_RED : COLOR_YELLOW;
284  std::string msg = print_title ? mooseMsgFmt(ss.str(), "*** Deprecation Warning ***", color)
285  : mooseMsgFmt(ss.str(), color);
286  oss << msg;
287  ss.str("");
288  if (Moose::show_trace)
289  {
290  if (libMesh::global_n_processors() == 1)
292  else
294  {
296  oss << ss.str() << std::endl;
297  };
298  };
299 }
316 std::string formatMooseDocumentedError(const std::string & repo_name,
317  const unsigned int issue_num,
318  const std::string & msg);
319 } // namespace internal
320 
324 void translateMetaPhysicLError(const MetaPhysicL::LogicError &);
325 
326 } // namespace moose
327 
331 template <typename... Args>
332 [[noreturn]] void
333 mooseError(Args &&... args)
334 {
335  std::ostringstream oss;
336  moose::internal::mooseStreamAll(oss, std::forward<Args>(args)...);
338 }
339 
352 template <typename... Args>
353 [[noreturn]] void
354 mooseDocumentedError(const std::string & repo_name, const unsigned int issue_num, Args &&... args)
355 {
356  std::ostringstream oss;
357  moose::internal::mooseStreamAll(oss, std::forward<Args>(args)...);
359  moose::internal::formatMooseDocumentedError(repo_name, issue_num, oss.str()));
360 }
361 
365 template <typename... Args>
366 void
367 mooseWarning(Args &&... args)
368 {
369  moose::internal::mooseWarningStream(Moose::out, std::forward<Args>(args)...);
370 }
371 
374 template <typename... Args>
375 void
376 mooseUnused(Args &&... args)
377 {
378  moose::internal::mooseUnusedStream(Moose::out, std::forward<Args>(args)...);
379 }
380 
382 template <typename... Args>
383 void
384 mooseDeprecated(Args &&... args)
385 {
386  moose::internal::mooseDeprecatedStream(Moose::out, false, true, std::forward<Args>(args)...);
387 }
388 
390 template <typename... Args>
391 void
392 mooseDeprecationExpired(Args &&... args)
393 {
394  moose::internal::mooseDeprecatedStream(Moose::out, true, true, std::forward<Args>(args)...);
395 }
396 
398 template <typename... Args>
399 void
400 mooseInfo(Args &&... args)
401 {
402  moose::internal::mooseInfoStream(Moose::out, std::forward<Args>(args)...);
403 }
404 
406 template <typename... Args>
407 void
408 mooseInfoRepeated(Args &&... args)
409 {
410  moose::internal::mooseInfoStreamRepeated(Moose::out, std::forward<Args>(args)...);
411 }
void mooseStreamAll(std::ostringstream &ss)
All of the following are not meant to be called directly - they are called by the normal macros (moos...
Definition: MooseError.C:100
std::ostream & operator<<(std::ostream &os, const std::tuple< T... > &tup)
Definition: MooseError.h:52
void mooseUnusedStream(S &oss, Args &&... args)
Definition: MooseError.h:239
bool show_trace
Set to true (the default) to print the stack trace with error and warning messages - false to omit it...
Definition: Moose.C:783
processor_id_type global_n_processors()
std::string incompatVarMsg(MooseVariableFieldBase &var1, MooseVariableFieldBase &var2)
Builds and returns a string of the form:
Definition: MooseError.C:26
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:333
bool _warnings_are_errors
Variable to toggle any warning into an error (includes deprecated code warnings)
Definition: Moose.C:778
void translateMetaPhysicLError(const MetaPhysicL::LogicError &)
emit a relatively clear error message when we catch a MetaPhysicL logic error
Definition: MooseError.C:118
void mooseWarning(Args &&... args)
Emit a warning message with the given stringified, concatenated args.
Definition: MooseError.h:367
void mooseDeprecationExpired(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:392
void mooseInfoRepeated(Args &&... args)
Emit an informational message with the given stringified, concatenated args.
Definition: MooseError.h:408
This class provides an interface for common operations on field variables of both FE and FV types wit...
We need to instantiate the following CompareTypes to tell the compiler that ADReal is a subtype of Ch...
std::basic_ostream< charT, traits > * os
Definition: InfixIterator.h:33
void write_traceout()
void mooseInfoStream(S &oss, Args &&... args)
Definition: MooseError.h:268
void mooseInfo(Args &&... args)
Emit an informational message with the given stringified, concatenated args.
Definition: MooseError.h:400
const hit::Node * getNode() const
Get the associated hit node, if any.
Definition: MooseError.h:152
void mooseUnused(Args &&... args)
Warning message used to notify the users of unused parts of their input files Really used internally ...
Definition: MooseError.h:376
bool _deprecated_is_error
Variable to toggle only deprecated warnings as errors.
Definition: Moose.C:779
void mooseWarningStream(S &oss, Args &&... args)
Definition: MooseError.h:220
void mooseInfoStreamRepeated(S &oss, Args &&... args)
Definition: MooseError.h:255
void mooseDeprecated(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:384
std::string demangle(const char *name)
Definition: Moose.h:42
void mooseDeprecatedStream(S &oss, const bool expired, const bool print_title, Args &&... args)
Definition: MooseError.h:275
void mooseDocumentedError(const std::string &repo_name, const unsigned int issue_num, Args &&... args)
Emit a documented error message with the given stringified, concatenated args and terminate the appli...
Definition: MooseError.h:354
void print_trace(std::ostream &out_stream=std::cerr)
void mooseErrorRaw(std::string msg, const std::string &prefix="", const hit::Node *node=nullptr)
Main callback for emitting a moose error.
Definition: MooseError.C:53
bool _throw_on_warning
Variable to turn on exceptions during mooseWarning(), should only be used in MOOSE unit tests...
Definition: Moose.C:781
MooseRuntimeError(const std::string &message, const hit::Node *const node)
Definition: MooseError.h:146
const hit::Node *const _node
The associated hit node, if any.
Definition: MooseError.h:156
Exception to be thrown whenever we have _throw_on_error set and a mooseError() is emitted...
Definition: MooseError.h:143
libMesh::Threads::spin_mutex moose_stream_lock
Definition: MooseError.h:166
void print_tuple(std::ostream &os, const std::tuple< T... > &tup)
Definition: MooseError.h:40
std::string mooseMsgFmt(const std::string &msg, const std::string &title, const std::string &color)
Format a message for output with a title.
Definition: MooseError.C:37
std::string formatMooseDocumentedError(const std::string &repo_name, const unsigned int issue_num, const std::string &msg)
Formats a documented error.
Definition: MooseError.C:105