dynamic-graph  4.1.0-8-gdab7-dirty
Dynamic graph library
debug.h
1 // -*- mode: c++ -*-
2 // Copyright 2010, François Bleibel, Thomas Moulard, Olivier Stasse,
3 // JRL, CNRS/AIST.
4 //
5 
6 #ifndef DYNAMIC_GRAPH_DEBUG_HH
7 #define DYNAMIC_GRAPH_DEBUG_HH
8 #include <cstdarg>
9 #include <cstdio>
10 #include <fstream>
11 #include <sstream>
12 
13 #include <dynamic-graph/dynamic-graph-api.h>
14 #include <dynamic-graph/fwd.hh>
15 
16 #ifndef VP_DEBUG_MODE
17 #define VP_DEBUG_MODE 0
18 #endif
19 
20 #ifndef VP_TEMPLATE_DEBUG_MODE
21 #define VP_TEMPLATE_DEBUG_MODE 0
22 #endif
23 
24 #define DG_COMMON_TRACES \
25  do { \
26  va_list arg; \
27  va_start(arg, format); \
28  vsnprintf(charbuffer, SIZE, format, arg); \
29  va_end(arg); \
30  outputbuffer << tmpbuffer.str() << charbuffer << std::endl; \
31  } while (0)
32 
33 namespace dynamicgraph {
40 class DYNAMIC_GRAPH_DLLAPI DebugTrace {
41 public:
42  static const int SIZE = 512;
43 
44  std::stringstream tmpbuffer;
45  std::ostream &outputbuffer;
46  char charbuffer[SIZE + 1];
47  int traceLevel;
48  int traceLevelTemplate;
49 
50  DebugTrace(std::ostream &os) : outputbuffer(os) {}
51 
52  inline void trace(const int level, const char *format, ...) {
53  if (level <= traceLevel)
54  DG_COMMON_TRACES;
55  tmpbuffer.str("");
56  }
57 
58  inline void trace(const char *format, ...) {
59  DG_COMMON_TRACES;
60  tmpbuffer.str("");
61  }
62 
63  inline void trace(const int level = -1) {
64  if (level <= traceLevel) {
65  outputbuffer << tmpbuffer.str();
66  tmpbuffer.str("");
67  }
68  }
69 
70  inline void traceTemplate(const int level, const char *format, ...) {
71  if (level <= traceLevelTemplate)
72  DG_COMMON_TRACES;
73  tmpbuffer.str("");
74  }
75 
76  inline void traceTemplate(const char *format, ...) {
77  DG_COMMON_TRACES;
78  tmpbuffer.str("");
79  }
80 
81  inline DebugTrace &pre(const std::ostream &) { return *this; }
82 
83  inline DebugTrace &pre(const std::ostream &, int level) {
84  traceLevel = level;
85  return *this;
86  }
87 
88  static const char *DEBUG_FILENAME_DEFAULT;
89  static void openFile(const char *filename = DEBUG_FILENAME_DEFAULT);
90  static void closeFile(const char *filename = DEBUG_FILENAME_DEFAULT);
91 };
92 
93 DYNAMIC_GRAPH_DLLAPI extern DebugTrace dgDEBUGFLOW;
94 DYNAMIC_GRAPH_DLLAPI extern DebugTrace dgERRORFLOW;
95 } // end of namespace dynamicgraph
96 
97 #ifdef VP_DEBUG
98 
99 #define dgPREDEBUG __FILE__ << ": " << __FUNCTION__ << "(#" << __LINE__ << ") :"
100 #define dgPREERROR \
101  "\t!! " << __FILE__ << ": " << __FUNCTION__ << "(#" << __LINE__ << ") :"
102 
103 #define dgDEBUG(level) \
104  if ((level > VP_DEBUG_MODE) || (!dgDEBUGFLOW.outputbuffer.good())) \
105  ; \
106  else \
107  dgDEBUGFLOW.outputbuffer << dgPREDEBUG
108 
109 #define dgDEBUGMUTE(level) \
110  if ((level > VP_DEBUG_MODE) || (!dgDEBUGFLOW.outputbuffer.good())) \
111  ; \
112  else \
113  dgDEBUGFLOW.outputbuffer
114 
115 #define dgERROR \
116  if (!dgDEBUGFLOW.outputbuffer.good()) \
117  ; \
118  else \
119  dgERRORFLOW.outputbuffer << dgPREERROR
120 
121 #define dgDEBUGF \
122  if (!dgDEBUGFLOW.outputbuffer.good()) \
123  ; \
124  else \
125  dgDEBUGFLOW.pre(dgDEBUGFLOW.tmpbuffer << dgPREDEBUG, VP_DEBUG_MODE).trace
126 
127 #define dgERRORF \
128  if (!dgDEBUGFLOW.outputbuffer.good()) \
129  ; \
130  else \
131  dgERRORFLOW.pre(dgERRORFLOW.tmpbuffer << dgPREERROR).trace
132 
133 // TEMPLATE
134 #define dgTDEBUG(level) \
135  if ((level > VP_TEMPLATE_DEBUG_MODE) || (!dgDEBUGFLOW.outputbuffer.good())) \
136  ; \
137  else \
138  dgDEBUGFLOW.outputbuffer << dgPREDEBUG
139 
140 #define dgTDEBUGF \
141  if (!dgDEBUGFLOW.outputbuffer.good()) \
142  ; \
143  else \
144  dgDEBUGFLOW \
145  .pre(dgDEBUGFLOW.tmpbuffer << dgPREDEBUG, VP_TEMPLATE_DEBUG_MODE) \
146  .trace
147 
148 inline bool dgDEBUG_ENABLE(const int &level) { return level <= VP_DEBUG_MODE; }
149 
150 inline bool dgTDEBUG_ENABLE(const int &level) {
151  return level <= VP_TEMPLATE_DEBUG_MODE;
152 }
153 
154 #else // VP_DEBUG
155 
156 #define dgPREERROR \
157  "\t!! " << __FILE__ << ": " << __FUNCTION__ << "(#" << __LINE__ << ") :"
158 
159 #define dgDEBUG(level) \
160  if (1) \
161  ; \
162  else \
163  ::dynamicgraph::__null_stream()
164 
165 #define dgDEBUGMUTE \
166  (level) if (1); \
167  else ::dynamicgraph::__null_stream()
168 
169 #define dgERROR dgERRORFLOW.outputbuffer << dgPREERROR
170 
171 inline void dgDEBUGF(const int, const char *, ...) { return; }
172 
173 inline void dgDEBUGF(const char *, ...) { return; }
174 
175 inline void dgERRORF(const int, const char *, ...) { return; }
176 
177 inline void dgERRORF(const char *, ...) { return; }
178 
179 namespace dynamicgraph {
180 inline std::ostream &__null_stream() {
181  // This function should never be called. With -O3,
182  // it should not appear in the generated binary.
183  static std::ostream os(NULL);
184  return os;
185 }
186 } // namespace dynamicgraph
187 
188 // TEMPLATE
189 #define dgTDEBUG(level) \
190  if (1) \
191  ; \
192  else \
193  ::dynamicgraph::__null_stream()
194 
195 inline void dgTDEBUGF(const int, const char *, ...) { return; }
196 
197 inline void dgTDEBUGF(const char *, ...) { return; }
198 
199 #define dgDEBUG_ENABLE(level) false
200 #define dgTDEBUG_ENABLE(level) false
201 
202 #endif
203 
204 #define dgDEBUGIN(level) dgDEBUG(level) << "# In {" << std::endl
205 
206 #define dgDEBUGOUT(level) dgDEBUG(level) << "# Out }" << std::endl
207 
208 #define dgDEBUGINOUT(level) dgDEBUG(level) << "# In/Out { }" << std::endl
209 
210 #define dgTDEBUGIN(level) dgTDEBUG(level) << "# In {" << std::endl
211 
212 #define dgTDEBUGOUT(level) dgTDEBUG(level) << "# Out }" << std::endl
213 
214 #define dgTDEBUGINOUT(level) dgTDEBUG(level) << "# In/Out { }" << std::endl
215 
216 #endif
Logging class.
Definition: debug.h:40