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
|
/**********************************************************************
* $Id: cpl_error.c,v 1.3 2005/06/03 03:49:59 daniel Exp $
*
* Name: cpl_error.cpp
* Project: CPL - Common Portability Library
* Purpose: Error handling functions.
* Author: Daniel Morissette, dmorissette@dmsolutions.ca
*
**********************************************************************
* Copyright (c) 1998, Daniel Morissette
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
**********************************************************************
*
* $Log: cpl_error.c,v $
* Revision 1.3 2005/06/03 03:49:59 daniel
* Update email address, website url, and copyright dates
*
* Revision 1.2 1999/11/23 04:18:45 daniel
* Fixed var. initialization that failed to compile in C
*
* Revision 1.1 1999/08/27 14:09:55 daniel
* Update CPL files
*
* Revision 1.8 1999/07/23 14:27:47 warmerda
* CPLSetErrorHandler returns old handler
*
* Revision 1.7 1999/06/27 16:50:52 warmerda
* added support for CPL_DEBUG and CPL_LOG variables
*
* Revision 1.6 1999/06/26 02:46:11 warmerda
* Fixed initialization of debug messages.
*
* Revision 1.5 1999/05/20 14:59:05 warmerda
* added CPLDebug()
*
* Revision 1.4 1999/05/20 02:54:38 warmerda
* Added API documentation
*
* Revision 1.3 1998/12/15 19:02:27 warmerda
* Avoid use of errno as a variable
*
* Revision 1.2 1998/12/06 02:52:52 warmerda
* Implement assert support
*
* Revision 1.1 1998/12/03 18:26:02 warmerda
* New
*
**********************************************************************/
#include "cpl_error.h"
#include "cpl_vsi.h"
/* static buffer to store the last error message. We'll assume that error
* messages cannot be longer than 2000 chars... which is quite reasonable
* (that's 25 lines of 80 chars!!!)
*/
static char gszCPLLastErrMsg[2000] = "";
static int gnCPLLastErrNo = 0;
static void CPLDefaultErrorHandler( CPLErr, int, const char *);
static CPLErrorHandler gpfnCPLErrorHandler = CPLDefaultErrorHandler;
/**********************************************************************
* CPLError()
**********************************************************************/
/**
* Report an error.
*
* This function reports an error in a manner that can be hooked
* and reported appropriate by different applications.
*
* The effect of this function can be altered by applications by installing
* a custom error handling using CPLSetErrorHandler().
*
* The eErrClass argument can have the value CE_Warning indicating that the
* message is an informational warning, CE_Failure indicating that the
* action failed, but that normal recover mechanisms will be used or
* CE_Fatal meaning that a fatal error has occured, and that CPLError()
* should not return.
*
* The default behaviour of CPLError() is to report errors to stderr,
* and to abort() after reporting a CE_Fatal error. It is expected that
* some applications will want to supress error reporting, and will want to
* install a C++ exception, or longjmp() approach to no local fatal error
* recovery.
*
* Regardless of how application error handlers or the default error
* handler choose to handle an error, the error number, and message will
* be stored for recovery with CPLGetLastErrorNo() and CPLGetLastErrorMsg().
*
* @param eErrClass one of CE_Warning, CE_Failure or CE_Fatal.
* @param err_no the error number (CPLE_*) from cpl_error.h.
* @param fmt a printf() style format string. Any additional arguments
* will be treated as arguments to fill in this format in a manner
* similar to printf().
*/
void CPLError(CPLErr eErrClass, int err_no, const char *fmt, ...)
{
va_list args;
/* Expand the error message
*/
va_start(args, fmt);
vsprintf(gszCPLLastErrMsg, fmt, args);
va_end(args);
/* If the user provided his own error handling function, then call
* it, otherwise print the error to stderr and return.
*/
gnCPLLastErrNo = err_no;
if( gpfnCPLErrorHandler )
gpfnCPLErrorHandler(eErrClass, err_no, gszCPLLastErrMsg);
if( eErrClass == CE_Fatal )
abort();
}
/************************************************************************/
/* CPLDebug() */
/************************************************************************/
/**
* Display a debugging message.
*
* The category argument is used in conjunction with the CPL_DEBUG
* environment variable to establish if the message should be displayed.
* If the CPL_DEBUG environment variable is not set, no debug messages
* are emitted (use CPLError(CE_Warning,...) to ensure messages are displayed).
* If CPL_DEBUG is set, but is an empty string or the word "ON" then all
* debug messages are shown. Otherwise only messages whose category appears
* somewhere within the CPL_DEBUG value are displayed (as determinted by
* strstr()).
*
* Categories are usually an identifier for the subsystem producing the
* error. For instance "GDAL" might be used for the GDAL core, and "TIFF"
* for messages from the TIFF translator.
*
* @param pszCategory name of the debugging message category.
* @param pszFormat printf() style format string for message to display.
* Remaining arguments are assumed to be for format.
*/
void CPLDebug( const char * pszCategory, const char * pszFormat, ... )
{
char *pszMessage;
va_list args;
const char *pszDebug = getenv("CPL_DEBUG");
/* -------------------------------------------------------------------- */
/* Does this message pass our current criteria? */
/* -------------------------------------------------------------------- */
if( pszDebug == NULL )
return;
if( !EQUAL(pszDebug,"ON") && !EQUAL(pszDebug,"") )
{
int i, nLen = strlen(pszCategory);
for( i = 0; pszDebug[i] != '\0'; i++ )
{
if( EQUALN(pszCategory,pszDebug+i,nLen) )
break;
}
if( pszDebug[i] == '\0' )
return;
}
/* -------------------------------------------------------------------- */
/* Format the error message */
/* -------------------------------------------------------------------- */
pszMessage = (char *) VSIMalloc(25000);
if( pszMessage == NULL )
return;
strcpy( pszMessage, pszCategory );
strcat( pszMessage, ": " );
va_start(args, pszFormat);
vsprintf(pszMessage+strlen(pszMessage), pszFormat, args);
va_end(args);
/* -------------------------------------------------------------------- */
/* If the user provided his own error handling function, then call */
/* it, otherwise print the error to stderr and return. */
/* -------------------------------------------------------------------- */
if( gpfnCPLErrorHandler )
gpfnCPLErrorHandler(CE_Debug, CPLE_None, pszMessage);
VSIFree( pszMessage );
}
/**********************************************************************
* CPLErrorReset()
**********************************************************************/
/**
* Erase any traces of previous errors.
*
* This is normally used to ensure that an error which has been recovered
* from does not appear to be still in play with high level functions.
*/
void CPLErrorReset()
{
gnCPLLastErrNo = CPLE_None;
gszCPLLastErrMsg[0] = '\0';
}
/**********************************************************************
* CPLGetLastErrorNo()
**********************************************************************/
/**
* Fetch the last error number.
*
* This is the error number, not the error class.
*
* @return the error number of the last error to occur, or CPLE_None (0)
* if there are no posted errors.
*/
int CPLGetLastErrorNo()
{
return gnCPLLastErrNo;
}
/**********************************************************************
* CPLGetLastErrorMsg()
**********************************************************************/
/**
* Get the last error message.
*
* Fetches the last error message posted with CPLError(), that hasn't
* been cleared by CPLErrorReset(). The returned pointer is to an internal
* string that should not be altered or freed.
*
* @return the last error message, or NULL if there is no posted error
* message.
*/
const char* CPLGetLastErrorMsg()
{
return gszCPLLastErrMsg;
}
/************************************************************************/
/* CPLDefaultErrorHandler() */
/************************************************************************/
static void CPLDefaultErrorHandler( CPLErr eErrClass, int nError,
const char * pszErrorMsg )
{
static int bLogInit = FALSE;
static FILE * fpLog;
fpLog = stderr;
if( !bLogInit )
{
bLogInit = TRUE;
if( getenv( "CPL_LOG" ) != NULL )
{
fpLog = fopen( getenv("CPL_LOg"), "wt" );
if( fpLog == NULL )
fpLog = stderr;
}
}
if( eErrClass == CE_Debug )
fprintf( fpLog, "%s\n", pszErrorMsg );
else if( eErrClass == CE_Warning )
fprintf( fpLog, "Warning %d: %s\n", nError, pszErrorMsg );
else
fprintf( fpLog, "ERROR %d: %s\n", nError, pszErrorMsg );
fflush( fpLog );
}
/**********************************************************************
* CPLSetErrorHandler()
**********************************************************************/
/**
* Install custom error handler.
*
* Allow the library's user to specify his own error handler function.
* A valid error handler is a C function with the following prototype:
*
* <pre>
* void MyErrorHandler(CPLErr eErrClass, int errno, const char *msg)
* </pre>
*
* Pass NULL to come back to the default behavior. The default behaviour
* is to write the message to
*
* The msg will be a partially formatted error message not containing the
* "ERROR %d:" portion emitted by the default handler. Message formatting
* is handled by CPLError() before calling the handler. If the error
* handler function is passed a CE_Fatal class error and returns, then
* CPLError() will call abort(). Applications wanting to interrupt this
* fatal behaviour will have to use longjmp(), or a C++ exception to
* indirectly exit the function.
*
* @param pfnErrorHandler new error handler function.
* @return returns the previously installed error handler.
*/
CPLErrorHandler CPLSetErrorHandler( CPLErrorHandler pfnErrorHandler )
{
CPLErrorHandler pfnOldHandler = gpfnCPLErrorHandler;
gpfnCPLErrorHandler = pfnErrorHandler;
return pfnOldHandler;
}
/************************************************************************/
/* _CPLAssert() */
/* */
/* This function is called only when an assertion fails. */
/************************************************************************/
/**
* Report failure of a logical assertion.
*
* Applications would normally use the CPLAssert() macro which expands
* into code calling _CPLAssert() only if the condition fails. _CPLAssert()
* will generate a CE_Fatal error call to CPLError(), indicating the file
* name, and line number of the failed assertion, as well as containing
* the assertion itself.
*
* There is no reason for application code to call _CPLAssert() directly.
*/
void _CPLAssert( const char * pszExpression, const char * pszFile,
int iLine )
{
CPLError( CE_Fatal, CPLE_AssertionFailed,
"Assertion `%s' failed\n"
"in file `%s', line %d\n",
pszExpression, pszFile, iLine );
}
|