diff --git a/src/core/qgslogger.cpp b/src/core/qgslogger.cpp index 6fc2d94f278..a920db57656 100644 --- a/src/core/qgslogger.cpp +++ b/src/core/qgslogger.cpp @@ -19,6 +19,8 @@ #include "qgslogger.h" #include +int QgsLogger::mDebugLevel = -999; // undefined value + void QgsLogger::debug( const QString& msg, int debuglevel, const char* file, const char* function, int line ) { const char* dfile = debugFile(); @@ -147,23 +149,33 @@ void QgsLogger::fatal( const QString& msg ) int QgsLogger::debugLevel() { - const char* dlevel = getenv( "QGIS_DEBUG" ); - if ( dlevel == NULL ) //environment variable not set + if ( mDebugLevel == -999 ) { + // read the environment variable QGIS_DEBUG just once, + // then reuse the value + + const char* dlevel = getenv( "QGIS_DEBUG" ); + if ( dlevel == NULL ) //environment variable not set + { #ifdef QGISDEBUG - return 1; //1 is default value in debug mode + mDebugLevel = 1; //1 is default value in debug mode #else - return 0; + mDebugLevel = 0; #endif - } - int level = atoi( dlevel ); + } + else + { + mDebugLevel = atoi( dlevel ); #ifdef QGISDEBUG - if ( level == 0 ) - { - level = 1; - } + if ( mDebugLevel == 0 ) + { + mDebugLevel = 1; + } #endif - return level; + } + } + + return mDebugLevel; } const char* QgsLogger::debugFile() diff --git a/src/core/qgslogger.h b/src/core/qgslogger.h index 341bb38a0fc..f8d7908cc51 100644 --- a/src/core/qgslogger.h +++ b/src/core/qgslogger.h @@ -24,8 +24,11 @@ #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 ( QgsLogger::debugLevel() >= (level) && (level) > 0 ) \ + QgsLogger::debug(QString(str), (level), __FILE__, __FUNCTION__, __LINE__); \ + } #else #define QgsDebugMsg(str) #define QgsDebugMsgLevel(str, level) @@ -99,13 +102,17 @@ class CORE_EXPORT QgsLogger /**Goes to qFatal*/ static void fatal( const QString& msg ); - private: /**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(); + private: + /**Reads the environment variable QGIS_DEBUG_FILE. Returns NULL if the variable is not set*/ static const char* debugFile(); + + /** current debug level */ + static int mDebugLevel; }; #endif