CppCommon  1.0.4.1
C++ Common Library
stack_trace.h
Go to the documentation of this file.
1 
9 #ifndef CPPCOMMON_SYSTEM_STACK_TRACE_H
10 #define CPPCOMMON_SYSTEM_STACK_TRACE_H
11 
12 #include "string/format.h"
13 
14 #include <sstream>
15 #include <string>
16 #include <vector>
17 
19 
22 #define __STACK__ CppCommon::StackTrace()
23 
24 namespace CppCommon {
25 
27 
33 {
34 public:
36  struct Frame
37  {
38  void* address;
39  std::string module;
40  std::string function;
41  std::string filename;
42  int line;
43 
45  std::string string() const
46  { std::stringstream ss; ss << *this; return ss.str(); }
47 
49  friend std::ostream& operator<<(std::ostream& os, const Frame& frame);
50  };
51 
52 public:
54 
57  explicit StackTrace(int skip = 0);
58  StackTrace(const StackTrace&) = default;
59  StackTrace(StackTrace&&) noexcept = default;
60  ~StackTrace() = default;
61 
62  StackTrace& operator=(const StackTrace&) = default;
63  StackTrace& operator=(StackTrace&&) noexcept = default;
64 
66  const std::vector<Frame>& frames() const noexcept { return _frames; }
67 
69  std::string string() const
70  { std::stringstream ss; ss << *this; return ss.str(); }
71 
73  friend std::ostream& operator<<(std::ostream& os, const StackTrace& stack_trace);
74 
75 private:
76  std::vector<Frame> _frames;
77 };
78 
81 } // namespace CppCommon
82 
83 #include "stack_trace.inl"
84 
85 #endif // CPPCOMMON_SYSTEM_STACK_TRACE_H
Stack trace snapshot provider.
Definition: stack_trace.h:33
std::string string() const
Get string from the current stack trace snapshot.
Definition: stack_trace.h:69
friend std::ostream & operator<<(std::ostream &os, const StackTrace &stack_trace)
Output stack trace into the given output stream.
StackTrace(StackTrace &&) noexcept=default
StackTrace(int skip=0)
Capture the current stack trace snapshot.
Definition: stack_trace.cpp:63
const std::vector< Frame > & frames() const noexcept
Get stack trace frames.
Definition: stack_trace.h:66
StackTrace(const StackTrace &)=default
Format string definition.
C++ Common project definitions.
Definition: token_bucket.h:15
Stack trace snapshot provider inline implementation.
Stack trace frame.
Definition: stack_trace.h:37
friend std::ostream & operator<<(std::ostream &os, const Frame &frame)
Output stack trace frame into the given output stream.
Definition: stack_trace.cpp:48
std::string filename
Frame file name.
Definition: stack_trace.h:41
int line
Frame line number.
Definition: stack_trace.h:42
std::string string() const
Get string from the current stack trace frame.
Definition: stack_trace.h:45
std::string module
Frame module.
Definition: stack_trace.h:39
void * address
Frame address.
Definition: stack_trace.h:38