Skip to content

Commit 0fac7ca

Browse files
author
wonder
committed
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
1 parent d4b9d2f commit 0fac7ca

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

src/core/qgslogger.cpp

+23-11
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include "qgslogger.h"
2020
#include <QtDebug>
2121

22+
int QgsLogger::mDebugLevel = -999; // undefined value
23+
2224
void QgsLogger::debug( const QString& msg, int debuglevel, const char* file, const char* function, int line )
2325
{
2426
const char* dfile = debugFile();
@@ -147,23 +149,33 @@ void QgsLogger::fatal( const QString& msg )
147149

148150
int QgsLogger::debugLevel()
149151
{
150-
const char* dlevel = getenv( "QGIS_DEBUG" );
151-
if ( dlevel == NULL ) //environment variable not set
152+
if ( mDebugLevel == -999 )
152153
{
154+
// read the environment variable QGIS_DEBUG just once,
155+
// then reuse the value
156+
157+
const char* dlevel = getenv( "QGIS_DEBUG" );
158+
if ( dlevel == NULL ) //environment variable not set
159+
{
153160
#ifdef QGISDEBUG
154-
return 1; //1 is default value in debug mode
161+
mDebugLevel = 1; //1 is default value in debug mode
155162
#else
156-
return 0;
163+
mDebugLevel = 0;
157164
#endif
158-
}
159-
int level = atoi( dlevel );
165+
}
166+
else
167+
{
168+
mDebugLevel = atoi( dlevel );
160169
#ifdef QGISDEBUG
161-
if ( level == 0 )
162-
{
163-
level = 1;
164-
}
170+
if ( mDebugLevel == 0 )
171+
{
172+
mDebugLevel = 1;
173+
}
165174
#endif
166-
return level;
175+
}
176+
}
177+
178+
return mDebugLevel;
167179
}
168180

169181
const char* QgsLogger::debugFile()

src/core/qgslogger.h

+10-3
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424

2525
#ifdef QGISDEBUG
2626
#define QgsDebugMsg(str) QgsLogger::debug(QString(str), 1, __FILE__, __FUNCTION__, __LINE__)
27-
#define QgsDebugMsgLevel(str, level) QgsLogger::debug(QString(str), level,\
28-
__FILE__, __FUNCTION__, __LINE__)
27+
#define QgsDebugMsgLevel(str, level) \
28+
{ \
29+
if ( QgsLogger::debugLevel() >= (level) && (level) > 0 ) \
30+
QgsLogger::debug(QString(str), (level), __FILE__, __FUNCTION__, __LINE__); \
31+
}
2932
#else
3033
#define QgsDebugMsg(str)
3134
#define QgsDebugMsgLevel(str, level)
@@ -99,13 +102,17 @@ class CORE_EXPORT QgsLogger
99102
/**Goes to qFatal*/
100103
static void fatal( const QString& msg );
101104

102-
private:
103105
/**Reads the environment variable QGIS_DEBUG and converts it to int. If QGIS_DEBUG is not set,
104106
the function returns 1 if QGISDEBUG is defined and 0 if not*/
105107
static int debugLevel();
106108

109+
private:
110+
107111
/**Reads the environment variable QGIS_DEBUG_FILE. Returns NULL if the variable is not set*/
108112
static const char* debugFile();
113+
114+
/** current debug level */
115+
static int mDebugLevel;
109116
};
110117

111118
#endif

0 commit comments

Comments
 (0)