// $Id$
//
// Copyright (C) 2006
-// Fraunhofer Institute for Open Communication Systems (FOKUS)
-// Competence Center NETwork research (NET), St. Augustin, GERMANY
+// 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
//#include "Exception.ih"
// Custom includes
-#include <cstring>
+#include <execinfo.h>
#include <sstream>
+#include "../config.hh"
+#include "impl/demangle.h"
#define prefix_
///////////////////////////////cc.p////////////////////////////////////////
-prefix_ void senf::throwErrno(std::string const & where, int code)
-{
-#ifndef SENF_NO_ERRNOEXC
- switch (code) {
+///////////////////////////////////////////////////////////////////////////
+// senf::Exception
- // BOOST_PP_REPEAT is limited to 256 repetitions. The max errno value I found in any header file
- // was somewhere around 530 or so. I assume going to 1024 will be good enough. This explicit
- // code will be optimized into a jump table by g++ (which is more efficient than trying to do
- // the table oneself)
+#ifdef SENF_DEBUG
+prefix_ void senf::ExceptionMixin::addBacktrace()
+{
+ void * entries[SENF_DEBUG_BACKTRACE_NUMCALLERS];
+ unsigned nEntries( ::backtrace(entries, SENF_DEBUG_BACKTRACE_NUMCALLERS) );
+ char ** symbols = ::backtrace_symbols(entries, nEntries);
+
+ std::stringstream ss;
+ ss << "\nException at\n";
+ for (unsigned i=0; i<nEntries; ++i) {
+ std::string sym (symbols[i]);
+ std::string::size_type fnStart (sym.find("("));
+ if (fnStart != std::string::npos) {
+ std::string::size_type fnEnd (sym.find(")",fnStart+1));
+ if (fnEnd != std::string::npos) {
+ std::string fn (sym,fnStart+1, fnEnd-fnStart-1);
+ char * demangled ( ::cplus_demangle(fn.c_str(),
+ DMGL_TYPES|DMGL_AUTO) );
+ if (demangled) {
+ ss << " " << demangled << "( ... )" << std::string(sym,fnEnd+1) << "\n";
+ continue;
+ }
+ }
+ }
+ ss << " " << sym << "\n";
+ }
+ ss << "-- \n" << message_;
+ message_ = ss.str();
+ free(symbols);
+}
+#endif
-# define ExceptionCase(z, n, data) case n: throw ErrnoException<n>(where);
- BOOST_PP_REPEAT(256, ExceptionCase, _) ;
-# undef ExceptionCase
+///////////////////////////////////////////////////////////////////////////
+// senf::Exception
-# define ExceptionCase(z, n, data) case 256+n: throw ErrnoException<256+n>(where);
- BOOST_PP_REPEAT(256, ExceptionCase, _) ;
-# undef ExceptionCase
+prefix_ senf::Exception::~Exception()
+ throw()
+{}
-# define ExceptionCase(z, n, data) case 512+n: throw ErrnoException<512+n>(where);
- BOOST_PP_REPEAT(256, ExceptionCase, _) ;
-# undef ExceptionCase
+prefix_ char const * senf::Exception::what()
+ const throw()
+{
+ return message().c_str();
+}
-# define ExceptionCase(z, n, data) case 768+n: throw ErrnoException<768+n>(where);
- BOOST_PP_REPEAT(256, ExceptionCase, _) ;
-# undef ExceptionCase
+///////////////////////////////////////////////////////////////////////////
+// senf::SystemException
- default:
- throw SystemException(where, code);
- }
-#else
- throw SystemException(where, code);
-#endif
+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////////////////////////////////////////