Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CPLErrorOnce() and CPLDebugOnce() #11606

Merged
merged 3 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
27 changes: 27 additions & 0 deletions autotest/cpp/test_ogr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4571,4 +4571,31 @@ TEST_F(test_ogr, OGRFieldDefnGetFieldTypeByName)
}
}

// Test OGRGeometryFactory::GetDefaultArcStepSize()
TEST_F(test_ogr, GetDefaultArcStepSize)
{
if (CPLGetConfigOption("OGR_ARC_STEPSIZE", nullptr) == nullptr)
{
EXPECT_EQ(OGRGeometryFactory::GetDefaultArcStepSize(), 4.0);
}
{
CPLConfigOptionSetter oSetter("OGR_ARC_STEPSIZE", "0.00001",
/* bSetOnlyIfUndefined = */ false);
CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler);
EXPECT_EQ(OGRGeometryFactory::GetDefaultArcStepSize(), 1e-2);
EXPECT_TRUE(
strstr(CPLGetLastErrorMsg(),
"Too small value for OGR_ARC_STEPSIZE. Clamping it to"));
}
{
CPLConfigOptionSetter oSetter("OGR_ARC_STEPSIZE", "190",
/* bSetOnlyIfUndefined = */ false);
CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler);
EXPECT_EQ(OGRGeometryFactory::GetDefaultArcStepSize(), 180);
EXPECT_TRUE(
strstr(CPLGetLastErrorMsg(),
"Too large value for OGR_ARC_STEPSIZE. Clamping it to"));
}
}

} // namespace
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
7 changes: 2 additions & 5 deletions ogr/gml2ogrgeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1876,9 +1876,7 @@ GML2OGRGeometry_XMLNode_Internal(const CPLXMLNode *psNode, const char *pszId,
if (bSRSUnitIsDegree && dfUOMConv > 0)
{
auto poLS = std::make_unique<OGRLineString>();
// coverity[tainted_data]
const double dfStep =
CPLAtof(CPLGetConfigOption("OGR_ARC_STEPSIZE", "4"));
const double dfStep = OGRGeometryFactory::GetDefaultArcStepSize();
const double dfSign = dfStartAngle < dfEndAngle ? 1 : -1;
for (double dfAngle = dfStartAngle;
(dfAngle - dfEndAngle) * dfSign < 0;
Expand Down Expand Up @@ -2010,8 +2008,7 @@ GML2OGRGeometry_XMLNode_Internal(const CPLXMLNode *psNode, const char *pszId,
if (bSRSUnitIsDegree && dfUOMConv > 0)
{
auto poLS = std::make_unique<OGRLineString>();
const double dfStep =
CPLAtof(CPLGetConfigOption("OGR_ARC_STEPSIZE", "4"));
const double dfStep = OGRGeometryFactory::GetDefaultArcStepSize();
for (double dfAngle = 0; dfAngle < 360; dfAngle += dfStep)
{
double dfLong = 0.0;
Expand Down
2 changes: 2 additions & 0 deletions ogr/ogr_geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -4381,6 +4381,8 @@ class CPL_DLL OGRGeometryFactory
char **papszOptions,
const TransformWithOptionsCache &cache = TransformWithOptionsCache());

static double GetDefaultArcStepSize();

static OGRGeometry *
approximateArcAngles(double dfX, double dfY, double dfZ,
double dfPrimaryRadius, double dfSecondaryAxis,
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
49 changes: 34 additions & 15 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 @@ -4197,13 +4192,37 @@ void OGR_GeomTransformer_Destroy(OGRGeomTransformerH hTransformer)
}

/************************************************************************/
/* OGRGF_GetDefaultStepSize() */
/* OGRGeometryFactory::GetDefaultArcStepSize() */
/************************************************************************/

static double OGRGF_GetDefaultStepSize()
/** Return the default value of the angular step used when stroking curves
* as lines. Defaults to 4 degrees.
* Can be modified by setting the OGR_ARC_STEPSIZE configuration option.
* Valid values are in [1e-2, 180] degree range.
* @since 3.11
*/

/* static */
double OGRGeometryFactory::GetDefaultArcStepSize()
{
// coverity[tainted_data]
return CPLAtofM(CPLGetConfigOption("OGR_ARC_STEPSIZE", "4"));
const double dfVal = CPLAtofM(CPLGetConfigOption("OGR_ARC_STEPSIZE", "4"));
constexpr double MIN_VAL = 1e-2;
if (dfVal < MIN_VAL)
{
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)
{
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 Expand Up @@ -4266,7 +4285,7 @@ OGRGeometry *OGRGeometryFactory::approximateArcAngles(
// Support default arc step setting.
if (dfMaxAngleStepSizeDegrees < 1e-6)
{
dfMaxAngleStepSizeDegrees = OGRGF_GetDefaultStepSize();
dfMaxAngleStepSizeDegrees = OGRGeometryFactory::GetDefaultArcStepSize();
}

// Determine maximum interpolation gap. This is the largest straight-line
Expand Down Expand Up @@ -5459,7 +5478,7 @@ OGRLineString *OGRGeometryFactory::curveToLineString(
// support default arc step setting.
if (dfMaxAngleStepSizeDegrees < 1e-6)
{
dfMaxAngleStepSizeDegrees = OGRGF_GetDefaultStepSize();
dfMaxAngleStepSizeDegrees = OGRGeometryFactory::GetDefaultArcStepSize();
}

double dfStep = dfMaxAngleStepSizeDegrees / 180 * M_PI;
Expand Down
4 changes: 1 addition & 3 deletions ogr/ogrpgeogeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1835,10 +1835,8 @@ static OGRCurve *OGRShapeCreateCompoundCurve(int nPartStartIdx, int nPartPoints,
dfStartAngle += 2 * M_PI;
else if (dfEndAngle + M_PI < dfStartAngle)
dfEndAngle += 2 * M_PI;
// coverity[tainted_data]
const double dfStepSizeRad =
CPLAtofM(CPLGetConfigOption("OGR_ARC_STEPSIZE", "4")) / 180.0 *
M_PI;
OGRGeometryFactory::GetDefaultArcStepSize() / 180.0 * M_PI;
const double dfLengthTangentStart =
(dfX1 - dfX0) * (dfX1 - dfX0) + (dfY1 - dfY0) * (dfY1 - dfY0);
const double dfLengthTangentEnd =
Expand Down
2 changes: 1 addition & 1 deletion ogr/ogrsf_frmts/dwg/ogrdgnv8layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ static void ProcessCurve(OGRFeature *poFeature, const CPLString &osPen,
else
poSC = new OGRLineString();
const double dfArcStepSize =
CPLAtofM(CPLGetConfigOption("OGR_ARC_STEPSIZE", "4"));
OGRGeometryFactory::GetDefaultArcStepSize();
if (!ellipse.isNull())
{
nPoints = std::max(2, static_cast<int>(360 / dfArcStepSize));
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
Loading
Loading