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  moose::internal::mooseErrorRaw(""); \
91  } while (0)
92 
93 #define mooseException(...) \
94  do \
95  { \
96  throw MooseException(__VA_ARGS__); \
97  } while (0)
98 
99 #ifdef NDEBUG
100 #define mooseAssert(asserted, msg) ((void)0)
101 #else
102 #define mooseAssert(asserted, msg) \
103  do \
104  { \
105  if (!(asserted)) \
106  { \
107  std::ostringstream _assert_oss_; \
108  _assert_oss_ << COLOR_RED << "\n\nAssertion `" #asserted "' failed\n" \
109  << msg << "\nat " << __FILE__ << ", line " << __LINE__ << COLOR_DEFAULT \
110  << std::endl; \
111  if (Moose::_throw_on_error) \
112  throw std::runtime_error(_assert_oss_.str()); \
113  else \
114  { \
115  Moose::err << _assert_oss_.str() << std::flush; \
116  moose::internal::mooseErrorRaw(""); \
117  } \
118  } \
119  } while (0)
120 #endif
121 
122 template <typename... Args>
123 [[noreturn]] void mooseError(Args &&... args);
124 
133 class MooseRuntimeError : public std::runtime_error
134 {
135 public:
136  MooseRuntimeError(const std::string & message, const hit::Node * const node)
137  : runtime_error(message), _node(node)
138  {
139  }
140 
142  const hit::Node * getNode() const { return _node; }
143 
144 private:
146  const hit::Node * const _node;
147 };
148 
150 
151 namespace moose
152 {
153 
154 namespace internal
155 {
157 
165 
173 std::string
174 mooseMsgFmt(const std::string & msg, const std::string & title, const std::string & color);
175 
182 std::string mooseMsgFmt(const std::string & msg, const std::string & color);
183 
190 [[noreturn]] void
191 mooseErrorRaw(std::string msg, const std::string & prefix = "", const hit::Node * node = nullptr);
192 
198 void mooseStreamAll(std::ostringstream & ss);
199 
200 template <typename T, typename... Args>
201 void
202 mooseStreamAll(std::ostringstream & ss, T && val, Args &&... args)
203 {
204  ss << val;
205  mooseStreamAll(ss, std::forward<Args>(args)...);
206 }
207 
208 template <typename S, typename... Args>
209 void
210 mooseWarningStream(S & oss, Args &&... args)
211 {
213  mooseError(std::forward<Args>(args)...);
214 
215  std::ostringstream ss;
216  mooseStreamAll(ss, args...);
217  std::string msg = mooseMsgFmt(ss.str(), "*** Warning ***", COLOR_YELLOW);
219  throw std::runtime_error(msg);
220 
221  {
223  oss << msg << std::flush;
224  }
225 }
226 
227 template <typename S, typename... Args>
228 void
229 mooseUnusedStream(S & oss, Args &&... args)
230 {
231  std::ostringstream ss;
232  mooseStreamAll(ss, args...);
233  std::string msg = mooseMsgFmt(ss.str(), "*** Warning ***", COLOR_YELLOW);
235  throw std::runtime_error(msg);
236 
237  {
239  oss << msg << std::flush;
240  }
241 }
242 
243 template <typename S, typename... Args>
244 void
245 mooseInfoStreamRepeated(S & oss, Args &&... args)
246 {
247  std::ostringstream ss;
248  mooseStreamAll(ss, args...);
249  std::string msg = mooseMsgFmt(ss.str(), "*** Info ***", COLOR_CYAN);
250  {
252  oss << msg << std::flush;
253  }
254 }
255 
256 template <typename S, typename... Args>
257 void
258 mooseInfoStream(S & oss, Args &&... args)
259 {
260  mooseDoOnce(mooseInfoStreamRepeated(oss, args...););
261 }
262 
263 template <typename S, typename... Args>
264 void
265 mooseDeprecatedStream(S & oss, const bool expired, const bool print_title, Args &&... args)
266 {
268  mooseError("\n\nDeprecated code:\n", std::forward<Args>(args)...);
269 
270  std::ostringstream ss;
271  mooseStreamAll(ss, args...);
272 
273  const auto color = expired ? COLOR_RED : COLOR_YELLOW;
274  std::string msg = print_title ? mooseMsgFmt(ss.str(), "*** Deprecation Warning ***", color)
275  : mooseMsgFmt(ss.str(), color);
276  oss << msg;
277  ss.str("");
278  if (Moose::show_trace)
279  {
280  if (libMesh::global_n_processors() == 1)
282  else
284  {
286  oss << ss.str() << std::endl;
287  };
288  };
289 }
306 std::string formatMooseDocumentedError(const std::string & repo_name,
307  const unsigned int issue_num,
308  const std::string & msg);
309 } // namespace internal
310 
314 void translateMetaPhysicLError(const MetaPhysicL::LogicError &);
315 
316 } // namespace moose
317 
321 template <typename... Args>
322 [[noreturn]] void
323 mooseError(Args &&... args)
324 {
325  std::ostringstream oss;
326  moose::internal::mooseStreamAll(oss, std::forward<Args>(args)...);
328 }
329 
342 template <typename... Args>
343 [[noreturn]] void
344 mooseDocumentedError(const std::string & repo_name, const unsigned int issue_num, Args &&... args)
345 {
346  std::ostringstream oss;
347  moose::internal::mooseStreamAll(oss, std::forward<Args>(args)...);
349  moose::internal::formatMooseDocumentedError(repo_name, issue_num, oss.str()));
350 }
351 
355 template <typename... Args>
356 void
357 mooseWarning(Args &&... args)
358 {
359  moose::internal::mooseWarningStream(Moose::out, std::forward<Args>(args)...);
360 }
361 
364 template <typename... Args>
365 void
366 mooseUnused(Args &&... args)
367 {
368  moose::internal::mooseUnusedStream(Moose::out, std::forward<Args>(args)...);
369 }
370 
372 template <typename... Args>
373 void
374 mooseDeprecated(Args &&... args)
375 {
376  moose::internal::mooseDeprecatedStream(Moose::out, false, true, std::forward<Args>(args)...);
377 }
378 
380 template <typename... Args>
381 void
382 mooseDeprecationExpired(Args &&... args)
383 {
384  moose::internal::mooseDeprecatedStream(Moose::out, true, true, std::forward<Args>(args)...);
385 }
386 
388 template <typename... Args>
389 void
390 mooseInfo(Args &&... args)
391 {
392  moose::internal::mooseInfoStream(Moose::out, std::forward<Args>(args)...);
393 }
394 
396 template <typename... Args>
397 void
398 mooseInfoRepeated(Args &&... args)
399 {
400  moose::internal::mooseInfoStreamRepeated(Moose::out, std::forward<Args>(args)...);
401 }
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:123
std::ostream & operator<<(std::ostream &os, const std::tuple< T... > &tup)
Definition: MooseError.h:52
void mooseUnusedStream(S &oss, Args &&... args)
Definition: MooseError.h:229
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:818
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:323
bool _warnings_are_errors
Variable to toggle any warning into an error (includes deprecated code warnings)
Definition: Moose.C:813
void translateMetaPhysicLError(const MetaPhysicL::LogicError &)
emit a relatively clear error message when we catch a MetaPhysicL logic error
Definition: MooseError.C:141
void mooseWarning(Args &&... args)
Emit a warning message with the given stringified, concatenated args.
Definition: MooseError.h:357
void mooseDeprecationExpired(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:382
void mooseInfoRepeated(Args &&... args)
Emit an informational message with the given stringified, concatenated args.
Definition: MooseError.h:398
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:258
void mooseInfo(Args &&... args)
Emit an informational message with the given stringified, concatenated args.
Definition: MooseError.h:390
const hit::Node * getNode() const
Get the associated hit node, if any.
Definition: MooseError.h:142
void mooseUnused(Args &&... args)
Warning message used to notify the users of unused parts of their input files Really used internally ...
Definition: MooseError.h:366
bool _deprecated_is_error
Variable to toggle only deprecated warnings as errors.
Definition: Moose.C:814
void mooseWarningStream(S &oss, Args &&... args)
Definition: MooseError.h:210
void mooseInfoStreamRepeated(S &oss, Args &&... args)
Definition: MooseError.h:245
void mooseDeprecated(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:374
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:265
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:344
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:816
MooseRuntimeError(const std::string &message, const hit::Node *const node)
Definition: MooseError.h:136
const hit::Node *const _node
The associated hit node, if any.
Definition: MooseError.h:146
Exception to be thrown whenever we have _throw_on_error set and a mooseError() is emitted...
Definition: MooseError.h:133
libMesh::Threads::spin_mutex moose_stream_lock
Definition: MooseError.h:156
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:128