Skip to content

Commit

Permalink
CPLErrorOnce()/CPLDebugOnce(): append a 'Further messages of this typ…
Browse files Browse the repository at this point in the history
…e will be suppressed' suffix text
  • Loading branch information
rouault committed Jan 9, 2025
1 parent ad15a8e commit fb37e01
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions port/cpl_error.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,21 +126,36 @@ void CPL_DLL CPLError(CPLErr eErrClass, CPLErrorNum err_no,
CPL_FORMAT_STRING(const char *fmt), ...)
CPL_PRINT_FUNC_FORMAT(3, 4);

#ifdef GDAL_COMPILATION

const char CPL_DLL *CPLSPrintf(CPL_FORMAT_STRING(const char *fmt), ...)
CPL_PRINT_FUNC_FORMAT(1, 2) CPL_WARN_UNUSED_RESULT;

/** Similar to CPLError(), but only execute it once during the life-time
* of a process.
*
* @since 3.11
*/
#define CPLErrorOnce(...) \
#define CPLErrorOnce(eErrClass, err_no, ...) \
do \
{ \
static bool lbCPLErrorOnce = false; \
if (!lbCPLErrorOnce) \
{ \
lbCPLErrorOnce = true; \
CPLError(__VA_ARGS__); \
const char *lCPLErrorMsg = CPLSPrintf(__VA_ARGS__); \
const size_t lCPLErrorMsgLen = strlen(lCPLErrorMsg); \
const char *lCPLErrorMsgSuffix = \
" Further messages of this type will be suppressed."; \
if (lCPLErrorMsgLen && lCPLErrorMsg[lCPLErrorMsgLen - 1] == '.') \
CPLError((eErrClass), (err_no), "%s%s", lCPLErrorMsg, \
lCPLErrorMsgSuffix); \
else \
CPLError((eErrClass), (err_no), "%s.%s", lCPLErrorMsg, \
lCPLErrorMsgSuffix); \
} \
} while (0)
#endif

void CPL_DLL CPLErrorV(CPLErr, CPLErrorNum, const char *, va_list);
void CPL_DLL CPLEmergencyError(const char *) CPL_NO_RETURN;
Expand Down Expand Up @@ -189,6 +204,7 @@ void CPL_DLL CPL_STDCALL CPLPopErrorHandler(void);
{ \
} while (0) /* Eat all CPLDebugProgress calls. */

#ifdef GDAL_COMPILATION
/** Similar to CPLDebug(), but only execute it once during the life-time
* of a process.
*
Expand All @@ -198,28 +214,40 @@ void CPL_DLL CPL_STDCALL CPLPopErrorHandler(void);
do \
{ \
} while (0)
#endif

#else
void CPL_DLL CPLDebug(const char *, CPL_FORMAT_STRING(const char *), ...)
CPL_PRINT_FUNC_FORMAT(2, 3);
void CPL_DLL CPLDebugProgress(const char *, CPL_FORMAT_STRING(const char *),
...) CPL_PRINT_FUNC_FORMAT(2, 3);

#ifdef GDAL_COMPILATION
/** Similar to CPLDebug(), but only execute it once during the life-time
* of a process.
*
* @since 3.11
*/
#define CPLDebugOnce(...) \
#define CPLDebugOnce(category, ...) \
do \
{ \
static bool lbCPLErrorOnce = false; \
if (!lbCPLErrorOnce) \
static bool lbCPLDebugOnce = false; \
if (!lbCPLDebugOnce) \
{ \
lbCPLErrorOnce = true; \
CPLDebug(__VA_ARGS__); \
lbCPLDebugOnce = true; \
const char *lCPLDebugMsg = CPLSPrintf(__VA_ARGS__); \
const size_t lCPLErrorMsgLen = strlen(lCPLDebugMsg); \
const char *lCPLDebugMsgSuffix = \
" Further messages of this type will be suppressed."; \
if (lCPLErrorMsgLen && lCPLDebugMsg[lCPLErrorMsgLen - 1] == '.') \
CPLDebug((category), "%s%s", lCPLDebugMsg, \
lCPLDebugMsgSuffix); \
else \
CPLDebug((category), "%s.%s", lCPLDebugMsg, \
lCPLDebugMsgSuffix); \
} \
} while (0)
#endif

#endif

Expand Down

0 comments on commit fb37e01

Please sign in to comment.