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