20 #include <QApplication> 
   23 #include <QElapsedTimer> 
   26 #ifndef CMAKE_SOURCE_DIR 
   27 #error CMAKE_SOURCE_DIR undefined 
   28 #endif // CMAKE_SOURCE_DIR 
   30 int QgsLogger::sDebugLevel = -999; 
 
   31 int QgsLogger::sPrefixLength = -1;
 
   36 void QgsLogger::init()
 
   38   if ( sDebugLevel != -999 )
 
   43   *sLogFile() = getenv( 
"QGIS_LOG_FILE" ) ? getenv( 
"QGIS_LOG_FILE" ) : 
"";
 
   44   *sFileFilter() = getenv( 
"QGIS_DEBUG_FILE" ) ? getenv( 
"QGIS_DEBUG_FILE" ) : 
"";
 
   45   sDebugLevel = getenv( 
"QGIS_DEBUG" ) ? atoi( getenv( 
"QGIS_DEBUG" ) ) :
 
   53   sPrefixLength = 
sizeof( CMAKE_SOURCE_DIR );
 
   54   if ( CMAKE_SOURCE_DIR[sPrefixLength - 1] == 
'/' )
 
   58 void QgsLogger::debug( 
const QString &msg, 
int debuglevel, 
const char *file, 
const char *
function, 
int line )
 
   62   if ( !file && !sFileFilter()->isEmpty() && !sFileFilter()->endsWith( file ) )
 
   65   if ( sDebugLevel == 0 || debuglevel > sDebugLevel )
 
   73     if ( qApp && qApp->thread() != QThread::currentThread() )
 
   75       m.prepend( QStringLiteral( 
"[thread:0x%1] " ).arg( 
reinterpret_cast< qint64 
>( QThread::currentThread() ), 0, 16 ) );
 
   78     m.prepend( QStringLiteral( 
"[%1ms] " ).arg( sTime()->elapsed() ) );
 
   83       m.prepend( QStringLiteral( 
" (%1) " ).arg( 
function ) );
 
   89       m.prepend( QStringLiteral( 
":%1 :" ).arg( line ) );
 
   91       m.prepend( QString( 
"(%1) :" ).arg( line ) );
 
   96     m.prepend( file + ( file[0] == 
'/' ? sPrefixLength : 0 ) );
 
  102   if ( sLogFile()->isEmpty() )
 
  104     qDebug( 
"%s", m.toUtf8().constData() );
 
  112 void QgsLogger::debug( 
const QString &var, 
int val, 
int debuglevel, 
const char *file, 
const char *
function, 
int line )
 
  114   debug( QStringLiteral( 
"%1: %2" ).arg( var ).arg( val ), debuglevel, file, 
function, line );
 
  117 void QgsLogger::debug( 
const QString &var, 
double val, 
int debuglevel, 
const char *file, 
const char *
function, 
int line )
 
  119   debug( QStringLiteral( 
"%1: %2" ).arg( var ).arg( val ), debuglevel, file, 
function, line );
 
  125   qWarning( 
"Logged warning: %s", msg.toLocal8Bit().constData() );
 
  131   qCritical( 
"Logged critical: %s", msg.toLocal8Bit().constData() );
 
  137   qFatal( 
"Logged fatal: %s", msg.toLocal8Bit().constData() );
 
  142   if ( sLogFile()->isEmpty() )
 
  146   QFile file( *sLogFile() );
 
  147   if ( !file.open( QIODevice::Append ) )
 
  149   file.write( message.toLocal8Bit().constData() );