QGIS API Documentation  3.8.0-Zanzibar (11aff65)
qgslogger.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgslogger.cpp - description
3  -------------------
4  begin : April 2006
5  copyright : (C) 2006 by Marco Hugentobler
6  email : marco.hugentobler at karto dot baug dot ethz dot ch
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgslogger.h"
19 
20 #include <QApplication>
21 #include <QtDebug>
22 #include <QFile>
23 #include <QThread>
24 
25 #ifndef CMAKE_SOURCE_DIR
26 #error CMAKE_SOURCE_DIR undefined
27 #endif // CMAKE_SOURCE_DIR
28 
29 int QgsLogger::sDebugLevel = -999; // undefined value
30 int QgsLogger::sPrefixLength = -1;
31 QString QgsLogger::sFileFilter;
32 QString QgsLogger::sLogFile;
33 QTime QgsLogger::sTime;
34 
35 void QgsLogger::init()
36 {
37  if ( sDebugLevel != -999 )
38  return;
39 
40  sTime.start();
41 
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" ) ) :
45 #ifdef QGISDEBUG
46  1
47 #else
48  0
49 #endif
50  ;
51 
52  sPrefixLength = sizeof( CMAKE_SOURCE_DIR );
53  if ( CMAKE_SOURCE_DIR[sPrefixLength - 1] == '/' )
54  sPrefixLength++;
55 }
56 
57 void QgsLogger::debug( const QString &msg, int debuglevel, const char *file, const char *function, int line )
58 {
59  init();
60 
61  if ( !file && !sFileFilter.isEmpty() && !sFileFilter.endsWith( file ) )
62  return;
63 
64  if ( sDebugLevel == 0 || debuglevel > sDebugLevel )
65  return;
66 
67 
68  QString m = msg;
69 
70  if ( file )
71  {
72  if ( qApp && qApp->thread() != QThread::currentThread() )
73  {
74  m.prepend( QStringLiteral( "[thread:0x%1] " ).arg( reinterpret_cast< qint64 >( QThread::currentThread() ), 0, 16 ) );
75  }
76 
77  m.prepend( QStringLiteral( "[%1ms] " ).arg( sTime.elapsed() ) );
78  sTime.restart();
79 
80  if ( function )
81  {
82  m.prepend( QStringLiteral( " (%1) " ).arg( function ) );
83  }
84 
85  if ( line != -1 )
86  {
87 #ifndef _MSC_VER
88  m.prepend( QStringLiteral( ":%1 :" ).arg( line ) );
89 #else
90  m.prepend( QString( "(%1) :" ).arg( line ) );
91 #endif
92  }
93 
94 #ifndef _MSC_VER
95  m.prepend( file + ( file[0] == '/' ? sPrefixLength : 0 ) );
96 #else
97  m.prepend( file );
98 #endif
99  }
100 
101  if ( sLogFile.isEmpty() )
102  {
103  qDebug( "%s", m.toUtf8().constData() );
104  }
105  else
106  {
107  logMessageToFile( m );
108  }
109 }
110 
111 void QgsLogger::debug( const QString &var, int val, int debuglevel, const char *file, const char *function, int line )
112 {
113  debug( QStringLiteral( "%1: %2" ).arg( var ).arg( val ), debuglevel, file, function, line );
114 }
115 
116 void QgsLogger::debug( const QString &var, double val, int debuglevel, const char *file, const char *function, int line )
117 {
118  debug( QStringLiteral( "%1: %2" ).arg( var ).arg( val ), debuglevel, file, function, line );
119 }
120 
121 void QgsLogger::warning( const QString &msg )
122 {
123  logMessageToFile( msg );
124  qWarning( "%s", msg.toLocal8Bit().constData() );
125 }
126 
127 void QgsLogger::critical( const QString &msg )
128 {
129  logMessageToFile( msg );
130  qCritical( "%s", msg.toLocal8Bit().constData() );
131 }
132 
133 void QgsLogger::fatal( const QString &msg )
134 {
135  logMessageToFile( msg );
136  qFatal( "%s", msg.toLocal8Bit().constData() );
137 }
138 
139 void QgsLogger::logMessageToFile( const QString &message )
140 {
141  if ( sLogFile.isEmpty() )
142  return;
143 
144  //Maybe more efficient to keep the file open for the life of qgis...
145  QFile file( sLogFile );
146  if ( !file.open( QIODevice::Append ) )
147  return;
148  file.write( message.toLocal8Bit().constData() );
149  file.write( "\n" );
150  file.close();
151 }
static void critical(const QString &msg)
Goes to qCritical.
Definition: qgslogger.cpp:127
static void warning(const QString &msg)
Goes to qWarning.
Definition: qgslogger.cpp:121
static void logMessageToFile(const QString &message)
Logs the message passed in to the logfile defined in QGIS_LOG_FILE if any. *.
Definition: qgslogger.cpp:139
static void debug(const QString &msg, int debuglevel=1, const char *file=nullptr, const char *function=nullptr, int line=-1)
Goes to qDebug.
Definition: qgslogger.cpp:57
static void fatal(const QString &msg)
Goes to qFatal.
Definition: qgslogger.cpp:133