Skip to content

Commit b0708e7

Browse files
author
mhugent
committed
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
1 parent efa9b29 commit b0708e7

File tree

4 files changed

+279
-26
lines changed

4 files changed

+279
-26
lines changed

src/core/Makefile.am

+2
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

+160
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

+96
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

0 commit comments

Comments
 (0)