20#include <QApplication>
21#include <QElapsedTimer>
27using namespace Qt::StringLiterals;
29#ifndef CMAKE_SOURCE_DIR
30#error CMAKE_SOURCE_DIR undefined
33int QgsLogger::sDebugLevel = -999;
34int QgsLogger::sPrefixLength = -1;
41 if ( sDebugLevel != -999 )
46 *sLogFile() = getenv(
"QGIS_LOG_FILE" ) ? getenv(
"QGIS_LOG_FILE" ) :
"";
47 *sFileFilter() = getenv(
"QGIS_DEBUG_FILE" ) ? getenv(
"QGIS_DEBUG_FILE" ) :
"";
48 sDebugLevel = getenv(
"QGIS_DEBUG" ) ? atoi( getenv(
"QGIS_DEBUG" ) ) :
56 sPrefixLength =
sizeof( CMAKE_SOURCE_DIR );
58 if ( CMAKE_SOURCE_DIR[sPrefixLength - 1] ==
'/' )
62void QgsLogger::debug(
const QString &msg,
int debuglevel,
const char *file,
const char *function,
int line )
66 if ( !file && !sFileFilter()->isEmpty() && !sFileFilter()->endsWith( file ) )
69 if ( sDebugLevel == 0 || debuglevel > sDebugLevel )
77 if ( qApp && qApp->thread() != QThread::currentThread() )
79 m.prepend( u
"[thread:0x%1] "_s.arg(
reinterpret_cast< qint64
>( QThread::currentThread() ), 0, 16 ) );
82 m.prepend( u
"[%1ms] "_s.arg( sTime()->elapsed() ) );
87 m.prepend( u
" (%1) "_s.arg( function ) );
93 m.prepend( u
":%1 :"_s.arg( line ) );
95 m.prepend( QString(
"(%1) :" ).arg( line ) );
100 m.prepend( file + ( file[0] ==
'/' ? sPrefixLength : 0 ) );
106 if ( sLogFile()->isEmpty() )
108 if ( debuglevel == 0 )
111 std::cerr << m.toUtf8().constData() << std::endl;
115 qDebug(
"%s", m.toUtf8().constData() );
124void QgsLogger::debug(
const QString &var,
int val,
int debuglevel,
const char *file,
const char *function,
int line )
126 debug( u
"%1: %2"_s.arg( var ).arg( val ), debuglevel, file, function, line );
129void QgsLogger::debug(
const QString &var,
double val,
int debuglevel,
const char *file,
const char *function,
int line )
131 debug( u
"%1: %2"_s.arg( var ).arg( val ), debuglevel, file, function, line );
137 qWarning(
"Logged warning: %s", msg.toLocal8Bit().constData() );
143 qCritical(
"Logged critical: %s", msg.toLocal8Bit().constData() );
149 qFatal(
"Logged fatal: %s", msg.toLocal8Bit().constData() );
154 if ( sLogFile()->isEmpty() )
158 QFile file( *sLogFile() );
159 if ( !file.open( QIODevice::Append ) )
161 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)