Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add method to silence noisy GEOS error logging handling when not
desirable.

By default these errors are logged to the console and in the QGIS UI.
But for some operations errors are expected and logging these just results
in noise.
  • Loading branch information
nyalldawson committed Oct 21, 2020
1 parent 2480814 commit 7e64d74
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 3 deletions.
20 changes: 20 additions & 0 deletions python/core/auto_generated/geometry/qgsgeometryengine.sip.in
Expand Up @@ -257,11 +257,31 @@ Splits this geometry according to a given line.

virtual QgsAbstractGeometry *offsetCurve( double distance, int segments, int joinStyle, double miterLimit, QString *errorMsg = 0 ) const = 0 /Factory/;

void setLogErrors( bool enabled );
%Docstring
Sets whether warnings and errors encountered during the geometry operations should be logged.

By default these errors are logged to the console and in the QGIS UI. But for some operations errors are expected and logging
these just results in noise. In this case setting ``enabled`` to ``False`` will avoid the automatic error reporting.

.. versionadded:: 3.16
%End

protected:

void logError( const QString &engineName, const QString &message ) const;
%Docstring
Logs an error ``message`` encountered during an operation.

.. seealso:: :py:func:`setLogErrors`

.. versionadded:: 3.16
%End

QgsGeometryEngine( const QgsAbstractGeometry *geometry );
};


/************************************************************************
* This file has been generated automatically from *
* *
Expand Down
29 changes: 29 additions & 0 deletions src/core/geometry/qgsgeometryengine.h
Expand Up @@ -19,6 +19,7 @@ email : marco.hugentobler at sourcepole dot com
#include "qgis_core.h"
#include "qgslinestring.h"
#include "qgsgeometry.h"
#include "qgslogger.h"

#include <QVector>

Expand Down Expand Up @@ -273,12 +274,40 @@ class CORE_EXPORT QgsGeometryEngine

virtual QgsAbstractGeometry *offsetCurve( double distance, int segments, int joinStyle, double miterLimit, QString *errorMsg = nullptr ) const = 0 SIP_FACTORY;

/**
* Sets whether warnings and errors encountered during the geometry operations should be logged.
*
* By default these errors are logged to the console and in the QGIS UI. But for some operations errors are expected and logging
* these just results in noise. In this case setting \a enabled to FALSE will avoid the automatic error reporting.
*
* \since QGIS 3.16
*/
void setLogErrors( bool enabled ) { mLogErrors = enabled; }

protected:
const QgsAbstractGeometry *mGeometry = nullptr;
bool mLogErrors = true;

/**
* Logs an error \a message encountered during an operation.
*
* \see setLogErrors()
*
* \since QGIS 3.16
*/
void logError( const QString &engineName, const QString &message ) const
{
if ( mLogErrors )
{
QgsDebugMsg( QStringLiteral( "%1 notice: %2" ).arg( engineName, message ) );
qWarning( "%s exception: %s", engineName.toLocal8Bit().constData(), message.toLocal8Bit().constData() );
}
}

QgsGeometryEngine( const QgsAbstractGeometry *geometry )
: mGeometry( geometry )
{}
};

#endif // QGSGEOMETRYENGINE_H

11 changes: 8 additions & 3 deletions src/core/geometry/qgsgeos.cpp
Expand Up @@ -57,7 +57,6 @@ static void throwGEOSException( const char *fmt, ... )
vsnprintf( buffer, sizeof buffer, fmt, ap );
va_end( ap );

qWarning( "GEOS exception: %s", buffer );
QString message = QString::fromUtf8( buffer );

#ifdef _MSC_VER
Expand Down Expand Up @@ -90,8 +89,6 @@ static void printGEOSNotice( const char *fmt, ... )
va_start( ap, fmt );
vsnprintf( buffer, sizeof buffer, fmt, ap );
va_end( ap );

QgsDebugMsg( QStringLiteral( "GEOS notice: %1" ).arg( QString::fromUtf8( buffer ) ) );
#else
Q_UNUSED( fmt )
#endif
Expand Down Expand Up @@ -238,6 +235,7 @@ std::unique_ptr<QgsAbstractGeometry> QgsGeos::clip( const QgsRectangle &rect, QS
}
catch ( GEOSException &e )
{
logError( QStringLiteral( "GEOS" ), e.what() );
if ( errorMsg )
{
*errorMsg = e.what();
Expand Down Expand Up @@ -545,6 +543,7 @@ QString QgsGeos::relate( const QgsAbstractGeometry *geom, QString *errorMsg ) co
}
catch ( GEOSException &e )
{
logError( QStringLiteral( "GEOS" ), e.what() );
if ( errorMsg )
{
*errorMsg = e.what();
Expand Down Expand Up @@ -574,6 +573,7 @@ bool QgsGeos::relatePattern( const QgsAbstractGeometry *geom, const QString &pat
}
catch ( GEOSException &e )
{
logError( QStringLiteral( "GEOS" ), e.what() );
if ( errorMsg )
{
*errorMsg = e.what();
Expand Down Expand Up @@ -1446,6 +1446,7 @@ std::unique_ptr<QgsAbstractGeometry> QgsGeos::overlay( const QgsAbstractGeometry
}
catch ( GEOSException &e )
{
logError( QStringLiteral( "GEOS" ), e.what() );
if ( errorMsg )
{
*errorMsg = e.what();
Expand Down Expand Up @@ -1530,6 +1531,7 @@ bool QgsGeos::relation( const QgsAbstractGeometry *geom, Relation r, QString *er
}
catch ( GEOSException &e )
{
logError( QStringLiteral( "GEOS" ), e.what() );
if ( errorMsg )
{
*errorMsg = e.what();
Expand Down Expand Up @@ -2241,6 +2243,7 @@ QgsGeometry QgsGeos::closestPoint( const QgsGeometry &other, QString *errorMsg )
}
catch ( GEOSException &e )
{
logError( QStringLiteral( "GEOS" ), e.what() );
if ( errorMsg )
{
*errorMsg = e.what();
Expand Down Expand Up @@ -2279,6 +2282,7 @@ QgsGeometry QgsGeos::shortestLine( const QgsGeometry &other, QString *errorMsg )
}
catch ( GEOSException &e )
{
logError( QStringLiteral( "GEOS" ), e.what() );
if ( errorMsg )
{
*errorMsg = e.what();
Expand Down Expand Up @@ -2312,6 +2316,7 @@ double QgsGeos::lineLocatePoint( const QgsPoint &point, QString *errorMsg ) cons
}
catch ( GEOSException &e )
{
logError( QStringLiteral( "GEOS" ), e.what() );
if ( errorMsg )
{
*errorMsg = e.what();
Expand Down
3 changes: 3 additions & 0 deletions src/core/pal/feature.cpp
Expand Up @@ -418,6 +418,7 @@ std::unique_ptr<LabelPosition> FeaturePart::createCandidatePointOnSurface( Point
}
catch ( GEOSException &e )
{
qWarning( "GEOS exception: %s", e.what() );
QgsMessageLog::logMessage( QObject::tr( "Exception: %1" ).arg( e.what() ), QObject::tr( "GEOS" ) );
return nullptr;
}
Expand Down Expand Up @@ -2216,6 +2217,7 @@ bool FeaturePart::isConnected( FeaturePart *p2 )
}
catch ( GEOSException &e )
{
qWarning( "GEOS exception: %s", e.what() );
QgsMessageLog::logMessage( QObject::tr( "Exception: %1" ).arg( e.what() ), QObject::tr( "GEOS" ) );
return false;
}
Expand Down Expand Up @@ -2256,6 +2258,7 @@ bool FeaturePart::mergeWithFeaturePart( FeaturePart *other )
}
catch ( GEOSException &e )
{
qWarning( "GEOS exception: %s", e.what() );
QgsMessageLog::logMessage( QObject::tr( "Exception: %1" ).arg( e.what() ), QObject::tr( "GEOS" ) );
return false;
}
Expand Down
1 change: 1 addition & 0 deletions src/core/pal/geomfunction.cpp
Expand Up @@ -383,6 +383,7 @@ bool GeomFunction::containsCandidate( const GEOSPreparedGeometry *geom, double x
}
catch ( GEOSException &e )
{
qWarning( "GEOS exception: %s", e.what() );
Q_NOWARN_UNREACHABLE_PUSH
QgsMessageLog::logMessage( QObject::tr( "Exception: %1" ).arg( e.what() ), QObject::tr( "GEOS" ) );
return false;
Expand Down
7 changes: 7 additions & 0 deletions src/core/pal/labelposition.cpp
Expand Up @@ -213,6 +213,7 @@ bool LabelPosition::intersects( const GEOSPreparedGeometry *geometry )
}
catch ( GEOSException &e )
{
qWarning( "GEOS exception: %s", e.what() );
QgsMessageLog::logMessage( QObject::tr( "Exception: %1" ).arg( e.what() ), QObject::tr( "GEOS" ) );
return false;
}
Expand All @@ -238,6 +239,7 @@ bool LabelPosition::within( const GEOSPreparedGeometry *geometry )
}
catch ( GEOSException &e )
{
qWarning( "GEOS exception: %s", e.what() );
QgsMessageLog::logMessage( QObject::tr( "Exception: %1" ).arg( e.what() ), QObject::tr( "GEOS" ) );
return false;
}
Expand Down Expand Up @@ -293,6 +295,7 @@ bool LabelPosition::isInConflictSinglePart( const LabelPosition *lp ) const
}
catch ( GEOSException &e )
{
qWarning( "GEOS exception: %s", e.what() );
QgsMessageLog::logMessage( QObject::tr( "Exception: %1" ).arg( e.what() ), QObject::tr( "GEOS" ) );
return false;
}
Expand Down Expand Up @@ -486,6 +489,7 @@ bool LabelPosition::crossesLine( PointSet *line ) const
}
catch ( GEOSException &e )
{
qWarning( "GEOS exception: %s", e.what() );
QgsMessageLog::logMessage( QObject::tr( "Exception: %1" ).arg( e.what() ), QObject::tr( "GEOS" ) );
return false;
}
Expand Down Expand Up @@ -516,6 +520,7 @@ bool LabelPosition::crossesBoundary( PointSet *polygon ) const
}
catch ( GEOSException &e )
{
qWarning( "GEOS exception: %s", e.what() );
QgsMessageLog::logMessage( QObject::tr( "Exception: %1" ).arg( e.what() ), QObject::tr( "GEOS" ) );
return false;
}
Expand Down Expand Up @@ -549,6 +554,7 @@ bool LabelPosition::intersectsWithPolygon( PointSet *polygon ) const
}
catch ( GEOSException &e )
{
qWarning( "GEOS exception: %s", e.what() );
QgsMessageLog::logMessage( QObject::tr( "Exception: %1" ).arg( e.what() ), QObject::tr( "GEOS" ) );
}

Expand Down Expand Up @@ -606,6 +612,7 @@ double LabelPosition::polygonIntersectionCostForParts( PointSet *polygon ) const
}
catch ( GEOSException &e )
{
qWarning( "GEOS exception: %s", e.what() );
QgsMessageLog::logMessage( QObject::tr( "Exception: %1" ).arg( e.what() ), QObject::tr( "GEOS" ) );
}

Expand Down
5 changes: 5 additions & 0 deletions src/core/pal/pointset.cpp
Expand Up @@ -262,6 +262,7 @@ bool PointSet::containsPoint( double x, double y ) const
}
catch ( GEOSException &e )
{
qWarning( "GEOS exception: %s", e.what() );
QgsMessageLog::logMessage( QObject::tr( "Exception: %1" ).arg( e.what() ), QObject::tr( "GEOS" ) );
return false;
}
Expand Down Expand Up @@ -839,6 +840,7 @@ double PointSet::minDistanceToPoint( double px, double py, double *rx, double *r
}
catch ( GEOSException &e )
{
qWarning( "GEOS exception: %s", e.what() );
QgsMessageLog::logMessage( QObject::tr( "Exception: %1" ).arg( e.what() ), QObject::tr( "GEOS" ) );
return 0;
}
Expand Down Expand Up @@ -895,6 +897,7 @@ void PointSet::getCentroid( double &px, double &py, bool forceInside ) const
}
catch ( GEOSException &e )
{
qWarning( "GEOS exception: %s", e.what() );
QgsMessageLog::logMessage( QObject::tr( "Exception: %1" ).arg( e.what() ), QObject::tr( "GEOS" ) );
return;
}
Expand Down Expand Up @@ -978,6 +981,7 @@ double PointSet::length() const
}
catch ( GEOSException &e )
{
qWarning( "GEOS exception: %s", e.what() );
QgsMessageLog::logMessage( QObject::tr( "Exception: %1" ).arg( e.what() ), QObject::tr( "GEOS" ) );
return -1;
}
Expand All @@ -1004,6 +1008,7 @@ double PointSet::area() const
}
catch ( GEOSException &e )
{
qWarning( "GEOS exception: %s", e.what() );
QgsMessageLog::logMessage( QObject::tr( "Exception: %1" ).arg( e.what() ), QObject::tr( "GEOS" ) );
return -1;
}
Expand Down

0 comments on commit 7e64d74

Please sign in to comment.