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