20#include <QApplication>
23#include <QElapsedTimer>
26#ifndef CMAKE_SOURCE_DIR
27#error CMAKE_SOURCE_DIR undefined
30int QgsLogger::sDebugLevel = -999;
31int QgsLogger::sPrefixLength = -1;
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] ==
'/' )
58void 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() );
112void 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 );
117void 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() );
static void fatal(const QString &msg)
Goes to qFatal.
static void debug(const QString &msg, int debuglevel=1, const char *file=nullptr, const char *function=nullptr, int line=-1)
Goes to qDebug.
static QString logFile()
Reads the environment variable QGIS_LOG_FILE.
static void critical(const QString &msg)
Goes to qCritical.
static void logMessageToFile(const QString &message)
Logs the message passed in to the logfile defined in QGIS_LOG_FILE if any.
static void warning(const QString &msg)
Goes to qWarning.
Q_GLOBAL_STATIC(QReadWriteLock, sDefinitionCacheLock)