20 #include <QApplication> 25 #ifndef CMAKE_SOURCE_DIR 26 #error CMAKE_SOURCE_DIR undefined 27 #endif // CMAKE_SOURCE_DIR 29 int QgsLogger::sDebugLevel = -999;
30 int QgsLogger::sPrefixLength = -1;
31 QString QgsLogger::sFileFilter;
32 QString QgsLogger::sLogFile;
33 QTime QgsLogger::sTime;
35 void QgsLogger::init()
37 if ( sDebugLevel != -999 )
42 sLogFile = getenv(
"QGIS_LOG_FILE" ) ? getenv(
"QGIS_LOG_FILE" ) :
"";
43 sFileFilter = getenv(
"QGIS_DEBUG_FILE" ) ? getenv(
"QGIS_DEBUG_FILE" ) :
"";
44 sDebugLevel = getenv(
"QGIS_DEBUG" ) ? atoi( getenv(
"QGIS_DEBUG" ) ) :
52 sPrefixLength =
sizeof( CMAKE_SOURCE_DIR );
53 if ( CMAKE_SOURCE_DIR[sPrefixLength - 1] ==
'/' )
57 void QgsLogger::debug(
const QString &msg,
int debuglevel,
const char *file,
const char *
function,
int line )
61 if ( !file && !sFileFilter.isEmpty() && !sFileFilter.endsWith( file ) )
64 if ( sDebugLevel == 0 || debuglevel > sDebugLevel )
72 if ( qApp && qApp->thread() != QThread::currentThread() )
74 m.prepend( QStringLiteral(
"[thread:0x%1] " ).arg( reinterpret_cast< qint64 >( QThread::currentThread() ), 0, 16 ) );
77 m.prepend( QStringLiteral(
"[%1ms] " ).arg( sTime.elapsed() ) );
82 m.prepend( QStringLiteral(
" (%1) " ).arg(
function ) );
88 m.prepend( QStringLiteral(
": %1:" ).arg( line ) );
90 m.prepend( QString(
"(%1) :" ).arg( line ) );
95 m.prepend( file + ( file[0] ==
'/' ? sPrefixLength : 0 ) );
101 if ( sLogFile.isEmpty() )
103 qDebug(
"%s", m.toUtf8().constData() );
111 void QgsLogger::debug(
const QString &var,
int val,
int debuglevel,
const char *file,
const char *
function,
int line )
113 debug( QStringLiteral(
"%1: %2" ).arg( var ).arg( val ), debuglevel, file,
function, line );
116 void QgsLogger::debug(
const QString &var,
double val,
int debuglevel,
const char *file,
const char *
function,
int line )
118 debug( QStringLiteral(
"%1: %2" ).arg( var ).arg( val ), debuglevel, file,
function, line );
124 qWarning(
"%s", msg.toLocal8Bit().constData() );
130 qCritical(
"%s", msg.toLocal8Bit().constData() );
136 qFatal(
"%s", msg.toLocal8Bit().constData() );
141 if ( sLogFile.isEmpty() )
145 QFile file( sLogFile );
146 if ( !file.open( QIODevice::Append ) )
148 file.write( message.toLocal8Bit().constData() );
static void critical(const QString &msg)
Goes to qCritical.
static void warning(const QString &msg)
Goes to qWarning.
static void logMessageToFile(const QString &message)
Logs the message passed in to the logfile defined in QGIS_LOG_FILE if any. *.
static void debug(const QString &msg, int debuglevel=1, const char *file=nullptr, const char *function=nullptr, int line=-1)
Goes to qDebug.
static void fatal(const QString &msg)
Goes to qFatal.