21 #include <QApplication>
26 #include "qgsconfig.h"
28 #ifndef CMAKE_SOURCE_DIR
29 #error CMAKE_SOURCE_DIR undefined
30 #endif // CMAKE_SOURCE_DIR
32 int QgsLogger::sDebugLevel = -999;
33 int QgsLogger::sPrefixLength = -1;
34 QString QgsLogger::sFileFilter;
35 QString QgsLogger::sLogFile;
36 QTime QgsLogger::sTime;
38 void QgsLogger::init()
40 if ( sDebugLevel != -999 )
45 sLogFile = getenv(
"QGIS_LOG_FILE" ) ? getenv(
"QGIS_LOG_FILE" ) :
"";
46 sFileFilter = getenv(
"QGIS_DEBUG_FILE" ) ? getenv(
"QGIS_DEBUG_FILE" ) :
"";
47 sDebugLevel = getenv(
"QGIS_DEBUG" ) ? atoi( getenv(
"QGIS_DEBUG" ) ) :
55 sPrefixLength =
sizeof( CMAKE_SOURCE_DIR );
56 if ( CMAKE_SOURCE_DIR[sPrefixLength-1] ==
'/' )
60 void QgsLogger::debug(
const QString& msg,
int debuglevel,
const char*
file,
const char*
function,
int line )
64 if ( !file && !sFileFilter.isEmpty() && !sFileFilter.endsWith( file ) )
67 if ( sDebugLevel == 0 || debuglevel > sDebugLevel )
75 if ( qApp && qApp->thread() != QThread::currentThread() )
77 m.prepend( QString(
"[thread:0x%1] " ).arg(( qint64 ) QThread::currentThread(), 0, 16 ) );
80 m.prepend( QString(
"[%1ms] " ).arg( sTime.elapsed() ) );
85 m.prepend( QString(
" (%1) " ).arg(
function ) );
91 m.prepend( QString(
": %1:" ).arg( line ) );
93 m.prepend( QString(
"(%1) :" ).arg( line ) );
98 m.prepend( file + sPrefixLength );
104 if ( sLogFile.isEmpty() )
106 qDebug(
"%s", m.toLocal8Bit().constData() );
114 void QgsLogger::debug(
const QString& var,
int val,
int debuglevel,
const char*
file,
const char*
function,
int line )
116 debug( QString(
"%1: %2" ).arg( var ).arg( val ), debuglevel, file,
function, line );
119 void QgsLogger::debug(
const QString& var,
double val,
int debuglevel,
const char*
file,
const char*
function,
int line )
121 debug( QString(
"%1: %2" ).arg( var ).arg( val ), debuglevel, file,
function, line );
127 qWarning(
"%s", msg.toLocal8Bit().constData() );
133 qCritical(
"%s", msg.toLocal8Bit().constData() );
139 qFatal(
"%s", msg.toLocal8Bit().constData() );
144 if ( sLogFile.isEmpty() )
148 QFile
file( sLogFile );
149 if ( !file.open( QIODevice::Append ) )
151 file.write( theMessage.toLocal8Bit().constData() );