[go: up one dir, main page]

File: SoDebugError.cpp

package info (click to toggle)
coin3 3.1.3-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 48,344 kB
  • ctags: 70,042
  • sloc: cpp: 314,328; ansic: 15,927; sh: 13,635; makefile: 8,780; perl: 2,149; lex: 1,302; lisp: 1,247; yacc: 184; xml: 175; sed: 68
file content (397 lines) | stat: -rw-r--r-- 11,126 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/**************************************************************************\
 *
 *  This file is part of the Coin 3D visualization library.
 *  Copyright (C) by Kongsberg Oil & Gas Technologies.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  ("GPL") version 2 as published by the Free Software Foundation.
 *  See the file LICENSE.GPL at the root directory of this source
 *  distribution for additional information about the GNU GPL.
 *
 *  For using Coin with software that can not be combined with the GNU
 *  GPL, and for taking advantage of the additional benefits of our
 *  support services, please contact Kongsberg Oil & Gas Technologies
 *  about acquiring a Coin Professional Edition License.
 *
 *  See http://www.coin3d.org/ for more information.
 *
 *  Kongsberg Oil & Gas Technologies, Bygdoy Alle 5, 0257 Oslo, NORWAY.
 *  http://www.sim.no/  sales@sim.no  coin-support@coin3d.org
 *
\**************************************************************************/

/*!
  \class SoDebugError SoDebugError.h Inventor/errors/SoDebugError.h
  \brief The SoDebugError class is the internal debugging message passing mechanism.
  \ingroup errors

  This class basically serves two purposes:

  1) It is the message interface through which error and warning
  conditions are passed to the programmer which is using the Coin
  library API for building applications.  These messages are generated
  when API methods are used in an incorrect manner, or if actions on
  the library has caused it to enter an inconsistent state. Coin
  programmers should then quickly be able to trace down errors in
  their application code.

  For this service to be available, the Coin library must be compiled
  with debugging information. For release builds, most of this
  "programmer's safety net" is removed to gain maximum speed and to
  use minimum space (build the smallest possible library).

  2) Coin application programmers can call the SoDebugError methods
  within their own code as a convenient way of debugging application
  code, replacing the usual cascades of fprintf() calls.


  Coin supports an environment variable to set conditional
  breakpoints.  The COIN_DEBUG_BREAK environment variable can be set
  to any number of functions in the form of a list separated by commas
  or spaces.  The functionnames must be given as
  "classname::functioname" (ie without return type, parenthesis or
  argument types or names).  If a debug message is posted from one of
  those functions, your program will be stopped (using assert(0)).

  This can be useful if you want to get core-dumps or enter a debugger
  whenever for instance a warning or an error is posted from some
  function (e.g. "SbVec3f::normalize"). This feature is only enabled
  in the debug version of Coin.
*/

// *************************************************************************

#include <Inventor/errors/SoDebugError.h>

#include <cassert>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <Inventor/C/errors/debugerror.h>
#include <Inventor/C/tidbits.h>
#include <Inventor/SbName.h>
#include <Inventor/SoType.h>
#include <Inventor/lists/SbList.h>

#include "coindefs.h"
#include "tidbitsp.h"

#ifndef COIN_WORKAROUND_NO_USING_STD_FUNCS
using std::strcmp;
using std::strlen;
using std::strcpy;
using std::strchr;
using std::memcpy;
#endif // !COIN_WORKAROUND_NO_USING_STD_FUNCS

#ifdef HAVE_UNISTD_H
  #include <sys/types.h>
  #include <unistd.h>
#endif

#if defined(_MSC_VER)
  #if _MSC_VER >= COIN_MSVC_8_0_VERSION
    #include <intrin.h>
  #endif
  #if _MSC_VER >= COIN_MSVC_7_0_VERSION
    #define COIN_DEBUGGER_BREAK(x) __debugbreak()
  #else
    #define COIN_DEBUGGER_BREAK(x) __asm { int 3 }
  #endif
#else
  #if defined (MINGW32) || defined (CYGWIN)
    #include <windows.h>
    #define COIN_DEBUGGER_BREAK(x) ::DebugBreak()
  #else
    #if defined (_POSIX_VERSION)
      #include <signal.h>
      #define COIN_DEBUGGER_BREAK(x) ::raise(SIGINT)
    #else
      #define COIN_DEBUGGER_BREAK(x) assert(0 && x)
    #endif
  #endif
#endif

// *************************************************************************

SoType SoDebugError::classTypeId STATIC_SOTYPE_INIT;
SoErrorCB * SoDebugError::callback = SoError::defaultHandlerCB;
void * SoDebugError::callbackData = NULL;

// *************************************************************************

#if COIN_DEBUG

// variables for run-time breakpoints
static int num_breakpoints = 0;
static char ** breakpoints = NULL;

extern "C" {

static void
debug_break_cleanup(void)
{
  for (int i = 0; i < num_breakpoints; i++) {
    delete[] breakpoints[i];
  }
  delete[] breakpoints;
  breakpoints = NULL;
  num_breakpoints = 0;
}

} // extern "C"

#endif // COIN_DEBUG

/*!
  \enum SoDebugError::Severity

  Specifies the available severity levels of the debug messages.
*/


// Documented for parent class.
void
SoDebugError::initClass(void)
{
  SoDebugError::callback = SoError::defaultHandlerCB;
  SoDebugError::callbackData = NULL;
  SoDebugError::classTypeId =
    SoType::createType(SoError::getClassTypeId(), "DebugError");

  // FIXME: the following "tokenizer" code could perhaps be factored
  // out and moved to e.g. SbStringList? 20030820 mortene.

#if COIN_DEBUG
  const char * env = coin_getenv("COIN_DEBUG_BREAK");
  if (env) {
    num_breakpoints = 1;
    const char * ptr = env;
    while (*ptr) {
      if (*ptr == ' ' || *ptr == ',') num_breakpoints++;
      ptr++;
    }
    breakpoints = new char*[num_breakpoints];
    coin_atexit(debug_break_cleanup, CC_ATEXIT_MSG_SUBSYSTEM);
    const size_t envstrlen = strlen(env);
    char * cpy = new char[envstrlen + 1];
    (void)strcpy(cpy, env);
    ptr = cpy;
    const char * end = strchr(ptr, ' ');
    const char * tst = strchr(ptr, ',');
    if (end == NULL || (tst && tst < end)) end = tst;
    if (end == NULL) end = strchr(ptr, '\0');
    int i = 0;
    while (end && i < num_breakpoints) {
      const ptrdiff_t len = end - ptr;
      breakpoints[i] = new char[len + 1];
      (void)memcpy(breakpoints[i], ptr, len);
      breakpoints[i][len] = '\0';
      i++;
      ptr = end+1;
      if (ptr < (cpy + envstrlen)) {
        end = strchr(ptr, ' ');
        tst = strchr(ptr, ',');
        if (end == NULL || (tst && tst < end)) end = tst;
        if (end == NULL) end = strchr(ptr, 0);
      }
    }
    num_breakpoints = i; // just in case parsing failed for some reason
    delete[] cpy;
  }
#endif // COIN_DEBUG
}

void
SoDebugError::callbackForwarder(const struct cc_debugerror * error,
                            void * COIN_UNUSED_ARG(data)
                            )
{
  SoDebugError wrappederr;

  switch (cc_debugerror_get_severity(error)) {
  case CC_DEBUGERROR_ERROR:
    wrappederr.severity = SoDebugError::ERROR;
    break;
  case CC_DEBUGERROR_WARNING:
    wrappederr.severity = SoDebugError::WARNING;
    break;
  case CC_DEBUGERROR_INFO:
    wrappederr.severity = SoDebugError::INFO;
    break;
  default:
    assert(FALSE);
    break;
  }

  const cc_string * dbgstr =
    cc_error_get_debug_string(reinterpret_cast<const cc_error *>(error));
  const char * dbgstrc = cc_string_get_text(dbgstr);
  wrappederr.setDebugString(dbgstrc);

  assert(SoDebugError::callback != NULL);
  SoDebugError::callback(&wrappederr, SoDebugError::callbackData);
}

// Documented for parent class.
void
SoDebugError::setHandlerCallback(SoErrorCB * const function, void * const data)
{
  if (SoDebugError::callback == SoError::defaultHandlerCB) {
    // The user is overriding the default handler, so set up a
    // "converter" callback function that makes an SoDebugError out of
    // an cc_debugerror and forwards control to the callback function
    // given as an argument to setHandlerCallback().
    cc_debugerror_set_handler_callback(
       reinterpret_cast<cc_debugerror_cb *>(SoDebugError::callbackForwarder), NULL);
  }

  SoDebugError::callback = function;
  SoDebugError::callbackData = data;
}

// Documented for parent class.
SoErrorCB *
SoDebugError::getHandlerCallback(void)
{
  return SoDebugError::callback;
}

// Documented for parent class.
void *
SoDebugError::getHandlerData(void)
{
  return SoDebugError::callbackData;
}

// Documented for parent class.
SoType
SoDebugError::getClassTypeId(void)
{
  return SoDebugError::classTypeId;
}

// Documented for parent class.
SoType
SoDebugError::getTypeId(void) const
{
  return SoDebugError::classTypeId;
}

/*!
  Returns severity level of current message.

  You can use this to filter out debug messages and only let warnings
  and/or errors pass through to the end-user if you have set your own
  handler callback.

  \sa setHandlerCallback()
*/
SoDebugError::Severity
SoDebugError::getSeverity(void) const
{
  return this->severity;
}

#if COIN_DEBUG

static inline void
check_breakpoints(const char * source)
{
  for (int i = 0; i < num_breakpoints; i++) {
    if (strcmp(breakpoints[i], source) == 0) {
      COIN_DEBUGGER_BREAK("Coin debug break");
    }
  }
}

#else // COIN_DEBUG

static inline void
check_breakpoints(const char *)
{
  // should be empty
}

#endif // ! COIN_DEBUG


void
SoDebugError::commonPostHandling(Severity severity, const char * type,
                                 const char * source, const SbString & s)
{
  SoDebugError error;
  error.severity = severity;
  error.setDebugString("Coin ");
  error.appendToDebugString(type);
  error.appendToDebugString(" in ");
  error.appendToDebugString(source);
  error.appendToDebugString("(): ");
  error.appendToDebugString(s.getString());

  if (SoDebugError::callback != SoError::defaultHandlerCB) {
    SoDebugError::callback(&error, SoDebugError::callbackData);
  }
  else {
    error.handleError();
  }

  check_breakpoints(source);
}

#define SODEBUGERROR_POST(SEVERITY, TYPE) \
  va_list args; \
  va_start(args, format); \
  SbString s; \
  s.vsprintf(format, args); \
  va_end(args); \
 \
  SoDebugError::commonPostHandling(SEVERITY, TYPE, source, s)


/*!
  This method posts a message with severity level "ERROR". \a source should
  be the name of the calling function.
*/
void
SoDebugError::post(const char * const source, const char * const format, ...)
{
  SODEBUGERROR_POST(ERROR, "error");
}

/*!
  This method posts a message with severity level "WARNING". \a source should
  be the name of the calling function.
*/
void
SoDebugError::postWarning(const char * const source, const char * const format, ...)
{
  SODEBUGERROR_POST(WARNING, "warning");
}

/*!
  This method posts a message with severity level "INFO". \a source should
  be the name of the calling function.
*/
void
SoDebugError::postInfo(const char * const source, const char * const format, ...)
{
  SODEBUGERROR_POST(INFO, "info");
}

// Documented for parent class.
SoErrorCB *
SoDebugError::getHandler(void * & data) const
{
  // FIXME: why the heck isn't this a static method? Looks to me like
  // an API bug. 20020526 mortene.

  data = SoDebugError::callbackData;
  return SoDebugError::callback;
}

#undef SODEBUGERROR_POST
#undef COIN_DEBUGGER_BREAK