15 # define H_CPPUT_TRACE
18 # ifdef CPPUT_TRACE_ENABLE
24 # ifndef CPPUT_TRACE_FUNCTION_NAME
26 # define CPPUT_TRACE_FUNCTION_NAME __func__
36 std::string tabulation_;
37 const std::string function_name_;
38 const std::string file_;
39 const int line_number_;
42 std::size_t getDepth(
const bool increment =
true)
44 static std::size_t depth = 0;
45 if (
true == increment)
55 template <
class t_First,
class... t_Args>
56 void outputFirst(t_First &&first, t_Args &&...args)
59 outputFirst(std::forward<t_Args>(args)...);
62 template <
class t_Last>
63 void outputFirst(t_Last &&last)
65 std::cout << last << std::endl;
70 Tracer(
const std::string &function_name,
const std::string &file,
const int line_number)
71 : function_name_(function_name), file_(file), line_number_(line_number)
73 tabulation_.assign(getDepth(
true),
' ');
75 std::cout << tabulation_
76 <<
">>> Entering function: " << function_name_
77 <<
" | File: " << file_
78 <<
" | Line: " << line_number_ << std::endl;
83 std::cout << tabulation_
84 <<
"<<< Leaving function: " << function_name_
85 <<
" | File: " << file_
86 <<
" | Line: " << line_number_ << std::endl;
91 template <
class... t_Args>
92 void output(t_Args &&...args)
94 std::cout << tabulation_;
95 outputFirst(std::forward<t_Args>(args)...);
98 std::string demangle(
const char *name)
const
103 char *demangled_name = abi::__cxa_demangle(name, NULL, &len, &status);
105 if (demangled_name != NULL)
107 const std::string result(demangled_name);
108 free(demangled_name);
120 # define CPPUT_TRACE_FUNCTION \
121 char trace_path[] = __FILE__; \
122 cpput::trace::Tracer tracer(CPPUT_TRACE_FUNCTION_NAME, basename(trace_path), __LINE__);
123 # define CPPUT_TRACE_VALUE(variable) tracer.output("Value: ", #variable, " = ", variable);
124 # define CPPUT_TRACE_TYPE(variable) tracer.output("Type: ", tracer.demangle(typeid(variable).name()));
126 # define CPPUT_TRACE_FUNCTION
127 # define CPPUT_TRACE_VALUE(value)
128 # define CPPUT_TRACE_TYPE(type)