debug.hh
Go to the documentation of this file.
1 /*
2  * Copyright 2010,
3  * François Bleibel,
4  * Olivier Stasse,
5  *
6  * CNRS/AIST
7  *
8  */
9 
10 #ifndef SOT_CORE_DEBUG_HH
11 #define SOT_CORE_DEBUG_HH
12 #include <cstdarg>
13 #include <cstdio>
14 #include <fstream>
15 #include <sstream>
16 
17 #include "sot/core/api.hh"
18 
19 #ifndef VP_DEBUG_MODE
20 #define VP_DEBUG_MODE 0
21 #endif
22 
23 #ifndef VP_TEMPLATE_DEBUG_MODE
24 #define VP_TEMPLATE_DEBUG_MODE 0
25 #endif
26 
27 #define SOT_COMMON_TRACES \
28  do { \
29  va_list arg; \
30  va_start(arg, format); \
31  vsnprintf(charbuffer, SIZE, format, arg); \
32  va_end(arg); \
33  outputbuffer << tmpbuffer.str() << charbuffer << std::endl; \
34  } while (0)
35 
36 namespace dynamicgraph {
37 namespace sot {
39  public:
40  static const int SIZE = 512;
41 
42  std::stringstream tmpbuffer;
43  std::ostream &outputbuffer;
44  char charbuffer[SIZE + 1];
47 
48  DebugTrace(std::ostream &os) : outputbuffer(os) {}
49 
50  inline void trace(const int level, const char *format, ...) {
51  if (level <= traceLevel) SOT_COMMON_TRACES;
52  tmpbuffer.str("");
53  }
54 
55  inline void trace(const char *format, ...) {
57  tmpbuffer.str("");
58  }
59 
60  inline void trace(const int level = -1) {
61  if (level <= traceLevel) outputbuffer << tmpbuffer.str();
62  tmpbuffer.str("");
63  }
64 
65  inline void traceTemplate(const int level, const char *format, ...) {
66  if (level <= traceLevelTemplate) SOT_COMMON_TRACES;
67  tmpbuffer.str("");
68  }
69 
70  inline void traceTemplate(const char *format, ...) {
72  tmpbuffer.str("");
73  }
74 
75  inline DebugTrace &pre(const std::ostream &) { return *this; }
76 
77  inline DebugTrace &pre(const std::ostream &, int level) {
78  traceLevel = level;
79  return *this;
80  }
81 
82  static const char *DEBUG_FILENAME_DEFAULT;
83  static void openFile(const char *filename = DEBUG_FILENAME_DEFAULT);
84  static void closeFile(const char *filename = DEBUG_FILENAME_DEFAULT);
85 };
86 
89 } // namespace sot
90 } // namespace dynamicgraph
91 
92 #ifdef VP_DEBUG
93 #define sotPREDEBUG \
94  __FILE__ << ": " << __FUNCTION__ << "(#" << __LINE__ << ") :"
95 
96 #define sotPREERROR \
97  "\t!! " << __FILE__ << ": " << __FUNCTION__ << "(#" << __LINE__ << ") :"
98 
99 #define sotDEBUG(level) \
100  if ((level > VP_DEBUG_MODE) || \
101  (!dynamicgraph::sot::sotDEBUGFLOW.outputbuffer.good())) \
102  ; \
103  else \
104  dynamicgraph::sot::sotDEBUGFLOW.outputbuffer << sotPREDEBUG
105 
106 #define sotDEBUGMUTE(level) \
107  if ((level > VP_DEBUG_MODE) || \
108  (!dynamicgraph::sot::sotDEBUGFLOW.outputbuffer.good())) \
109  ; \
110  else \
111  dynamicgraph::sot::sotDEBUGFLOW.outputbuffer
112 
113 #define sotERROR \
114  if (!dynamicgraph::sot::sotDEBUGFLOW.outputbuffer.good()) \
115  ; \
116  else \
117  dynamicgraph::sot::sotERRORFLOW.outputbuffer << sotPREERROR
118 
119 #define sotDEBUGF \
120  if (!dynamicgraph::sot::sotDEBUGFLOW.outputbuffer.good()) \
121  ; \
122  else \
123  dynamicgraph::sot::sotDEBUGFLOW \
124  .pre(dynamicgraph::sot::sotDEBUGFLOW.tmpbuffer << sotPREDEBUG, \
125  VP_DEBUG_MODE) \
126  .trace
127 
128 #define sotERRORF \
129  if (!dynamicgraph::sot::sotDEBUGFLOW.outputbuffer.good()) \
130  ; \
131  else \
132  sot::sotERRORFLOW.pre(sot::sotERRORFLOW.tmpbuffer << sotPREERROR).trace
133 
134 // TEMPLATE
135 #define sotTDEBUG(level) \
136  if ((level > VP_TEMPLATE_DEBUG_MODE) || \
137  (!dynamicgraph::sot::sotDEBUGFLOW.outputbuffer.good())) \
138  ; \
139  else \
140  dynamicgraph::sot::sotDEBUGFLOW.outputbuffer << sotPREDEBUG
141 
142 #define sotTDEBUGF \
143  if (!dynamicgraph::sot::sotDEBUGFLOW.outputbuffer.good()) \
144  ; \
145  else \
146  dynamicgraph::sot::sotDEBUGFLOW \
147  .pre(dynamicgraph::sot::sotDEBUGFLOW.tmpbuffer << sotPREDEBUG, \
148  VP_TEMPLATE_DEBUG_MODE) \
149  .trace
150 
151 namespace dynamicgraph {
152 namespace sot {
153 inline bool sotDEBUG_ENABLE(const int &level) { return level <= VP_DEBUG_MODE; }
154 
155 inline bool sotTDEBUG_ENABLE(const int &level) {
156  return level <= VP_TEMPLATE_DEBUG_MODE;
157 }
158 } // namespace sot
159 } // namespace dynamicgraph
160 
161 /* -------------------------------------------------------------------------- */
162 #else // VP_DEBUG
163 #define sotPREERROR \
164  "\t!! " << __FILE__ << ": " << __FUNCTION__ << "(#" << __LINE__ << ") :"
165 #define sotDEBUG(level) \
166  if (1) \
167  ; \
168  else \
169  ::dynamicgraph::sot::__null_stream()
170 #define sotDEBUGMUTE(level) \
171  if (1) \
172  ; \
173  else \
174  ::dynamicgraph::sot::__null_stream()
175 #define sotERROR sotERRORFLOW.outputbuffer << sotPREERROR
176 
177 namespace dynamicgraph {
178 namespace sot {
179 inline void sotDEBUGF(const int, const char *, ...) {}
180 inline void sotDEBUGF(const char *, ...) {}
181 inline void sotERRORF(const int, const char *, ...) {}
182 inline void sotERRORF(const char *, ...) {}
183 inline std::ostream &__null_stream() {
184  // This function should never be called. With -O3,
185  // it should not appear in the generated binary.
186  static std::ostream os(NULL);
187  return os;
188 }
189 } // namespace sot
190 } // namespace dynamicgraph
191 
192 // TEMPLATE
193 #define sotTDEBUG(level) \
194  if (1) \
195  ; \
196  else \
197  ::dynamicgraph::sot::__null_stream()
198 
199 namespace dynamicgraph {
200 namespace sot {
201 inline void sotTDEBUGF(const int, const char *, ...) {}
202 inline void sotTDEBUGF(const char *, ...) {}
203 } // namespace sot
204 } // namespace dynamicgraph
205 
206 #define sotDEBUG_ENABLE(level) false
207 #define sotTDEBUG_ENABLE(level) false
208 
209 #endif // VP_DEBUG
210 
211 #define sotDEBUGIN(level) sotDEBUG(level) << "# In {" << std::endl
212 #define sotDEBUGOUT(level) sotDEBUG(level) << "# Out }" << std::endl
213 #define sotDEBUGINOUT(level) sotDEBUG(level) << "# In/Out { }" << std::endl
214 
215 #define sotTDEBUGIN(level) sotTDEBUG(level) << "# In {" << std::endl
216 #define sotTDEBUGOUT(level) sotTDEBUG(level) << "# Out }" << std::endl
217 #define sotTDEBUGINOUT(level) sotTDEBUG(level) << "# In/Out { }" << std::endl
218 
219 #endif
220 
221 // Local variables:
222 // c-basic-offset: 2
223 // End:
SOT_CORE_EXPORT DebugTrace sotERRORFLOW
void trace(const char *format,...)
Definition: debug.hh:55
DebugTrace(std::ostream &os)
Definition: debug.hh:48
#define VP_DEBUG_MODE
Definition: debug.hh:20
#define sotTDEBUG_ENABLE(level)
Definition: debug.hh:207
DebugTrace & pre(const std::ostream &)
Definition: debug.hh:75
#define sotDEBUG_ENABLE(level)
Definition: debug.hh:206
std::ostream & __null_stream()
Definition: debug.hh:183
std::stringstream tmpbuffer
Definition: debug.hh:42
void sotTDEBUGF(const int, const char *,...)
Definition: debug.hh:201
void traceTemplate(const char *format,...)
Definition: debug.hh:70
#define SOT_CORE_EXPORT
Definition: api.hh:20
void traceTemplate(const int level, const char *format,...)
Definition: debug.hh:65
void sotERRORF(const int, const char *,...)
Definition: debug.hh:181
void trace(const int level=-1)
Definition: debug.hh:60
#define SOT_COMMON_TRACES
VP_TEMPLATE_DEBUG_MODE.
Definition: debug.hh:27
void trace(const int level, const char *format,...)
Definition: debug.hh:50
DebugTrace & pre(const std::ostream &, int level)
Definition: debug.hh:77
void sotDEBUGF(const int, const char *,...)
Definition: debug.hh:179
#define VP_TEMPLATE_DEBUG_MODE
VP_DEBUG_MODE.
Definition: debug.hh:24
std::ostream & outputbuffer
Definition: debug.hh:43
SOT_CORE_EXPORT DebugTrace sotDEBUGFLOW
static const char * DEBUG_FILENAME_DEFAULT
Definition: debug.hh:82


sot-core
Author(s): Olivier Stasse, ostasse@laas.fr
autogenerated on Wed Jun 21 2023 02:51:26