Skip to content

Commit

Permalink
Rework QgsDebugMsgLevel to avoid construction of strings which
Browse files Browse the repository at this point in the history
won't be logged at the current debug level

Instead of always constructing debug strings, and then potentially
ignoring them if they fall outside the current debug level, we instead
rework the QgsDebugMsgLevel macro so that strings are only ever
constructed when they WILL be logged.

This avoids the (often very expensive) string construction for
debug messages whenever the results won't be used. It allows low
level (i.e. level 3 or 4) debug messages to be safely used without
incurring huge slowdowns in debug builds.

TODO: ensure we only ever use QgsDebugMsg() for ERROR reporting,
and move all other debugging calls to QgsDebugMsgLevel instead.

Credit for original idea goes to @wonder-sk!
  • Loading branch information
nyalldawson committed Nov 25, 2019
1 parent 529aea6 commit 65ad06d
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/core/qgslogger.h
Expand Up @@ -31,7 +31,7 @@ class QFile;

#ifdef QGISDEBUG
#define QgsDebugMsg(str) QgsLogger::debug(QString(str), 1, __FILE__, __FUNCTION__, __LINE__)
#define QgsDebugMsgLevel(str, level) QgsLogger::debug(QString(str), (level), __FILE__, __FUNCTION__, __LINE__)
#define QgsDebugMsgLevel(str, level) if ( level <= QgsLogger::debugLevel() ) { QgsLogger::debug(QString(str), (level), __FILE__, __FUNCTION__, __LINE__); }(void)(0)
#define QgsDebugCall QgsScopeLogger _qgsScopeLogger(__FILE__, __FUNCTION__, __LINE__)
#else
#define QgsDebugCall
Expand Down Expand Up @@ -102,7 +102,12 @@ class CORE_EXPORT QgsLogger
/**
* Reads the environment variable QGIS_DEBUG and converts it to int. If QGIS_DEBUG is not set,
the function returns 1 if QGISDEBUG is defined and 0 if not*/
static int debugLevel() { init(); return sDebugLevel; }
static int debugLevel()
{
if ( sDebugLevel == -999 )
init();
return sDebugLevel;
}

//! Logs the message passed in to the logfile defined in QGIS_LOG_FILE if any. *
static void logMessageToFile( const QString &message );
Expand Down

0 comments on commit 65ad06d

Please sign in to comment.