Skip to content

Commit

Permalink
Merge pull request #11606 from rouault/CPLErrorOnce
Browse files Browse the repository at this point in the history
Add CPLErrorOnce() and CPLDebugOnce()
  • Loading branch information
rouault authored Jan 9, 2025
2 parents 39cbe84 + fb37e01 commit 6713bba
Show file tree
Hide file tree
Showing 17 changed files with 111 additions and 144 deletions.
7 changes: 1 addition & 6 deletions alg/gdalwarpkernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1240,13 +1240,8 @@ CPLErr GDALWarpKernel::PerformWarp()
if (pafUnifiedSrcDensity != nullptr)
{
// Typically if there's a cutline or an alpha band
static bool bHasWarned = false;
if (!bHasWarned)
{
bHasWarned = true;
CPLDebug("WARP", "pafUnifiedSrcDensity is not null, "
CPLDebugOnce("WARP", "pafUnifiedSrcDensity is not null, "
"hence OpenCL warper cannot be used");
}
}
else
{
Expand Down
5 changes: 1 addition & 4 deletions apps/ogr2ogr_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5987,12 +5987,9 @@ SetupCT(TargetLayerInfo *psInfo, OGRLayer *poSrcLayer, bool bTransform,
}
else
{
static bool bHasWarned = false;
if (!bHasWarned)
CPLError(CE_Failure, CPLE_IllegalArg,
CPLErrorOnce(CE_Failure, CPLE_IllegalArg,
"-wrapdateline option only works when "
"reprojecting to a geographic SRS");
bHasWarned = true;
}

psInfo->m_aoReprojectionInfo[iGeom].m_aosTransformOptions.Assign(
Expand Down
10 changes: 3 additions & 7 deletions frmts/aigrid/gridlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

#include "aigrid.h"

#include <stdbool.h>

#ifndef CPL_IGNORE_RET_VAL_INT_defined
#define CPL_IGNORE_RET_VAL_INT_defined

Expand Down Expand Up @@ -766,21 +768,15 @@ CPLErr AIGReadBlock(VSILFILE *fp, GUInt32 nBlockOffset, int nBlockSize,

if (eErr == CE_Failure)
{
static int bHasWarned = FALSE;

for (i = 0; i < nBlockXSize * nBlockYSize; i++)
panData[i] = ESRI_GRID_NO_DATA;

if (!bHasWarned)
{
CPLError(CE_Warning, CPLE_AppDefined,
CPLErrorOnce(CE_Warning, CPLE_AppDefined,
"Unsupported Arc/Info Binary Grid tile of type 0x%X"
" encountered.\n"
"This and subsequent unsupported tile types set to"
" no data value.\n",
nMagic);
bHasWarned = TRUE;
}
}
}

Expand Down
7 changes: 1 addition & 6 deletions frmts/bsb/bsb_read.c
Original file line number Diff line number Diff line change
Expand Up @@ -849,12 +849,7 @@ int BSBReadScanline(BSBInfo *psInfo, int nScanline,
}
if (nRunCount > psInfo->nXSize)
{
static int bHasWarned = FALSE;
if (!bHasWarned)
{
CPLDebug("BSB", "Too big run count : %d", nRunCount);
bHasWarned = TRUE;
}
CPLDebugOnce("BSB", "Too big run count : %d", nRunCount);
}

if (iPixel + nRunCount + 1 > psInfo->nXSize)
Expand Down
7 changes: 1 addition & 6 deletions frmts/gtiff/geotiff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,11 @@ void GTIFFGetOverviewBlockSize(GDALRasterBandH hBand, int *pnBlockXSize,
if (nOvrBlockSize < 64 || nOvrBlockSize > 4096 ||
!CPLIsPowerOfTwo(nOvrBlockSize))
{
static bool bHasWarned = false;
if (!bHasWarned)
{
CPLError(CE_Warning, CPLE_NotSupported,
CPLErrorOnce(CE_Warning, CPLE_NotSupported,
"Wrong value for GDAL_TIFF_OVR_BLOCKSIZE : %s. "
"Should be a power of 2 between 64 and 4096. "
"Defaulting to 128",
pszVal);
bHasWarned = true;
}
nOvrBlockSize = 128;
}

Expand Down
7 changes: 1 addition & 6 deletions frmts/hfa/hfaband.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2028,16 +2028,11 @@ static int HFAGetOverviewBlockSize()
if (nOvrBlockSize < 32 || nOvrBlockSize > 2048 ||
!CPLIsPowerOfTwo(nOvrBlockSize))
{
static bool bHasWarned = false;
if (!bHasWarned)
{
CPLError(CE_Warning, CPLE_NotSupported,
CPLErrorOnce(CE_Warning, CPLE_NotSupported,
"Wrong value for GDAL_HFA_OVR_BLOCKSIZE : %s. "
"Should be a power of 2 between 32 and 2048. "
"Defaulting to 64",
pszVal);
bHasWarned = true;
}
nOvrBlockSize = 64;
}

Expand Down
7 changes: 1 addition & 6 deletions frmts/vrt/vrtsources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3535,13 +3535,8 @@ CPLErr VRTComplexSource::RasterIOInternal(
}
else
{
static bool bHasWarned = false;
if (!bHasWarned)
{
bHasWarned = true;
CPLError(CE_Failure, CPLE_AppDefined,
CPLErrorOnce(CE_Failure, CPLE_AppDefined,
"No entry %d.", static_cast<int>(fResult));
}
continue;
}
}
Expand Down
14 changes: 2 additions & 12 deletions gcore/gdalrasterblock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,9 @@ int CPL_STDCALL GDALGetCacheMax()
GIntBig nRes = GDALGetCacheMax64();
if (nRes > INT_MAX)
{
static bool bHasWarned = false;
if (!bHasWarned)
{
CPLError(CE_Warning, CPLE_AppDefined,
CPLErrorOnce(CE_Warning, CPLE_AppDefined,
"Cache max value doesn't fit on a 32 bit integer. "
"Call GDALGetCacheMax64() instead");
bHasWarned = true;
}
nRes = INT_MAX;
}
return static_cast<int>(nRes);
Expand Down Expand Up @@ -280,14 +275,9 @@ int CPL_STDCALL GDALGetCacheUsed()
{
if (nCacheUsed > INT_MAX)
{
static bool bHasWarned = false;
if (!bHasWarned)
{
CPLError(CE_Warning, CPLE_AppDefined,
CPLErrorOnce(CE_Warning, CPLE_AppDefined,
"Cache used value doesn't fit on a 32 bit integer. "
"Call GDALGetCacheUsed64() instead");
bHasWarned = true;
}
return INT_MAX;
}
return static_cast<int>(nCacheUsed);
Expand Down
17 changes: 4 additions & 13 deletions ogr/ogrct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1764,15 +1764,10 @@ static PJ *op_to_pj(PJ_CONTEXT *ctx, PJ *op,
const char *pszUseETMERC = CPLGetConfigOption("OSR_USE_ETMERC", nullptr);
if (pszUseETMERC && pszUseETMERC[0])
{
static bool bHasWarned = false;
if (!bHasWarned)
{
CPLError(CE_Warning, CPLE_AppDefined,
CPLErrorOnce(CE_Warning, CPLE_AppDefined,
"OSR_USE_ETMERC is a legacy configuration option, which "
"now has only effect when set to NO (YES is the default). "
"Use OSR_USE_APPROX_TMERC=YES instead");
bHasWarned = true;
}
bForceApproxTMerc = !CPLTestBool(pszUseETMERC);
}
else
Expand Down Expand Up @@ -2737,18 +2732,14 @@ int OGRProjCT::TransformWithErrorCodes(size_t nCount, double *x, double *y,
x[i] = HUGE_VAL;
y[i] = HUGE_VAL;
err = PROJ_ERR_COORD_TRANSFM_OUTSIDE_PROJECTION_DOMAIN;
static bool bHasWarned = false;
if (!bHasWarned)
{

#ifdef DEBUG
CPLError(CE_Warning, CPLE_AppDefined,
CPLErrorOnce(CE_Warning, CPLE_AppDefined,
"PROJ returned a NaN value. It should be fixed");
#else
CPLDebug("OGR_CT",
CPLDebugOnce("OGR_CT",
"PROJ returned a NaN value. It should be fixed");
#endif
bHasWarned = true;
}
}
else if (coord.xyzt.x == HUGE_VAL)
{
Expand Down
7 changes: 1 addition & 6 deletions ogr/ogrgeojsonwriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -968,13 +968,8 @@ json_object *OGRGeoJSONWriteAttributes(OGRFeature *poFeature,
{
if (!oOptions.bAllowNonFiniteValues)
{
static bool bHasWarned = false;
if (!bHasWarned)
{
bHasWarned = true;
CPLError(CE_Warning, CPLE_AppDefined,
CPLErrorOnce(CE_Warning, CPLE_AppDefined,
"NaN of Infinity value found. Skipped");
}
continue;
}
}
Expand Down
27 changes: 6 additions & 21 deletions ogr/ogrgeometryfactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4024,15 +4024,10 @@ OGRGeometry *OGRGeometryFactory::transformWithOptions(
if (poDstGeom->getSpatialReference() &&
!poDstGeom->getSpatialReference()->IsGeographic())
{
static bool bHasWarned = false;
if (!bHasWarned)
{
CPLError(
CE_Warning, CPLE_AppDefined,
"WRAPDATELINE is without effect when reprojecting to a "
"non-geographic CRS");
bHasWarned = true;
}
CPLErrorOnce(
CE_Warning, CPLE_AppDefined,
"WRAPDATELINE is without effect when reprojecting to a "
"non-geographic CRS");
return poDstGeom.release();
}
// TODO and we should probably also test that the axis order + data axis
Expand Down Expand Up @@ -4214,27 +4209,17 @@ double OGRGeometryFactory::GetDefaultArcStepSize()
constexpr double MIN_VAL = 1e-2;
if (dfVal < MIN_VAL)
{
static bool bWarned = false;
if (!bWarned)
{
bWarned = true;
CPLError(CE_Warning, CPLE_AppDefined,
CPLErrorOnce(CE_Warning, CPLE_AppDefined,
"Too small value for OGR_ARC_STEPSIZE. Clamping it to %f",
MIN_VAL);
}
return MIN_VAL;
}
constexpr double MAX_VAL = 180;
if (dfVal > MAX_VAL)
{
static bool bWarned = false;
if (!bWarned)
{
bWarned = true;
CPLError(CE_Warning, CPLE_AppDefined,
CPLErrorOnce(CE_Warning, CPLE_AppDefined,
"Too large value for OGR_ARC_STEPSIZE. Clamping it to %f",
MAX_VAL);
}
return MAX_VAL;
}
return dfVal;
Expand Down
15 changes: 5 additions & 10 deletions ogr/ogrsf_frmts/elastic/ogrelasticlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2405,16 +2405,11 @@ CPLString OGRElasticLayer::BuildJSonFromFeature(OGRFeature *poFeature)
else if (env.MinX < -180 || env.MinY < -90 || env.MaxX > 180 ||
env.MaxY > 90)
{
static bool bHasWarned = false;
if (!bHasWarned)
{
bHasWarned = true;
CPLError(
CE_Warning, CPLE_AppDefined,
"At least one geometry has a bounding box outside "
"of [-180,180] longitude range and/or [-90,90] "
"latitude range. Undefined behavior");
}
CPLErrorOnce(
CE_Warning, CPLE_AppDefined,
"At least one geometry has a bounding box outside "
"of [-180,180] longitude range and/or [-90,90] "
"latitude range. Undefined behavior");
}

std::vector<CPLString> aosPath = m_aaosGeomFieldPaths[i];
Expand Down
7 changes: 1 addition & 6 deletions ogr/ogrsf_frmts/filegdb/FGdbLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1302,16 +1302,11 @@ OGRErr FGdbLayer::PopulateRowWithFeature(Row &fgdb_row, OGRFeature *poFeature)
{
if (fldvalue < -32768 || fldvalue > 32767)
{
static int bHasWarned = FALSE;
if (!bHasWarned)
{
bHasWarned = TRUE;
CPLError(CE_Warning, CPLE_NotSupported,
CPLErrorOnce(CE_Warning, CPLE_NotSupported,
"Value %d for field %s does not fit into a "
"short and will be clamped. "
"This warning will not be emitted any more",
fldvalue, field_name.c_str());
}
if (fldvalue < -32768)
fldvalue = -32768;
else
Expand Down
14 changes: 2 additions & 12 deletions ogr/ogrsf_frmts/gmlas/ogrgmlasxsdcache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,7 @@ bool GMLASXSDCache::CacheAllGML321()
CPLHTTPDestroyResult(psResult);
if (!bSuccess)
{
static bool bHasWarned = false;
if (!bHasWarned)
{
bHasWarned = true;
CPLDebug("GMLAS", "Cannot get GML schemas from %s", pszHTTPZIP);
}
CPLDebugOnce("GMLAS", "Cannot get GML schemas from %s", pszHTTPZIP);
}
return bSuccess;
}
Expand Down Expand Up @@ -248,12 +243,7 @@ bool GMLASXSDCache::CacheAllISO20070417()
CPLHTTPDestroyResult(psResult);
if (!bSuccess)
{
static bool bHasWarned = false;
if (!bHasWarned)
{
bHasWarned = true;
CPLDebug("GMLAS", "Cannot get ISO schemas from %s", pszHTTPZIP);
}
CPLDebugOnce("GMLAS", "Cannot get ISO schemas from %s", pszHTTPZIP);
}
return bSuccess;
}
Expand Down
17 changes: 6 additions & 11 deletions ogr/ogrsf_frmts/parquet/ogrparquetwriterlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,17 +356,12 @@ bool OGRParquetWriterLayer::SetOptions(CSLConstList papszOptions,
m_eGeomEncoding = OGRArrowGeomEncoding::WKT;
else if (EQUAL(pszGeomEncoding, "GEOARROW_INTERLEAVED"))
{
static bool bHasWarned = false;
if (!bHasWarned)
{
bHasWarned = true;
CPLError(
CE_Warning, CPLE_AppDefined,
"Use of GEOMETRY_ENCODING=GEOARROW_INTERLEAVED is not "
"recommended. "
"GeoParquet 1.1 uses GEOMETRY_ENCODING=GEOARROW (struct) "
"instead.");
}
CPLErrorOnce(
CE_Warning, CPLE_AppDefined,
"Use of GEOMETRY_ENCODING=GEOARROW_INTERLEAVED is not "
"recommended. "
"GeoParquet 1.1 uses GEOMETRY_ENCODING=GEOARROW (struct) "
"instead.");
m_eGeomEncoding = OGRArrowGeomEncoding::GEOARROW_FSL_GENERIC;
}
else if (EQUAL(pszGeomEncoding, "GEOARROW") ||
Expand Down
14 changes: 2 additions & 12 deletions ogr/ogrspatialreference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11469,14 +11469,9 @@ OGRErr OGRSpatialReference::importFromProj4(const char *pszProj4)
if (osProj4.find("+init=epsg:") != std::string::npos &&
getenv("PROJ_USE_PROJ4_INIT_RULES") == nullptr)
{
static bool bHasWarned = false;
if (!bHasWarned)
{
CPLError(CE_Warning, CPLE_AppDefined,
CPLErrorOnce(CE_Warning, CPLE_AppDefined,
"+init=epsg:XXXX syntax is deprecated. It might return "
"a CRS with a non-EPSG compliant axis order.");
bHasWarned = true;
}
}
proj_context_use_proj4_init_rules(d->getPROJContext(), true);
d->setPjCRS(proj_create(d->getPROJContext(), osProj4.c_str()));
Expand Down Expand Up @@ -11571,15 +11566,10 @@ OGRErr OGRSpatialReference::exportToProj4(char **ppszProj4) const
const char *pszUseETMERC = CPLGetConfigOption("OSR_USE_ETMERC", nullptr);
if (pszUseETMERC && pszUseETMERC[0])
{
static bool bHasWarned = false;
if (!bHasWarned)
{
CPLError(CE_Warning, CPLE_AppDefined,
CPLErrorOnce(CE_Warning, CPLE_AppDefined,
"OSR_USE_ETMERC is a legacy configuration option, which "
"now has only effect when set to NO (YES is the default). "
"Use OSR_USE_APPROX_TMERC=YES instead");
bHasWarned = true;
}
bForceApproxTMerc = !CPLTestBool(pszUseETMERC);
}
else
Expand Down
Loading

0 comments on commit 6713bba

Please sign in to comment.