Leosac  0.8.0
Open Source Access Control
Stacktrace.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero 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  Leosac 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 Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "Stacktrace.hpp"
21 #include "Colorize.hpp"
22 #include "log.hpp"
23 
24 #if (!defined(__GNUG__) && defined(__clang__)) || defined(LEOSAC_STACKTRACE_DISABLED)
25 #define LEOSAC_STACKTRACE_DISABLED
26 #else
27 #include <backtrace.h>
28 #endif
29 
30 #include <boost/core/demangle.hpp>
31 #include <boost/range/adaptor/reversed.hpp>
32 
33 using namespace Leosac;
34 using namespace Leosac::Tools;
35 
36 Stacktrace::Stacktrace(uint skip /* = 0 */)
37 {
38 #ifndef LEOSAC_STACKTRACE_DISABLED
39  auto cb_error = [](void * /*data*/, const char *msg, int errnum) {
40  ERROR("Problem when generating stacktrace: " << msg
41  << ". Error code: " << errnum);
42  };
43 
44  auto cb_full = [](void *data, uintptr_t /*pc*/, const char *filename, int line,
45  const char *function) -> int {
46  std::vector<Frame> *stack = static_cast<std::vector<Frame> *>(data);
47  Frame frame;
48  if (!function)
49  {
50  frame.suppressed_ = true;
51  }
52  else
53  {
54  frame.function_ = boost::core::demangle(function);
55  frame.line_ = line;
56  frame.file_ = filename;
57  }
58  stack->push_back(frame);
59  return 0;
60  };
61 
62  struct backtrace_state *bt =
63  backtrace_create_state(nullptr, 0, nullptr, nullptr);
64  backtrace_full(bt, skip, cb_full, cb_error, &stack_);
65 #endif
66 }
67 
68 std::string Stacktrace::str(int max_frames /* = 10 */) const
69 {
70  int frame_count = 0;
71  int suppressed_count = 0; // count suppressed frame that follows each other.
72  std::stringstream ss;
73 
74  for (const auto &frame : stack_)
75  {
76  if (frame_count == max_frames)
77  break;
78  if (frame.suppressed_)
79  {
80  suppressed_count++;
81  continue;
82  }
83  if (suppressed_count)
84  ss << Colorize::red("Frames suppressed: ") << suppressed_count
85  << std::endl;
86  suppressed_count = 0;
87 
88  ss << "#" << frame_count << " in " << Colorize::cyan(frame.function_)
89  << " at " << Colorize::bright_green(frame.file_) << ":" << frame.line_
90  << std::endl;
91  frame_count++;
92  }
93  return ss.str();
94 }
Stacktrace.hpp
ERROR
@ ERROR
Definition: log.hpp:32
Colorize.hpp
Leosac::Tools::Stacktrace::str
std::string str(int max_frames=10) const
Definition: Stacktrace.cpp:68
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Colorize::bright_green
std::string bright_green(const T &in)
Definition: Colorize.hpp:88
Leosac::Tools::Stacktrace::stack_
std::vector< Frame > stack_
We use a vector to represent the stack so we can iterate on the frames.
Definition: Stacktrace.hpp:77
Leosac::Colorize::red
std::string red(const T &in)
Definition: Colorize.hpp:70
log.hpp
Leosac::Colorize::cyan
std::string cyan(const T &in)
Definition: Colorize.hpp:94
Leosac::Tools::Stacktrace::Frame
Definition: Stacktrace.hpp:52
Leosac::Tools::Stacktrace::Stacktrace
Stacktrace(uint skip)
Build a new stacktrace.
Definition: Stacktrace.cpp:36
Leosac::Tools
Definition: DatabaseLogSink.hpp:27