LCOV - code coverage report
Current view: top level - port - cpl_error.h (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 15 15 100.0 %
Date: 2025-05-24 03:54:53 Functions: 5 6 83.3 %

          Line data    Source code
       1             : /**********************************************************************
       2             :  *
       3             :  * Name:     cpl_error.h
       4             :  * Project:  CPL - Common Portability Library
       5             :  * Purpose:  CPL Error handling
       6             :  * Author:   Daniel Morissette, danmo@videotron.ca
       7             :  *
       8             :  **********************************************************************
       9             :  * Copyright (c) 1998, Daniel Morissette
      10             :  *
      11             :  * SPDX-License-Identifier: MIT
      12             :  ****************************************************************************/
      13             : 
      14             : #ifndef CPL_ERROR_H_INCLUDED
      15             : #define CPL_ERROR_H_INCLUDED
      16             : 
      17             : #include "cpl_port.h"
      18             : 
      19             : #include <stdarg.h>
      20             : #include <stdbool.h>
      21             : #include <stddef.h>
      22             : 
      23             : /*=====================================================================
      24             :                    Error handling functions (cpl_error.c)
      25             :  =====================================================================*/
      26             : 
      27             : /**
      28             :  * \file cpl_error.h
      29             :  *
      30             :  * CPL error handling services.
      31             :  */
      32             : 
      33             : CPL_C_START
      34             : 
      35             : /** Error category */
      36             : typedef enum
      37             : {
      38             :     CE_None = 0,
      39             :     CE_Debug = 1,
      40             :     CE_Warning = 2,
      41             :     CE_Failure = 3,
      42             :     CE_Fatal = 4
      43             : } CPLErr;
      44             : 
      45             : /* ==================================================================== */
      46             : /*      Well known error codes.                                         */
      47             : /* ==================================================================== */
      48             : 
      49             : #ifdef STRICT_CPLERRORNUM_TYPE
      50             : 
      51             : /* This is not appropriate for the general case, as there are parts */
      52             : /* of GDAL which use custom error codes, but this can help diagnose confusions
      53             :  */
      54             : /* between CPLErr and CPLErrorNum */
      55             : typedef enum
      56             : {
      57             :     CPLE_None,
      58             :     CPLE_AppDefined,
      59             :     CPLE_OutOfMemory,
      60             :     CPLE_FileIO,
      61             :     CPLE_OpenFailed,
      62             :     CPLE_IllegalArg,
      63             :     CPLE_NotSupported,
      64             :     CPLE_AssertionFailed,
      65             :     CPLE_NoWriteAccess,
      66             :     CPLE_UserInterrupt,
      67             :     CPLE_ObjectNull,
      68             :     CPLE_HttpResponse,
      69             :     CPLE_AWSBucketNotFound,
      70             :     CPLE_AWSObjectNotFound,
      71             :     CPLE_AWSAccessDenied,
      72             :     CPLE_AWSInvalidCredentials,
      73             :     CPLE_AWSSignatureDoesNotMatch,
      74             : } CPLErrorNum;
      75             : 
      76             : #else
      77             : 
      78             : /** Error number */
      79             : typedef int CPLErrorNum;
      80             : 
      81             : /** No error */
      82             : #define CPLE_None 0
      83             : /** Application defined error */
      84             : #define CPLE_AppDefined 1
      85             : /** Out of memory error */
      86             : #define CPLE_OutOfMemory 2
      87             : /** File I/O error */
      88             : #define CPLE_FileIO 3
      89             : /** Open failed */
      90             : #define CPLE_OpenFailed 4
      91             : /** Illegal argument */
      92             : #define CPLE_IllegalArg 5
      93             : /** Not supported */
      94             : #define CPLE_NotSupported 6
      95             : /** Assertion failed */
      96             : #define CPLE_AssertionFailed 7
      97             : /** No write access */
      98             : #define CPLE_NoWriteAccess 8
      99             : /** User interrupted */
     100             : #define CPLE_UserInterrupt 9
     101             : /** NULL object */
     102             : #define CPLE_ObjectNull 10
     103             : 
     104             : /*
     105             :  * Filesystem-specific errors
     106             :  */
     107             : /** HTTP response */
     108             : #define CPLE_HttpResponse 11
     109             : /** AWSBucketNotFound */
     110             : #define CPLE_AWSBucketNotFound 12
     111             : /** AWSObjectNotFound */
     112             : #define CPLE_AWSObjectNotFound 13
     113             : /** AWSAccessDenied */
     114             : #define CPLE_AWSAccessDenied 14
     115             : /** AWSInvalidCredentials */
     116             : #define CPLE_AWSInvalidCredentials 15
     117             : /** AWSSignatureDoesNotMatch */
     118             : #define CPLE_AWSSignatureDoesNotMatch 16
     119             : /** VSIE_AWSError */
     120             : #define CPLE_AWSError 17
     121             : 
     122             : /* 100 - 299 reserved for GDAL */
     123             : 
     124             : #endif
     125             : 
     126             : void CPL_DLL CPLError(CPLErr eErrClass, CPLErrorNum err_no,
     127             :                       CPL_FORMAT_STRING(const char *fmt), ...)
     128             :     CPL_PRINT_FUNC_FORMAT(3, 4);
     129             : 
     130             : #ifdef GDAL_COMPILATION
     131             : 
     132             : const char CPL_DLL *CPLSPrintf(CPL_FORMAT_STRING(const char *fmt), ...)
     133             :     CPL_PRINT_FUNC_FORMAT(1, 2) CPL_WARN_UNUSED_RESULT;
     134             : 
     135             : /** Similar to CPLError(), but only execute it once during the life-time
     136             :  * of a process.
     137             :  *
     138             :  * @since 3.11
     139             :  */
     140             : #define CPLErrorOnce(eErrClass, err_no, ...)                                   \
     141             :     do                                                                         \
     142             :     {                                                                          \
     143             :         static bool lbCPLErrorOnce = false;                                    \
     144             :         if (!lbCPLErrorOnce)                                                   \
     145             :         {                                                                      \
     146             :             lbCPLErrorOnce = true;                                             \
     147             :             const char *lCPLErrorMsg = CPLSPrintf(__VA_ARGS__);                \
     148             :             const size_t lCPLErrorMsgLen = strlen(lCPLErrorMsg);               \
     149             :             const char *lCPLErrorMsgSuffix =                                   \
     150             :                 " Further messages of this type will be suppressed.";          \
     151             :             if (lCPLErrorMsgLen && lCPLErrorMsg[lCPLErrorMsgLen - 1] == '.')   \
     152             :                 CPLError((eErrClass), (err_no), "%s%s", lCPLErrorMsg,          \
     153             :                          lCPLErrorMsgSuffix);                                  \
     154             :             else                                                               \
     155             :                 CPLError((eErrClass), (err_no), "%s.%s", lCPLErrorMsg,         \
     156             :                          lCPLErrorMsgSuffix);                                  \
     157             :         }                                                                      \
     158             :     } while (0)
     159             : #endif
     160             : 
     161             : void CPL_DLL CPLErrorV(CPLErr, CPLErrorNum, const char *, va_list);
     162             : void CPL_DLL CPLEmergencyError(const char *) CPL_NO_RETURN;
     163             : void CPL_DLL CPL_STDCALL CPLErrorReset(void);
     164             : CPLErrorNum CPL_DLL CPL_STDCALL CPLGetLastErrorNo(void);
     165             : CPLErr CPL_DLL CPL_STDCALL CPLGetLastErrorType(void);
     166             : const char CPL_DLL *CPL_STDCALL CPLGetLastErrorMsg(void);
     167             : GUInt32 CPL_DLL CPL_STDCALL CPLGetErrorCounter(void);
     168             : void CPL_DLL *CPL_STDCALL CPLGetErrorHandlerUserData(void);
     169             : void CPL_DLL CPLErrorSetState(CPLErr eErrClass, CPLErrorNum err_no,
     170             :                               const char *pszMsg);
     171             : #if defined(GDAL_COMPILATION) && defined(__cplusplus)
     172             : extern "C++"
     173             : {
     174             :     void CPL_DLL CPLErrorSetState(CPLErr eErrClass, CPLErrorNum err_no,
     175             :                                   const char *pszMsg,
     176             :                                   const GUInt32 *pnErrorCounter);
     177             : }
     178             : #endif
     179             : 
     180             : void CPL_DLL CPLCallPreviousHandler(CPLErr eErrClass, CPLErrorNum err_no,
     181             :                                     const char *pszMsg);
     182             : /*! @cond Doxygen_Suppress */
     183             : void CPL_DLL CPLCleanupErrorMutex(void);
     184             : /*! @endcond */
     185             : 
     186             : /** Callback for a custom error handler */
     187             : typedef void(CPL_STDCALL *CPLErrorHandler)(CPLErr, CPLErrorNum, const char *);
     188             : 
     189             : void CPL_DLL CPL_STDCALL CPLLoggingErrorHandler(CPLErr, CPLErrorNum,
     190             :                                                 const char *);
     191             : void CPL_DLL CPL_STDCALL CPLDefaultErrorHandler(CPLErr, CPLErrorNum,
     192             :                                                 const char *);
     193             : void CPL_DLL CPL_STDCALL CPLQuietErrorHandler(CPLErr, CPLErrorNum,
     194             :                                               const char *);
     195             : void CPL_DLL CPL_STDCALL CPLQuietWarningsErrorHandler(CPLErr, CPLErrorNum,
     196             :                                                       const char *);
     197             : void CPL_DLL CPLTurnFailureIntoWarning(int bOn);
     198             : 
     199             : CPLErrorHandler CPL_DLL CPLGetErrorHandler(void **ppUserData);
     200             : 
     201             : CPLErrorHandler CPL_DLL CPL_STDCALL CPLSetErrorHandler(CPLErrorHandler);
     202             : CPLErrorHandler CPL_DLL CPL_STDCALL CPLSetErrorHandlerEx(CPLErrorHandler,
     203             :                                                          void *);
     204             : void CPL_DLL CPL_STDCALL CPLPushErrorHandler(CPLErrorHandler);
     205             : void CPL_DLL CPL_STDCALL CPLPushErrorHandlerEx(CPLErrorHandler, void *);
     206             : void CPL_DLL CPL_STDCALL CPLSetCurrentErrorHandlerCatchDebug(int bCatchDebug);
     207             : void CPL_DLL CPL_STDCALL CPLPopErrorHandler(void);
     208             : 
     209             : #ifdef WITHOUT_CPLDEBUG
     210             : #define CPLDebug(...)                                                          \
     211             :     do                                                                         \
     212             :     {                                                                          \
     213             :     } while (0) /* Eat all CPLDebug calls. */
     214             : #define CPLDebugProgress(...)                                                  \
     215             :     do                                                                         \
     216             :     {                                                                          \
     217             :     } while (0) /* Eat all CPLDebugProgress calls. */
     218             : 
     219             : #ifdef GDAL_COMPILATION
     220             : /** Similar to CPLDebug(), but only execute it once during the life-time
     221             :  * of a process.
     222             :  *
     223             :  * @since 3.11
     224             :  */
     225             : #define CPLDebugOnce(...)                                                      \
     226             :     do                                                                         \
     227             :     {                                                                          \
     228             :     } while (0)
     229             : #endif
     230             : 
     231             : #else
     232             : void CPL_DLL CPLDebug(const char *, CPL_FORMAT_STRING(const char *), ...)
     233             :     CPL_PRINT_FUNC_FORMAT(2, 3);
     234             : void CPL_DLL CPLDebugProgress(const char *, CPL_FORMAT_STRING(const char *),
     235             :                               ...) CPL_PRINT_FUNC_FORMAT(2, 3);
     236             : 
     237             : #ifdef GDAL_COMPILATION
     238             : /** Similar to CPLDebug(), but only execute it once during the life-time
     239             :  * of a process.
     240             :  *
     241             :  * @since 3.11
     242             :  */
     243             : #define CPLDebugOnce(category, ...)                                            \
     244             :     do                                                                         \
     245             :     {                                                                          \
     246             :         static bool lbCPLDebugOnce = false;                                    \
     247             :         if (!lbCPLDebugOnce)                                                   \
     248             :         {                                                                      \
     249             :             lbCPLDebugOnce = true;                                             \
     250             :             const char *lCPLDebugMsg = CPLSPrintf(__VA_ARGS__);                \
     251             :             const size_t lCPLErrorMsgLen = strlen(lCPLDebugMsg);               \
     252             :             const char *lCPLDebugMsgSuffix =                                   \
     253             :                 " Further messages of this type will be suppressed.";          \
     254             :             if (lCPLErrorMsgLen && lCPLDebugMsg[lCPLErrorMsgLen - 1] == '.')   \
     255             :                 CPLDebug((category), "%s%s", lCPLDebugMsg,                     \
     256             :                          lCPLDebugMsgSuffix);                                  \
     257             :             else                                                               \
     258             :                 CPLDebug((category), "%s.%s", lCPLDebugMsg,                    \
     259             :                          lCPLDebugMsgSuffix);                                  \
     260             :         }                                                                      \
     261             :     } while (0)
     262             : #endif
     263             : 
     264             : #endif
     265             : 
     266             : #if defined(DEBUG) || defined(GDAL_DEBUG)
     267             : /** Same as CPLDebug(), but expands to nothing for non-DEBUG builds.
     268             :  * @since GDAL 3.1
     269             :  */
     270             : #define CPLDebugOnly(...) CPLDebug(__VA_ARGS__)
     271             : #else
     272             : /** Same as CPLDebug(), but expands to nothing for non-DEBUG builds.
     273             :  * @since GDAL 3.1
     274             :  */
     275             : #define CPLDebugOnly(...)                                                      \
     276             :     do                                                                         \
     277             :     {                                                                          \
     278             :     } while (0)
     279             : #endif
     280             : 
     281             : void CPL_DLL CPL_STDCALL _CPLAssert(const char *, const char *,
     282             :                                     int) CPL_NO_RETURN;
     283             : 
     284             : #if defined(DEBUG) && !defined(CPPCHECK)
     285             : /** Assert on an expression. Only enabled in DEBUG mode */
     286             : #define CPLAssert(expr)                                                        \
     287             :     ((expr) ? (void)(0) : _CPLAssert(#expr, __FILE__, __LINE__))
     288             : /** Assert on an expression in DEBUG mode. Evaluate it also in non-DEBUG mode
     289             :  * (useful to 'consume' a error return variable) */
     290             : #define CPLAssertAlwaysEval(expr) CPLAssert(expr)
     291             : #else
     292             : /** Assert on an expression. Only enabled in DEBUG mode */
     293             : #define CPLAssert(expr)                                                        \
     294             :     do                                                                         \
     295             :     {                                                                          \
     296             :     } while (0)
     297             : #ifdef __cplusplus
     298             : /** Assert on an expression in DEBUG mode. Evaluate it also in non-DEBUG mode
     299             :  * (useful to 'consume' a error return variable) */
     300             : #define CPLAssertAlwaysEval(expr) CPL_IGNORE_RET_VAL(expr)
     301             : #else
     302             : /** Assert on an expression in DEBUG mode. Evaluate it also in non-DEBUG mode
     303             :  * (useful to 'consume' a error return variable) */
     304             : #define CPLAssertAlwaysEval(expr) (void)(expr)
     305             : #endif
     306             : #endif
     307             : 
     308             : CPL_C_END
     309             : 
     310             : /*! @cond Doxygen_Suppress */
     311             : /*
     312             :  * Helper macros used for input parameters validation.
     313             :  */
     314             : #ifdef DEBUG
     315             : #define VALIDATE_POINTER_ERR CE_Fatal
     316             : #else
     317             : #define VALIDATE_POINTER_ERR CE_Failure
     318             : #endif
     319             : 
     320             : /*! @endcond */
     321             : 
     322             : #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
     323             : 
     324             : extern "C++"
     325             : {
     326             :     /*! @cond Doxygen_Suppress */
     327             :     template <class T> T *CPLAssertNotNull(T *x) CPL_RETURNS_NONNULL;
     328             : 
     329           3 :     template <class T> T *CPLAssertNotNull(T *x)
     330             :     {
     331           3 :         CPLAssert(x);
     332           3 :         return x;
     333             :     }
     334             : 
     335             : #include <memory>
     336             : #include <string>
     337             : 
     338             :     /*! @endcond */
     339             : 
     340             :     /** Class that installs a (thread-local) error handler on construction, and
     341             :      * restore the initial one on destruction.
     342             :      */
     343             :     class CPL_DLL CPLErrorHandlerPusher
     344             :     {
     345             :       public:
     346             :         /** Constructor that installs a thread-local temporary error handler
     347             :          * (typically CPLQuietErrorHandler)
     348             :          */
     349       91477 :         explicit CPLErrorHandlerPusher(CPLErrorHandler hHandler)
     350             :         {
     351       91477 :             CPLPushErrorHandler(hHandler);
     352       91479 :         }
     353             : 
     354             :         /** Constructor that installs a thread-local temporary error handler,
     355             :          * and its user data.
     356             :          */
     357             :         CPLErrorHandlerPusher(CPLErrorHandler hHandler, void *user_data)
     358             :         {
     359             :             CPLPushErrorHandlerEx(hHandler, user_data);
     360             :         }
     361             : 
     362             :         /** Destructor that restores the initial error handler. */
     363       91478 :         ~CPLErrorHandlerPusher()
     364             :         {
     365       91478 :             CPLPopErrorHandler();
     366       91477 :         }
     367             :     };
     368             : 
     369             :     /** Class that saves the error state on construction, and
     370             :      * restores it on destruction.
     371             :      */
     372             :     class CPL_DLL CPLErrorStateBackuper
     373             :     {
     374             :         CPLErrorNum m_nLastErrorNum;
     375             :         CPLErr m_nLastErrorType;
     376             :         std::string m_osLastErrorMsg;
     377             :         GUInt32 m_nLastErrorCounter;
     378             :         std::unique_ptr<CPLErrorHandlerPusher> m_poErrorHandlerPusher;
     379             : 
     380             :       public:
     381             :         /** Constructor that backs up the error state, and optionally installs
     382             :          * a thread-local temporary error handler (typically CPLQuietErrorHandler).
     383             :          */
     384             :         explicit CPLErrorStateBackuper(CPLErrorHandler hHandler = nullptr);
     385             : 
     386             :         /** Destructor that restores the error state to its initial state
     387             :          * before construction.
     388             :          */
     389             :         ~CPLErrorStateBackuper();
     390             :     };
     391             : 
     392             :     /** Class that turns errors into warning on construction, and
     393             :      *  restores the previous state on destruction.
     394             :      */
     395             :     class CPL_DLL CPLTurnFailureIntoWarningBackuper
     396             :     {
     397             :       public:
     398        5160 :         CPLTurnFailureIntoWarningBackuper()
     399             :         {
     400        5160 :             CPLTurnFailureIntoWarning(true);
     401        5160 :         }
     402             : 
     403        5160 :         ~CPLTurnFailureIntoWarningBackuper()
     404             :         {
     405        5160 :             CPLTurnFailureIntoWarning(false);
     406        5160 :         }
     407             :     };
     408             : }
     409             : 
     410             : #ifdef GDAL_COMPILATION
     411             : /*! @cond Doxygen_Suppress */
     412             : // internal only
     413             : bool CPLIsDefaultErrorHandlerAndCatchDebug();
     414             : /*! @endcond */
     415             : #endif
     416             : 
     417             : #endif
     418             : 
     419             : /** Validate that a pointer is not NULL */
     420             : #define VALIDATE_POINTER0(ptr, func)                                           \
     421             :     do                                                                         \
     422             :     {                                                                          \
     423             :         if (CPL_NULLPTR == ptr)                                                \
     424             :         {                                                                      \
     425             :             CPLErr const ret = VALIDATE_POINTER_ERR;                           \
     426             :             CPLError(ret, CPLE_ObjectNull,                                     \
     427             :                      "Pointer \'%s\' is NULL in \'%s\'.\n", #ptr, (func));     \
     428             :             return;                                                            \
     429             :         }                                                                      \
     430             :     } while (0)
     431             : 
     432             : /** Validate that a pointer is not NULL, and return rc if it is NULL */
     433             : #define VALIDATE_POINTER1(ptr, func, rc)                                       \
     434             :     do                                                                         \
     435             :     {                                                                          \
     436             :         if (CPL_NULLPTR == ptr)                                                \
     437             :         {                                                                      \
     438             :             CPLErr const ret = VALIDATE_POINTER_ERR;                           \
     439             :             CPLError(ret, CPLE_ObjectNull,                                     \
     440             :                      "Pointer \'%s\' is NULL in \'%s\'.\n", #ptr, (func));     \
     441             :             return (rc);                                                       \
     442             :         }                                                                      \
     443             :     } while (0)
     444             : 
     445             : #endif /* CPL_ERROR_H_INCLUDED */

Generated by: LCOV version 1.14