Skip to content

Commit b0708e7

Browse files
author
mhugent
committedApr 4, 2006
Added QgsLogger for handling of debug/warning/error messages. Replaced the std::couts in QgsProviderregistry to test the logger class
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@5160 c8812cc2-4d05-0410-92ff-de0c093fc19c

File tree

4 files changed

+279
-26
lines changed

4 files changed

+279
-26
lines changed
 

‎src/core/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ libqgis_coreHEADERS = \
6464
qgslabelattributes.h \
6565
qgsline.h \
6666
qgslinesymbol.h \
67+
qgslogger.h \
6768
qgsmaptopixel.h \
6869
qgsmarkercatalogue.h \
6970
qgsmarkersymbol.h \
@@ -116,6 +117,7 @@ libqgis_core_la_SOURCES =\
116117
qgslabelattributes.cpp \
117118
qgsline.cpp \
118119
qgslinesymbol.cpp \
120+
qgslogger.cpp \
119121
qgsmaptopixel.cpp \
120122
qgsmarkercatalogue.cpp \
121123
qgsmarkersymbol.cpp \

‎src/core/qgslogger.cpp

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/***************************************************************************
2+
qgslogger.cpp - description
3+
-------------------
4+
begin : April 2006
5+
copyright : (C) 2006 by Marco Hugentobler
6+
email : marco.hugentobler at karto dot baug dot ethz dot ch
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
19+
#include "qgslogger.h"
20+
21+
void QgsLogger::debug(const QString& msg, int debuglevel, const char* file, const char* function, int line)
22+
{
23+
const char* dfile = debugFile();
24+
if(dfile) //exit if QGIS_DEBUG_FILE is set and the message comes from the wrong file
25+
{
26+
if(!file || strcmp(dfile, file) != 0)
27+
{
28+
return;
29+
}
30+
}
31+
32+
int dlevel = debugLevel();
33+
if(dlevel >= debuglevel && debuglevel > 0)
34+
{
35+
if(file == NULL)
36+
{
37+
qDebug(msg.toLocal8Bit().data());
38+
}
39+
else if(function == NULL)
40+
{
41+
qDebug("File: %s, Message: %s", file, msg.toLocal8Bit().data());
42+
}
43+
else if(line == -1)
44+
{
45+
qDebug("File: %s, Function: %s, Message: %s", file, function, msg.toLocal8Bit().data());
46+
}
47+
else
48+
{
49+
qDebug("File: %s, Function: %s, Line: %d, Message: %s", file, function, line, msg.toLocal8Bit().data());
50+
}
51+
}
52+
}
53+
54+
void QgsLogger::debug(const QString& var, int val, int debuglevel, const char* file, const char* function, int line)
55+
{
56+
const char* dfile = debugFile();
57+
if(dfile) //exit if QGIS_DEBUG_FILE is set and the message comes from the wrong file
58+
{
59+
if(!file || strcmp(dfile, file) != 0)
60+
{
61+
return;
62+
}
63+
}
64+
65+
int dlevel = debugLevel();
66+
if(dlevel >= debuglevel && debuglevel > 0)
67+
{
68+
if(file == NULL)
69+
{
70+
qDebug("Variable: %s, Value: %d", var.toLocal8Bit().data(), val);
71+
}
72+
else if(function == NULL)
73+
{
74+
qDebug("File: %s, Variable: %s, Value: %d", file, var.toLocal8Bit().data(), val);
75+
}
76+
else if(line == -1)
77+
{
78+
qDebug("File: %s, Function: %s, Variable: %s, Value: %d", file, function, var.toLocal8Bit().data(), val);
79+
}
80+
else
81+
{
82+
qDebug("File: %s, Function: %s, Line: %d, Variable: %s, Value: %d", file, function, line, var.toLocal8Bit().data(), val);
83+
}
84+
}
85+
}
86+
87+
void QgsLogger::debug(const QString& var, double val, int debuglevel, const char* file, const char* function, int line)
88+
{
89+
const char* dfile = debugFile();
90+
if(dfile) //exit if QGIS_DEBUG_FILE is set and the message comes from the wrong file
91+
{
92+
if(!file || strcmp(dfile, file) != 0)
93+
{
94+
return;
95+
}
96+
}
97+
98+
int dlevel = debugLevel();
99+
if(dlevel >= debuglevel && debuglevel > 0)
100+
{
101+
if(file == NULL)
102+
{
103+
qDebug("Variable: %s, Value: %f", var.toLocal8Bit().data(), val);
104+
}
105+
else if(function == NULL)
106+
{
107+
qDebug("File: %s, Variable: %s, Value: %f", file, var.toLocal8Bit().data(), val);
108+
}
109+
else if(line == -1)
110+
{
111+
qDebug("File: %s, Function: %s, Variable: %s, Value: %f", file, function, var.toLocal8Bit().data(), val);
112+
}
113+
else
114+
{
115+
qDebug("File: %s, Function: %s, Line: %d, Variable: %s, Value: %f", file, function, line, var.toLocal8Bit().data(), val);
116+
}
117+
}
118+
}
119+
120+
void QgsLogger::warning(const QString& msg)
121+
{
122+
qWarning(msg.toLocal8Bit().data());
123+
}
124+
125+
void QgsLogger::critical(const QString& msg)
126+
{
127+
qCritical(msg.toLocal8Bit().data());
128+
}
129+
130+
void QgsLogger::fatal(const QString& msg)
131+
{
132+
qFatal(msg.toLocal8Bit().data());
133+
}
134+
135+
int QgsLogger::debugLevel()
136+
{
137+
const char* dlevel = getenv("QGIS_DEBUG");
138+
if(dlevel == NULL) //environment variable not set
139+
{
140+
#ifdef QGISDEBUG
141+
return 1; //1 is default value in debug mode
142+
#else
143+
return 0;
144+
#endif
145+
}
146+
int level = atoi(dlevel);
147+
#ifdef QGISDEBUG
148+
if(level == 0)
149+
{
150+
level = 1;
151+
}
152+
#endif
153+
return level;
154+
}
155+
156+
const char* QgsLogger::debugFile()
157+
{
158+
const char* dfile = getenv("QGIS_DEBUG_FILE");
159+
return dfile;
160+
}

‎src/core/qgslogger.h

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/***************************************************************************
2+
qgslogger.h - description
3+
-------------------
4+
begin : April 2006
5+
copyright : (C) 2006 by Marco Hugentobler
6+
email : marco.hugentobler at karto dot baug dot ethz dot ch
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
#ifndef QGSLOGGER
19+
#define QGSLOGGER
20+
21+
#include <iostream>
22+
#include <sstream>
23+
#include <QString>
24+
25+
#define QgsDebugMsg(str) QgsLogger::debug(QString(str), 1, __FILE__, __FUNCTION__, __LINE__);
26+
27+
/**QgsLogger is a class to print debug/warning/error messages to the console. The advantage of this class over std::cout, std::cerr & co. is that the output can be controlled with environment variables:
28+
29+
QGIS_DEBUG is an int describing what debug messages are written to the console. If the debug level of a message is <= QGIS_DEBUG, the message is written to the console. It the variable QGIS_DEBUG is not defined, it defaults to 1 for debug mode and to 0 for release mode
30+
31+
QGIS_DEBUG_FILE may contain a filename. Only the messages from this file are printed (provided they have the right debuglevel). If QGIS_DEBUG_FILE is not set, messages from all files are printed
32+
*/
33+
34+
class QgsLogger
35+
{
36+
public:
37+
38+
/**Goes to qDebug.
39+
@param msg the message to be printed
40+
@param debuglevel
41+
@param file filename where the message comes from
42+
@param function function where the message comes from
43+
@param line place in file where the message comes from*/
44+
static void debug(const QString& msg, int debuglevel = 1, const char* file = NULL, const char* function = NULL, int line = -1);
45+
46+
/**Similar to the previous method, but prints a variable int-value pair*/
47+
static void debug(const QString& var, int val, int debuglevel = 1, const char* file = NULL, const char* function = NULL, int line = -1);
48+
49+
/**Similar to the previous method, but prints a variable double-value pair*/
50+
static void debug(const QString& var, double val, int debuglevel = 1, const char* file = NULL, const char* function = NULL, int line = -1);
51+
52+
/**Prints out a variable/value pair for types with overloaded operator<<*/
53+
template <typename T> static void debug(const QString& var, T val, const char* file = 0, const char* function = 0, \
54+
int line = -1, int debuglevel = 1)
55+
{
56+
const char* dfile = debugFile();
57+
if(dfile) //exit if QGIS_DEBUG_FILE is set and the message comes from the wrong file
58+
{
59+
if(!file || strcmp(dfile, file) != 0)
60+
{
61+
return;
62+
}
63+
}
64+
std::ostringstream os;
65+
os << var.toLocal8Bit().data() << " = " << val;
66+
if(line == -1)
67+
{
68+
qDebug("File: %s\nFunction: %s\nMessage: %s",
69+
file, function, os.str().c_str());
70+
}
71+
else
72+
{
73+
qDebug("File: %s\nFunction: %s\nLine: %s\nMessage: %s", file, function, \
74+
QString::number(line).toLocal8Bit().data(), os.str().c_str());
75+
}
76+
}
77+
78+
/**Goes to qWarning*/
79+
static void warning(const QString& msg);
80+
81+
/**Goes to qCritical*/
82+
static void critical(const QString& msg);
83+
84+
/**Goes to qFatal*/
85+
static void fatal(const QString& msg);
86+
87+
private:
88+
/**Reads the environment variable QGIS_DEBUG and converts it to int. If QGIS_DEBUG is not set,\
89+
the function returns 1 if QGISDEBUG is defined and 0 if not*/
90+
static int debugLevel();
91+
92+
/**Reads the environment variable QGIS_DEBUG_FILE. Returns NULL if the variable is not set*/
93+
static const char* debugFile();
94+
};
95+
96+
#endif

‎src/core/qgsproviderregistry.cpp

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ using namespace std;
3232

3333
#include "qgis.h"
3434
#include "qgsdataprovider.h"
35+
#include "qgslogger.h"
3536
#include "qgsprovidermetadata.h"
3637

3738

@@ -80,7 +81,7 @@ QgsProviderRegistry::QgsProviderRegistry(QString pluginPath)
8081
#endif
8182

8283
#ifdef QGISDEBUG
83-
cerr << "Checking " << mLibraryDirectory.path().data() << " for provider plugins\n";
84+
QgsLogger::debug("Checking " + mLibraryDirectory.path() + " for provider plugins\n", 1, __FILE__, __FUNCTION__, __LINE__);
8485
#endif
8586

8687
if (mLibraryDirectory.count() == 0)
@@ -108,7 +109,7 @@ QgsProviderRegistry::QgsProviderRegistry(QString pluginPath)
108109
if (loaded)
109110
{
110111
#ifdef QGISDEBUG
111-
std::cout << "Checking " << myLib->library().toLocal8Bit().data() << std::endl;
112+
QgsLogger::debug("Checking " + myLib->library(), 1, __FILE__, __FUNCTION__, __LINE__);
112113
#endif
113114
// get the description and the key for the provider plugin
114115
isprovider_t *isProvider = (isprovider_t *) myLib->resolve("isProvider");
@@ -127,7 +128,7 @@ QgsProviderRegistry::QgsProviderRegistry(QString pluginPath)
127128
mProviders[pKey()] =
128129
new QgsProviderMetadata(pKey(), pDesc(), myLib->library());
129130
#ifdef QGISDEBUG
130-
std::cout << "Loaded " << pDesc().toLocal8Bit().data() << std::endl;
131+
QgsDebugMsg("Loaded " + pDesc());
131132
#endif
132133

133134
// now get vector file filters, if any
@@ -153,31 +154,29 @@ QgsProviderRegistry::QgsProviderRegistry(QString pluginPath)
153154
}
154155
else
155156
{
156-
QgsDebug( QString("No vector file filters for " + pKey()).ascii() );
157+
QgsLogger::debug("No vector file filters for " + pKey(), 1, __FILE__, __FUNCTION__, __LINE__);
157158
}
158159
}
159160
}
160161
else
161162
{
162-
QgsDebug( "Unable to invoke fileVectorFilters()" );
163+
QgsDebugMsg("Unable to invoke fileVectorFilters()");
163164
}
164165
}
165166
else
166167
{
167-
cout << "Unable to find one of the required provider functions (providerKey() or description()) in "
168-
<< myLib->library().toLocal8Bit().data() << endl;
168+
QgsLogger::debug("Unable to find one of the required provider functions (providerKey() or description()) in " + myLib->library());
169169
}
170170
}
171171
else
172172
{
173-
QgsDebug( "Unable to invoke fileVectorFilters()" );
173+
QgsDebugMsg("Unable to invoke fileVectorFilters()");
174174
}
175175
}
176176
else
177177
{
178178
#ifdef QGISDEBUG
179-
cout << myLib->library().toLocal8Bit().data()
180-
<< " is not a provider" << std::endl;
179+
QgsDebugMsg(myLib->library() + " is not a provider");
181180
#endif
182181
}
183182
}
@@ -316,12 +315,11 @@ QgsDataProvider* QgsProviderRegistry::getProvider( QString const & providerKey,
316315
void *handle = dlopen(cOgrLib, RTLD_LAZY | RTLD_GLOBAL);
317316
if (!handle)
318317
{
319-
cout << "Error in dlopen: " << dlerror() << endl;
320-
318+
QgsLogger::warning("Error in dlopen");
321319
}
322320
else
323321
{
324-
cout << "dlopen suceeded" << endl;
322+
QgsDebugMsg("dlopen suceeded");
325323
dlclose(handle);
326324
}
327325

@@ -331,24 +329,22 @@ QgsDataProvider* QgsProviderRegistry::getProvider( QString const & providerKey,
331329
QLibrary* myLib = new QLibrary((const char *) lib);
332330

333331
#ifdef QGISDEBUG
334-
std::cout << "QgsProviderRegistry::getRasterProvider: Library name is "
335-
<< myLib->library().toLocal8Bit().data()
336-
<< std::endl;
332+
QgsDebugMsg("QgsProviderRegistry::getRasterProvider: Library name is " + myLib->library());
337333
#endif
338334

339335
bool loaded = myLib->load();
340336

341337
if (loaded)
342338
{
343-
QgsDebug( "Loaded data provider library" );
344-
QgsDebug( "Attempting to resolve the classFactory function" );
339+
QgsDebugMsg( "Loaded data provider library" );
340+
QgsDebugMsg( "Attempting to resolve the classFactory function" );
345341

346342
classFactoryFunction_t * classFactory =
347343
(classFactoryFunction_t *) myLib->resolve("classFactory");
348344

349345
if (classFactory)
350346
{
351-
QgsDebug( "Getting pointer to a dataProvider object from the library" );
347+
QgsDebugMsg( "Getting pointer to a dataProvider object from the library" );
352348

353349
//XXX - This was a dynamic cast but that kills the Windows
354350
// version big-time with an abnormal termination error
@@ -360,9 +356,8 @@ QgsDataProvider* QgsProviderRegistry::getProvider( QString const & providerKey,
360356
if (dataProvider)
361357
{
362358
#ifdef QGISDEBUG
363-
QgsDebug( "Instantiated the data provider plugin" );
364-
cerr << "provider name: " << dataProvider->name().data() << "\n";
365-
//cout << "provider description: " << dataProvider->description() << "\n";
359+
QgsDebugMsg( "Instantiated the data provider plugin" );
360+
QgsDebugMsg("provider name: " + dataProvider->name());
366361
#endif
367362
if (dataProvider->isValid())
368363
{
@@ -371,7 +366,7 @@ QgsDataProvider* QgsProviderRegistry::getProvider( QString const & providerKey,
371366
else
372367
{ // this is likely because the dataSource is invalid, and isn't
373368
// necessarily a reflection on the data provider itself
374-
QgsDebug( "Invalid data provider" );
369+
QgsDebugMsg( "Invalid data provider" );
375370

376371
myLib->unload();
377372

@@ -380,7 +375,7 @@ QgsDataProvider* QgsProviderRegistry::getProvider( QString const & providerKey,
380375
}
381376
else
382377
{
383-
QgsDebug( "Unable to instantiate the data provider plugin" );
378+
QgsLogger::warning( "Unable to instantiate the data provider plugin" );
384379

385380
myLib->unload();
386381

@@ -390,12 +385,12 @@ QgsDataProvider* QgsProviderRegistry::getProvider( QString const & providerKey,
390385
}
391386
else
392387
{
393-
QgsDebug( "Failed to load ../providers/libproviders.so" );
388+
QgsLogger::warning( "Failed to load ../providers/libproviders.so" );
394389

395390
return 0;
396391
}
397392

398-
QgsDebug( "exiting" );
393+
QgsDebugMsg( "exiting" );
399394

400395
return 0; // factory didn't exist
401396

0 commit comments

Comments
 (0)
Please sign in to comment.