Packets: Fix VariantParser invalid parser access bug
[senf.git] / Utils / Exception.cc
index ade974a..b2c750f 100644 (file)
@@ -1,9 +1,9 @@
 // $Id$
 //
 // Copyright (C) 2006
-// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
-// Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
-//     Stefan Bund <stefan.bund@fokus.fraunhofer.de>
+// Fraunhofer Institute for Open Communication Systems (FOKUS)
+// Competence Center NETwork research (NET), St. Augustin, GERMANY
+//     Stefan Bund <g0dil@berlios.de>
 //
 // 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
 //#include "Exception.ih"
 
 // Custom includes
-#include <cstring>
+#include <execinfo.h>
 #include <sstream>
+#include "../config.hh"
+#include "Backtrace.hh"
 
 #define prefix_
 ///////////////////////////////cc.p////////////////////////////////////////
 
-prefix_ void senf::SystemException::init()
+///////////////////////////////////////////////////////////////////////////
+// senf::Exception
+
+#ifdef SENF_DEBUG
+prefix_ void senf::ExceptionMixin::addBacktrace()
 {
-    // We normallyl don't want to consume memory in an exception,
-    // however all other solutions to format the message are terribly
-    // ugly (since thay must use a static and shared buffer ...)
-    std::stringstream s;
-    if (where)
-        s << where << ": ";
-    s << "(" << err << ") " << std::strerror(err);
-    buffer_ = s.str();
+    void * entries[SENF_DEBUG_BACKTRACE_NUMCALLERS];
+    unsigned nEntries( ::backtrace(entries, SENF_DEBUG_BACKTRACE_NUMCALLERS) );
+
+    std::stringstream ss;
+    ss << "\nException at\n";
+    formatBacktrace(ss, entries, nEntries);
+    ss << "-- \n" << message_;
+    message_ = ss.str();
 }
+#endif
+
+/////////////////////////////////////////////////////////////////////////// 
+// senf::Exception
+
+prefix_ senf::Exception::~Exception()
+    throw()
+{}
 
-prefix_ char const * senf::SystemException::what()
+prefix_ char const * senf::Exception::what()
     const throw()
 {
-    return buffer_.c_str();
+    return message().c_str();
+}
+
+///////////////////////////////////////////////////////////////////////////
+// senf::SystemException
+
+prefix_ void senf::SystemException::init(std::string const & descr, int code
+                                         _SENF_EXC_DEBUG_ARGS_ND)
+{
+    code_ = code;
+#   ifdef SENF_DEBUG
+    if (file && line)
+        (*this) << "Exception at " << file << ":" << line << "\n";
+#   endif
+    (*this) << "[" << errorString() << "]";
+    if (! descr.empty()) (*this) << " " << descr;
 }
 
 ///////////////////////////////cc.e////////////////////////////////////////
@@ -61,4 +90,6 @@ prefix_ char const * senf::SystemException::what()
 // c-file-style: "senf"
 // indent-tabs-mode: nil
 // ispell-local-dictionary: "american"
+// compile-command: "scons -u test"
+// comment-column: 40
 // End: