librostlab
1.0.20
|
00001 /* 00002 Copyright (C) 2011 Laszlo Kajan, Technical University of Munich, Germany 00003 00004 This file is part of librostlab. 00005 00006 librostlab is free software: you can redistribute it and/or modify 00007 it under the terms of the GNU Lesser General Public License as published by 00008 the Free Software Foundation, either version 3 of the License, or 00009 (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU Lesser General Public License for more details. 00015 00016 You should have received a copy of the GNU Lesser General Public License 00017 along with this program. If not, see <http://www.gnu.org/licenses/>. 00018 */ 00019 #ifndef ROSTLAB_STDEXCEPT 00020 #define ROSTLAB_STDEXCEPT 00021 00022 #include <execinfo.h> 00023 #include <stdexcept> 00024 #include <stdio.h> 00025 #include <stdlib.h> 00026 #include <string> 00027 00028 namespace rostlab { 00029 00030 class error_backtracer 00031 { 00032 private: 00033 std::string _backtrace; 00034 public: 00035 error_backtracer() 00036 { 00037 const int bufsize = 64; 00038 void* buffer[bufsize]; 00039 char **strings; 00040 00041 _backtrace.reserve(4096); 00042 00043 size_t nptrs = ::backtrace(buffer, bufsize); 00044 strings = backtrace_symbols(buffer, nptrs); 00045 00046 if (strings == NULL) { perror("backtrace_symbols"); exit(EXIT_FAILURE); } 00047 00048 for( size_t j = 0; j < nptrs; ++j ) 00049 { 00050 _backtrace += std::string( strings[j] ) + "\n"; 00051 if( _backtrace.capacity() == _backtrace.size() ) _backtrace.reserve( _backtrace.size() * 2 ); 00052 } 00053 00054 free(strings); 00055 } 00056 virtual ~error_backtracer() throw() {} 00057 00058 virtual const std::string& 00059 backtrace() const { return _backtrace; } 00060 }; 00061 00062 class exception : public error_backtracer, public std::exception 00063 { 00064 public: 00065 exception() : error_backtracer(), std::exception(){} 00066 }; 00067 00068 class runtime_error : public error_backtracer, public std::runtime_error 00069 { 00070 public: 00071 runtime_error( const std::string& __msg ) : error_backtracer(), std::runtime_error( __msg + "\nBacktrace:\n" + backtrace() ){} 00072 }; 00073 00074 } // namespace rostlab 00075 00076 #endif // ROSTLAB_STDEXCEPT 00077 // vim:et:ts=4:ai:syntax=cpp: