Ignore:
Timestamp:
12/11/18 13:22:07 (5 years ago)
Author:
oabramkina
Message:

Exception handling on trunk.

To activate it, compilation flag -DXIOS_EXCEPTION should be added.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • XIOS/trunk/src/exception.hpp

    r792 r1622  
    55#include "xios_spl.hpp" 
    66#include "object.hpp" 
     7#include <iomanip> 
     8#include <stdexcept> 
    79 
    810namespace xios 
    911{ 
    1012   /// ////////////////////// Déclarations ////////////////////// /// 
     13 
    1114   class CException 
    1215      : private CObject, public StdOStringStream 
     
    3336         virtual void fromString(const StdString & str); 
    3437 
     38         struct StackInfo 
     39         { 
     40           StdString file; 
     41           StdString function; 
     42           int line; 
     43           StdString info; 
     44         }; 
     45 
     46         std::list<StackInfo> stack; 
     47 
    3548      private : 
    3649 
    3750         /// Propriétés /// 
    3851         bool desc_rethrow; // throw destructor 
     52         StdOStringStream stream; 
    3953 
    4054   }; // CException 
     
    4357/// //////////////////////////// Macros //////////////////////////// /// 
    4458 
     59#define FILE_NAME (std::strrchr("/" __FILE__, '/') + 1) 
     60 
     61#define FUNCTION_NAME (StdString(BOOST_CURRENT_FUNCTION).length() > 100 ? \ 
     62                       StdString(BOOST_CURRENT_FUNCTION).substr(0,100).append("...)") : BOOST_CURRENT_FUNCTION) 
     63 
    4564#define INFO(x) \ 
    46    "In file \'" __FILE__ "\', line " << __LINE__ << " -> " x << std::endl; 
     65   "In file \""<< FILE_NAME <<"\", function \"" << BOOST_CURRENT_FUNCTION <<"\", line " << __LINE__ << " -> " x << std::endl; 
    4766 
    4867#ifdef __XIOS_DEBUG 
     
    5271#endif 
    5372 
    54 #define ERROR(id, x) xios::CException(id).getStream() << INFO(x) 
     73#define ERROR(id, x)  \ 
     74{                     \ 
     75       xios::CException exc(id);                \ 
     76       exc.getStream() << INFO(x);              \ 
     77       error << exc.getMessage() << std::endl;  \ 
     78       throw exc;                               \ 
     79} 
     80 
     81#ifdef __XIOS_EXCEPTION 
     82  #define TRY                          \ 
     83    {                                  \ 
     84      int funcFirstLine = __LINE__;    \ 
     85      try 
     86  #define CATCH                              \ 
     87      catch(CException& e)                   \ 
     88      {                                      \ 
     89        CException::StackInfo stk;           \ 
     90        stk.file = FILE_NAME;                \ 
     91        stk.function = FUNCTION_NAME;        \ 
     92        stk.line = funcFirstLine;            \ 
     93        e.stack.push_back(stk);              \ 
     94        if (CXios::xiosStack)                \ 
     95          throw;                             \ 
     96       else                                  \ 
     97         throw 0;                            \ 
     98      }                                      \ 
     99    } 
     100  #define CATCH_DUMP_ATTR                    \ 
     101      catch(CException& e)                   \ 
     102      {                                      \ 
     103        CException::StackInfo stk;           \ 
     104        stk.info.append(StdString("Object id=\"" + this->getId()) + "\" object type=\"" + this->getName() + "\"\n"); \ 
     105        stk.info.append("*** XIOS attributes as defined in XML file(s) or via Fortran interface:\n");                \ 
     106        stk.info.append("[" + this->dumpXiosAttributes() + "]\n");     \ 
     107        stk.info.append("*** Additional information:\n");              \ 
     108        stk.info.append("[" + this->dumpClassAttributes() + "]\n");    \ 
     109        stk.file = FILE_NAME;                \ 
     110        stk.function = FUNCTION_NAME;        \ 
     111        stk.line = funcFirstLine;            \ 
     112        e.stack.push_back(stk);              \ 
     113        if (CXios::xiosStack)                \ 
     114          throw;                             \ 
     115       else                                  \ 
     116         throw 0;                            \ 
     117      }                                      \ 
     118    } 
     119  #define CATCH_DUMP_STACK                                           \ 
     120      catch(CException& e)                                           \ 
     121      {                                                              \ 
     122        CException::StackInfo stk;                                   \ 
     123        int i = 1;                                                   \ 
     124        stk.file = FILE_NAME;                                        \ 
     125        stk.function = FUNCTION_NAME;                                \ 
     126        stk.line = funcFirstLine;                                    \ 
     127        e.stack.push_back(stk);                                      \ 
     128        for (auto itr = e.stack.crbegin(); itr!=e.stack.crend(); ++itr) \ 
     129        {                                                            \ 
     130          error << "("<< i <<") **************** ";                  \ 
     131          error << itr->function << std::endl;                       \ 
     132          error << itr->info << std::endl;                           \ 
     133          ++i;                                                       \ 
     134        }                                                            \ 
     135        error << left << "      ";                                   \ 
     136        error << left << std::setw(40) << "File " ;                  \ 
     137        error << left << std::setw(106) << "Function " ;             \ 
     138        error << std::setw(5)   << "Line " << std::endl;             \ 
     139        i = e.stack.size();                                          \ 
     140        for (auto itr = e.stack.begin(); itr!=e.stack.end(); itr++)  \ 
     141        {                                                            \ 
     142          StdOStringStream tmp;                                      \ 
     143          tmp <<  "("<< i <<")";                                     \ 
     144          error << left << std::setw(6) << tmp.str();                \ 
     145          error << left << std::setw(40)  << itr->file;              \ 
     146          error << left << std::setw(106) << itr->function;          \ 
     147          error << left << std::setw(5)   << itr->line << std::endl; \ 
     148          --i;                                                       \ 
     149        }                                                            \ 
     150        throw;                                                       \ 
     151      }                                                              \ 
     152    } 
     153#else 
     154  #define TRY 
     155  #define CATCH 
     156  #define CATCH_DUMP_ATTR 
     157  #define CATCH_DUMP_STACK 
     158#endif 
    55159 
    56160#endif // __XIOS_CException__ 
     161 
Note: See TracChangeset for help on using the changeset viewer.