source: XIOS/dev/dev_olga/src/exception.hpp @ 1612

Last change on this file since 1612 was 1612, checked in by oabramkina, 5 years ago

Dev: adding exception handling.

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

  • Property copyright set to
    Software name : XIOS (Xml I/O Server)
    http://forge.ipsl.jussieu.fr/ioserver
    Creation date : January 2009
    Licence : CeCCIL version2
    see license file in root directory : Licence_CeCILL_V2-en.txt
    or http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
    Holder : CEA/LSCE (Laboratoire des Sciences du CLimat et de l'Environnement)
    CNRS/IPSL (Institut Pierre Simon Laplace)
    Project Manager : Yann Meurdesoif
    yann.meurdesoif@cea.fr
File size: 6.3 KB
RevLine 
[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"
[1612]7#include <iomanip>
8#include <stdexcept>
[219]9
[335]10namespace xios
[219]11{
12   /// ////////////////////// Déclarations ////////////////////// ///
[1612]13
[219]14   class CException
15      : private CObject, public StdOStringStream
16   {
17         typedef CObject SuperClass;
18
19      public :
20
21         /// Constructeurs ///
22         CException(void);
23         explicit CException(const StdString & id);
24         CException(const CException & exception);
25         CException(const CException * const exception); // Not implemented.
26
27         /// Accesseurs ///
28         StdString getMessage(void) const;
29         StdOStringStream & getStream(void);
30
31         /// Destructeur ///
32         virtual ~CException(void);
33
34         /// Autre ///
35         virtual StdString toString(void) const;
36         virtual void fromString(const StdString & str);
37
[1612]38         struct StackInfo
39         {
40           StdString file;
41           StdString function;
42           int line;
43           StdString info;
44         };
45
46         std::list<StackInfo> stack;
47
[219]48      private :
49
50         /// Propriétés ///
51         bool desc_rethrow; // throw destructor
[1612]52         StdOStringStream stream;
[219]53
54   }; // CException
[335]55} // namespace xios
[219]56
57/// //////////////////////////// Macros //////////////////////////// ///
58
[1612]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
[219]64#define INFO(x) \
[1612]65   "In file \""<< FILE_NAME <<"\", function \"" << BOOST_CURRENT_FUNCTION <<"\",  line " << __LINE__ << " -> " x << std::endl;
[219]66
[268]67#ifdef __XIOS_DEBUG
[219]68#  define DEBUG(x) std::clog << "> Debug " << INFO(x)
69#else
70#  define DEBUG(x)
71#endif
72
[1612]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}
[219]80
[1612]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
159
[591]160#endif // __XIOS_CException__
[1612]161
Note: See TracBrowser for help on using the repository browser.