Skip to content

Commit

Permalink
[FEATURE] Added support for logging to a file. To enable it, set the …
Browse files Browse the repository at this point in the history
…QGIS_LOG_FILE environment variable to a writable file on your file system. When using the QGIS Mapserver, you can enable logging by adding a directive such as 'SetEnv QGIS_LOG_FILE /tmp/qgislog.txt' to the cgi-bin section of your apache config file.
  • Loading branch information
timlinux committed Jun 10, 2011
1 parent 49e9286 commit 6f8deec
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
36 changes: 36 additions & 0 deletions src/core/qgslogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "qgslogger.h"
#include <QtDebug>
#include <QFile>

int QgsLogger::mDebugLevel = -999; // undefined value

Expand All @@ -38,14 +39,17 @@ void QgsLogger::debug( const QString& msg, int debuglevel, const char* file, con
if ( file == NULL )
{
qDebug( "%s", msg.toLocal8Bit().constData() );
logMessageToFile( msg );
}
else if ( function == NULL )
{
qDebug( "%s: %s", file, msg.toLocal8Bit().constData() );
logMessageToFile( msg );
}
else if ( line == -1 )
{
qDebug( "%s: (%s) %s", file, function, msg.toLocal8Bit().constData() );
logMessageToFile( msg );
}
else
{
Expand All @@ -54,6 +58,7 @@ void QgsLogger::debug( const QString& msg, int debuglevel, const char* file, con
#else
qDebug( "%s(%d) : (%s) %s", file, line, function, msg.toLocal8Bit().constData() );
#endif
logMessageToFile( msg );
}
}
}
Expand All @@ -75,14 +80,17 @@ void QgsLogger::debug( const QString& var, int val, int debuglevel, const char*
if ( file == NULL )
{
qDebug( "%s: %d", var.toLocal8Bit().constData(), val );
logMessageToFile( QString( "%s: %d" ).arg( var.toLocal8Bit().constData() ).arg( val ) );
}
else if ( function == NULL )
{
qDebug( "%s: %s: %d", file, var.toLocal8Bit().constData(), val );
logMessageToFile( QString( "%s: %s: %d" ).arg( file ).arg( var.toLocal8Bit().constData() ).arg( val ) );
}
else if ( line == -1 )
{
qDebug( "%s: (%s): %s: %d", file, function, var.toLocal8Bit().constData(), val );
logMessageToFile( QString( "%s: (%s): %s: %d" ).arg( file ).arg( function ).arg( var.toLocal8Bit().constData() ).arg( val ) );
}
else
{
Expand All @@ -91,6 +99,7 @@ void QgsLogger::debug( const QString& var, int val, int debuglevel, const char*
#else
qDebug( "%s: %d: (%s), %s: %d", file, line, function, var.toLocal8Bit().constData(), val );
#endif
logMessageToFile( QString( "%s: %d: (%s), %s: %d" ).arg( file ).arg( line ).arg( function ).arg( var.toLocal8Bit().constData() ).arg( val ) );
}
}
}
Expand All @@ -112,14 +121,17 @@ void QgsLogger::debug( const QString& var, double val, int debuglevel, const cha
if ( file == NULL )
{
qDebug( "%s: %f", var.toLocal8Bit().constData(), val );
logMessageToFile( QString( "%s: %f" ).arg( var.toLocal8Bit().constData() ).arg( val ) );
}
else if ( function == NULL )
{
qDebug( "%s: %s: %f", file, var.toLocal8Bit().constData(), val );
logMessageToFile( QString( "%s: %s: %f" ).arg( file ).arg( var.toLocal8Bit().constData() ).arg( val ) );
}
else if ( line == -1 )
{
qDebug( "%s: (%s): %s: %f", file, function, var.toLocal8Bit().constData(), val );
logMessageToFile( QString( "%s: (%s): %s: %f" ).arg( file ).arg( function ).arg( var.toLocal8Bit().constData() ).arg( val ) );
}
else
{
Expand All @@ -128,23 +140,27 @@ void QgsLogger::debug( const QString& var, double val, int debuglevel, const cha
#else
qDebug( "%s: %d: (%s), %s: %f", file, line, function, var.toLocal8Bit().constData(), val );
#endif
logMessageToFile( QString( "%s: %d: (%s), %s: %f").arg( file ).arg( line ).arg( function ).arg( var.toLocal8Bit().constData() ).arg( val ) );
}
}
}

void QgsLogger::warning( const QString& msg )
{
qWarning( "%s", msg.toLocal8Bit().constData() );
logMessageToFile( msg );
}

void QgsLogger::critical( const QString& msg )
{
qCritical( "%s", msg.toLocal8Bit().constData() );
logMessageToFile( msg );
}

void QgsLogger::fatal( const QString& msg )
{
qFatal( "%s", msg.toLocal8Bit().constData() );
logMessageToFile( msg );
}

int QgsLogger::debugLevel()
Expand Down Expand Up @@ -178,6 +194,26 @@ int QgsLogger::debugLevel()
return mDebugLevel;
}

const QString QgsLogger::logFile()
{
const QString logFile = getenv( "QGIS_LOG_FILE" );
return logFile;
}

const void QgsLogger::logMessageToFile( QString theMessage )
{
if ( ! logFile().isEmpty() )
{
//Maybe more efficient to keep the file open for the life of qgis...
QFile file( logFile() );
file.open(QIODevice::Append);
file.write( theMessage.toStdString().c_str() );
file.write( "\n" );
file.close();
}
return;
}

const char* QgsLogger::debugFile()
{
const char* dfile = getenv( "QGIS_DEBUG_FILE" );
Expand Down
13 changes: 12 additions & 1 deletion src/core/qgslogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <iostream>
#include <sstream>
#include <QString>
class QFile;

#ifdef QGISDEBUG
#define QgsDebugMsg(str) QgsLogger::debug(QString(str), 1, __FILE__, __FUNCTION__, __LINE__)
Expand All @@ -45,6 +46,9 @@
* QGIS_DEBUG_FILE may contain a file name. 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
*
* QGIS_LOG_FILE may contain a file name. If set, all messages will be appended
* to this file rather than to stdout.
*/

class CORE_EXPORT QgsLogger
Expand Down Expand Up @@ -107,8 +111,15 @@ class CORE_EXPORT QgsLogger
static int debugLevel();

private:
/**Reads the environment variable QGIS_LOG_FILE. Returns NULL if the variable is not set,
* otherwise returns a file name for writing log messages to.*/
static const QString logFile();

/**Reads the environment variable QGIS_DEBUG_FILE. Returns NULL if the variable is not set*/
/** Logs the message passed in to the logfile defined in QGIS_LOG_FILE if any. **/
static const void logMessageToFile( QString theMessage );

/**Reads the environment variable QGIS_DEBUG_FILE. Returns NULL if the variable is not set.
* If set, only messages from this source file will be sent to logs. */
static const char* debugFile();

/** current debug level */
Expand Down

0 comments on commit 6f8deec

Please sign in to comment.