Console: Add log support to network client (every client is a log target)
[senf.git] / Utils / Exception.hh
index 53d962f..5f00926 100644 (file)
@@ -34,6 +34,7 @@
 #include <boost/preprocessor/repeat.hpp>
 #include <boost/preprocessor/cat.hpp>
 #include <boost/utility.hpp>
+#include <boost/type_traits/is_convertible.hpp>
 
 //#include "Exception.mpp"
 ///////////////////////////////hh.p////////////////////////////////////////
@@ -103,15 +104,10 @@ namespace senf {
         ///////////////////////////////////////////////////////////////////////////
 
         virtual char const * what() const throw();
+        std::string const & message() const;
 
-        template <class Arg>
-        Exception & operator<<(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 <code>boost::lexical_cast</code> and is
-                                             therefor identical to a streaming operation. 
-                                             \see \ref exception */
+        void append(std::string text); ///< Extend exception description
+                                        /**< Adds \a text to the description text. */
 
     protected:
         Exception(std::string const & description = ""); ///< Initialize exception with string
@@ -124,6 +120,26 @@ namespace senf {
         std::string message_;
     };
 
+    template <class Exc, class Arg>
+    typename boost::enable_if< boost::is_convertible<Exc*,Exception*>, 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 <code>boost::lexical_cast</code> 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 Exception handling standard UNIX errors (errno)
 
@@ -143,6 +159,18 @@ namespace senf {
         throw senf::SystemException();
         \endcode
 
+        From within SENF (<em>and only there because it depends on the \c SENF_DEBUG symbol</em>),
+        SystemException should be thrown using wrapper macros which add additional information to
+        the exception description:
+        \code
+        // 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
      */
     class SystemException : public Exception
@@ -152,9 +180,9 @@ namespace senf {
         ///\name Structors and default members
         ///@{
 
-        explicit SystemException(std::string const & descr = ""); 
-        explicit SystemException(int code);
-        SystemException(std::string const & descr, int code);
+        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);
 
         virtual ~SystemException() throw();
 
@@ -171,12 +199,20 @@ namespace senf {
 
 
     private:
-        void init(std::string const & descr, int code);
+        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
+
+#   define SENF_THROW_SYSTEM_EXCEPTION(desc) throw SystemException(desc SENF_EXC_DEBUGINFO)
+
 }
 
 ///////////////////////////////hh.e////////////////////////////////////////