My Project
Loading...
Searching...
No Matches
ErrorMacros.hpp
1/*
2 Copyright 2013 Andreas Lauser
3 Copyright 2009, 2010 SINTEF ICT, Applied Mathematics.
4 Copyright 2009, 2010 Statoil ASA.
5
6 This file is part of the Open Porous Media project (OPM).
7
8 OPM is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 OPM is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with OPM. If not, see <http://www.gnu.org/licenses/>.
20*/
21#ifndef OPM_ERRORMACROS_HPP
22#define OPM_ERRORMACROS_HPP
23
24#include <opm/common/OpmLog/OpmLog.hpp>
25
26#include <string>
27#include <exception>
28#include <stdexcept>
29#include <cassert>
30
31// macros for reporting to stderr
32#ifdef OPM_VERBOSE // Verbose mode
33# include <iostream>
34# define OPM_REPORT do { std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] " } while (false)
35# define OPM_MESSAGE(x) do { OPM_REPORT; std::cerr << x << "\n"; } while (false)
36# define OPM_MESSAGE_IF(cond, m) do {if(cond) OPM_MESSAGE(m);} while (false)
37#else // non-verbose mode (default)
38# define OPM_REPORT do {} while (false)
39# define OPM_MESSAGE(x) do {} while (false)
40# define OPM_MESSAGE_IF(cond, m) do {} while (false)
41#endif
42
43// Macro to throw an exception that counts as an error in PRT file.
44// NOTE: For this macro to work, the
45// exception class must exhibit a constructor with the signature
46// (const std::string &message). Since this condition is not fulfilled
47// for the std::exception, you should use this macro with some
48// exception class derived from either std::logic_error or
49// std::runtime_error.
50//
51// Usage: OPM_THROW(ExceptionClass, "Error message");
52#define OPM_THROW(Exception, message) \
53 do { \
54 std::string oss_ = std::string{"["} + __FILE__ + ":" + \
55 std::to_string(__LINE__) + "] " + \
56 message; \
57 ::Opm::OpmLog::error(oss_); \
58 throw Exception(oss_); \
59 } while (false)
60
61// Macro to throw an exception that only counts as a problem in PRT file.
62// NOTE: For this macro to work, the
63// exception class must exhibit a constructor with the signature
64// (const std::string &message). Since this condition is not fulfilled
65// for the std::exception, you should use this macro with some
66// exception class derived from either std::logic_error or
67// std::runtime_error.
68//
69// Usage: OPM_THROW_PROBLEM(ExceptionClass, "Error message");
70#define OPM_THROW_PROBLEM(Exception, message) \
71 do { \
72 std::string oss_ = std::string{"["} + __FILE__ + ":" + \
73 std::to_string(__LINE__) + "] " + \
74 message; \
75 ::Opm::OpmLog::problem(oss_); \
76 throw Exception(oss_); \
77 } while (false)
78
79// Same as OPM_THROW, except for not making an OpmLog::error() call.
80//
81// Usage: OPM_THROW_NOLOG(ExceptionClass, "Error message");
82#define OPM_THROW_NOLOG(Exception, message) \
83 do { \
84 std::string oss_ = std::string{"["} + __FILE__ + ":" + \
85 std::to_string(__LINE__) + "] " + \
86 message; \
87 throw Exception(oss_); \
88 } while (false)
89
90// throw an exception if a condition is true
91#define OPM_ERROR_IF(condition, message) do {if(condition){ OPM_THROW(std::logic_error, message);}} while(false)
92
93#endif // OPM_ERRORMACROS_HPP