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_); \
73void _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);
104inline 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);
138#define OPM_BEGIN_PARALLEL_TRY_CATCH() \
139std::string obptc_exc_msg; \
140auto obptc_exc_type = Opm::ExceptionType::NONE; \
146#define OPM_PARALLEL_CATCH_CLAUSE(obptc_exc_type, \
148catch (const Opm::NumericalProblem& e){ \
149 obptc_exc_type = Opm::ExceptionType::NUMERICAL_ISSUE; \
150 obptc_exc_msg = e.what(); \
151} catch (const std::runtime_error& e) { \
152 obptc_exc_type = Opm::ExceptionType::RUNTIME_ERROR; \
153 obptc_exc_msg = e.what(); \
154} catch (const std::invalid_argument& e) { \
155 obptc_exc_type = Opm::ExceptionType::INVALID_ARGUMENT; \
156 obptc_exc_msg = e.what(); \
157} catch (const std::logic_error& e) { \
158 obptc_exc_type = Opm::ExceptionType::LOGIC_ERROR; \
159 obptc_exc_msg = e.what(); \
160} catch (const std::exception& e) { \
161 obptc_exc_type = Opm::ExceptionType::DEFAULT; \
162 obptc_exc_msg = e.what(); \
164 obptc_exc_type = Opm::ExceptionType::DEFAULT; \
165 obptc_exc_msg = "Unknown exception was thrown"; \
172#define OPM_END_PARALLEL_TRY_CATCH(prefix, comm) \
174OPM_PARALLEL_CATCH_CLAUSE(obptc_exc_type, obptc_exc_msg);\
175checkForExceptionsAndThrow(obptc_exc_type, \
176 prefix + obptc_exc_msg, comm);
182#define OPM_END_PARALLEL_TRY_CATCH_LOG(obptc_logger, obptc_prefix, obptc_output, comm)\
184OPM_PARALLEL_CATCH_CLAUSE(obptc_exc_type, obptc_exc_msg); \
185logAndCheckForExceptionsAndThrow(obptc_logger, obptc_exc_type, \
186 obptc_prefix + obptc_exc_msg, obptc_output, comm);
Definition DeferredLogger.hpp:57
void logMessages()
Log all messages to the OpmLog backends, and clear the message container.
Definition DeferredLogger.cpp:85
void clearMessages()
Clear the message container without logging them.
Definition DeferredLogger.cpp:93
Opm::DeferredLogger gatherDeferredLogger(const Opm::DeferredLogger &local_deferredlogger, Opm::Parallel::Communication)
Create a global log combining local logs.
Definition gatherDeferredLogger.cpp:168