aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
exceptions.cpp
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library 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 library 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 library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 #include <cstdio>
23 #include <iostream>
24 #include <sstream>
25 #include <utility>
26 #include <string.h>
27 
28 #include <agrum/agrum.h>
29 #include <agrum/tools/core/exceptions.h>
30 #ifdef GUM_DEBUG_MODE
31 # ifdef HAVE_EXECINFO_H
32 # include <execinfo.h>
33 # endif // HAVE_EXECINFO_H
34 #endif // GUM_DEBUG_MODE
35 
36 
37 #ifndef DOXYGEN_SHOULD_SKIP_THIS
38 
39 namespace gum {
40  const std::string _createMsg_(const std::string& filename,
41  const std::string& function,
42  const int line,
43  const std::string& msg) {
44  std::stringstream stream;
45 # ifdef SWIG
46  stream << std::endl << msg << std::endl;
47 # else
48  stream << std::endl
49  << "<" << filename << "> " << function << "() #" << std::setw(6) << std::dec << line
50  << " :" << std::endl
51  << "--------------" << std::endl
52  << "! " << msg << std::endl
53  << "--------------" << std::endl;
54 # endif // SWIG
55  return stream.str();
56  }
57  Exception::Exception(const Exception& e) : msg_(e.msg_), type_(e.type_) {}
58 
59  Exception::Exception(std::string aMsg, std::string aType) :
60  msg_(std::move(aMsg)), type_(std::move(aType)) {
61 # ifdef GUM_DEBUG_MODE
62 # ifdef HAVE_EXECINFO_H
63 # define callStackDepth 20
64  void* array[callStackDepth];
65  size_t size;
66  char** strings;
67  size = backtrace(array, callStackDepth);
68  strings = backtrace_symbols(array, size);
69 
70  std::stringstream stream;
71 
72  for (size_t i = 1; i < size; ++i) {
73  stream << i << " :" << strings[i] << std::endl;
74  }
75 
76  free(strings);
77  callstack_ = stream.str();
78 # else // HAVE_EXECINFO_H
79  callstack_ = "Callstack only in linux debug mode when execinfo.h available";
80 # endif // HAVE_EXECINFO_H
81 # else // GUM_DEBUG_MODE
82  callstack_ = "Callstack only in linux debug mod ewhen execinfo.h available";
83 # endif // GUM_DEBUG_MODE
84  }
85 
86 } /* namespace gum */
87 
88 #endif // DOXYGEN_SHOULD_SKIP_THIS