opennurbs_error.cpp
Go to the documentation of this file.
00001 /* $NoKeywords: $ */
00002 /*
00003 //
00004 // Copyright (c) 1993-2012 Robert McNeel & Associates. All rights reserved.
00005 // OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert
00006 // McNeel & Associates.
00007 //
00008 // THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
00009 // ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
00010 // MERCHANTABILITY ARE HEREBY DISCLAIMED.
00011 //                              
00012 // For complete openNURBS copyright information see <http://www.opennurbs.org>.
00013 //
00015 */
00016 
00017 #include "pcl/surface/3rdparty/opennurbs/opennurbs.h"
00018 
00019 // openNURBS Geometry Library Errors and Warnings
00020 //
00021 //   If an error condition occurs during a openNURBS Geometry Library
00022 //   computation, the ON_Error() function is called, the computation is
00023 //   stopped, and an error code (negative integer ) is returned.  If a
00024 //   warning condition occurs during a Trout Lake Geometry Library 
00025 //   computation, the ON_Warning() function is called and the computation
00026 //   continues.
00027 //
00028 //   ON_GetErrorCount()
00029 //   ON_GetWarningCount()
00030 //   ON_Error()
00031 //   ON_Warning()
00032 //
00033 
00034 static int ON_ERROR_COUNT = 0;
00035 static int ON_WARNING_COUNT = 0;
00036 static int ON_MATH_ERROR_COUNT = 0;
00037 static int ON_DEBUG_ERROR_MESSAGE_OPTION = 0; 
00038 
00039 int ON_GetErrorCount(void)
00040 {
00041   return ON_ERROR_COUNT;        
00042 }       
00043 
00044 
00045 int ON_GetWarningCount(void)
00046 {
00047   return ON_WARNING_COUNT;      
00048 }       
00049 
00050 int ON_GetMathErrorCount(void)
00051 {
00052   return ON_MATH_ERROR_COUNT;   
00053 }       
00054 
00055 int ON_GetDebugErrorMessage(void)
00056 {
00057   return ON_DEBUG_ERROR_MESSAGE_OPTION?true:false;
00058 }
00059 
00060 
00061 void ON_EnableDebugErrorMessage( int bEnableDebugErrorMessage )
00062 {
00063   ON_DEBUG_ERROR_MESSAGE_OPTION = bEnableDebugErrorMessage ? 1 : 0;
00064 }
00065 
00066 // The sMessage[] string is used by ON_Error() and ON_Warning() 
00067 // to hold the message.  The static function ON_FormatMessage() 
00068 // is used to do most of the actual formatting.  
00069 
00070 #define MAX_MSG_LENGTH 2048
00071 static char sMessage[MAX_MSG_LENGTH];
00072 static bool ON_FormatMessage(const char*, va_list );
00073 
00074 void ON_MathError( 
00075         const char* sModuleName,
00076         const char* sErrorType,
00077         const char* sFunctionName
00078         )
00079 {
00080   ON_MATH_ERROR_COUNT++; // <- Good location for a debugger breakpoint.
00081 
00082   if ( !sModuleName)
00083     sModuleName = "";
00084   if ( !sErrorType )
00085     sErrorType = "";
00086   if ( !sFunctionName )
00087     sFunctionName = "";
00088 
00089   ON_Error(__FILE__,__LINE__,
00090            "Math library or floating point ERROR # %d module=%s type=%s function=%s",
00091            ON_MATH_ERROR_COUNT, 
00092            sModuleName, // rhino.exe, opennurbs.dll, etc.
00093            sErrorType,   
00094            sFunctionName 
00095            );
00096 }       
00097 
00098 static void ON_IncrementErrorCount()
00099 {
00100   ON_ERROR_COUNT++;
00101 }
00102 
00103 static void ON_IncrementWarningCount()
00104 {
00105   ON_WARNING_COUNT++;
00106 }
00107 
00108 bool ON_IsNotValid()
00109 {
00110   return false;
00111 }
00112 
00113 static bool ON_PrintErrorHeader(
00114     int type, // 0 = warning, 1 = error, 2 = assert
00115     const char* sFileName, 
00116     int line_number,
00117     const char* sFunctionName
00118     )
00119 {
00120   bool bPrintMessage = false;
00121   sMessage[0] = 0;
00122 
00123 #if defined(ON_COMPILER_MSC)
00124   // use sprintf_s() ...
00125   size_t sz = (sizeof(sMessage)/sizeof(sMessage[0])) - 1;
00126 #define ON_SPRINTF4(s,count,fname,ln,func) sprintf_s(sMessage,sz,s,count,fname,ln,func)
00127 #define ON_SPRINTF3(s,count,fname,ln) sprintf_s(sMessage,sz,s,count,fname,ln)
00128 #define ON_SPRINTF1(s,count) sprintf_s(sMessage,sz,s,count)
00129 #else
00130   // use sprintf() ...
00131 #define ON_SPRINTF4(s,count,fname,ln,func) sprintf(sMessage,s,count,fname,ln,func)
00132 #define ON_SPRINTF3(s,count,fname,ln) sprintf(sMessage,s,count,fname,ln)
00133 #define ON_SPRINTF1(s,count) sprintf(sMessage,s,count)
00134 #endif
00135 
00136   if ( ON_DEBUG_ERROR_MESSAGE_OPTION )
00137   {
00138     if ( 0 == type )
00139     {
00140       if ( ON_WARNING_COUNT < 50 )
00141       {
00142         if (0 == sFileName )
00143           sFileName = "";
00144         if ( sFunctionName && sFunctionName[0] )
00145           ON_SPRINTF4("openNURBS WARNING # %d %s.%d %s(): ",ON_WARNING_COUNT,sFileName,line_number,sFunctionName);
00146         else
00147           ON_SPRINTF3("openNURBS WARNING # %d %s.%d ",ON_WARNING_COUNT,sFileName,line_number);
00148         bPrintMessage = true;
00149       }
00150       else if ( 50 == ON_ERROR_COUNT )
00151       {
00152         ON_SPRINTF1("openNURBS WARNING # %d - Too many warnings.  No more printed messages.",ON_WARNING_COUNT);
00153         bPrintMessage = true;
00154       }
00155     }
00156     else if ( 1 == type || 2 == type )
00157     {
00158       if ( ON_ERROR_COUNT < 50 )
00159       {
00160         if (0 == sFileName )
00161           sFileName = "";
00162         if ( sFunctionName && sFunctionName[0] )
00163           ON_SPRINTF4("openNURBS ERROR # %d %s.%d %s(): ",ON_ERROR_COUNT,sFileName,line_number,sFunctionName);
00164         else
00165           ON_SPRINTF3("openNURBS ERROR # %d %s.%d ",ON_ERROR_COUNT,sFileName,line_number);
00166         bPrintMessage = true;
00167       }
00168       else if ( 50 == ON_ERROR_COUNT )
00169       {
00170         ON_SPRINTF1("openNURBS ERROR # %d - Too many errors.  No more printed messages.",ON_ERROR_COUNT);
00171         bPrintMessage = true;
00172       }
00173     }
00174   }
00175 
00176 #undef ON_SPRINTF4
00177 #undef ON_SPRINTF3
00178 #undef ON_SPRINTF1
00179 
00180   return bPrintMessage;
00181 }
00182 
00183 void ON_Error(const char* sFileName, int line_number, 
00184               const char* sFormat, ...)
00185 {
00186   ON_IncrementErrorCount();
00187 
00188   bool bPrintMessage = ON_PrintErrorHeader(1,sFileName,line_number,0);
00189 
00190   if ( bPrintMessage )
00191   {
00192     if (sFormat && sFormat[0]) 
00193     {
00194       // append formatted error message to sMessage[]
00195       va_list args;
00196       va_start(args, sFormat);
00197       bPrintMessage = ON_FormatMessage(sFormat,args);
00198       va_end(args);
00199     }
00200     if ( bPrintMessage )
00201       ON_ErrorMessage(1,sMessage);
00202   }
00203 }
00204 
00205 void ON_ErrorEx(const char* sFileName, int line_number, const char* sFunctionName, 
00206               const char* sFormat, ...)
00207 {
00208   ON_IncrementErrorCount();
00209 
00210   bool bPrintMessage = ON_PrintErrorHeader(1,sFileName,line_number,sFunctionName);
00211 
00212   if ( bPrintMessage )
00213   {
00214     if (sFormat && sFormat[0]) 
00215     {
00216       // append formatted error message to sMessage[]
00217       va_list args;
00218       va_start(args, sFormat);
00219       bPrintMessage = ON_FormatMessage(sFormat,args);
00220       va_end(args);
00221     }
00222     if ( bPrintMessage )
00223       ON_ErrorMessage(1,sMessage);
00224   }
00225 }
00226 
00227 void ON_Warning(const char* sFileName, int line_number, 
00228                 const char* sFormat, ...)
00229 {
00230   ON_IncrementWarningCount();
00231 
00232   bool bPrintMessage = ON_PrintErrorHeader(0,sFileName,line_number,0);
00233 
00234   if ( bPrintMessage )
00235   {
00236     if (sFormat && sFormat[0]) 
00237     {
00238       // append formatted error message to sMessage[]
00239       va_list args;
00240       va_start(args, sFormat);
00241       bPrintMessage = ON_FormatMessage(sFormat,args);
00242       va_end(args);
00243     }
00244     if ( bPrintMessage )
00245       ON_ErrorMessage(0,sMessage);
00246   }
00247 }
00248 
00249 
00250 void ON_WarningEx(const char* sFileName, int line_number, const char* sFunctionName,
00251                 const char* sFormat, ...)
00252 {
00253   ON_IncrementWarningCount();
00254 
00255   bool bPrintMessage = ON_PrintErrorHeader(0,sFileName,line_number,sFunctionName);
00256 
00257   if ( bPrintMessage )
00258   {
00259     if (sFormat && sFormat[0]) 
00260     {
00261       // append formatted error message to sMessage[]
00262       va_list args;
00263       va_start(args, sFormat);
00264       bPrintMessage = ON_FormatMessage(sFormat,args);
00265       va_end(args);
00266     }
00267     if ( bPrintMessage )
00268       ON_ErrorMessage(0,sMessage);
00269   }
00270 }
00271 
00272 void ON_Assert(int bCondition,
00273                const char* sFileName, int line_number, 
00274                const char* sFormat, ...)
00275 {
00276   if ( !bCondition ) 
00277   {
00278     ON_IncrementErrorCount();
00279 
00280     bool bPrintMessage = ON_PrintErrorHeader(2,sFileName,line_number,0);
00281 
00282     if ( bPrintMessage )
00283     {
00284       if (sFormat && sFormat[0]) 
00285       {
00286         // append formatted error message to sMessage[]
00287         va_list args;
00288         va_start(args, sFormat);
00289         bPrintMessage = ON_FormatMessage(sFormat,args);
00290         va_end(args);
00291       }
00292       if ( bPrintMessage )
00293         ON_ErrorMessage(2,sMessage);
00294     }
00295   }
00296 }
00297 
00298 void ON_AssertEx(int bCondition,
00299                const char* sFileName, int line_number, const char* sFunctionName,
00300                const char* sFormat, ...)
00301 {
00302   if ( !bCondition ) 
00303   {
00304     ON_IncrementErrorCount();
00305 
00306     bool bPrintMessage = ON_PrintErrorHeader(2,sFileName,line_number,sFunctionName);
00307 
00308     if ( bPrintMessage )
00309     {
00310       if (sFormat && sFormat[0]) 
00311       {
00312         // append formatted error message to sMessage[]
00313         va_list args;
00314         va_start(args, sFormat);
00315         bPrintMessage = ON_FormatMessage(sFormat,args);
00316         va_end(args);
00317       }
00318       if ( bPrintMessage )
00319         ON_ErrorMessage(2,sMessage);
00320     }
00321   }
00322 }
00323 
00324 static bool ON_FormatMessage(const char* format, va_list args)
00325 {
00326   // appends formatted message to sMessage[]
00327   int len = ((int)strlen(sMessage));
00328   if (len < 0 )
00329     return false;
00330   if (MAX_MSG_LENGTH-1-len < 2)
00331     return false;
00332   sMessage[MAX_MSG_LENGTH-1] = 0;
00333   on_vsnprintf(sMessage+len, MAX_MSG_LENGTH-1-len, format, args);
00334   return true;
00335 }       


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:27:00