Navigation Menu

Skip to content

Commit

Permalink
refactor QgsLogger and add duration and thread id
Browse files Browse the repository at this point in the history
  • Loading branch information
jef-n committed Nov 12, 2014
1 parent 3d2cc14 commit b5f6d2c
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 200 deletions.
245 changes: 77 additions & 168 deletions src/core/qgslogger.cpp
Expand Up @@ -18,8 +18,10 @@

#include "qgslogger.h"

#include <QApplication>
#include <QtDebug>
#include <QFile>
#include <QThread>

#include "qgsconfig.h"

Expand All @@ -29,136 +31,94 @@

int QgsLogger::sDebugLevel = -999; // undefined value
int QgsLogger::sPrefixLength = -1;
QString QgsLogger::sFileFilter;
QString QgsLogger::sLogFile;
QTime QgsLogger::sTime;

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

int dlevel = debugLevel();
if ( dlevel >= debuglevel && debuglevel > 0 )
{
QString m;

if ( !file )
{
m = msg;
}
else if ( !function )
{
m = QString( "%1: %2" ).arg( file + sPrefixLength ).arg( msg );
}
else if ( line == -1 )
{
m = QString( "%1: (%2) %3" ).arg( file + sPrefixLength ).arg( function ).arg( msg );
}
else
{
#ifndef _MSC_VER
m = QString( "%1: %2: (%3) %4" ).arg( file + sPrefixLength ).arg( line ).arg( function ).arg( msg );
sTime.start();

sFileFilter = getenv( "QGIS_DEBUG_FILE" ) ? getenv( "QGIS_DEBUG_FILE" ) : "";
sDebugLevel = getenv( "QGIS_DEBUG" ) ? atoi( getenv( "QGIS_DEBUG" ) ) :
#ifdef QGISDEBUG
1
#else
m = QString( "%1(%2) : (%3) %4" ).arg( file ).arg( line ).arg( function ).arg( msg );
0
#endif
}
if ( logFile().isEmpty() )
{
qDebug( "%s", m.toLocal8Bit().constData() );
}
else
{
logMessageToFile( m );
}
}
;

sPrefixLength = sizeof( CMAKE_SOURCE_DIR );
if ( CMAKE_SOURCE_DIR[sPrefixLength-1] == '/' )
sPrefixLength++;
}

void QgsLogger::debug( const QString& var, int val, int debuglevel, const char* file, const char* function, int line )
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
init();

if ( !file && !sFileFilter.isEmpty() && !sFileFilter.endsWith( file ) )
return;

if ( sDebugLevel == 0 || debuglevel > sDebugLevel )
return;

QString m = msg;

if ( qApp && qApp->thread() != QThread::currentThread() )
{
if ( !file || strncmp( dfile, file, strlen( dfile ) ) != 0 )
{
return;
}
m.prepend( QString( "[thread:0x%1] " ).arg( (qint64) QThread::currentThread(), 0, 16 ) );
}

int dlevel = debugLevel();
if ( dlevel >= debuglevel && debuglevel > 0 )
m.prepend( QString( "[%1ms] " ).arg( sTime.elapsed() ) );
sTime.restart();

if ( function )
{
if ( !file )
{
qDebug( "%s: %d", var.toLocal8Bit().constData(), val );
logMessageToFile( QString( "%s: %d" ).arg( var.toLocal8Bit().constData() ).arg( val ) );
}
else if ( !function )
{
qDebug( "%s: %s: %d", file + sPrefixLength, var.toLocal8Bit().constData(), val );
logMessageToFile( QString( "%s: %s: %d" ).arg( file + sPrefixLength ).arg( var.toLocal8Bit().constData() ).arg( val ) );
}
else if ( line == -1 )
{
qDebug( "%s: (%s): %s: %d", file + sPrefixLength, function, var.toLocal8Bit().constData(), val );
logMessageToFile( QString( "%s: (%s): %s: %d" ).arg( file + sPrefixLength ).arg( function ).arg( var.toLocal8Bit().constData() ).arg( val ) );
}
else
{
#ifdef _MSC_VER
qDebug( "%s(%d): (%s), %s: %d", file + sPrefixLength, line, function, var.toLocal8Bit().constData(), val );
m.prepend( QString( " (%1) " ).arg( function ) );
}

if ( file )
{
if( line != -1 )
{
#ifndef _MSC_VER
m.prepend( QString( ": %1:" ).arg( line ) );
#else
m.prepend( QString( "(%1) :" ).arg( line ) );
#endif
}

#ifndef _MSC_VER
m.prepend( file + sPrefixLength );
#else
qDebug( "%s: %d: (%s), %s: %d", file + sPrefixLength, line, function, var.toLocal8Bit().constData(), val );
m.prepend( file );
#endif
logMessageToFile( QString( "%s: %d: (%s), %s: %d" ).arg( file + sPrefixLength ).arg( line ).arg( function ).arg( var.toLocal8Bit().constData() ).arg( 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 ( sLogFile.isEmpty() )
{
if ( !file || strncmp( dfile, file, strlen( dfile ) ) != 0 )
{
return;
}
qDebug( "%s", m.toLocal8Bit().constData() );
}

int dlevel = debugLevel();
if ( dlevel >= debuglevel && debuglevel > 0 )
else
{
if ( !file )
{
qDebug( "%s: %f", var.toLocal8Bit().constData(), val );
logMessageToFile( QString( "%s: %f" ).arg( var.toLocal8Bit().constData() ).arg( val ) );
}
else if ( !function )
{
qDebug( "%s: %s: %f", file + sPrefixLength, var.toLocal8Bit().constData(), val );
logMessageToFile( QString( "%s: %s: %f" ).arg( file + sPrefixLength ).arg( var.toLocal8Bit().constData() ).arg( val ) );
}
else if ( line == -1 )
{
qDebug( "%s: (%s): %s: %f", file + sPrefixLength, function, var.toLocal8Bit().constData(), val );
logMessageToFile( QString( "%s: (%s): %s: %f" ).arg( file + sPrefixLength ).arg( function ).arg( var.toLocal8Bit().constData() ).arg( val ) );
}
else
{
#ifdef _MSC_VER
qDebug( "%s(%d): (%s), %s: %f", file + sPrefixLength, line, function, var.toLocal8Bit().constData(), val );
#else
qDebug( "%s: %d: (%s), %s: %f", file + sPrefixLength, line, function, var.toLocal8Bit().constData(), val );
#endif
logMessageToFile( QString( "%s: %d: (%s), %s: %f" ).arg( file + sPrefixLength ).arg( line ).arg( function ).arg( var.toLocal8Bit().constData() ).arg( val ) );
}
logMessageToFile( m );
}
}

void QgsLogger::debug( const QString& var, int val, int debuglevel, const char* file, const char* function, int line )
{
debug( QString( "%1: %2" ).arg( var ).arg( val ), debuglevel, file, function, line );
}

void QgsLogger::debug( const QString& var, double val, int debuglevel, const char* file, const char* function, int line )
{
debug( QString( "%1: %2" ).arg( var ).arg( val ), debuglevel, file, function, line );
}

void QgsLogger::warning( const QString& msg )
{
logMessageToFile( msg );
Expand All @@ -177,66 +137,15 @@ void QgsLogger::fatal( const QString& msg )
qFatal( "%s", msg.toLocal8Bit().constData() );
}

int QgsLogger::debugLevel()
{
if ( sPrefixLength == -1 )
{
sPrefixLength = sizeof( CMAKE_SOURCE_DIR );
if ( CMAKE_SOURCE_DIR[sPrefixLength-1] == '/' )
sPrefixLength++;
}

if ( sDebugLevel == -999 )
{
// read the environment variable QGIS_DEBUG just once,
// then reuse the value

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

return sDebugLevel;
}

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

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" );
return dfile;
if ( sLogFile.isEmpty() )
return;

//Maybe more efficient to keep the file open for the life of qgis...
QFile file( sLogFile );
file.open( QIODevice::Append );
file.write( theMessage.toLocal8Bit().constData() );
file.write( "\n" );
file.close();
}
41 changes: 9 additions & 32 deletions src/core/qgslogger.h
Expand Up @@ -21,15 +21,12 @@
#include <iostream>
#include <sstream>
#include <QString>
#include <QTime>
class QFile;

#ifdef QGISDEBUG
#define QgsDebugMsg(str) QgsLogger::debug(QString(str), 1, __FILE__, __FUNCTION__, __LINE__)
#define QgsDebugMsgLevel(str, level) \
{ \
if ( QgsLogger::debugLevel() >= (level) && (level) > 0 ) \
QgsLogger::debug(QString(str), (level), __FILE__, __FUNCTION__, __LINE__); \
}
#define QgsDebugMsgLevel(str, level) QgsLogger::debug(QString(str), (level), __FILE__, __FUNCTION__, __LINE__)
#define QgsDebugCall QgsScopeLogger _qgsScopeLogger(__FILE__, __FUNCTION__, __LINE__)
#else
#define QgsDebugCall
Expand Down Expand Up @@ -77,29 +74,9 @@ class CORE_EXPORT QgsLogger
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 )
{
Q_UNUSED( debuglevel );
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( "%s: (%s) %s", file + sPrefixLength, function, os.str().c_str() );
}
else
{
#if defined(_MSC_VER)
qDebug( "%s(%d): (%s) %s", file + sPrefixLength, line, function, os.str().c_str() );
#else
qDebug( "%s: %d: (%s) %s", file + sPrefixLength, line, function, os.str().c_str() );
#endif
}
debug( var, os.str().c_str(), file, function, line, debuglevel );
}

/**Goes to qWarning*/
Expand All @@ -113,24 +90,24 @@ class CORE_EXPORT QgsLogger

/**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();
static int debugLevel() { init(); return sDebugLevel; }

/** Logs the message passed in to the logfile defined in QGIS_LOG_FILE if any. **/
static void logMessageToFile( QString theMessage );

/**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();
static const QString logFile() { init(); return sLogFile; }

private:

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

/** current debug level */
static int sDebugLevel;
static int sPrefixLength;
static QString sLogFile;
static QString sFileFilter;
static QTime sTime;
};

class QgsScopeLogger
Expand Down

0 comments on commit b5f6d2c

Please sign in to comment.