X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=Utils%2FException.cc;h=b2c750f383c55a27d449c0853fdec59a085b4fa2;hb=532240d72e09e19e57fac9bb55c2560b9c9e5b97;hp=de36e8620af07d6054d0eae2bf68124151fda45d;hpb=96d3a2a43b4e6d7693a136a3adeb099440f19068;p=senf.git diff --git a/Utils/Exception.cc b/Utils/Exception.cc index de36e86..b2c750f 100644 --- a/Utils/Exception.cc +++ b/Utils/Exception.cc @@ -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 @@ -27,52 +27,57 @@ //#include "Exception.ih" // Custom includes -#include +#include #include +#include "../config.hh" +#include "Backtrace.hh" #define prefix_ ///////////////////////////////cc.p//////////////////////////////////////// -prefix_ void senf::SystemException::init() -{ - // 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 << "(" << code_ << ") " << description(); - buffer_ = s.str(); -} +/////////////////////////////////////////////////////////////////////////// +// senf::Exception -prefix_ void senf::throwErrno(char const * where, int code) +#ifdef SENF_DEBUG +prefix_ void senf::ExceptionMixin::addBacktrace() { - switch (code) { + void * entries[SENF_DEBUG_BACKTRACE_NUMCALLERS]; + unsigned nEntries( ::backtrace(entries, SENF_DEBUG_BACKTRACE_NUMCALLERS) ); - // 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) + std::stringstream ss; + ss << "\nException at\n"; + formatBacktrace(ss, entries, nEntries); + ss << "-- \n" << message_; + message_ = ss.str(); +} +#endif -# define ExceptionCase(z, n, data) case n: throw ErrnoException(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); - } +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////////////////////////////////////////