21 #ifndef OPM_DEFERREDLOGGINGERRORHELPERS_HPP 22 #define OPM_DEFERREDLOGGINGERRORHELPERS_HPP 24 #include <opm/common/Exceptions.hpp> 26 #include <opm/simulators/utils/DeferredLogger.hpp> 27 #include <opm/simulators/utils/gatherDeferredLogger.hpp> 29 #include <opm/simulators/utils/ParallelCommunication.hpp> 45 #define OPM_DEFLOG_THROW(Exception, message, deferred_logger) \ 47 std::string oss_ = std::string{"["} + __FILE__ + ":" + \ 48 std::to_string(__LINE__) + "] " + \ 50 deferred_logger.error(oss_); \ 51 throw Exception(oss_); \ 61 #define OPM_DEFLOG_PROBLEM(Exception, message, deferred_logger) \ 63 std::string oss_ = std::string{"["} + __FILE__ + ":" + \ 64 std::to_string(__LINE__) + "] " + \ 66 deferred_logger.problem(oss_); \ 67 throw Exception(oss_); \ 73 void _throw(Opm::ExceptionType::ExcEnum exc_type,
74 const std::string& message,
75 Opm::Parallel::Communication comm)
77 auto global_exc = comm.max(exc_type);
80 case Opm::ExceptionType::NONE:
82 case Opm::ExceptionType::RUNTIME_ERROR:
83 throw std::runtime_error(message);
85 case Opm::ExceptionType::INVALID_ARGUMENT:
86 throw std::invalid_argument(message);
88 case Opm::ExceptionType::NUMERICAL_ISSUE:
89 throw Opm::NumericalProblem(message);
91 case Opm::ExceptionType::DEFAULT:
92 case Opm::ExceptionType::LOGIC_ERROR:
93 throw std::logic_error(message);
96 throw std::logic_error(message);
104 inline void checkForExceptionsAndThrow(Opm::ExceptionType::ExcEnum exc_type,
105 const std::string& message,
106 Opm::Parallel::Communication comm)
108 _throw(exc_type, message, comm);
112 Opm::ExceptionType::ExcEnum exc_type,
113 const std::string& message,
114 const bool terminal_output,
115 Opm::Parallel::Communication comm)
118 if (exc_type != Opm::ExceptionType::NONE && comm.size() > 1) {
119 deferred_logger.error(
"[Exception on rank " + std::to_string(comm.rank()) +
"]: " + message);
123 if (terminal_output) {
130 _throw(exc_type, message, comm);
134 Opm::ExceptionType::ExcEnum exc_type,
135 const std::string& message,
136 const bool terminal_output,
137 Opm::Parallel::Communication comm)
140 if (exc_type != Opm::ExceptionType::NONE && comm.size() > 1) {
141 deferred_logger.problem(
"[Exception on rank " + std::to_string(comm.rank()) +
"]: " + message);
145 if (terminal_output) {
152 _throw(exc_type, message, comm);
158 #define OPM_BEGIN_PARALLEL_TRY_CATCH() \ 159 std::string obptc_exc_msg; \ 160 auto obptc_exc_type = Opm::ExceptionType::NONE; \ 166 #define OPM_PARALLEL_CATCH_CLAUSE(obptc_exc_type, \ 168 catch (const Opm::NumericalProblem& e){ \ 169 obptc_exc_type = Opm::ExceptionType::NUMERICAL_ISSUE; \ 170 obptc_exc_msg = e.what(); \ 171 } catch (const std::runtime_error& e) { \ 172 obptc_exc_type = Opm::ExceptionType::RUNTIME_ERROR; \ 173 obptc_exc_msg = e.what(); \ 174 } catch (const std::invalid_argument& e) { \ 175 obptc_exc_type = Opm::ExceptionType::INVALID_ARGUMENT; \ 176 obptc_exc_msg = e.what(); \ 177 } catch (const std::logic_error& e) { \ 178 obptc_exc_type = Opm::ExceptionType::LOGIC_ERROR; \ 179 obptc_exc_msg = e.what(); \ 180 } catch (const std::exception& e) { \ 181 obptc_exc_type = Opm::ExceptionType::DEFAULT; \ 182 obptc_exc_msg = e.what(); \ 184 obptc_exc_type = Opm::ExceptionType::DEFAULT; \ 185 obptc_exc_msg = "Unknown exception was thrown"; \ 192 #define OPM_END_PARALLEL_TRY_CATCH(prefix, comm) \ 194 OPM_PARALLEL_CATCH_CLAUSE(obptc_exc_type, obptc_exc_msg);\ 195 checkForExceptionsAndThrow(obptc_exc_type, \ 196 prefix + obptc_exc_msg, comm); 202 #define OPM_END_PARALLEL_TRY_CATCH_LOG(obptc_logger, obptc_prefix, obptc_output, comm)\ 204 OPM_PARALLEL_CATCH_CLAUSE(obptc_exc_type, obptc_exc_msg); \ 205 logAndCheckForExceptionsAndThrow(obptc_logger, obptc_exc_type, \ 206 obptc_prefix + obptc_exc_msg, obptc_output, comm); 207 #endif // OPM_DEFERREDLOGGINGERRORHELPERS_HPP void clearMessages()
Clear the message container without logging them.
Definition: DeferredLogger.cpp:100
Definition: DeferredLogger.hpp:56
void logMessages()
Log all messages to the OpmLog backends, and clear the message container.
Definition: DeferredLogger.cpp:92
Opm::DeferredLogger gatherDeferredLogger(const Opm::DeferredLogger &local_deferredlogger, Opm::Parallel::Communication)
Create a global log combining local logs.
Definition: gatherDeferredLogger.cpp:168