trace.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstdint>
4 #include <iosfwd>
5 #include <string>
6 
7 #include <boost/current_function.hpp>
8 
9 namespace armarx::detail
10 {
11  struct Trace;
12 
14  {
15  struct Location
16  {
17  const std::string file;
18  const std::string func;
19  const int line;
20  };
21 
22  using FncLoc = Location (*)();
24  };
25 
26  struct Trace
27  {
28  // ///////////////////////////////////////////// //
29  // /////////////////// data //////////////////// //
30  // ///////////////////////////////////////////// //
31  public:
32  const Trace* parent;
33  const int uncaughtExceptions;
35 
36  private:
37  static thread_local const Trace* Top;
38  static thread_local std::uintmax_t Size;
39  // ///////////////////////////////////////////// //
40  // ////////////// static functions ///////////// //
41  // ///////////////////////////////////////////// //
42  public:
43  static void PrintStackTrace(std::ostream& out, const std::string& pre = "");
44 
45  static void PrintExceptionBacktrace(std::ostream& out, const std::string& pre = "");
46 
47  static void ClearExceptionBacktrace();
48 
49  static std::uintmax_t
51  {
52  return Size;
53  }
54 
55  // ///////////////////////////////////////////// //
56  // ///////////////// functions ///////////////// //
57  // ///////////////////////////////////////////// //
58  protected:
60  parent{Top}, uncaughtExceptions{std::uncaught_exceptions()}, location{l}
61  {
62  Top = this;
63  ++Size;
64  }
65 
66  public:
67  ~Trace();
68 
69  bool
70  exceptional() const
71  {
72  return uncaughtExceptions != std::uncaught_exceptions();
73  }
74  };
75 } // namespace armarx::detail
76 
77 #define ARMARX_TRACE _detail_TRACE_expand(__FILE__, __LINE__, __COUNTER__)
78 #define _detail_TRACE_expand(...) _detail_TRACE_mk_var(__VA_ARGS__)
79 #define _detail_TRACE_mk_var(fi, li, cnt) _detail_TRACE(fi, li, _detail_trace_variable_##cnt##_##li)
80 #define _detail_TRACE(fi, li, var) \
81  static constexpr auto var##_cfunc = BOOST_CURRENT_FUNCTION; \
82  const struct var##TraceType : ::armarx::detail::Trace \
83  { \
84  ::armarx::detail::LocationProvider::Location static Location() \
85  { \
86  return {fi, var##_cfunc, li}; \
87  } \
88  var##TraceType() : Trace{var##TraceType::Location} \
89  { \
90  } \
91  } var
92 
93 namespace armarx
94 {
95  class LogSender;
96 }
97 
98 #define ARMARX_TRACE_LITE _detail_TRACElite_expand(__FILE__, __LINE__, __COUNTER__)
99 #define _detail_TRACElite_expand(...) _detail_TRACElite_mk_var(__VA_ARGS__)
100 #define _detail_TRACElite_mk_var(fi, li, cnt) \
101  _detail_TRACElite_f(fi, li, _detail_trace_variable_##cnt##_##li)
102 #define _detail_TRACElite_f(fi, li, var) \
103  [[maybe_unused]] struct var##TraceType : ::armarx::detail::Trace \
104  { \
105  ::armarx::detail::LocationProvider::Location static Location() \
106  { \
107  return {fi, "fuction name is unknown!", li}; \
108  } \
109  var##TraceType(const ::armarx::LogSender&) : Trace{var##TraceType::Location} \
110  { \
111  } \
112  var##TraceType() : Trace{var##TraceType::Location} \
113  { \
114  } \
115  } var
armarx::detail::Trace::Trace
Trace(LocationProvider::FncLoc l)
Definition: trace.h:59
armarx::detail::Trace::~Trace
~Trace()
Definition: trace.cpp:105
armarx::detail::LocationProvider::location
const FncLoc location
Definition: trace.h:23
armarx::detail::LocationProvider::Location::file
const std::string file
Definition: trace.h:17
armarx::detail::LocationProvider
Definition: trace.h:13
armarx::detail::Trace::PrintStackTrace
static void PrintStackTrace(std::ostream &out, const std::string &pre="")
Definition: trace.cpp:182
armarx::detail::Trace::uncaughtExceptions
const int uncaughtExceptions
Definition: trace.h:33
armarx::detail::Trace
Definition: trace.h:26
armarx::detail::Trace::exceptional
bool exceptional() const
Definition: trace.h:70
armarx::detail
Definition: ApplicationNetworkStats.cpp:36
GfxTL::Trace
T Trace(const MatrixXX< N, N, T > &a)
Definition: MatrixXX.h:478
armarx::detail::LocationProvider::Location::line
const int line
Definition: trace.h:19
armarx::detail::Trace::GetStackTraceSize
static std::uintmax_t GetStackTraceSize()
Definition: trace.h:50
armarx::detail::Trace::location
const LocationProvider location
Definition: trace.h:34
armarx::detail::Trace::parent
const Trace * parent
Definition: trace.h:32
armarx::detail::LocationProvider::Location::func
const std::string func
Definition: trace.h:18
armarx::detail::Trace::ClearExceptionBacktrace
static void ClearExceptionBacktrace()
Definition: trace.cpp:100
armarx::detail::Trace::PrintExceptionBacktrace
static void PrintExceptionBacktrace(std::ostream &out, const std::string &pre="")
Definition: trace.cpp:216
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::detail::LocationProvider::FncLoc
Location(*)() FncLoc
Definition: trace.h:22
armarx::detail::LocationProvider::Location
Definition: trace.h:15