[QGIS-Developer] C++ debugging policy/strategy

Nyall Dawson nyall.dawson at gmail.com
Sun Sep 25 16:05:14 PDT 2022


On Sat, 24 Sept 2022 at 03:06, Sandro Santilli via QGIS-Developer
<qgis-developer at lists.osgeo.org> wrote:
>
> I often find myself in need to place console printouts to better
> understand the flow of data in qgis while analizing bugs, but I never
> know how to implement such printouts.
>
> The attempts I usually make are:
>
>   (1)
>
>     qDebug() << "XXX: whatever is " << whatever;

This is fine as a temporary strategy, but don't ever leave these in
code you push to QGIS master. (qWarn is also useful as you'll get a
stracktrace)

>
>   (2)
>
>     std::cerr << "XXX: whatever is " << whatever << std::endl;

Again, ok as a temporary thing, but this one isn't to be left in code
pushed to QGIS.

>
>   (3)
>
>     QgsDebugMsg( QString( "XXX: whatever is %1".arg( whatever) );

This is the best approach for anything which will be pushed to QGIS
master, as the QgsDebugMsg macro will become a no-op on release builds
so there's no extra overhead caused by the debug output.
BUT

Don't push code to QGIS master which uses QgsDebugMsg() for anything
BUT reporting unanticipated errors. For all other cases you must use
QgsDebugMsgLevel( "message", level ), where
-   level = 2: for occasionally hit code paths and highly useful
priority debug messages
-   level = 3: more often hit code paths, more esoteric debug messages
-   level = 4: for super-noisy debugging messages in frequently called code

>
>   CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsRectangle &rectangle );
>
>   inline std::ostream &operator << ( std::ostream &os, const QgsRectangle &r) {...}

This is an outlier -- it's ancient unused code from > 15 years ago

Also relevant are the various "char *toString( const QgsXXXXX& val )"
methods defined in qgstest.h -- these ARE relevant, and are used when
reporting test failures by the QCOMPARE macro.

Nyall

>
> Is there a development guide to recommend one approach over the other ?
> I could not find a specific chapter on this here:
>
>   https://docs.qgis.org/3.22/en/docs/developers_guide/codingstandards.html
>
> --strk;
>
>   Libre GIS consultant/developer
>   https://strk.kbt.io/services.html
> _______________________________________________
> QGIS-Developer mailing list
> QGIS-Developer at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer


More information about the QGIS-Developer mailing list