error_handling.h

Go to the documentation of this file.
00001 /*
00002  *  This file is part of libcxxsupport.
00003  *
00004  *  libcxxsupport is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  libcxxsupport is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with libcxxsupport; if not, write to the Free Software
00016  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00017  */
00018 
00019 /*
00020  *  libcxxsupport is being developed at the Max-Planck-Institut fuer Astrophysik
00021  *  and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
00022  *  (DLR).
00023  */
00024 
00025 /*! \file error_handling.h
00026  *  Utilities for error reporting
00027  *
00028  *  Copyright (C) 2003-2011 Max-Planck-Society
00029  *  Authors: Reinhard Hell, Martin Reinecke
00030  */
00031 
00032 #ifndef PLANCK_ERROR_HANDLING_H
00033 #define PLANCK_ERROR_HANDLING_H
00034 
00035 #include <string>
00036 #include <iostream>
00037 
00038 #if defined (__GNUC__)
00039 #define PLANCK_FUNC_NAME__ __PRETTY_FUNCTION__
00040 #else
00041 #define PLANCK_FUNC_NAME__ 0
00042 #endif
00043 
00044 void planck_failure__(const char *file, int line, const char *func,
00045   const std::string &msg);
00046 void planck_failure__(const char *file, int line, const char *func,
00047   const char *msg);
00048 void killjob__();
00049 
00050 class PlanckError
00051   {
00052   private:
00053     std::string msg;
00054 
00055   public:
00056     explicit PlanckError(const std::string &message);
00057     explicit PlanckError(const char *message);
00058 
00059     virtual const char* what() const
00060       { return msg.c_str(); }
00061 
00062     virtual ~PlanckError();
00063   };
00064 
00065 /*! \defgroup errorgroup Error handling */
00066 /*! \{ */
00067 
00068 /*! Writes diagnostic output and exits with an error status. */
00069 #define planck_fail(msg) \
00070 do { planck_failure__(__FILE__,__LINE__,PLANCK_FUNC_NAME__,msg); \
00071 throw PlanckError(msg); } while(0)
00072 
00073 /*! Throws a PlanckError without diagnostic message. */
00074 #define planck_fail_quietly(msg) \
00075 do { throw PlanckError(msg); } while(0)
00076 
00077 /*! Writes diagnostic output and exits with an error status if \a testval
00078     is \a false. */
00079 #define planck_assert(testval,msg) \
00080 do { if (testval); else planck_fail(msg); } while(0)
00081 
00082 /*! Macro for improving error diagnostics. Should be placed immediately
00083     after the opening brace of \c main(). Must be used in conjunction with
00084     \c PLANCK_DIAGNOSIS_END. */
00085 #define PLANCK_DIAGNOSIS_BEGIN try {
00086 /*! Macro for improving error diagnostics. Should be placed immediately
00087     before the closing brace of \c main(). Must be used in conjunction with
00088     \c PLANCK_DIAGNOSIS_BEGIN. */
00089 #define PLANCK_DIAGNOSIS_END \
00090 } \
00091 catch (PlanckError &) \
00092   { killjob__(); /* no need for further diagnostics; they were shown already */ } \
00093 catch (std::exception &e) \
00094   { std::cerr << "std::exception: " << e.what() << std::endl; killjob__(); } \
00095 catch (...) \
00096   { std::cerr << "Unknown exception" << std::endl; killjob__(); }
00097 
00098 /*! \} */
00099 
00100 #endif

Generated on Thu Oct 8 14:48:51 2015 for LevelS C++ support library