Open Inventor Reference
SoTrace.h
Go to the documentation of this file.
1// **InsertLicense** code
2
3#ifndef _SO_TRACE_
4#define _SO_TRACE_
5
7#include <Inventor/SbString.h>
9
19
20// Tracing is considered deprecated and will be removed in a future Inventor version.
21#if 0
22
23# define SO_TRACE_IN(__classAndMethod___) \
24 static const char *const __SO_TRACE_IN_STRING_BUF = __FILE__ ":" __classAndMethod___; \
25 SoTrace<uint16_t> __SoTraceObject(__SO_TRACE_IN_STRING_BUF, SoGlobalTraceBuffer);
26
27#else
28
29# define SO_TRACE_IN(__classAndMethod___)
30
31#endif
32
35
36
37
56
57template <typename INDEXTYPE> class SoTraceBuffer
58{
59 public:
60
63 inline SoTraceBuffer() {
64 _initClass();
65 }
66
73 inline void add(const char *const traceInString) {
81 _traceStack[_traceStackTop] = traceInString;
82 ++_traceStackTop;
83
86 _traceList[_traceListEnd] = traceInString;
87 ++_traceListEnd;
88 }
89
93 inline void remove() {
101 _traceStackTop = static_cast<INDEXTYPE>((_traceStackTop - 1) & _bufferIndexMask);
102
105 _traceStack[_traceStackTop] = NULL;
106 }
107
110 inline INDEXTYPE getTraceStackTop() const {
111 return _traceStackTop;
112 }
113
116 inline INDEXTYPE getTraceListEnd() const {
117 return _traceListEnd;
118 }
119
121 inline const char *const *getTraceStack() const {
122 return _traceStack;
123 }
124
126 inline const char *const *getTraceList() const {
127 return _traceList;
128 }
129
132 return _maxNumTraceListDumps;
133 }
134
137 _maxNumTraceListDumps = num;
138 }
139
142 return _maxNumTraceStackDumps;
143 }
144
147 _maxNumTraceStackDumps = num;
148 }
149
153
154 private:
155
158 inline void _initClass() {
160 memset(_traceStack, 0, _numBufBytes);
161 memset(_traceList, 0, _numBufBytes);
162
164 _traceStackTop = 0;
165 _traceListEnd = 0;
166 }
167
172 enum {
173 _numBufEntries = (1L << (sizeof(INDEXTYPE) << 3)) ,
174 _numBufBytes = _numBufEntries * sizeof(const char *),
175 _bufferIndexMask = _numBufEntries - 1
176 };
177
179 INDEXTYPE _traceStackTop;
180
182 INDEXTYPE _traceListEnd;
183
185 const char *_traceStack[_numBufEntries];
186
188 const char *_traceList[_numBufEntries];
189
192 SoGlobalTraceBufferType _maxNumTraceListDumps;
193
196 SoGlobalTraceBufferType _maxNumTraceStackDumps;
197};
198
199
208
210
211
212template <typename INDEXTYPE>
215{
216 try {
218 char num[512] = "";
219
220 SbString endLineStr = "\n";
221
224 SbString traceListStr = "";
225 SoGlobalTraceBufferType indexCounter = 0;
226 SoGlobalTraceBufferType entry = 0;
227
228 if((_maxNumTraceStackDumps > 0) && (getTraceStackTop() > 0)) {
229 sprintf(num, "\nCall stack of the last %d traced functions:\n", _maxNumTraceStackDumps);
230 traceListStr += num;
231
233 SoGlobalTraceBufferType stackCnt = 0;
234 const char * const *traceStack = getTraceStack();
235 SoGlobalTraceBufferType stackTop = getTraceStackTop();
236 SoGlobalTraceBufferType stackPreTop = stackTop;
237 stackPreTop--;
238 indexCounter = 1;
239
240 for(entry = stackPreTop; (entry != stackTop) && (stackCnt < _maxNumTraceStackDumps); entry--, stackCnt++) {
242 sprintf(num, "%5d ", indexCounter);
243
245 const char *entryStrPtr = traceStack[entry];
246
247 if(entryStrPtr) {
248 traceListStr += ((SbString)num);
249 traceListStr += entryStrPtr;
250 traceListStr += endLineStr;
251 }
252
254 indexCounter++;
255 }
256 }
257
258 if(_maxNumTraceListDumps > 0) {
259 SoGlobalTraceBufferType listCnt = 0;
260 sprintf(num, "\nCall list of the %d most recently traced functions:\n", _maxNumTraceListDumps);
261 traceListStr += num;
262 SoGlobalTraceBufferType listEnd = getTraceListEnd();
263 SoGlobalTraceBufferType listPreEnd = listEnd;
264 listPreEnd--;
265 indexCounter = 1;
266 const char * const *traceList = SoGlobalTraceBuffer.getTraceList();
267 entry=0;
268
269 for(entry = listPreEnd; (entry != listEnd) && (listCnt < _maxNumTraceListDumps); entry--, listCnt++) {
271 sprintf(num, "%5d ", indexCounter);
272
274 const char *entryStrPtr = traceList[entry];
275
276 if(entryStrPtr) {
277 traceListStr += ((SbString)num);
278 traceListStr += entryStrPtr;
279 traceListStr += endLineStr;
280 }
281
283 indexCounter++;
284 }
285 }
286
288 return traceListStr;
289 }
290 catch(...) {
291 return "<Could not determine last called functions or function stack>";
292 }
293}
294
295
310
311template <typename INDEXTYPE> class SoTrace
312{
313 public:
314
325 inline SoTrace(const char *const traceInString, SoTraceBuffer<INDEXTYPE> &traceBuf) : _traceBuf(traceBuf) {
327 traceBuf.add(traceInString);
328 }
329
336 inline ~SoTrace() {
337 _traceBuf.remove();
338 }
339
340 private:
341
345 SoTraceBuffer<INDEXTYPE> &_traceBuf;
346
349 inline SoTrace(const SoTrace &trace) : _traceBuf(trace._traceBuf) {
350 }
351
355 inline SoTrace<INDEXTYPE> &operator=(const SoTrace &) {
356 return *this;
357 }
358};
359
360#endif // _SO_TYPE_
#define INVENTOR_API
Disable some annoying warnings on MSVC 6.
Definition SbSystem.h:77
unsigned short int uint16_t
Definition SbTypeDefs.h:47
INVENTOR_API SoTraceBuffer< SoGlobalTraceBufferType > SoGlobalTraceBuffer
This is a global singleton of the SoTraceBuffer class.
uint16_t SoGlobalTraceBufferType
The type used in the SoGlobalTraceBuffer.
Definition SoTrace.h:34
Class for smart character strings.
Definition SbString.h:85
This class manages a list and a stack of pointers to permanent strings.
Definition SoTrace.h:58
SoGlobalTraceBufferType getMaxNumTraceStackDumps() const
Maximum number of trace stack entries to be shown on fatal error outputs.
Definition SoTrace.h:141
void add(const char *const traceInString)
Add trace description.
Definition SoTrace.h:73
INDEXTYPE getTraceStackTop() const
Returns index into trace stack buffer.
Definition SoTrace.h:110
void setMaxNumTraceStackDumps(SoGlobalTraceBufferType num)
Sets the maximum number of trace stack entries to be shown on fatal error outputs.
Definition SoTrace.h:146
SoGlobalTraceBufferType getMaxNumTraceListDumps() const
Maximum number of trace list entries to be shown on fatal error outputs.
Definition SoTrace.h:131
INDEXTYPE getTraceListEnd() const
Returns index into trace list buffer.
Definition SoTrace.h:116
SbString getTraceDumpString() const
Returns an SbString which returns a dump of trace list and trace dump entries for debugging purposes.
Definition SoTrace.h:214
void remove()
Destructor.
Definition SoTrace.h:93
const char *const * getTraceStack() const
Returns the pointer to the first element of the trace stack.
Definition SoTrace.h:121
const char *const * getTraceList() const
Returns the pointer to the first element of the trace list.
Definition SoTrace.h:126
SoTraceBuffer()
Initializing constructor.
Definition SoTrace.h:63
void setMaxNumTraceListDumps(SoGlobalTraceBufferType num)
Sets the maximum number of trace list entries to be shown on fatal error outputs.
Definition SoTrace.h:136
This class simply implements a constructor and a destructor.
Definition SoTrace.h:312
~SoTrace()
Destructor.
Definition SoTrace.h:336
SoTrace(const char *const traceInString, SoTraceBuffer< INDEXTYPE > &traceBuf)
Constructor.
Definition SoTrace.h:325