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 );
55 if ( CMAKE_SOURCE_DIR[sPrefixLength - 1] ==
'/' )
59void QgsLogger::debug(
const QString &msg,
int debuglevel,
const char *file,
const char *function,
int line )
63 if ( !file && !sFileFilter()->isEmpty() && !sFileFilter()->endsWith( file ) )
66 if ( sDebugLevel == 0 || debuglevel > sDebugLevel )
74 if ( qApp && qApp->thread() != QThread::currentThread() )
76 m.prepend( QStringLiteral(
"[thread:0x%1] " ).arg(
reinterpret_cast< qint64
>( QThread::currentThread() ), 0, 16 ) );
79 m.prepend( QStringLiteral(
"[%1ms] " ).arg( sTime()->elapsed() ) );
84 m.prepend( QStringLiteral(
" (%1) " ).arg( function ) );
90 m.prepend( QStringLiteral(
":%1 :" ).arg( line ) );
92 m.prepend( QString(
"(%1) :" ).arg( line ) );
97 m.prepend( file + ( file[0] ==
'/' ? sPrefixLength : 0 ) );
103 if ( sLogFile()->isEmpty() )
105 if ( debuglevel == 0 )
108 std::cerr << m.toUtf8().constData() << std::endl;
112 qDebug(
"%s", m.toUtf8().constData() );
121void QgsLogger::debug(
const QString &var,
int val,
int debuglevel,
const char *file,
const char *function,
int line )
123 debug( QStringLiteral(
"%1: %2" ).arg( var ).arg( val ), debuglevel, file, function, line );
126void QgsLogger::debug(
const QString &var,
double val,
int debuglevel,
const char *file,
const char *function,
int line )
128 debug( QStringLiteral(
"%1: %2" ).arg( var ).arg( val ), debuglevel, file, function, line );
134 qWarning(
"Logged warning: %s", msg.toLocal8Bit().constData() );
140 qCritical(
"Logged critical: %s", msg.toLocal8Bit().constData() );
146 qFatal(
"Logged fatal: %s", msg.toLocal8Bit().constData() );
151 if ( sLogFile()->isEmpty() )
155 QFile file( *sLogFile() );
156 if ( !file.open( QIODevice::Append ) )
158 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)