Quantum GIS API Documentation
1.8
|
00001 /*************************************************************************** 00002 qgslogger.cpp - description 00003 ------------------- 00004 begin : April 2006 00005 copyright : (C) 2006 by Marco Hugentobler 00006 email : marco.hugentobler at karto dot baug dot ethz dot ch 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * * 00011 * This program is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU General Public License as published by * 00013 * the Free Software Foundation; either version 2 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 ***************************************************************************/ 00017 00018 00019 #include "qgslogger.h" 00020 00021 #include <QtDebug> 00022 #include <QFile> 00023 00024 #include "qgsconfig.h" 00025 00026 #ifndef CMAKE_SOURCE_DIR 00027 #error CMAKE_SOURCE_DIR undefinied 00028 #endif // CMAKE_SOURCE_DIR 00029 00030 int QgsLogger::sDebugLevel = -999; // undefined value 00031 int QgsLogger::sPrefixLength = -1; 00032 00033 void QgsLogger::debug( const QString& msg, int debuglevel, const char* file, const char* function, int line ) 00034 { 00035 const char* dfile = debugFile(); 00036 if ( dfile ) //exit if QGIS_DEBUG_FILE is set and the message comes from the wrong file 00037 { 00038 if ( !file || strcmp( dfile, file ) != 0 ) 00039 { 00040 return; 00041 } 00042 } 00043 00044 int dlevel = debugLevel(); 00045 if ( dlevel >= debuglevel && debuglevel > 0 ) 00046 { 00047 QString m; 00048 00049 if ( !file ) 00050 { 00051 m = msg; 00052 } 00053 else if ( !function ) 00054 { 00055 m = QString( "%1: %2" ).arg( file + sPrefixLength ).arg( msg ); 00056 } 00057 else if ( line == -1 ) 00058 { 00059 m = QString( "%1: (%2) %3" ).arg( file + sPrefixLength ).arg( function ).arg( msg ); 00060 } 00061 else 00062 { 00063 #ifndef _MSC_VER 00064 m = QString( "%1: %2: (%3) %4" ).arg( file + sPrefixLength ).arg( line ).arg( function ).arg( msg ); 00065 #else 00066 m = QString( "%1(%2) : (%3) %4" ).arg( file ).arg( line ).arg( function ).arg( msg ); 00067 #endif 00068 } 00069 if ( logFile().isEmpty() ) 00070 { 00071 qDebug( "%s", m.toLocal8Bit().constData() ); 00072 } 00073 else 00074 { 00075 logMessageToFile( m ); 00076 } 00077 } 00078 } 00079 00080 void QgsLogger::debug( const QString& var, int val, int debuglevel, const char* file, const char* function, int line ) 00081 { 00082 const char* dfile = debugFile(); 00083 if ( dfile ) //exit if QGIS_DEBUG_FILE is set and the message comes from the wrong file 00084 { 00085 if ( !file || strcmp( dfile, file ) != 0 ) 00086 { 00087 return; 00088 } 00089 } 00090 00091 int dlevel = debugLevel(); 00092 if ( dlevel >= debuglevel && debuglevel > 0 ) 00093 { 00094 if ( !file ) 00095 { 00096 qDebug( "%s: %d", var.toLocal8Bit().constData(), val ); 00097 logMessageToFile( QString( "%s: %d" ).arg( var.toLocal8Bit().constData() ).arg( val ) ); 00098 } 00099 else if ( !function ) 00100 { 00101 qDebug( "%s: %s: %d", file + sPrefixLength, var.toLocal8Bit().constData(), val ); 00102 logMessageToFile( QString( "%s: %s: %d" ).arg( file + sPrefixLength ).arg( var.toLocal8Bit().constData() ).arg( val ) ); 00103 } 00104 else if ( line == -1 ) 00105 { 00106 qDebug( "%s: (%s): %s: %d", file + sPrefixLength, function, var.toLocal8Bit().constData(), val ); 00107 logMessageToFile( QString( "%s: (%s): %s: %d" ).arg( file + sPrefixLength ).arg( function ).arg( var.toLocal8Bit().constData() ).arg( val ) ); 00108 } 00109 else 00110 { 00111 #ifdef _MSC_VER 00112 qDebug( "%s(%d): (%s), %s: %d", file + sPrefixLength, line, function, var.toLocal8Bit().constData(), val ); 00113 #else 00114 qDebug( "%s: %d: (%s), %s: %d", file + sPrefixLength, line, function, var.toLocal8Bit().constData(), val ); 00115 #endif 00116 logMessageToFile( QString( "%s: %d: (%s), %s: %d" ).arg( file + sPrefixLength ).arg( line ).arg( function ).arg( var.toLocal8Bit().constData() ).arg( val ) ); 00117 } 00118 } 00119 } 00120 00121 void QgsLogger::debug( const QString& var, double val, int debuglevel, const char* file, const char* function, int line ) 00122 { 00123 const char* dfile = debugFile(); 00124 if ( dfile ) //exit if QGIS_DEBUG_FILE is set and the message comes from the wrong file 00125 { 00126 if ( !file || strcmp( dfile, file ) != 0 ) 00127 { 00128 return; 00129 } 00130 } 00131 00132 int dlevel = debugLevel(); 00133 if ( dlevel >= debuglevel && debuglevel > 0 ) 00134 { 00135 if ( !file ) 00136 { 00137 qDebug( "%s: %f", var.toLocal8Bit().constData(), val ); 00138 logMessageToFile( QString( "%s: %f" ).arg( var.toLocal8Bit().constData() ).arg( val ) ); 00139 } 00140 else if ( !function ) 00141 { 00142 qDebug( "%s: %s: %f", file + sPrefixLength, var.toLocal8Bit().constData(), val ); 00143 logMessageToFile( QString( "%s: %s: %f" ).arg( file + sPrefixLength ).arg( var.toLocal8Bit().constData() ).arg( val ) ); 00144 } 00145 else if ( line == -1 ) 00146 { 00147 qDebug( "%s: (%s): %s: %f", file + sPrefixLength, function, var.toLocal8Bit().constData(), val ); 00148 logMessageToFile( QString( "%s: (%s): %s: %f" ).arg( file + sPrefixLength ).arg( function ).arg( var.toLocal8Bit().constData() ).arg( val ) ); 00149 } 00150 else 00151 { 00152 #ifdef _MSC_VER 00153 qDebug( "%s(%d): (%s), %s: %f", file + sPrefixLength, line, function, var.toLocal8Bit().constData(), val ); 00154 #else 00155 qDebug( "%s: %d: (%s), %s: %f", file + sPrefixLength, line, function, var.toLocal8Bit().constData(), val ); 00156 #endif 00157 logMessageToFile( QString( "%s: %d: (%s), %s: %f" ).arg( file + sPrefixLength ).arg( line ).arg( function ).arg( var.toLocal8Bit().constData() ).arg( val ) ); 00158 } 00159 } 00160 } 00161 00162 void QgsLogger::warning( const QString& msg ) 00163 { 00164 logMessageToFile( msg ); 00165 qWarning( "%s", msg.toLocal8Bit().constData() ); 00166 } 00167 00168 void QgsLogger::critical( const QString& msg ) 00169 { 00170 logMessageToFile( msg ); 00171 qCritical( "%s", msg.toLocal8Bit().constData() ); 00172 } 00173 00174 void QgsLogger::fatal( const QString& msg ) 00175 { 00176 logMessageToFile( msg ); 00177 qFatal( "%s", msg.toLocal8Bit().constData() ); 00178 } 00179 00180 int QgsLogger::debugLevel() 00181 { 00182 if ( sPrefixLength == -1 ) 00183 { 00184 sPrefixLength = strlen( CMAKE_SOURCE_DIR ) + 1; 00185 } 00186 00187 if ( sDebugLevel == -999 ) 00188 { 00189 // read the environment variable QGIS_DEBUG just once, 00190 // then reuse the value 00191 00192 const char* dlevel = getenv( "QGIS_DEBUG" ); 00193 if ( dlevel == NULL ) //environment variable not set 00194 { 00195 #ifdef QGISDEBUG 00196 sDebugLevel = 1; //1 is default value in debug mode 00197 #else 00198 sDebugLevel = 0; 00199 #endif 00200 } 00201 else 00202 { 00203 sDebugLevel = atoi( dlevel ); 00204 #ifdef QGISDEBUG 00205 if ( sDebugLevel == 0 ) 00206 { 00207 sDebugLevel = 1; 00208 } 00209 #endif 00210 } 00211 } 00212 00213 return sDebugLevel; 00214 } 00215 00216 const QString QgsLogger::logFile() 00217 { 00218 const QString logFile = getenv( "QGIS_LOG_FILE" ); 00219 return logFile; 00220 } 00221 00222 void QgsLogger::logMessageToFile( QString theMessage ) 00223 { 00224 if ( ! logFile().isEmpty() ) 00225 { 00226 //Maybe more efficient to keep the file open for the life of qgis... 00227 QFile file( logFile() ); 00228 file.open( QIODevice::Append ); 00229 file.write( theMessage.toStdString().c_str() ); 00230 file.write( "\n" ); 00231 file.close(); 00232 } 00233 return; 00234 } 00235 00236 const char* QgsLogger::debugFile() 00237 { 00238 const char* dfile = getenv( "QGIS_DEBUG_FILE" ); 00239 return dfile; 00240 }