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 
23 #if defined(LIBMESH_HAVE_MPI)
24 #define MOOSE_ABORT \
25  do \
26  { \
27  MPI_Abort(libMesh::GLOBAL_COMM_WORLD, 1); \
28  std::abort(); \
29  } while (0)
30 #else
31 #define MOOSE_ABORT \
32  do \
33  { \
34  std::abort(); \
35  } while (0)
36 #endif
37 
38 #define mooseDoOnce(do_this) \
39  do \
40  { \
41  static bool did_this_already = false; \
42  if (!did_this_already) \
43  { \
44  did_this_already = true; \
45  do_this; \
46  } \
47  } while (0)
48 
49 #define mooseCheckMPIErr(err) \
50  do \
51  { \
52  if (err != MPI_SUCCESS) \
53  { \
54  if (libMesh::global_n_processors() == 1) \
55  print_trace(); \
56  libmesh_here(); \
57  MOOSE_ABORT; \
58  } \
59  } while (0)
60 
61 #define mooseException(msg) \
62  do \
63  { \
64  std::ostringstream _exception_oss_; \
65  _exception_oss_ << msg; \
66  \
67  throw MooseException(_exception_oss_.str()); \
68  } while (0)
69 
70 #ifdef NDEBUG
71 #define mooseAssert(asserted, msg)
72 #else
73 #define mooseAssert(asserted, msg) \
74  do \
75  { \
76  if (!(asserted)) \
77  { \
78  std::ostringstream _assert_oss_; \
79  _assert_oss_ << COLOR_RED << "\n\nAssertion `" #asserted "' failed\n" \
80  << msg << "\nat " << __FILE__ << ", line " << __LINE__ << COLOR_DEFAULT \
81  << std::endl; \
82  if (Moose::_throw_on_error) \
83  throw std::runtime_error(_assert_oss_.str()); \
84  else \
85  { \
86  Moose::err << _assert_oss_.str() << std::flush; \
87  if (libMesh::global_n_processors() == 1) \
88  print_trace(); \
89  else \
90  libMesh::write_traceout(); \
91  libmesh_here(); \
92  MOOSE_ABORT; \
93  } \
94  } \
95  } while (0)
96 #endif
97 
98 template <typename... Args>
99 [[noreturn]] void mooseError(Args &&... args);
100 
101 class MooseVariableFEBase;
102 
103 namespace moose
104 {
105 namespace internal
106 {
107 
114 std::string incompatVarMsg(MooseVariableFEBase & var1, MooseVariableFEBase & var2);
115 
116 std::string
117 mooseMsgFmt(const std::string & msg, const std::string & title, const std::string & color);
118 
119 [[noreturn]] void mooseErrorRaw(std::string msg, const std::string prefix = "");
120 
126 void mooseStreamAll(std::ostringstream & ss);
127 
128 template <typename T, typename... Args>
129 void
130 mooseStreamAll(std::ostringstream & ss, T && val, Args &&... args)
131 {
132  ss << val;
133  mooseStreamAll(ss, std::forward<Args>(args)...);
134 }
135 
136 template <typename S, typename... Args>
137 void
138 mooseWarningStream(S & oss, Args &&... args)
139 {
141  mooseError(std::forward<Args>(args)...);
142 
143  std::ostringstream ss;
144  mooseStreamAll(ss, args...);
145  std::string msg = mooseMsgFmt(ss.str(), "*** Warning ***", COLOR_YELLOW);
147  throw std::runtime_error(msg);
148 
149  oss << msg << std::flush;
150 }
151 
152 template <typename S, typename... Args>
153 void
154 mooseUnusedStream(S & oss, Args &&... args)
155 {
156  std::ostringstream ss;
157  mooseStreamAll(ss, args...);
158  std::string msg = mooseMsgFmt(ss.str(), "*** Warning ***", COLOR_YELLOW);
160  throw std::runtime_error(msg);
161 
162  oss << msg << std::flush;
163 }
164 
165 template <typename S, typename... Args>
166 void
167 mooseInfoStream(S & oss, Args &&... args)
168 {
169  mooseDoOnce({
170  std::ostringstream ss;
171  mooseStreamAll(ss, args...);
172  std::string msg = mooseMsgFmt(ss.str(), "*** Info ***", COLOR_CYAN);
173  oss << msg << std::flush;
174  });
175 }
176 
177 template <typename S, typename... Args>
178 void
179 mooseDeprecatedStream(S & oss, bool expired, Args &&... args)
180 {
182  mooseError("\n\nDeprecated code:\n", std::forward<Args>(args)...);
183 
184  mooseDoOnce(std::ostringstream ss; mooseStreamAll(ss, args...);
185  std::string msg = mooseMsgFmt(
186  ss.str(),
187  "*** Warning, This code is deprecated and will be removed in future versions!\n",
188  expired ? COLOR_RED : COLOR_YELLOW);
189  oss << msg;
190  ss.str("");
191  if (libMesh::global_n_processors() == 1) print_trace(ss);
192  else libMesh::write_traceout();
193  oss << ss.str() << std::endl;);
194 }
199 } // namespace internal
200 } // namespace moose
201 
205 template <typename... Args>
206 [[noreturn]] void
207 mooseError(Args &&... args)
208 {
209  std::ostringstream oss;
210  moose::internal::mooseStreamAll(oss, std::forward<Args>(args)...);
212 }
213 
217 template <typename... Args>
218 void
219 mooseWarning(Args &&... args)
220 {
221  moose::internal::mooseWarningStream(Moose::out, std::forward<Args>(args)...);
222 }
223 
226 template <typename... Args>
227 void
228 mooseUnused(Args &&... args)
229 {
230  moose::internal::mooseUnusedStream(Moose::out, std::forward<Args>(args)...);
231 }
232 
234 template <typename... Args>
235 void
236 mooseDeprecated(Args &&... args)
237 {
238  moose::internal::mooseDeprecatedStream(Moose::out, false, std::forward<Args>(args)...);
239 }
240 
242 template <typename... Args>
243 void
244 mooseDeprecationExpired(Args &&... args)
245 {
246  moose::internal::mooseDeprecatedStream(Moose::out, true, std::forward<Args>(args)...);
247 }
248 
250 template <typename... Args>
251 void
252 mooseInfo(Args &&... args)
253 {
254  moose::internal::mooseInfoStream(Moose::out, std::forward<Args>(args)...);
255 }
256 
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:87
void mooseUnusedStream(S &oss, Args &&... args)
Definition: MooseError.h:154
std::string incompatVarMsg(MooseVariableFEBase &var1, MooseVariableFEBase &var2)
Builds and returns a string of the form:
Definition: MooseError.C:22
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:207
bool _warnings_are_errors
Variable to toggle any warning into an error (includes deprecated code warnings)
Definition: Moose.C:570
void mooseWarning(Args &&... args)
Emit a warning message with the given stringified, concatenated args.
Definition: MooseError.h:219
void mooseDeprecationExpired(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:244
void mooseInfoStream(S &oss, Args &&... args)
Definition: MooseError.h:167
void mooseInfo(Args &&... args)
Emit an informational message with the given stringified, concatenated args.
Definition: MooseError.h:252
void mooseUnused(Args &&... args)
Warning message used to notify the users of unused parts of their input files Really used internally ...
Definition: MooseError.h:228
bool _deprecated_is_error
Variable to toggle only deprecated warnings as errors.
Definition: Moose.C:572
void mooseWarningStream(S &oss, Args &&... args)
Definition: MooseError.h:138
void mooseDeprecated(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:236
void mooseErrorRaw(std::string msg, const std::string prefix="")
Definition: MooseError.C:43
void mooseDeprecatedStream(S &oss, bool expired, Args &&... args)
Definition: MooseError.h:179
bool _throw_on_error
Variable to turn on exceptions during mooseError() and mooseWarning(), should only be used with MOOSE...
Definition: Moose.C:574
std::string mooseMsgFmt(const std::string &msg, const std::string &title, const std::string &color)
Definition: MooseError.C:33