Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Logger updates:
- evaluate QGIS_DEBUG environment variable just once
- QgsDebugMsgLevel macro will evaluate the passed string only when it will really print it.


git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@15364 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder committed Mar 6, 2011
1 parent d4b9d2f commit 0fac7ca
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
34 changes: 23 additions & 11 deletions src/core/qgslogger.cpp
Expand Up @@ -19,6 +19,8 @@
#include "qgslogger.h"
#include <QtDebug>

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();
Expand Down Expand Up @@ -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()
Expand Down
13 changes: 10 additions & 3 deletions src/core/qgslogger.h
Expand Up @@ -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)
Expand Down Expand Up @@ -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

0 comments on commit 0fac7ca

Please sign in to comment.