https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
26
27namespace MetaPhysicL
28{
29class LogicError;
30}
31
32namespace hit
33{
34class Node;
35}
36
37// this function allows streaming tuples to ostreams
38template <size_t n, typename... T>
39void
40print_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}
50template <typename... T>
51std::ostream &
52operator<<(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
106template <typename... Args>
107[[noreturn]] void mooseError(Args &&... args);
108
117class MooseRuntimeError : public std::runtime_error
118{
119public:
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
128private:
130 const hit::Node * const _node;
131};
132
134
135namespace moose
136{
137
138namespace internal
139{
141
149
157std::string
158mooseMsgFmt(const std::string & msg, const std::string & title, const std::string & color);
159
166std::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
185void mooseStreamAll(std::ostringstream & ss);
186
187template <typename T, typename... Args>
188void
189mooseStreamAll(std::ostringstream & ss, T && val, Args &&... args)
190{
191 ss << val;
192 mooseStreamAll(ss, std::forward<Args>(args)...);
193}
194
195template <typename S, typename... Args>
196void
197mooseWarningStream(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
214template <typename S, typename... Args>
215void
216mooseUnusedStream(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
230template <typename S, typename... Args>
231void
232mooseInfoStreamRepeated(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
243template <typename S, typename... Args>
244void
245mooseInfoStream(S & oss, Args &&... args)
246{
247 mooseDoOnce(mooseInfoStreamRepeated(oss, args...););
248}
249
250template <typename S, typename... Args>
251void
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 {
270 else
272 {
274 oss << ss.str() << std::endl;
275 };
276 };
277}
294std::string formatMooseDocumentedError(const std::string & repo_name,
295 const unsigned int issue_num,
296 const std::string & msg);
297} // namespace internal
298
302void translateMetaPhysicLError(const MetaPhysicL::LogicError &);
303
304} // namespace moose
305
309template <typename... Args>
310[[noreturn]] void
311mooseError(Args &&... args)
312{
313 std::ostringstream oss;
314 moose::internal::mooseStreamAll(oss, std::forward<Args>(args)...);
316}
317
330template <typename... Args>
331[[noreturn]] void
332mooseDocumentedError(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
343template <typename... Args>
344void
345mooseWarning(Args &&... args)
346{
347 moose::internal::mooseWarningStream(Moose::out, std::forward<Args>(args)...);
348}
349
352template <typename... Args>
353void
354mooseUnused(Args &&... args)
355{
356 moose::internal::mooseUnusedStream(Moose::out, std::forward<Args>(args)...);
357}
358
361template <typename... Args>
362void
363mooseDeprecated(Args &&... args)
364{
366 Moose::out, false, true, true, std::forward<Args>(args)...);
367}
368
371template <typename... Args>
372void
374{
376 Moose::out, false, true, false, std::forward<Args>(args)...);
377}
378
381template <typename... Args>
382void
384{
385 moose::internal::mooseDeprecatedStream(Moose::out, true, true, true, std::forward<Args>(args)...);
386}
387
390template <typename... Args>
391void
393{
395 Moose::out, true, true, false, std::forward<Args>(args)...);
396}
397
399template <typename... Args>
400void
401mooseInfo(Args &&... args)
402{
403 moose::internal::mooseInfoStream(Moose::out, std::forward<Args>(args)...);
404}
405
407template <typename... Args>
408void
409mooseInfoRepeated(Args &&... args)
410{
411 moose::internal::mooseInfoStreamRepeated(Moose::out, std::forward<Args>(args)...);
412}
void mooseInfoRepeated(Args &&... args)
Emit an informational message with the given stringified, concatenated args.
Definition MooseError.h:409
void mooseDeprecatedNoTrace(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition MooseError.h:373
void print_tuple(std::ostream &os, const std::tuple< T... > &tup)
Definition MooseError.h:40
void mooseInfo(Args &&... args)
Emit an informational message with the given stringified, concatenated args.
Definition MooseError.h:401
void mooseDeprecationExpiredNoTrace(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition MooseError.h:392
void mooseWarning(Args &&... args)
Emit a warning message with the given stringified, concatenated args.
Definition MooseError.h:345
std::ostream & operator<<(std::ostream &os, const std::tuple< T... > &tup)
Definition MooseError.h:52
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 mooseDeprecationExpired(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition MooseError.h:383
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
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
void mooseDeprecated(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition MooseError.h:363
Exception to be thrown whenever we have _throw_on_error set and a mooseError() is emitted.
Definition MooseError.h:118
MooseRuntimeError(const std::string &message, const hit::Node *const node)
Definition MooseError.h:120
const hit::Node * getNode() const
Get the associated hit node, if any.
Definition MooseError.h:126
const hit::Node *const _node
The associated hit node, if any.
Definition MooseError.h:130
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...
bool _deprecated_is_error
Variable to toggle only deprecated warnings as errors.
Definition Moose.C:895
bool _throw_on_warning
Variable to turn on exceptions during mooseWarning(), should only be used in MOOSE unit tests.
Definition Moose.C:897
bool _warnings_are_errors
Variable to toggle any warning into an error (includes deprecated code warnings)
Definition Moose.C:894
Definition Moose.h:48
void write_traceout()
std::string demangle(const char *name)
processor_id_type global_n_processors()
void print_trace(std::ostream &out_stream=std::cerr)
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
void mooseWarningStream(S &oss, Args &&... args)
Definition MooseError.h:197
void mooseDeprecatedStream(S &oss, const bool expired, const bool print_title, const bool show_trace, Args &&... args)
Definition MooseError.h:252
libMesh::Threads::spin_mutex moose_stream_lock
Definition MooseError.h:140
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
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
void mooseUnusedStream(S &oss, Args &&... args)
Definition MooseError.h:216
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::string incompatVarMsg(MooseVariableFieldBase &var1, MooseVariableFieldBase &var2)
Builds and returns a string of the form:
Definition MooseError.C:26
void mooseInfoStreamRepeated(S &oss, Args &&... args)
Definition MooseError.h:232
void mooseInfoStream(S &oss, Args &&... args)
Definition MooseError.h:245
void translateMetaPhysicLError(const MetaPhysicL::LogicError &)
emit a relatively clear error message when we catch a MetaPhysicL logic error
Definition MooseError.C:155