[591] | 1 | #ifndef __XIOS_CException__ |
---|
| 2 | #define __XIOS_CException__ |
---|
[219] | 3 | |
---|
[591] | 4 | /// XIOS headers /// |
---|
| 5 | #include "xios_spl.hpp" |
---|
[219] | 6 | #include "object.hpp" |
---|
[2629] | 7 | #include <boost_extract.hpp> |
---|
[1622] | 8 | #include <iomanip> |
---|
| 9 | #include <stdexcept> |
---|
[219] | 10 | |
---|
[335] | 11 | namespace xios |
---|
[219] | 12 | { |
---|
| 13 | /// ////////////////////// Déclarations ////////////////////// /// |
---|
[1622] | 14 | |
---|
[219] | 15 | class CException |
---|
| 16 | : private CObject, public StdOStringStream |
---|
| 17 | { |
---|
| 18 | typedef CObject SuperClass; |
---|
| 19 | |
---|
| 20 | public : |
---|
| 21 | |
---|
| 22 | /// Constructeurs /// |
---|
| 23 | CException(void); |
---|
| 24 | explicit CException(const StdString & id); |
---|
| 25 | CException(const CException & exception); |
---|
| 26 | CException(const CException * const exception); // Not implemented. |
---|
| 27 | |
---|
| 28 | /// Accesseurs /// |
---|
| 29 | StdString getMessage(void) const; |
---|
| 30 | StdOStringStream & getStream(void); |
---|
| 31 | |
---|
| 32 | /// Destructeur /// |
---|
| 33 | virtual ~CException(void); |
---|
| 34 | |
---|
| 35 | /// Autre /// |
---|
| 36 | virtual StdString toString(void) const; |
---|
| 37 | virtual void fromString(const StdString & str); |
---|
| 38 | |
---|
[1622] | 39 | struct StackInfo |
---|
| 40 | { |
---|
| 41 | StdString file; |
---|
| 42 | StdString function; |
---|
| 43 | int line; |
---|
| 44 | StdString info; |
---|
| 45 | }; |
---|
| 46 | |
---|
| 47 | std::list<StackInfo> stack; |
---|
| 48 | |
---|
[219] | 49 | private : |
---|
| 50 | |
---|
| 51 | /// Propriétés /// |
---|
| 52 | bool desc_rethrow; // throw destructor |
---|
[1622] | 53 | StdOStringStream stream; |
---|
[219] | 54 | |
---|
| 55 | }; // CException |
---|
[335] | 56 | } // namespace xios |
---|
[219] | 57 | |
---|
| 58 | /// //////////////////////////// Macros //////////////////////////// /// |
---|
| 59 | |
---|
[1622] | 60 | #define FILE_NAME (std::strrchr("/" __FILE__, '/') + 1) |
---|
| 61 | |
---|
[2629] | 62 | #define FUNCTION_NAME (StdString(XIOS_CURRENT_FUNCTION).length() > 100 ? \ |
---|
| 63 | StdString(XIOS_CURRENT_FUNCTION).substr(0,100).append("...)") : XIOS_CURRENT_FUNCTION) |
---|
[1622] | 64 | |
---|
[219] | 65 | #define INFO(x) \ |
---|
[2629] | 66 | "In file \""<< FILE_NAME <<"\", function \"" << XIOS_CURRENT_FUNCTION <<"\", line " << __LINE__ << " -> " x << std::endl; |
---|
[219] | 67 | |
---|
[268] | 68 | #ifdef __XIOS_DEBUG |
---|
[219] | 69 | # define DEBUG(x) std::clog << "> Debug " << INFO(x) |
---|
| 70 | #else |
---|
| 71 | # define DEBUG(x) |
---|
| 72 | #endif |
---|
| 73 | |
---|
[1622] | 74 | #define ERROR(id, x) \ |
---|
| 75 | { \ |
---|
| 76 | xios::CException exc(id); \ |
---|
| 77 | exc.getStream() << INFO(x); \ |
---|
| 78 | error << exc.getMessage() << std::endl; \ |
---|
| 79 | throw exc; \ |
---|
| 80 | } |
---|
[219] | 81 | |
---|
[2282] | 82 | #define MISSING_RETURN(id) \ |
---|
| 83 | { \ |
---|
| 84 | xios::CException exc(id); \ |
---|
| 85 | exc.getStream() << INFO("Missing return"); \ |
---|
| 86 | error << exc.getMessage() << std::endl; \ |
---|
| 87 | throw exc; \ |
---|
| 88 | } |
---|
| 89 | |
---|
[1622] | 90 | #ifdef __XIOS_EXCEPTION |
---|
| 91 | #define TRY \ |
---|
| 92 | { \ |
---|
| 93 | int funcFirstLine = __LINE__; \ |
---|
| 94 | try |
---|
| 95 | #define CATCH \ |
---|
| 96 | catch(CException& e) \ |
---|
| 97 | { \ |
---|
| 98 | CException::StackInfo stk; \ |
---|
| 99 | stk.file = FILE_NAME; \ |
---|
| 100 | stk.function = FUNCTION_NAME; \ |
---|
| 101 | stk.line = funcFirstLine; \ |
---|
| 102 | e.stack.push_back(stk); \ |
---|
| 103 | if (CXios::xiosStack) \ |
---|
| 104 | throw; \ |
---|
| 105 | else \ |
---|
| 106 | throw 0; \ |
---|
| 107 | } \ |
---|
| 108 | } |
---|
| 109 | #define CATCH_DUMP_ATTR \ |
---|
| 110 | catch(CException& e) \ |
---|
| 111 | { \ |
---|
| 112 | CException::StackInfo stk; \ |
---|
| 113 | stk.info.append(StdString("Object id=\"" + this->getId()) + "\" object type=\"" + this->getName() + "\"\n"); \ |
---|
| 114 | stk.info.append("*** XIOS attributes as defined in XML file(s) or via Fortran interface:\n"); \ |
---|
| 115 | stk.info.append("[" + this->dumpXiosAttributes() + "]\n"); \ |
---|
| 116 | stk.info.append("*** Additional information:\n"); \ |
---|
| 117 | stk.info.append("[" + this->dumpClassAttributes() + "]\n"); \ |
---|
| 118 | stk.file = FILE_NAME; \ |
---|
| 119 | stk.function = FUNCTION_NAME; \ |
---|
| 120 | stk.line = funcFirstLine; \ |
---|
| 121 | e.stack.push_back(stk); \ |
---|
| 122 | if (CXios::xiosStack) \ |
---|
| 123 | throw; \ |
---|
| 124 | else \ |
---|
| 125 | throw 0; \ |
---|
| 126 | } \ |
---|
| 127 | } |
---|
| 128 | #define CATCH_DUMP_STACK \ |
---|
| 129 | catch(CException& e) \ |
---|
| 130 | { \ |
---|
| 131 | CException::StackInfo stk; \ |
---|
| 132 | int i = 1; \ |
---|
| 133 | stk.file = FILE_NAME; \ |
---|
| 134 | stk.function = FUNCTION_NAME; \ |
---|
| 135 | stk.line = funcFirstLine; \ |
---|
| 136 | e.stack.push_back(stk); \ |
---|
| 137 | for (auto itr = e.stack.crbegin(); itr!=e.stack.crend(); ++itr) \ |
---|
| 138 | { \ |
---|
| 139 | error << "("<< i <<") **************** "; \ |
---|
| 140 | error << itr->function << std::endl; \ |
---|
| 141 | error << itr->info << std::endl; \ |
---|
| 142 | ++i; \ |
---|
| 143 | } \ |
---|
| 144 | error << left << " "; \ |
---|
| 145 | error << left << std::setw(40) << "File " ; \ |
---|
| 146 | error << left << std::setw(106) << "Function " ; \ |
---|
| 147 | error << std::setw(5) << "Line " << std::endl; \ |
---|
| 148 | i = e.stack.size(); \ |
---|
| 149 | for (auto itr = e.stack.begin(); itr!=e.stack.end(); itr++) \ |
---|
| 150 | { \ |
---|
| 151 | StdOStringStream tmp; \ |
---|
| 152 | tmp << "("<< i <<")"; \ |
---|
| 153 | error << left << std::setw(6) << tmp.str(); \ |
---|
| 154 | error << left << std::setw(40) << itr->file; \ |
---|
| 155 | error << left << std::setw(106) << itr->function; \ |
---|
| 156 | error << left << std::setw(5) << itr->line << std::endl; \ |
---|
| 157 | --i; \ |
---|
| 158 | } \ |
---|
| 159 | throw; \ |
---|
| 160 | } \ |
---|
| 161 | } |
---|
| 162 | #else |
---|
| 163 | #define TRY |
---|
| 164 | #define CATCH |
---|
| 165 | #define CATCH_DUMP_ATTR |
---|
| 166 | #define CATCH_DUMP_STACK |
---|
| 167 | #endif |
---|
| 168 | |
---|
[591] | 169 | #endif // __XIOS_CException__ |
---|
[1622] | 170 | |
---|