X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=Utils%2FException.hh;h=840379e7bdd6fdbceef80a6aa96c05c594ac8742;hb=d2459b6c8249291588fd3d0d125ed3d38e003b55;hp=a99171002e92780f2da8ba37682174da9ad6f517;hpb=f00a102138bcbabdaab1caab1db6a8876463dedc;p=senf.git diff --git a/Utils/Exception.hh b/Utils/Exception.hh index a991710..840379e 100644 --- a/Utils/Exception.hh +++ b/Utils/Exception.hh @@ -1,9 +1,9 @@ // $Id$ // // Copyright (C) 2006 -// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) -// Kompetenzzentrum fuer Satelitenkommunikation (SatCom) -// Stefan Bund +// Fraunhofer Institute for Open Communication Systems (FOKUS) +// Competence Center NETwork research (NET), St. Augustin, GERMANY +// Stefan Bund // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by @@ -29,111 +29,280 @@ // Custom includes #include #include +#include +#include #include #include +#include +#include //#include "Exception.mpp" ///////////////////////////////hh.p//////////////////////////////////////// +/** \defgroup exception Exception classes + + All exceptions in %senf are derived from senf::Exception. This class adds the possibility to + extend the exception description while it is processed. The functionality is provided by a mixin + class senf::ExceptionMixin: + + \code + try { + + // Some code which might raise an arbitrary senf exception + + } + catch (senf::ExceptionMixin & e) { + e << "\handling user " << user; + throw; + } + \endcode + + This will add the user information to any %senf exception thrown. The Exception is however not a + stream. If you need to do more extensive formating, either use an intermediate string-stream or + use Boost.Format: + + \code + try { + // ... + } + catch (senf::ExceptionMixin & e) { + e << boost::format("\ncall id 0x%04x@%s") % id % address; + } + \endcode + + senf::SystemException is thrown for all operating system errors (failures which result in the + operating system setting the errno value). It is also derived from senf::Exception and can + therefore be extended as well. + + Defining your own exception classes derived from senf::Exception is very simple: + + \code + struct FooException : public senf::Exception + { FooException() : senf::Exception("Foo hit the fan") {} }; + \endcode + + If SENF is compiled in debug mode (SENF_DEBUG is defined), the exception message will + automatically include a stack backtrace. For this to work, you need to add the + -rdynamic option to all link commands. This feature depends on gcc and + the GNU-libc. + + To apply these features (extensibility, backtrace) to a non-senf exception, the non-senf + exception can be wrapped and rethrown. + \code + void foo() { + try { + // ... code that might throw std::bad_cast or somelib::FooException + } + SENF_WRAP_EXC(std::bad_cast) + SENF_WRAP_EXC(somelib::FooException) + } + \endcode The re-thrown exception can then be caught as std::bad_cast or as + senf::ExceptionMixin as needed. It is safe, to wrap an exception twice (the macro will detect + this case). + \code + bar() { + try { + try { + foo(); + } + catch (senf::ExceptionMixin & ex) { + ex << "\nadd this info"; + } + } + catch (std::bad_cast const & ex) { + std::cerr << ex.what() << std::endl; + } + \endcode + The final error output will include + \li a backtrace if compiled in debug mode + \li the original error message from the std::bad_cast exception + \li the additional error message "add this info" + + \todo Link against libcwd to add file-name/line-number information to the backtrace and remove + the dependency on -rdynamic + */ + namespace senf { - /** \brief Exception handling standard UNIX errors (errno) + /** \brief Generic extensible exception mixin + + ExceptionMixin is a generic exception mixin which allows the exception to be later extended + by catching and re-throwing it (See example in \ref exception). + + \ingroup exception + */ + class ExceptionMixin + { + public: + std::string const & message() const; + + void append(std::string text); ///< Extend exception description + /**< Adds \a text to the description text. */ + + protected: + explicit ExceptionMixin(std::string const & description = ""); + ///< Initialize exception with string + /**< \a description is the initial error description + string. This should probably be a string constant + describing the exception for most derived + exceptions. */ + + private: +#ifdef SENF_DEBUG + void addBacktrace(); +#endif + std::string message_; + }; - This exception is thrown to signal generic \c errno failures. In addition to the \c errno - number (the code()), this class manages optional origin information. This parameter should - be provided to further describe, in what context the exception was created. + /** \brief Extensible exception base-class - This exception should not be used directly. Instead the derived class ErrnoException should - be thrown via one of the senf::throwErrno() helpers. + This base-class is an exception which already includes the ExceptionMixin. All SENF + exceptions are derived from this class. Other user-exception may be defined by deriving from + this class too. - \see ErrnoException + \see \ref exception + + \ingroup exception */ - class SystemException : public std::exception + class Exception + : public ExceptionMixin, public std::exception { public: - SystemException(); ///< SystemException without error location infor - /**< The error code is taken from the current value of the - global \c errno variable */ + virtual ~Exception() throw(); - explicit SystemException(int code); ///< SystemException without error location info - /**< \param[in] code error number (the \c errno value) */ + virtual char const * what() const throw(); - explicit SystemException(char const * where); ///< SystemException with error location info - /**< The error code is taken from the current value of the - global \c errno variable - \param[in] where description of error origin */ + protected: + explicit Exception(std::string const & description = ""); + }; + + /** \brief Wrapper for standard non-senf exceptions - SystemException(char const * where, int code); ///< SystemException with error location info - /**< \param[in] where description of error origin - \param[in] code error number (the \c errno value) */ + This class wraps an exception of type \a BaseException and adds functionality from + senf::ExceptionMixin. - virtual char const * what() const throw(); ///< Return verbose error description + \ingroup exception + */ + template + class WrapException + : public ExceptionMixin, public BaseException + { + public: + typedef BaseException Base; - char const * where() const; ///< Error origin - int code() const; ///< Error code (\c errno number) - char const * description() const; ///< Error description (strerror() value) + WrapException(BaseException const & base); + virtual ~WrapException() throw(); - bool anyOf(int c0, int c1=0, int c2=0, int c3=0, int c4=0, int c5=0, - int c6=0, int c7=0, int c8=0, int c9=0); - ///< \c true, if code() is one of \a c0 ... \a c9 + virtual char const * what() const throw(); + }; - virtual ~SystemException() throw(); + /** \brief Wrap a non-senf exception - private: - void init(); + This macro allows to wrap a non-senf exception adding functionality from ExceptionMixin + using the WrapException template. For an example, see \ref exception. - char const * const where_; - int const code_; // This must be const to make the derived ErrnoException - // class a valid derived class. - std::string buffer_; - }; + \ingroup exception + */ +# define SENF_WRAP_EXC(Ex) \ + catch (Ex const & base) { \ + if (dynamic_cast(&base)) \ + throw; \ + else \ + throw senf::WrapException(base); \ + } + + template + typename boost::enable_if< boost::is_convertible, Exc & >::type + operator<<(Exc const & exc, Arg const & arg); ///< Extend exception description + /**< Adds \a arg converted to string to the end of the + exception description string. This operator allows to + use Exception instances like streams. The conversion is + performed using boost::lexical_cast and is + therefor identical to a streaming operation. + \see \ref exception */ + + +# ifdef SENF_DEBUG +# define _SENF_EXC_DEBUG_ARGS ,char const * file=0,int line=0 +# define _SENF_EXC_DEBUG_ARGS_ND ,char const *file,int line +# define _SENF_EXC_DEBUG_ARGS_P ,file,line +# else +# define _SENF_EXC_DEBUG_ARGS +# define _SENF_EXC_DEBUG_ARGS_ND +# define _SENF_EXC_DEBUG_ARGS_P +# endif - /** \brief Error specific system exception + /** \brief Exception handling standard UNIX errors (errno) + + This exception is thrown to signal generic \c errno failures. Normally the \c errno value is + automatically taken from the \c errno variable but it may also be specified explicitly: + + \code + // Standard usage: Take \c errno from environment + throw senf::SystemException("::open()") + << " while opening configuration file: " << filename; - This template restricts the generic SystemException to a specific, compile-time constant - error number \p Code. This allows a specific \c errno number to be cached explicitly. + // You may however explicitly specify the errno value + throw senf::SystemException("::open()", ENOFILE) - This exception is normally thrown via one of the senf::throwErrno() helpers. These helpers - take the numeric \c errno value (either from the \c errno variable or from their - argument) and will throw the corresponding ErrnoException: + // Or leave the location information empty + throw senf::SystemException(ENOFILE); + throw senf::SystemException(); + \endcode + + From within SENF (and only there because it depends on the \c SENF_DEBUG symbol), + SystemException should be thrown using wrapper macros which add additional information to + the exception description: \code - if ((fd = ::open(filename, O_RDWR)) < 0) - senf::throwErrno("open()"); + // Standard usage: Take \c errno from environment + SENF_THROW_SYSTEM_EXCEPTION() + << " while opening configuration file: " << filename; + + // You may however explicitly specify the errno value + throw senf::SystemException("::open()", ENOFILE SENF_EXC_DEBUGINFO) \endcode + + \ingroup exception */ - template - class ErrnoException : public SystemException + class SystemException : public Exception { public: - static int const fixed_code = Code; + /////////////////////////////////////////////////////////////////////////// + ///\name Structors and default members + ///@{ - ErrnoException(); ///< ErrnoException without error location information - explicit ErrnoException(char const * where); - ///< ErrnoException with error location information - }; + explicit SystemException(std::string const & descr = "" _SENF_EXC_DEBUG_ARGS); + explicit SystemException(int code _SENF_EXC_DEBUG_ARGS); + SystemException(std::string const & descr, int code _SENF_EXC_DEBUG_ARGS); - - /** \brief Throw ErrnoException based on current \c errno value - \related ErrnoException - */ - void throwErrno(); + virtual ~SystemException() throw(); - /** \brief Throw ErrnoException based on current \c errno value (with location info) - \related ErrnoException - */ - void throwErrno(char const * where); + ///@} + /////////////////////////////////////////////////////////////////////////// - /** \brief Throw ErrnoException based on given \c errno value - \related ErrnoException - */ - void throwErrno(int code); + int errorNumber() const; ///< Error code (\c errno number) + char const * errorString() const; ///< Error string (\c strerror() value) - /** \brief Throw ErrnoException based on given \c errno value (with location info) - \related ErrnoException - */ - void throwErrno(char const * where, int code); + bool anyOf(int c0, int c1=0, int c2=0, int c3=0, int c4=0, int c5=0, + int c6=0, int c7=0, int c8=0, int c9=0); + ///< \c true, if errorNumber() is one of \a c0 ... \a c9 + + + + private: + void init(std::string const & descr, int code _SENF_EXC_DEBUG_ARGS_ND); + + int code_; + std::string what_; + }; + +# ifdef SENF_DEBUG +# define SENF_EXC_DEBUGINFO ,__FILE__,__LINE__ +# else +# define SENF_EXC_DEBUGINFO +# endif - enum NoThrow_t { nothrow }; +# define SENF_THROW_SYSTEM_EXCEPTION(desc) throw SystemException(desc SENF_EXC_DEBUGINFO) }