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 // this function allows streaming tuples to ostreams
33 template <size_t n, typename... T>
34 void
35 print_tuple(std::ostream & os, const std::tuple<T...> & tup)
36 {
37  if constexpr (n < sizeof...(T))
38  {
39  if (n != 0)
40  os << ", ";
41  os << std::get<n>(tup);
42  print_tuple<n + 1>(os, tup);
43  }
44 }
45 template <typename... T>
46 std::ostream &
47 operator<<(std::ostream & os, const std::tuple<T...> & tup)
48 {
49  os << "[";
50  print_tuple<0>(os, tup);
51  return os << "]";
52 }
53 
55 #if defined(LIBMESH_HAVE_MPI)
56 #define MOOSE_ABORT \
57  do \
58  { \
59  MPI_Abort(libMesh::GLOBAL_COMM_WORLD, 1); \
60  std::abort(); \
61  } while (0)
62 #else
63 #define MOOSE_ABORT \
64  do \
65  { \
66  std::abort(); \
67  } while (0)
68 #endif
69 
70 #define mooseDoOnce(do_this) \
71  do \
72  { \
73  static bool did_this_already = false; \
74  if (Moose::show_multiple || !did_this_already) \
75  { \
76  did_this_already = true; \
77  do_this; \
78  } \
79  } while (0)
80 
81 #define mooseCheckMPIErr(err) \
82  do \
83  { \
84  if (err != MPI_SUCCESS) \
85  { \
86  if (libMesh::global_n_processors() == 1) \
87  libMesh::print_trace(); \
88  libmesh_here(); \
89  MOOSE_ABORT; \
90  } \
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  if (libMesh::global_n_processors() == 1) \
117  libMesh::print_trace(); \
118  else \
119  libMesh::write_traceout(); \
120  libmesh_here(); \
121  MOOSE_ABORT; \
122  } \
123  } \
124  } while (0)
125 #endif
126 
127 template <typename... Args>
128 [[noreturn]] void mooseError(Args &&... args);
129 
131 
132 namespace moose
133 {
134 
135 namespace internal
136 {
138 
146 
154 std::string
155 mooseMsgFmt(const std::string & msg, const std::string & title, const std::string & color);
156 
163 std::string mooseMsgFmt(const std::string & msg, const std::string & color);
164 
165 [[noreturn]] void mooseErrorRaw(std::string msg, const std::string prefix = "");
166 
172 void mooseStreamAll(std::ostringstream & ss);
173 
174 template <typename T, typename... Args>
175 void
176 mooseStreamAll(std::ostringstream & ss, T && val, Args &&... args)
177 {
178  ss << val;
179  mooseStreamAll(ss, std::forward<Args>(args)...);
180 }
181 
182 template <typename S, typename... Args>
183 void
184 mooseWarningStream(S & oss, Args &&... args)
185 {
187  mooseError(std::forward<Args>(args)...);
188 
189  std::ostringstream ss;
190  mooseStreamAll(ss, args...);
191  std::string msg = mooseMsgFmt(ss.str(), "*** Warning ***", COLOR_YELLOW);
193  throw std::runtime_error(msg);
194 
195  {
197  oss << msg << std::flush;
198  }
199 }
200 
201 template <typename S, typename... Args>
202 void
203 mooseUnusedStream(S & oss, Args &&... args)
204 {
205  std::ostringstream ss;
206  mooseStreamAll(ss, args...);
207  std::string msg = mooseMsgFmt(ss.str(), "*** Warning ***", COLOR_YELLOW);
209  throw std::runtime_error(msg);
210 
211  {
213  oss << msg << std::flush;
214  }
215 }
216 
217 template <typename S, typename... Args>
218 void
219 mooseInfoStreamRepeated(S & oss, Args &&... args)
220 {
221  std::ostringstream ss;
222  mooseStreamAll(ss, args...);
223  std::string msg = mooseMsgFmt(ss.str(), "*** Info ***", COLOR_CYAN);
224  {
226  oss << msg << std::flush;
227  }
228 }
229 
230 template <typename S, typename... Args>
231 void
232 mooseInfoStream(S & oss, Args &&... args)
233 {
234  mooseDoOnce(mooseInfoStreamRepeated(oss, args...););
235 }
236 
237 template <typename S, typename... Args>
238 void
239 mooseDeprecatedStream(S & oss, const bool expired, const bool print_title, Args &&... args)
240 {
242  mooseError("\n\nDeprecated code:\n", std::forward<Args>(args)...);
243 
244  std::ostringstream ss;
245  mooseStreamAll(ss, args...);
246 
247  const auto color = expired ? COLOR_RED : COLOR_YELLOW;
248  std::string msg =
249  print_title
250  ? mooseMsgFmt(
251  ss.str(),
252  "*** Warning, This code is deprecated and will be removed in future versions:",
253  color)
254  : mooseMsgFmt(ss.str(), color);
255  oss << msg;
256  ss.str("");
257  if (Moose::show_trace)
258  {
259  if (libMesh::global_n_processors() == 1)
261  else
263  {
265  oss << ss.str() << std::endl;
266  };
267  };
268 }
285 std::string formatMooseDocumentedError(const std::string & repo_name,
286  const unsigned int issue_num,
287  const std::string & msg);
288 } // namespace internal
289 
293 void translateMetaPhysicLError(const MetaPhysicL::LogicError &);
294 
295 } // namespace moose
296 
300 template <typename... Args>
301 [[noreturn]] void
302 mooseError(Args &&... args)
303 {
304  std::ostringstream oss;
305  moose::internal::mooseStreamAll(oss, std::forward<Args>(args)...);
307 }
308 
321 template <typename... Args>
322 [[noreturn]] void
323 mooseDocumentedError(const std::string & repo_name, const unsigned int issue_num, Args &&... args)
324 {
325  std::ostringstream oss;
326  moose::internal::mooseStreamAll(oss, std::forward<Args>(args)...);
328  moose::internal::formatMooseDocumentedError(repo_name, issue_num, oss.str()));
329 }
330 
334 template <typename... Args>
335 void
336 mooseWarning(Args &&... args)
337 {
338  moose::internal::mooseWarningStream(Moose::out, std::forward<Args>(args)...);
339 }
340 
343 template <typename... Args>
344 void
345 mooseUnused(Args &&... args)
346 {
347  moose::internal::mooseUnusedStream(Moose::out, std::forward<Args>(args)...);
348 }
349 
351 template <typename... Args>
352 void
353 mooseDeprecated(Args &&... args)
354 {
355  moose::internal::mooseDeprecatedStream(Moose::out, false, true, std::forward<Args>(args)...);
356 }
357 
359 template <typename... Args>
360 void
361 mooseDeprecationExpired(Args &&... args)
362 {
363  moose::internal::mooseDeprecatedStream(Moose::out, true, true, std::forward<Args>(args)...);
364 }
365 
367 template <typename... Args>
368 void
369 mooseInfo(Args &&... args)
370 {
371  moose::internal::mooseInfoStream(Moose::out, std::forward<Args>(args)...);
372 }
373 
375 template <typename... Args>
376 void
377 mooseInfoRepeated(Args &&... args)
378 {
379  moose::internal::mooseInfoStreamRepeated(Moose::out, std::forward<Args>(args)...);
380 }
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:94
std::ostream & operator<<(std::ostream &os, const std::tuple< T... > &tup)
Definition: MooseError.h:47
void mooseUnusedStream(S &oss, Args &&... args)
Definition: MooseError.h:203
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:761
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:302
bool _warnings_are_errors
Variable to toggle any warning into an error (includes deprecated code warnings)
Definition: Moose.C:756
void translateMetaPhysicLError(const MetaPhysicL::LogicError &)
emit a relatively clear error message when we catch a MetaPhysicL logic error
Definition: MooseError.C:112
void mooseWarning(Args &&... args)
Emit a warning message with the given stringified, concatenated args.
Definition: MooseError.h:336
void mooseDeprecationExpired(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:361
void mooseInfoRepeated(Args &&... args)
Emit an informational message with the given stringified, concatenated args.
Definition: MooseError.h:377
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:232
void mooseInfo(Args &&... args)
Emit an informational message with the given stringified, concatenated args.
Definition: MooseError.h:369
void mooseUnused(Args &&... args)
Warning message used to notify the users of unused parts of their input files Really used internally ...
Definition: MooseError.h:345
bool _deprecated_is_error
Variable to toggle only deprecated warnings as errors.
Definition: Moose.C:757
void mooseWarningStream(S &oss, Args &&... args)
Definition: MooseError.h:184
void mooseInfoStreamRepeated(S &oss, Args &&... args)
Definition: MooseError.h:219
void mooseDeprecated(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:353
void mooseErrorRaw(std::string msg, const std::string prefix="")
Definition: MooseError.C:53
std::string demangle(const char *name)
void mooseDeprecatedStream(S &oss, const bool expired, const bool print_title, Args &&... args)
Definition: MooseError.h:239
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:323
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:759
libMesh::Threads::spin_mutex moose_stream_lock
Definition: MooseError.h:137
void print_tuple(std::ostream &os, const std::tuple< T... > &tup)
Definition: MooseError.h:35
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:99