Ariles
trace.h
Go to the documentation of this file.
1 /**
2  @file
3  @author Alexander Sherikov
4 
5  @copyright 2017-2020 Alexander Sherikov, Licensed under the Apache License, Version 2.0.
6  (see @ref LICENSE or http://www.apache.org/licenses/LICENSE-2.0)
7 
8  @brief
9 */
10 
11 #pragma once
12 
13 
14 #ifdef ARILES_TRACE_ENABLE
15 # include <iostream>
16 # include <libgen.h>
17 # include <typeinfo>
18 # include <cxxabi.h>
19 
20 namespace ariles
21 {
22  namespace debug
23  {
24  class ARILES_VISIBILITY_ATTRIBUTE Tracer
25  {
26  public:
27  std::string tabulation_;
28  const std::string function_name_;
29  const std::string file_;
30  const int line_number_;
31 
32  protected:
33  std::size_t getDepth(const bool increment = true)
34  {
35  static std::size_t depth = 0;
36  if (true == increment)
37  {
38  return (depth++);
39  }
40  else
41  {
42  return (--depth);
43  }
44  }
45 
46  template <class t_First, class... t_Args>
47  void outputFirst(t_First &&first, t_Args &&... args)
48  {
49  std::cout << first;
50  outputFirst(std::forward<t_Args>(args)...);
51  }
52 
53  template <class t_Last>
54  void outputFirst(t_Last &&last)
55  {
56  std::cout << last << std::endl;
57  }
58 
59 
60  public:
61  Tracer(const std::string &function_name, const std::string &file, const int line_number)
62  : function_name_(function_name), file_(file), line_number_(line_number)
63  {
64  tabulation_.assign(getDepth(true), ' ');
65 
66  std::cout << tabulation_ //
67  << ">>> Entering function: " << function_name_ //
68  << " | File: " << file_ //
69  << " | Line: " << line_number_ << std::endl;
70  }
71 
72  ~Tracer()
73  {
74  std::cout << tabulation_ //
75  << "<<< Leaving function: " << function_name_ //
76  << " | File: " << file_ //
77  << " | Line: " << line_number_ << std::endl;
78  getDepth(false);
79  }
80 
81 
82  template <class... t_Args>
83  void output(t_Args &&... args)
84  {
85  std::cout << tabulation_;
86  outputFirst(std::forward<t_Args>(args)...);
87  }
88 
89  std::string demangle(const char *name) const
90  {
91  std::size_t len = 0;
92  int status = 0;
93 
94  char *demangled_name = abi::__cxa_demangle(name, NULL, &len, &status);
95 
96  if (demangled_name != NULL)
97  {
98  const std::string result(demangled_name);
99  free(demangled_name);
100  return (result);
101  }
102  else
103  {
104  return ("");
105  }
106  }
107  };
108  } // namespace debug
109 } // namespace ariles
110 
111 # define ARILES_TRACE_FUNCTION \
112  char trace_path[] = __FILE__; \
113  ariles::debug::Tracer tracer(__func__, basename(trace_path), __LINE__);
114  // ariles::debug::Tracer tracer(__PRETTY_FUNCTION__, basename(trace_path), __LINE__);
115 # define ARILES_TRACE_ENTRY(entry_name) tracer.output("Processing entry: ", entry_name);
116 # define ARILES_TRACE_TYPE(entry) tracer.output("Type: ", tracer.demangle(typeid(entry).name()));
117 #else
118 # define ARILES_TRACE_FUNCTION
119 # define ARILES_TRACE_ENTRY(entry_name)
120 # define ARILES_TRACE_TYPE(entry)
121 #endif
#define ARILES_VISIBILITY_ATTRIBUTE
Definition: helpers.h:69
Definition: basic.h:17