Skip to content
Permalink
Browse files

Added QgsLogger for handling of debug/warning/error messages. Replace…

…d 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
  • Loading branch information
mhugent
mhugent committed Apr 4, 2006
1 parent efa9b29 commit b0708e7d8ba1a87b9772f3fd7e7d5d8aa43913d8
Showing with 279 additions and 26 deletions.
  1. +2 −0 src/core/Makefile.am
  2. +160 −0 src/core/qgslogger.cpp
  3. +96 −0 src/core/qgslogger.h
  4. +21 −26 src/core/qgsproviderregistry.cpp
@@ -64,6 +64,7 @@ libqgis_coreHEADERS = \
qgslabelattributes.h \
qgsline.h \
qgslinesymbol.h \
qgslogger.h \
qgsmaptopixel.h \
qgsmarkercatalogue.h \
qgsmarkersymbol.h \
@@ -116,6 +117,7 @@ libqgis_core_la_SOURCES =\
qgslabelattributes.cpp \
qgsline.cpp \
qgslinesymbol.cpp \
qgslogger.cpp \
qgsmaptopixel.cpp \
qgsmarkercatalogue.cpp \
qgsmarkersymbol.cpp \
@@ -0,0 +1,160 @@
/***************************************************************************
qgslogger.cpp - description
-------------------
begin : April 2006
copyright : (C) 2006 by Marco Hugentobler
email : marco.hugentobler at karto dot baug dot ethz dot ch
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/


#include "qgslogger.h"

void QgsLogger::debug(const QString& msg, int debuglevel, const char* file, const char* function, int line)
{
const char* dfile = debugFile();
if(dfile) //exit if QGIS_DEBUG_FILE is set and the message comes from the wrong file
{
if(!file || strcmp(dfile, file) != 0)
{
return;
}
}

int dlevel = debugLevel();
if(dlevel >= debuglevel && debuglevel > 0)
{
if(file == NULL)
{
qDebug(msg.toLocal8Bit().data());
}
else if(function == NULL)
{
qDebug("File: %s, Message: %s", file, msg.toLocal8Bit().data());
}
else if(line == -1)
{
qDebug("File: %s, Function: %s, Message: %s", file, function, msg.toLocal8Bit().data());
}
else
{
qDebug("File: %s, Function: %s, Line: %d, Message: %s", file, function, line, msg.toLocal8Bit().data());
}
}
}

void QgsLogger::debug(const QString& var, int val, int debuglevel, const char* file, const char* function, int line)
{
const char* dfile = debugFile();
if(dfile) //exit if QGIS_DEBUG_FILE is set and the message comes from the wrong file
{
if(!file || strcmp(dfile, file) != 0)
{
return;
}
}

int dlevel = debugLevel();
if(dlevel >= debuglevel && debuglevel > 0)
{
if(file == NULL)
{
qDebug("Variable: %s, Value: %d", var.toLocal8Bit().data(), val);
}
else if(function == NULL)
{
qDebug("File: %s, Variable: %s, Value: %d", file, var.toLocal8Bit().data(), val);
}
else if(line == -1)
{
qDebug("File: %s, Function: %s, Variable: %s, Value: %d", file, function, var.toLocal8Bit().data(), val);
}
else
{
qDebug("File: %s, Function: %s, Line: %d, Variable: %s, Value: %d", file, function, line, var.toLocal8Bit().data(), val);
}
}
}

void QgsLogger::debug(const QString& var, double val, int debuglevel, const char* file, const char* function, int line)
{
const char* dfile = debugFile();
if(dfile) //exit if QGIS_DEBUG_FILE is set and the message comes from the wrong file
{
if(!file || strcmp(dfile, file) != 0)
{
return;
}
}

int dlevel = debugLevel();
if(dlevel >= debuglevel && debuglevel > 0)
{
if(file == NULL)
{
qDebug("Variable: %s, Value: %f", var.toLocal8Bit().data(), val);
}
else if(function == NULL)
{
qDebug("File: %s, Variable: %s, Value: %f", file, var.toLocal8Bit().data(), val);
}
else if(line == -1)
{
qDebug("File: %s, Function: %s, Variable: %s, Value: %f", file, function, var.toLocal8Bit().data(), val);
}
else
{
qDebug("File: %s, Function: %s, Line: %d, Variable: %s, Value: %f", file, function, line, var.toLocal8Bit().data(), val);
}
}
}

void QgsLogger::warning(const QString& msg)
{
qWarning(msg.toLocal8Bit().data());
}

void QgsLogger::critical(const QString& msg)
{
qCritical(msg.toLocal8Bit().data());
}

void QgsLogger::fatal(const QString& msg)
{
qFatal(msg.toLocal8Bit().data());
}

int QgsLogger::debugLevel()
{
const char* dlevel = getenv("QGIS_DEBUG");
if(dlevel == NULL) //environment variable not set
{
#ifdef QGISDEBUG
return 1; //1 is default value in debug mode
#else
return 0;
#endif
}
int level = atoi(dlevel);
#ifdef QGISDEBUG
if(level == 0)
{
level = 1;
}
#endif
return level;
}

const char* QgsLogger::debugFile()
{
const char* dfile = getenv("QGIS_DEBUG_FILE");
return dfile;
}
@@ -0,0 +1,96 @@
/***************************************************************************
qgslogger.h - description
-------------------
begin : April 2006
copyright : (C) 2006 by Marco Hugentobler
email : marco.hugentobler at karto dot baug dot ethz dot ch
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSLOGGER
#define QGSLOGGER

#include <iostream>
#include <sstream>
#include <QString>

#define QgsDebugMsg(str) QgsLogger::debug(QString(str), 1, __FILE__, __FUNCTION__, __LINE__);

/**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:
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
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
*/

class QgsLogger
{
public:

/**Goes to qDebug.
@param msg the message to be printed
@param debuglevel
@param file filename where the message comes from
@param function function where the message comes from
@param line place in file where the message comes from*/
static void debug(const QString& msg, int debuglevel = 1, const char* file = NULL, const char* function = NULL, int line = -1);

/**Similar to the previous method, but prints a variable int-value pair*/
static void debug(const QString& var, int val, int debuglevel = 1, const char* file = NULL, const char* function = NULL, int line = -1);

/**Similar to the previous method, but prints a variable double-value pair*/
static void debug(const QString& var, double val, int debuglevel = 1, const char* file = NULL, const char* function = NULL, int line = -1);

/**Prints out a variable/value pair for types with overloaded operator<<*/
template <typename T> static void debug(const QString& var, T val, const char* file = 0, const char* function = 0, \
int line = -1, int debuglevel = 1)
{
const char* dfile = debugFile();
if(dfile) //exit if QGIS_DEBUG_FILE is set and the message comes from the wrong file
{
if(!file || strcmp(dfile, file) != 0)
{
return;
}
}
std::ostringstream os;
os << var.toLocal8Bit().data() << " = " << val;
if(line == -1)
{
qDebug("File: %s\nFunction: %s\nMessage: %s",
file, function, os.str().c_str());
}
else
{
qDebug("File: %s\nFunction: %s\nLine: %s\nMessage: %s", file, function, \
QString::number(line).toLocal8Bit().data(), os.str().c_str());
}
}

/**Goes to qWarning*/
static void warning(const QString& msg);

/**Goes to qCritical*/
static void critical(const QString& msg);

/**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();

/**Reads the environment variable QGIS_DEBUG_FILE. Returns NULL if the variable is not set*/
static const char* debugFile();
};

#endif

0 comments on commit b0708e7

Please sign in to comment.
You can’t perform that action at this time.