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
9namespace 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;
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
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
93namespace 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
This file offers overloads of toIce() and fromIce() functions for STL container types.
const Trace * parent
Definition trace.h:32
Trace(LocationProvider::FncLoc l)
Definition trace.h:59
const LocationProvider location
Definition trace.h:34
bool exceptional() const
Definition trace.h:70
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:33
static std::uintmax_t GetStackTraceSize()
Definition trace.h:50
static void PrintStackTrace(std::ostream &out, const std::string &pre="")
Definition trace.cpp:182