-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathopennurbs_error.cpp
More file actions
335 lines (292 loc) · 8.84 KB
/
opennurbs_error.cpp
File metadata and controls
335 lines (292 loc) · 8.84 KB
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
/* $NoKeywords: $ */
/*
//
// Copyright (c) 1993-2012 Robert McNeel & Associates. All rights reserved.
// OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert
// McNeel & Associates.
//
// THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
// ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
// MERCHANTABILITY ARE HEREBY DISCLAIMED.
//
// For complete openNURBS copyright information see <http://www.opennurbs.org>.
//
////////////////////////////////////////////////////////////////
*/
#include "opennurbs.h"
// openNURBS Geometry Library Errors and Warnings
//
// If an error condition occurs during a openNURBS Geometry Library
// computation, the ON_Error() function is called, the computation is
// stopped, and an error code (negative integer ) is returned. If a
// warning condition occurs during a Trout Lake Geometry Library
// computation, the ON_Warning() function is called and the computation
// continues.
//
// ON_GetErrorCount()
// ON_GetWarningCount()
// ON_Error()
// ON_Warning()
//
static int ON_ERROR_COUNT = 0;
static int ON_WARNING_COUNT = 0;
static int ON_MATH_ERROR_COUNT = 0;
static int ON_DEBUG_ERROR_MESSAGE_OPTION = 0;
int ON_GetErrorCount(void)
{
return ON_ERROR_COUNT;
}
int ON_GetWarningCount(void)
{
return ON_WARNING_COUNT;
}
int ON_GetMathErrorCount(void)
{
return ON_MATH_ERROR_COUNT;
}
int ON_GetDebugErrorMessage(void)
{
return ON_DEBUG_ERROR_MESSAGE_OPTION?true:false;
}
void ON_EnableDebugErrorMessage( int bEnableDebugErrorMessage )
{
ON_DEBUG_ERROR_MESSAGE_OPTION = bEnableDebugErrorMessage ? 1 : 0;
}
// The sMessage[] string is used by ON_Error() and ON_Warning()
// to hold the message. The static function ON_FormatMessage()
// is used to do most of the actual formatting.
#define MAX_MSG_LENGTH 2048
static char sMessage[MAX_MSG_LENGTH];
static bool ON_FormatMessage(const char*, va_list );
void ON_MathError(
const char* sModuleName,
const char* sErrorType,
const char* sFunctionName
)
{
ON_MATH_ERROR_COUNT++; // <- Good location for a debugger breakpoint.
if ( !sModuleName)
sModuleName = "";
if ( !sErrorType )
sErrorType = "";
if ( !sFunctionName )
sFunctionName = "";
ON_Error(__FILE__,__LINE__,
"Math library or floating point ERROR # %d module=%s type=%s function=%s",
ON_MATH_ERROR_COUNT,
sModuleName, // rhino.exe, opennurbs.dll, etc.
sErrorType,
sFunctionName
);
}
static void ON_IncrementErrorCount()
{
ON_ERROR_COUNT++;
}
static void ON_IncrementWarningCount()
{
ON_WARNING_COUNT++;
}
bool ON_IsNotValid()
{
return false;
}
static bool ON_PrintErrorHeader(
int type, // 0 = warning, 1 = error, 2 = assert
const char* sFileName,
int line_number,
const char* sFunctionName
)
{
bool bPrintMessage = false;
sMessage[0] = 0;
#if defined(ON_COMPILER_MSC)
// use sprintf_s() ...
size_t sz = (sizeof(sMessage)/sizeof(sMessage[0])) - 1;
#define ON_SPRINTF4(s,count,fname,ln,func) sprintf_s(sMessage,sz,s,count,fname,ln,func)
#define ON_SPRINTF3(s,count,fname,ln) sprintf_s(sMessage,sz,s,count,fname,ln)
#define ON_SPRINTF1(s,count) sprintf_s(sMessage,sz,s,count)
#else
// use sprintf() ...
#define ON_SPRINTF4(s,count,fname,ln,func) sprintf(sMessage,s,count,fname,ln,func)
#define ON_SPRINTF3(s,count,fname,ln) sprintf(sMessage,s,count,fname,ln)
#define ON_SPRINTF1(s,count) sprintf(sMessage,s,count)
#endif
if ( ON_DEBUG_ERROR_MESSAGE_OPTION )
{
if ( 0 == type )
{
if ( ON_WARNING_COUNT < 50 )
{
if (0 == sFileName )
sFileName = "";
if ( sFunctionName && sFunctionName[0] )
ON_SPRINTF4("openNURBS WARNING # %d %s.%d %s(): ",ON_WARNING_COUNT,sFileName,line_number,sFunctionName);
else
ON_SPRINTF3("openNURBS WARNING # %d %s.%d ",ON_WARNING_COUNT,sFileName,line_number);
bPrintMessage = true;
}
else if ( 50 == ON_ERROR_COUNT )
{
ON_SPRINTF1("openNURBS WARNING # %d - Too many warnings. No more printed messages.",ON_WARNING_COUNT);
bPrintMessage = true;
}
}
else if ( 1 == type || 2 == type )
{
if ( ON_ERROR_COUNT < 50 )
{
if (0 == sFileName )
sFileName = "";
if ( sFunctionName && sFunctionName[0] )
ON_SPRINTF4("openNURBS ERROR # %d %s.%d %s(): ",ON_ERROR_COUNT,sFileName,line_number,sFunctionName);
else
ON_SPRINTF3("openNURBS ERROR # %d %s.%d ",ON_ERROR_COUNT,sFileName,line_number);
bPrintMessage = true;
}
else if ( 50 == ON_ERROR_COUNT )
{
ON_SPRINTF1("openNURBS ERROR # %d - Too many errors. No more printed messages.",ON_ERROR_COUNT);
bPrintMessage = true;
}
}
}
#undef ON_SPRINTF4
#undef ON_SPRINTF3
#undef ON_SPRINTF1
return bPrintMessage;
}
void ON_Error(const char* sFileName, int line_number,
const char* sFormat, ...)
{
ON_IncrementErrorCount();
bool bPrintMessage = ON_PrintErrorHeader(1,sFileName,line_number,0);
if ( bPrintMessage )
{
if (sFormat && sFormat[0])
{
// append formatted error message to sMessage[]
va_list args;
va_start(args, sFormat);
bPrintMessage = ON_FormatMessage(sFormat,args);
va_end(args);
}
if ( bPrintMessage )
ON_ErrorMessage(1,sMessage);
}
}
void ON_ErrorEx(const char* sFileName, int line_number, const char* sFunctionName,
const char* sFormat, ...)
{
ON_IncrementErrorCount();
bool bPrintMessage = ON_PrintErrorHeader(1,sFileName,line_number,sFunctionName);
if ( bPrintMessage )
{
if (sFormat && sFormat[0])
{
// append formatted error message to sMessage[]
va_list args;
va_start(args, sFormat);
bPrintMessage = ON_FormatMessage(sFormat,args);
va_end(args);
}
if ( bPrintMessage )
ON_ErrorMessage(1,sMessage);
}
}
void ON_Warning(const char* sFileName, int line_number,
const char* sFormat, ...)
{
ON_IncrementWarningCount();
bool bPrintMessage = ON_PrintErrorHeader(0,sFileName,line_number,0);
if ( bPrintMessage )
{
if (sFormat && sFormat[0])
{
// append formatted error message to sMessage[]
va_list args;
va_start(args, sFormat);
bPrintMessage = ON_FormatMessage(sFormat,args);
va_end(args);
}
if ( bPrintMessage )
ON_ErrorMessage(0,sMessage);
}
}
void ON_WarningEx(const char* sFileName, int line_number, const char* sFunctionName,
const char* sFormat, ...)
{
ON_IncrementWarningCount();
bool bPrintMessage = ON_PrintErrorHeader(0,sFileName,line_number,sFunctionName);
if ( bPrintMessage )
{
if (sFormat && sFormat[0])
{
// append formatted error message to sMessage[]
va_list args;
va_start(args, sFormat);
bPrintMessage = ON_FormatMessage(sFormat,args);
va_end(args);
}
if ( bPrintMessage )
ON_ErrorMessage(0,sMessage);
}
}
void ON_Assert(int bCondition,
const char* sFileName, int line_number,
const char* sFormat, ...)
{
if ( !bCondition )
{
ON_IncrementErrorCount();
bool bPrintMessage = ON_PrintErrorHeader(2,sFileName,line_number,0);
if ( bPrintMessage )
{
if (sFormat && sFormat[0])
{
// append formatted error message to sMessage[]
va_list args;
va_start(args, sFormat);
bPrintMessage = ON_FormatMessage(sFormat,args);
va_end(args);
}
if ( bPrintMessage )
ON_ErrorMessage(2,sMessage);
}
}
}
void ON_AssertEx(int bCondition,
const char* sFileName, int line_number, const char* sFunctionName,
const char* sFormat, ...)
{
if ( !bCondition )
{
ON_IncrementErrorCount();
bool bPrintMessage = ON_PrintErrorHeader(2,sFileName,line_number,sFunctionName);
if ( bPrintMessage )
{
if (sFormat && sFormat[0])
{
// append formatted error message to sMessage[]
va_list args;
va_start(args, sFormat);
bPrintMessage = ON_FormatMessage(sFormat,args);
va_end(args);
}
if ( bPrintMessage )
ON_ErrorMessage(2,sMessage);
}
}
}
static bool ON_FormatMessage(const char* format, va_list args)
{
// appends formatted message to sMessage[]
int len = ((int)strlen(sMessage));
if (len < 0 )
return false;
if (MAX_MSG_LENGTH-1-len < 2)
return false;
sMessage[MAX_MSG_LENGTH-1] = 0;
on_vsnprintf(sMessage+len, MAX_MSG_LENGTH-1-len, format, args);
return true;
}