librostlab 1.0.20
rostlab_stdexcept.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2011 Laszlo Kajan, Technical University of Munich, Germany
3
4 This file is part of librostlab.
5
6 librostlab is free software: you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19#ifndef ROSTLAB_STDEXCEPT
20#define ROSTLAB_STDEXCEPT
21
22#include <execinfo.h>
23#include <stdexcept>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string>
27
28namespace rostlab {
29
31{
32 private:
33 std::string _backtrace;
34 public:
36 {
37 const int bufsize = 64;
38 void* buffer[bufsize];
39 char **strings;
40
41 _backtrace.reserve(4096);
42
43 size_t nptrs = ::backtrace(buffer, bufsize);
44 strings = backtrace_symbols(buffer, nptrs);
45
46 if (strings == NULL) { perror("backtrace_symbols"); exit(EXIT_FAILURE); }
47
48 for( size_t j = 0; j < nptrs; ++j )
49 {
50 _backtrace += std::string( strings[j] ) + "\n";
51 if( _backtrace.capacity() == _backtrace.size() ) _backtrace.reserve( _backtrace.size() * 2 );
52 }
53
54 free(strings);
55 }
56 virtual ~error_backtracer() throw() {}
57
58 virtual const std::string&
59 backtrace() const { return _backtrace; }
60};
61
62class exception : public error_backtracer, public std::exception
63{
64 public:
66};
67
68class runtime_error : public error_backtracer, public std::runtime_error
69{
70 public:
71 runtime_error( const std::string& __msg ) : error_backtracer(), std::runtime_error( __msg + "\nBacktrace:\n" + backtrace() ){}
72};
73
74} // namespace rostlab
75
76#endif // ROSTLAB_STDEXCEPT
77// vim:et:ts=4:ai:syntax=cpp:
virtual const std::string & backtrace() const
runtime_error(const std::string &__msg)