Directory: | ./ |
---|---|
File: | include/sot/core/debug.hh |
Date: | 2024-08-13 12:13:25 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 6 | 7 | 85.7% |
Branches: | 6 | 12 | 50.0% |
Line | Branch | Exec | Source |
---|---|---|---|
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 //! VP_DEBUG_MODE | ||
22 | |||
23 | #ifndef VP_TEMPLATE_DEBUG_MODE | ||
24 | #define VP_TEMPLATE_DEBUG_MODE 0 | ||
25 | #endif //! VP_TEMPLATE_DEBUG_MODE | ||
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 { | ||
38 | class SOT_CORE_EXPORT DebugTrace { | ||
39 | public: | ||
40 | static const int SIZE = 512; | ||
41 | |||
42 | std::stringstream tmpbuffer; | ||
43 | std::ostream &outputbuffer; | ||
44 | char charbuffer[SIZE + 1]; | ||
45 | int traceLevel; | ||
46 | int traceLevelTemplate; | ||
47 | |||
48 | 46 | 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 | 1 | inline void trace(const char *format, ...) { | |
56 |
4/8✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
|
1 | SOT_COMMON_TRACES; |
57 |
2/4✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
|
1 | tmpbuffer.str(""); |
58 | 1 | } | |
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, ...) { | ||
71 | SOT_COMMON_TRACES; | ||
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 | |||
87 | SOT_CORE_EXPORT extern DebugTrace sotDEBUGFLOW; | ||
88 | SOT_CORE_EXPORT extern DebugTrace sotERRORFLOW; | ||
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 | 6 | 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 //! #ifdef SOT_CORE_DEBUG_HH | ||
220 | |||
221 | // Local variables: | ||
222 | // c-basic-offset: 2 | ||
223 | // End: | ||
224 |