Exception.cc
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
17 #include "Exception.hh"
18 //#include "Exception.ih"
19 
20 // Custom includes
21 #ifdef SENF_BACKTRACE
22  #include <execinfo.h>
23 #endif
24 #include <sstream>
25 #include "Backtrace.hh"
26 
27 #define prefix_
28 //-/////////////////////////////////////////////////////////////////////////////////////////////////
29 
30 //-/////////////////////////////////////////////////////////////////////////////////////////////////
31 // senf::Exception
32 
33 #ifdef SENF_BACKTRACE
34 
35 prefix_ void senf::ExceptionMixin::addBacktrace()
36 {
37  void * entries[SENF_DEBUG_BACKTRACE_NUMCALLERS];
38  int nEntries (::backtrace(entries, SENF_DEBUG_BACKTRACE_NUMCALLERS));
39 
40  std::stringstream ss;
41  ss << "\nException at\n";
42  formatBacktrace(ss, entries, nEntries);
43  ss << "-- \n";
44  excLen_ = ss.str().size();
45  ss << what_;
46  what_ = ss.str();
47 }
48 
49 #endif
50 
51 //-/////////////////////////////////////////////////////////////////////////////////////////////////
52 // senf::Exception
53 
55  throw()
56 {}
57 
59  const throw()
60 {
61  return what_.c_str();
62 }
63 
64 //-/////////////////////////////////////////////////////////////////////////////////////////////////
65 // senf::SystemException
66 
67 prefix_ void senf::SystemException::init(std::string const & descr, int code
69 {
70  code_ = code;
71 # ifdef SENF_DEBUG
72  if (file && line)
73  (*this) << "Exception at " << file << ":" << line << "\n";
74 # endif
75  (*this) << "[" << errorString() << "]";
76  if (! descr.empty()) (*this) << " " << descr;
77 }
78 
79 //-/////////////////////////////////////////////////////////////////////////////////////////////////
80 #undef prefix_
81 
82 
83 // Local Variables:
84 // mode: c++
85 // fill-column: 100
86 // c-file-style: "senf"
87 // indent-tabs-mode: nil
88 // ispell-local-dictionary: "american"
89 // compile-command: "scons -u test"
90 // comment-column: 40
91 // End:
std::string backtrace() const
Return backtrace (if available)
#define prefix_
Definition: Exception.cc:27
Backtrace public header.
virtual ~Exception()
Definition: Exception.cc:54
Exception public header.
virtual char const * what() const
get exception description and backtrace if available
Definition: Exception.cc:58
#define _SENF_EXC_DEBUG_ARGS_ND
Definition: Exception.hh:250
std::string what_
Definition: Exception.hh:145
void formatBacktrace(std::ostream &os, void **backtrace, int numEntries)
Format a given backtrace.
Definition: Backtrace.cc:34