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 
59 #define mooseDoOnce(do_this) \
60  do \
61  { \
62  static bool did_this_already = false; \
63  if (Moose::show_multiple || !did_this_already) \
64  { \
65  did_this_already = true; \
66  do_this; \
67  } \
68  } while (0)
69 
70 #define mooseCheckMPIErr(err) \
71  do \
72  { \
73  if (err != MPI_SUCCESS) \
74  moose::internal::mooseErrorRaw(""); \
75  } while (0)
76 
77 #define mooseException(...) \
78  do \
79  { \
80  throw MooseException(__VA_ARGS__); \
81  } while (0)
82 
83 #ifdef NDEBUG
84 #define mooseAssert(asserted, msg) ((void)0)
85 #else
86 #define mooseAssert(asserted, msg) \
87  do \
88  { \
89  if (!(asserted)) \
90  { \
91  std::ostringstream _assert_oss_; \
92  _assert_oss_ << COLOR_RED << "\n\nAssertion `" #asserted "' failed\n" \
93  << msg << "\nat " << __FILE__ << ", line " << __LINE__ << COLOR_DEFAULT \
94  << std::endl; \
95  if (Moose::_throw_on_error) \
96  throw std::runtime_error(_assert_oss_.str()); \
97  else \
98  { \
99  Moose::err << _assert_oss_.str() << std::flush; \
100  moose::internal::mooseErrorRaw(""); \
101  } \
102  } \
103  } while (0)
104 #endif
105 
106 template <typename... Args>
107 [[noreturn]] void mooseError(Args &&... args);
108 
117 class MooseRuntimeError : public std::runtime_error
118 {
119 public:
120  MooseRuntimeError(const std::string & message, const hit::Node * const node)
121  : runtime_error(message), _node(node)
122  {
123  }
124 
126  const hit::Node * getNode() const { return _node; }
127 
128 private:
130  const hit::Node * const _node;
131 };
132 
134 
135 namespace moose
136 {
137 
138 namespace internal
139 {
141 
149 
157 std::string
158 mooseMsgFmt(const std::string & msg, const std::string & title, const std::string & color);
159 
166 std::string mooseMsgFmt(const std::string & msg, const std::string & color);
167 
175 [[noreturn]] void mooseErrorRaw(std::string msg,
176  const std::string & prefix = "",
177  const hit::Node * node = nullptr,
178  const bool show_trace = true);
179 
185 void mooseStreamAll(std::ostringstream & ss);
186 
187 template <typename T, typename... Args>
188 void
189 mooseStreamAll(std::ostringstream & ss, T && val, Args &&... args)
190 {
191  ss << val;
192  mooseStreamAll(ss, std::forward<Args>(args)...);
193 }
194 
195 template <typename S, typename... Args>
196 void
197 mooseWarningStream(S & oss, Args &&... args)
198 {
200  mooseError(std::forward<Args>(args)...);
201 
202  std::ostringstream ss;
203  mooseStreamAll(ss, args...);
204  std::string msg = mooseMsgFmt(ss.str(), "*** Warning ***", COLOR_YELLOW);
206  throw std::runtime_error(msg);
207 
208  {
210  oss << msg << std::flush;
211  }
212 }
213 
214 template <typename S, typename... Args>
215 void
216 mooseUnusedStream(S & oss, Args &&... args)
217 {
218  std::ostringstream ss;
219  mooseStreamAll(ss, args...);
220  std::string msg = mooseMsgFmt(ss.str(), "*** Warning ***", COLOR_YELLOW);
222  throw std::runtime_error(msg);
223 
224  {
226  oss << msg << std::flush;
227  }
228 }
229 
230 template <typename S, typename... Args>
231 void
232 mooseInfoStreamRepeated(S & oss, Args &&... args)
233 {
234  std::ostringstream ss;
235  mooseStreamAll(ss, args...);
236  std::string msg = mooseMsgFmt(ss.str(), "*** Info ***", COLOR_CYAN);
237  {
239  oss << msg << std::flush;
240  }
241 }
242 
243 template <typename S, typename... Args>
244 void
245 mooseInfoStream(S & oss, Args &&... args)
246 {
247  mooseDoOnce(mooseInfoStreamRepeated(oss, args...););
248 }
249 
250 template <typename S, typename... Args>
251 void
253  S & oss, const bool expired, const bool print_title, const bool show_trace, Args &&... args)
254 {
256  mooseError("\n\nDeprecated code:\n", std::forward<Args>(args)...);
257 
258  std::ostringstream ss;
259  mooseStreamAll(ss, args...);
260 
261  const auto color = expired ? COLOR_RED : COLOR_YELLOW;
262  std::string msg = print_title ? mooseMsgFmt(ss.str(), "*** Deprecation Warning ***", color)
263  : mooseMsgFmt(ss.str(), color);
264  oss << msg;
265  ss.str("");
266  if (show_trace)
267  {
268  if (libMesh::global_n_processors() == 1)
270  else
272  {
274  oss << ss.str() << std::endl;
275  };
276  };
277 }
294 std::string formatMooseDocumentedError(const std::string & repo_name,
295  const unsigned int issue_num,
296  const std::string & msg);
297 } // namespace internal
298 
302 void translateMetaPhysicLError(const MetaPhysicL::LogicError &);
303 
304 } // namespace moose
305 
309 template <typename... Args>
310 [[noreturn]] void
311 mooseError(Args &&... args)
312 {
313  std::ostringstream oss;
314  moose::internal::mooseStreamAll(oss, std::forward<Args>(args)...);
316 }
317 
330 template <typename... Args>
331 [[noreturn]] void
332 mooseDocumentedError(const std::string & repo_name, const unsigned int issue_num, Args &&... args)
333 {
334  std::ostringstream oss;
335  moose::internal::mooseStreamAll(oss, std::forward<Args>(args)...);
337  moose::internal::formatMooseDocumentedError(repo_name, issue_num, oss.str()));
338 }
339 
343 template <typename... Args>
344 void
345 mooseWarning(Args &&... args)
346 {
347  moose::internal::mooseWarningStream(Moose::out, std::forward<Args>(args)...);
348 }
349 
352 template <typename... Args>
353 void
354 mooseUnused(Args &&... args)
355 {
356  moose::internal::mooseUnusedStream(Moose::out, std::forward<Args>(args)...);
357 }
358 
361 template <typename... Args>
362 void
363 mooseDeprecated(Args &&... args)
364 {
366  Moose::out, false, true, true, std::forward<Args>(args)...);
367 }
368 
371 template <typename... Args>
372 void
373 mooseDeprecatedNoTrace(Args &&... args)
374 {
376  Moose::out, false, true, false, std::forward<Args>(args)...);
377 }
378 
381 template <typename... Args>
382 void
383 mooseDeprecationExpired(Args &&... args)
384 {
385  moose::internal::mooseDeprecatedStream(Moose::out, true, true, true, std::forward<Args>(args)...);
386 }
387 
390 template <typename... Args>
391 void
393 {
395  Moose::out, true, true, false, std::forward<Args>(args)...);
396 }
397 
399 template <typename... Args>
400 void
401 mooseInfo(Args &&... args)
402 {
403  moose::internal::mooseInfoStream(Moose::out, std::forward<Args>(args)...);
404 }
405 
407 template <typename... Args>
408 void
409 mooseInfoRepeated(Args &&... args)
410 {
411  moose::internal::mooseInfoStreamRepeated(Moose::out, std::forward<Args>(args)...);
412 }
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:137
std::ostream & operator<<(std::ostream &os, const std::tuple< T... > &tup)
Definition: MooseError.h:52
void mooseUnusedStream(S &oss, Args &&... args)
Definition: MooseError.h:216
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:311
bool _warnings_are_errors
Variable to toggle any warning into an error (includes deprecated code warnings)
Definition: Moose.C:843
void translateMetaPhysicLError(const MetaPhysicL::LogicError &)
emit a relatively clear error message when we catch a MetaPhysicL logic error
Definition: MooseError.C:155
void mooseWarning(Args &&... args)
Emit a warning message with the given stringified, concatenated args.
Definition: MooseError.h:345
void mooseDeprecationExpired(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:383
void mooseInfoRepeated(Args &&... args)
Emit an informational message with the given stringified, concatenated args.
Definition: MooseError.h:409
void mooseDeprecationExpiredNoTrace(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:392
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:34
void write_traceout()
void mooseInfoStream(S &oss, Args &&... args)
Definition: MooseError.h:245
void mooseInfo(Args &&... args)
Emit an informational message with the given stringified, concatenated args.
Definition: MooseError.h:401
const hit::Node * getNode() const
Get the associated hit node, if any.
Definition: MooseError.h:126
void mooseUnused(Args &&... args)
Warning message used to notify the users of unused parts of their input files Really used internally ...
Definition: MooseError.h:354
bool _deprecated_is_error
Variable to toggle only deprecated warnings as errors.
Definition: Moose.C:844
void mooseDeprecatedStream(S &oss, const bool expired, const bool print_title, const bool show_trace, Args &&... args)
Definition: MooseError.h:252
void mooseWarningStream(S &oss, Args &&... args)
Definition: MooseError.h:197
void mooseInfoStreamRepeated(S &oss, Args &&... args)
Definition: MooseError.h:232
void mooseDeprecated(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:363
void mooseErrorRaw(std::string msg, const std::string &prefix="", const hit::Node *node=nullptr, const bool show_trace=true)
Main callback for emitting a moose error.
Definition: MooseError.C:53
std::string demangle(const char *name)
Definition: Moose.h:46
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:332
void print_trace(std::ostream &out_stream=std::cerr)
bool _throw_on_warning
Variable to turn on exceptions during mooseWarning(), should only be used in MOOSE unit tests...
Definition: Moose.C:846
MooseRuntimeError(const std::string &message, const hit::Node *const node)
Definition: MooseError.h:120
void mooseDeprecatedNoTrace(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:373
const hit::Node *const _node
The associated hit node, if any.
Definition: MooseError.h:130
Exception to be thrown whenever we have _throw_on_error set and a mooseError() is emitted...
Definition: MooseError.h:117
libMesh::Threads::spin_mutex moose_stream_lock
Definition: MooseError.h:140
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:142