source: XIOS3/trunk/src/exception.hpp @ 2426

Last change on this file since 2426 was 2282, checked in by jderouillat, 2 years ago

Add missing return statements detected using -fsanitize=return. Return errors at runtime if reached.

  • 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 <iomanip>
8#include <stdexcept>
9
10namespace xios
11{
12   /// ////////////////////// Déclarations ////////////////////// ///
13
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
38         struct StackInfo
39         {
40           StdString file;
41           StdString function;
42           int line;
43           StdString info;
44         };
45
46         std::list<StackInfo> stack;
47
48      private :
49
50         /// Propriétés ///
51         bool desc_rethrow; // throw destructor
52         StdOStringStream stream;
53
54   }; // CException
55} // namespace xios
56
57/// //////////////////////////// Macros //////////////////////////// ///
58
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
64#define INFO(x) \
65   "In file \""<< FILE_NAME <<"\", function \"" << BOOST_CURRENT_FUNCTION <<"\",  line " << __LINE__ << " -> " x << std::endl;
66
67#ifdef __XIOS_DEBUG
68#  define DEBUG(x) std::clog << "> Debug " << INFO(x)
69#else
70#  define DEBUG(x)
71#endif
72
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#define MISSING_RETURN(id)  \
82{                     \
83       xios::CException exc(id);                \
84       exc.getStream() << INFO("Missing return");              \
85       error << exc.getMessage() << std::endl;  \
86       throw exc;                               \
87}
88
89#ifdef __XIOS_EXCEPTION
90  #define TRY                          \
91    {                                  \
92      int funcFirstLine = __LINE__;    \
93      try
94  #define CATCH                              \
95      catch(CException& e)                   \
96      {                                      \
97        CException::StackInfo stk;           \
98        stk.file = FILE_NAME;                \
99        stk.function = FUNCTION_NAME;        \
100        stk.line = funcFirstLine;            \
101        e.stack.push_back(stk);              \
102        if (CXios::xiosStack)                \
103          throw;                             \
104       else                                  \
105         throw 0;                            \
106      }                                      \
107    }
108  #define CATCH_DUMP_ATTR                    \
109      catch(CException& e)                   \
110      {                                      \
111        CException::StackInfo stk;           \
112        stk.info.append(StdString("Object id=\"" + this->getId()) + "\" object type=\"" + this->getName() + "\"\n"); \
113        stk.info.append("*** XIOS attributes as defined in XML file(s) or via Fortran interface:\n");                \
114        stk.info.append("[" + this->dumpXiosAttributes() + "]\n");     \
115        stk.info.append("*** Additional information:\n");              \
116        stk.info.append("[" + this->dumpClassAttributes() + "]\n");    \
117        stk.file = FILE_NAME;                \
118        stk.function = FUNCTION_NAME;        \
119        stk.line = funcFirstLine;            \
120        e.stack.push_back(stk);              \
121        if (CXios::xiosStack)                \
122          throw;                             \
123       else                                  \
124         throw 0;                            \
125      }                                      \
126    }
127  #define CATCH_DUMP_STACK                                           \
128      catch(CException& e)                                           \
129      {                                                              \
130        CException::StackInfo stk;                                   \
131        int i = 1;                                                   \
132        stk.file = FILE_NAME;                                        \
133        stk.function = FUNCTION_NAME;                                \
134        stk.line = funcFirstLine;                                    \
135        e.stack.push_back(stk);                                      \
136        for (auto itr = e.stack.crbegin(); itr!=e.stack.crend(); ++itr) \
137        {                                                            \
138          error << "("<< i <<") **************** ";                  \
139          error << itr->function << std::endl;                       \
140          error << itr->info << std::endl;                           \
141          ++i;                                                       \
142        }                                                            \
143        error << left << "      ";                                   \
144        error << left << std::setw(40) << "File " ;                  \
145        error << left << std::setw(106) << "Function " ;             \
146        error << std::setw(5)   << "Line " << std::endl;             \
147        i = e.stack.size();                                          \
148        for (auto itr = e.stack.begin(); itr!=e.stack.end(); itr++)  \
149        {                                                            \
150          StdOStringStream tmp;                                      \
151          tmp <<  "("<< i <<")";                                     \
152          error << left << std::setw(6) << tmp.str();                \
153          error << left << std::setw(40)  << itr->file;              \
154          error << left << std::setw(106) << itr->function;          \
155          error << left << std::setw(5)   << itr->line << std::endl; \
156          --i;                                                       \
157        }                                                            \
158        throw;                                                       \
159      }                                                              \
160    }
161#else
162  #define TRY
163  #define CATCH
164  #define CATCH_DUMP_ATTR
165  #define CATCH_DUMP_STACK
166#endif
167
168#endif // __XIOS_CException__
169
Note: See TracBrowser for help on using the repository browser.