source: XIOS3/trunk/src/exception.hpp

Last change on this file was 2629, checked in by jderouillat, 7 weeks ago

Delete boost dependencies, the few features used are replaced by functions stored in extern/boost_extraction

  • 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.6 KB
Line 
1#ifndef __XIOS_CException__
2#define __XIOS_CException__
3
4/// XIOS headers ///
5#include "xios_spl.hpp"
6#include "object.hpp"
7#include <boost_extract.hpp>
8#include <iomanip>
9#include <stdexcept>
10
11namespace xios
12{
13   /// ////////////////////// Déclarations ////////////////////// ///
14
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
39         struct StackInfo
40         {
41           StdString file;
42           StdString function;
43           int line;
44           StdString info;
45         };
46
47         std::list<StackInfo> stack;
48
49      private :
50
51         /// Propriétés ///
52         bool desc_rethrow; // throw destructor
53         StdOStringStream stream;
54
55   }; // CException
56} // namespace xios
57
58/// //////////////////////////// Macros //////////////////////////// ///
59
60#define FILE_NAME (std::strrchr("/" __FILE__, '/') + 1)
61
62#define FUNCTION_NAME (StdString(XIOS_CURRENT_FUNCTION).length() > 100 ? \
63                       StdString(XIOS_CURRENT_FUNCTION).substr(0,100).append("...)") : XIOS_CURRENT_FUNCTION)
64
65#define INFO(x) \
66   "In file \""<< FILE_NAME <<"\", function \"" << XIOS_CURRENT_FUNCTION <<"\",  line " << __LINE__ << " -> " x << std::endl;
67
68#ifdef __XIOS_DEBUG
69#  define DEBUG(x) std::clog << "> Debug " << INFO(x)
70#else
71#  define DEBUG(x)
72#endif
73
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}
81
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
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
169#endif // __XIOS_CException__
170
Note: See TracBrowser for help on using the repository browser.