www.mooseframework.org
MooseError.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 namespace MetaPhysicL
25 {
26 class LogicError;
27 }
28 
29 // this function allows streaming tuples to ostreams
30 template <size_t n, typename... T>
31 void
32 print_tuple(std::ostream & os, const std::tuple<T...> & tup)
33 {
34  if constexpr (n < sizeof...(T))
35  {
36  if (n != 0)
37  os << ", ";
38  os << std::get<n>(tup);
39  print_tuple<n + 1>(os, tup);
40  }
41 }
42 template <typename... T>
43 std::ostream &
44 operator<<(std::ostream & os, const std::tuple<T...> & tup)
45 {
46  os << "[";
47  print_tuple<0>(os, tup);
48  return os << "]";
49 }
50 
52 #if defined(LIBMESH_HAVE_MPI)
53 #define MOOSE_ABORT \
54  do \
55  { \
56  MPI_Abort(libMesh::GLOBAL_COMM_WORLD, 1); \
57  std::abort(); \
58  } while (0)
59 #else
60 #define MOOSE_ABORT \
61  do \
62  { \
63  std::abort(); \
64  } while (0)
65 #endif
66 
67 #define mooseDoOnce(do_this) \
68  do \
69  { \
70  static bool did_this_already = false; \
71  if (Moose::show_multiple || !did_this_already) \
72  { \
73  did_this_already = true; \
74  do_this; \
75  } \
76  } while (0)
77 
78 #define mooseCheckMPIErr(err) \
79  do \
80  { \
81  if (err != MPI_SUCCESS) \
82  { \
83  if (libMesh::global_n_processors() == 1) \
84  print_trace(); \
85  libmesh_here(); \
86  MOOSE_ABORT; \
87  } \
88  } while (0)
89 
90 #define mooseException(...) \
91  do \
92  { \
93  throw MooseException(__VA_ARGS__); \
94  } while (0)
95 
96 #ifdef NDEBUG
97 #define mooseAssert(asserted, msg) ((void)0)
98 #else
99 #define mooseAssert(asserted, msg) \
100  do \
101  { \
102  if (!(asserted)) \
103  { \
104  std::ostringstream _assert_oss_; \
105  _assert_oss_ << COLOR_RED << "\n\nAssertion `" #asserted "' failed\n" \
106  << msg << "\nat " << __FILE__ << ", line " << __LINE__ << COLOR_DEFAULT \
107  << std::endl; \
108  if (Moose::_throw_on_error) \
109  throw std::runtime_error(_assert_oss_.str()); \
110  else \
111  { \
112  Moose::err << _assert_oss_.str() << std::flush; \
113  if (libMesh::global_n_processors() == 1) \
114  print_trace(); \
115  else \
116  libMesh::write_traceout(); \
117  libmesh_here(); \
118  MOOSE_ABORT; \
119  } \
120  } \
121  } while (0)
122 #endif
123 
124 template <typename... Args>
125 [[noreturn]] void mooseError(Args &&... args);
126 
128 
129 namespace moose
130 {
131 
132 namespace internal
133 {
134 inline Threads::spin_mutex moose_stream_lock;
135 
143 
151 std::string
152 mooseMsgFmt(const std::string & msg, const std::string & title, const std::string & color);
153 
160 std::string mooseMsgFmt(const std::string & msg, const std::string & color);
161 
162 [[noreturn]] void mooseErrorRaw(std::string msg, const std::string prefix = "");
163 
169 void mooseStreamAll(std::ostringstream & ss);
170 
171 template <typename T, typename... Args>
172 void
173 mooseStreamAll(std::ostringstream & ss, T && val, Args &&... args)
174 {
175  ss << val;
176  mooseStreamAll(ss, std::forward<Args>(args)...);
177 }
178 
179 template <typename S, typename... Args>
180 void
181 mooseWarningStream(S & oss, Args &&... args)
182 {
184  mooseError(std::forward<Args>(args)...);
185 
186  std::ostringstream ss;
187  mooseStreamAll(ss, args...);
188  std::string msg = mooseMsgFmt(ss.str(), "*** Warning ***", COLOR_YELLOW);
190  throw std::runtime_error(msg);
191 
192  {
193  Threads::spin_mutex::scoped_lock lock(moose_stream_lock);
194  oss << msg << std::flush;
195  }
196 }
197 
198 template <typename S, typename... Args>
199 void
200 mooseUnusedStream(S & oss, 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  {
209  Threads::spin_mutex::scoped_lock lock(moose_stream_lock);
210  oss << msg << std::flush;
211  }
212 }
213 
214 template <typename S, typename... Args>
215 void
216 mooseInfoStreamRepeated(S & oss, Args &&... args)
217 {
218  std::ostringstream ss;
219  mooseStreamAll(ss, args...);
220  std::string msg = mooseMsgFmt(ss.str(), "*** Info ***", COLOR_CYAN);
221  {
222  Threads::spin_mutex::scoped_lock lock(moose_stream_lock);
223  oss << msg << std::flush;
224  }
225 }
226 
227 template <typename S, typename... Args>
228 void
229 mooseInfoStream(S & oss, Args &&... args)
230 {
231  mooseDoOnce(mooseInfoStreamRepeated(oss, args...););
232 }
233 
234 template <typename S, typename... Args>
235 void
236 mooseDeprecatedStream(S & oss, const bool expired, const bool print_title, Args &&... args)
237 {
239  mooseError("\n\nDeprecated code:\n", std::forward<Args>(args)...);
240 
241  std::ostringstream ss;
242  mooseStreamAll(ss, args...);
243 
244  const auto color = expired ? COLOR_RED : COLOR_YELLOW;
245  std::string msg =
246  print_title
247  ? mooseMsgFmt(
248  ss.str(),
249  "*** Warning, This code is deprecated and will be removed in future versions:",
250  color)
251  : mooseMsgFmt(ss.str(), color);
252  oss << msg;
253  ss.str("");
254  if (Moose::show_trace)
255  {
256  if (libMesh::global_n_processors() == 1)
257  print_trace(ss);
258  else
260  {
261  Threads::spin_mutex::scoped_lock lock(moose_stream_lock);
262  oss << ss.str() << std::endl;
263  };
264  };
265 }
270 } // namespace internal
271 
275 void translateMetaPhysicLError(const MetaPhysicL::LogicError &);
276 
277 } // namespace moose
278 
282 template <typename... Args>
283 [[noreturn]] void
284 mooseError(Args &&... args)
285 {
286  std::ostringstream oss;
287  moose::internal::mooseStreamAll(oss, std::forward<Args>(args)...);
289 }
290 
294 template <typename... Args>
295 void
296 mooseWarning(Args &&... args)
297 {
298  moose::internal::mooseWarningStream(Moose::out, std::forward<Args>(args)...);
299 }
300 
303 template <typename... Args>
304 void
305 mooseUnused(Args &&... args)
306 {
307  moose::internal::mooseUnusedStream(Moose::out, std::forward<Args>(args)...);
308 }
309 
311 template <typename... Args>
312 void
313 mooseDeprecated(Args &&... args)
314 {
315  moose::internal::mooseDeprecatedStream(Moose::out, false, true, std::forward<Args>(args)...);
316 }
317 
319 template <typename... Args>
320 void
321 mooseDeprecationExpired(Args &&... args)
322 {
323  moose::internal::mooseDeprecatedStream(Moose::out, true, true, std::forward<Args>(args)...);
324 }
325 
327 template <typename... Args>
328 void
329 mooseInfo(Args &&... args)
330 {
331  moose::internal::mooseInfoStream(Moose::out, std::forward<Args>(args)...);
332 }
333 
335 template <typename... Args>
336 void
337 mooseInfoRepeated(Args &&... args)
338 {
339  moose::internal::mooseInfoStreamRepeated(Moose::out, std::forward<Args>(args)...);
340 }
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:91
std::ostream & operator<<(std::ostream &os, const std::tuple< T... > &tup)
Definition: MooseError.h:44
void mooseUnusedStream(S &oss, Args &&... args)
Definition: MooseError.h:200
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:642
processor_id_type global_n_processors()
std::string incompatVarMsg(MooseVariableFieldBase &var1, MooseVariableFieldBase &var2)
Builds and returns a string of the form:
Definition: MooseError.C:23
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:284
bool _warnings_are_errors
Variable to toggle any warning into an error (includes deprecated code warnings)
Definition: Moose.C:637
void translateMetaPhysicLError(const MetaPhysicL::LogicError &)
emit a relatively clear error message when we catch a MetaPhysicL logic error
Definition: MooseError.C:98
void mooseWarning(Args &&... args)
Emit a warning message with the given stringified, concatenated args.
Definition: MooseError.h:296
void mooseDeprecationExpired(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:321
void mooseInfoRepeated(Args &&... args)
Emit an informational message with the given stringified, concatenated args.
Definition: MooseError.h:337
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:229
void mooseInfo(Args &&... args)
Emit an informational message with the given stringified, concatenated args.
Definition: MooseError.h:329
void mooseUnused(Args &&... args)
Warning message used to notify the users of unused parts of their input files Really used internally ...
Definition: MooseError.h:305
bool _deprecated_is_error
Variable to toggle only deprecated warnings as errors.
Definition: Moose.C:638
Threads::spin_mutex moose_stream_lock
Definition: MooseError.h:134
void mooseWarningStream(S &oss, Args &&... args)
Definition: MooseError.h:181
void mooseInfoStreamRepeated(S &oss, Args &&... args)
Definition: MooseError.h:216
void mooseDeprecated(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:313
void mooseErrorRaw(std::string msg, const std::string prefix="")
Definition: MooseError.C:50
void mooseDeprecatedStream(S &oss, const bool expired, const bool print_title, Args &&... args)
Definition: MooseError.h:236
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:640
void print_tuple(std::ostream &os, const std::tuple< T... > &tup)
Definition: MooseError.h:32
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:34