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

Use a scoped proj logger to avoid user data lifetime issues (backport) #59087

Merged
merged 3 commits into from
Oct 17, 2024
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
38 changes: 2 additions & 36 deletions src/core/proj/qgscoordinatetransform_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,36 +244,6 @@ void QgsCoordinateTransformPrivate::calculateTransforms( const QgsCoordinateTran
}
}

static void proj_collecting_logger( void *user_data, int /*level*/, const char *message )
{
QStringList *dest = reinterpret_cast< QStringList * >( user_data );
dest->append( QString( message ) );
}

static void proj_logger( void *, int level, const char *message )
{
#ifndef QGISDEBUG
Q_UNUSED( message )
#endif
if ( level == PJ_LOG_ERROR )
{
const QString messageString( message );
if ( messageString == QLatin1String( "push: Invalid latitude" ) )
{
// these messages tend to spam the console as they can be repeated 1000s of times
QgsDebugMsgLevel( messageString, 3 );
}
else
{
QgsDebugError( messageString );
}
}
else if ( level == PJ_LOG_DEBUG )
{
QgsDebugMsgLevel( QString( message ), 3 );
}
}

ProjData QgsCoordinateTransformPrivate::threadLocalProjData()
{
QgsReadWriteLocker locker( mProjLock, QgsReadWriteLocker::Read );
Expand All @@ -291,8 +261,7 @@ ProjData QgsCoordinateTransformPrivate::threadLocalProjData()
locker.changeMode( QgsReadWriteLocker::Write );

// use a temporary proj error collector
QStringList projErrors;
proj_log_func( context, &projErrors, proj_collecting_logger );
QgsScopedProjCollectingLogger errorLogger;

mIsReversed = false;

Expand Down Expand Up @@ -342,7 +311,6 @@ ProjData QgsCoordinateTransformPrivate::threadLocalProjData()
{
if ( !mSourceCRS.projObject() || ! mDestCRS.projObject() )
{
proj_log_func( context, nullptr, nullptr );
return nullptr;
}

Expand Down Expand Up @@ -494,6 +462,7 @@ ProjData QgsCoordinateTransformPrivate::threadLocalProjData()
if ( !transform && nonAvailableError.isEmpty() )
{
const int errNo = proj_context_errno( context );
const QStringList projErrors = errorLogger.errors();
if ( errNo && errNo != -61 )
{
nonAvailableError = QString( proj_errno_string( errNo ) );
Expand Down Expand Up @@ -529,9 +498,6 @@ ProjData QgsCoordinateTransformPrivate::threadLocalProjData()
}
}

// reset logger to terminal output
proj_log_func( context, nullptr, proj_logger );

if ( !transform )
{
// ouch!
Expand Down
50 changes: 50 additions & 0 deletions src/core/proj/qgsprojutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "qgis.h"
#include "qgscoordinatetransform.h"
#include "qgsexception.h"
#include "qgslogger.h"
#include <QString>
#include <QSet>
#include <QRegularExpression>
Expand Down Expand Up @@ -259,6 +260,40 @@ QgsProjUtils::proj_pj_unique_ptr QgsProjUtils::crsToDatumEnsemble( const PJ *crs
#endif
}

void QgsProjUtils::proj_collecting_logger( void *user_data, int /*level*/, const char *message )
{
QStringList *dest = reinterpret_cast< QStringList * >( user_data );
QString messageString( message );
messageString.replace( QLatin1String( "internal_proj_create: " ), QString() );
dest->append( messageString );
}

void QgsProjUtils::proj_logger( void *, int level, const char *message )
{
#ifdef QGISDEBUG
if ( level == PJ_LOG_ERROR )
{
const QString messageString( message );
if ( messageString == QLatin1String( "push: Invalid latitude" ) )
{
// these messages tend to spam the console as they can be repeated 1000s of times
QgsDebugMsgLevel( messageString, 3 );
}
else
{
QgsDebugError( messageString );
}
}
else if ( level == PJ_LOG_DEBUG )
{
QgsDebugMsgLevel( QString( message ), 3 );
}
#else
( void )level;
( void )message;
#endif
}

bool QgsProjUtils::identifyCrs( const PJ *crs, QString &authName, QString &authCode, IdentifyFlags flags )
{
authName.clear();
Expand Down Expand Up @@ -456,3 +491,18 @@ QStringList QgsProjUtils::searchPaths()
}
return res;
}

//
// QgsScopedProjCollectingLogger
//

QgsScopedProjCollectingLogger::QgsScopedProjCollectingLogger()
{
proj_log_func( QgsProjContext::get(), &mProjErrors, QgsProjUtils::proj_collecting_logger );
}

QgsScopedProjCollectingLogger::~QgsScopedProjCollectingLogger()
{
// reset logger back to terminal output
proj_log_func( QgsProjContext::get(), nullptr, QgsProjUtils::proj_logger );
}
56 changes: 56 additions & 0 deletions src/core/proj/qgsprojutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,22 @@ class CORE_EXPORT QgsProjUtils
*/
static QList< QgsDatumTransform::GridDetails > gridsUsed( const QString &proj );

/**
* Default QGIS proj log function.
*
* Uses QgsDebugError or QgsDebugMsgLevel to report errors in debug builds only.
*/
static void proj_logger( void *user_data, int level, const char *message );

/**
* QGIS proj log function which collects errors to a QStringList.
*
* \warning The user_data argument passed to proj_log_func MUST be a QStringList object,
* and must exist for the duration where proj_collecting_logger is used. You MUST reset
* the proj_log_func to proj_logger before the user data QStringList is destroyed.
*/
static void proj_collecting_logger( void *user_data, int level, const char *message );

#if 0 // not possible in current Proj 6 API

/**
Expand Down Expand Up @@ -265,6 +281,46 @@ class CORE_EXPORT QgsProjContext
#endif
};


/**
* \ingroup core
*
* \brief Scoped object for temporary swapping to an error-collecting PROJ log function.
*
* Temporarily sets the PROJ log function to one which collects errors for the lifetime of the object,
* before returning it to the default QGIS proj logging function on destruction.
*
* \note The collecting logger ONLY applies to the current thread.
*
* \note Not available in Python bindings
* \since QGIS 3.40
*/
class CORE_EXPORT QgsScopedProjCollectingLogger
{
public:

/**
* Constructor for QgsScopedProjCollectingLogger.
*
* PROJ errors will be collected, and can be retrieved by calling errors().
*/
QgsScopedProjCollectingLogger();

/**
* Returns the PROJ logger back to the default QGIS PROJ logger.
*/
~QgsScopedProjCollectingLogger();

/**
* Returns the (possibly empty) list of collected errors.
*/
QStringList errors() const { return mProjErrors; }

private:

QStringList mProjErrors;
};

Q_DECLARE_OPERATORS_FOR_FLAGS( QgsProjUtils::IdentifyFlags )
#endif
#endif // QGSPROJUTILS_H
20 changes: 3 additions & 17 deletions src/gui/qgscrsdefinitionwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,22 +110,13 @@ void QgsCrsDefinitionWidget::pbnCopyCRS_clicked()
}
}

static void proj_collecting_logger( void *user_data, int /*level*/, const char *message )
{
QStringList *dest = reinterpret_cast< QStringList * >( user_data );
QString messageString( message );
messageString.replace( QLatin1String( "internal_proj_create: " ), QString() );
dest->append( messageString );
}

void QgsCrsDefinitionWidget::validateCurrent()
{
const QString projDef = mTextEditParameters->toPlainText();

PJ_CONTEXT *context = proj_context_create();
PJ_CONTEXT *context = QgsProjContext::get();

QStringList projErrors;
proj_log_func( context, &projErrors, proj_collecting_logger );
QgsScopedProjCollectingLogger projLogger;
QgsProjUtils::proj_pj_unique_ptr crs;

switch ( static_cast< Qgis::CrsDefinitionFormat >( mFormatComboBox->currentData().toInt() ) )
Expand Down Expand Up @@ -169,16 +160,11 @@ void QgsCrsDefinitionWidget::validateCurrent()
else
{
QMessageBox::warning( this, tr( "Custom Coordinate Reference System" ),
tr( "This proj projection definition is not valid:" ) + QStringLiteral( "\n\n" ) + projErrors.join( '\n' ) );
tr( "This proj projection definition is not valid:" ) + QStringLiteral( "\n\n" ) + projLogger.errors().join( '\n' ) );
}
break;
}
}

// reset logger to terminal output
proj_log_func( context, nullptr, nullptr );
proj_context_destroy( context );
context = nullptr;
}

void QgsCrsDefinitionWidget::formatChanged()
Expand Down
Loading