Quantum GIS API Documentation  1.7.4
src/core/qgslogger.cpp
Go to the documentation of this file.
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 #include <QtDebug>
00021 
00022 int QgsLogger::mDebugLevel = -999; // undefined value
00023 
00024 void QgsLogger::debug( const QString& msg, int debuglevel, const char* file, const char* function, int line )
00025 {
00026   const char* dfile = debugFile();
00027   if ( dfile ) //exit if QGIS_DEBUG_FILE is set and the message comes from the wrong file
00028   {
00029     if ( !file || strcmp( dfile, file ) != 0 )
00030     {
00031       return;
00032     }
00033   }
00034 
00035   int dlevel = debugLevel();
00036   if ( dlevel >= debuglevel && debuglevel > 0 )
00037   {
00038     if ( file == NULL )
00039     {
00040       qDebug( "%s", msg.toLocal8Bit().constData() );
00041     }
00042     else if ( function == NULL )
00043     {
00044       qDebug( "%s: %s", file, msg.toLocal8Bit().constData() );
00045     }
00046     else if ( line == -1 )
00047     {
00048       qDebug( "%s: (%s) %s", file, function, msg.toLocal8Bit().constData() );
00049     }
00050     else
00051     {
00052 #ifndef _MSC_VER
00053       qDebug( "%s: %d: (%s) %s", file, line, function, msg.toLocal8Bit().constData() );
00054 #else
00055       qDebug( "%s(%d) : (%s) %s", file, line, function, msg.toLocal8Bit().constData() );
00056 #endif
00057     }
00058   }
00059 }
00060 
00061 void QgsLogger::debug( const QString& var, int val, int debuglevel, const char* file, const char* function, int line )
00062 {
00063   const char* dfile = debugFile();
00064   if ( dfile ) //exit if QGIS_DEBUG_FILE is set and the message comes from the wrong file
00065   {
00066     if ( !file || strcmp( dfile, file ) != 0 )
00067     {
00068       return;
00069     }
00070   }
00071 
00072   int dlevel = debugLevel();
00073   if ( dlevel >= debuglevel && debuglevel > 0 )
00074   {
00075     if ( file == NULL )
00076     {
00077       qDebug( "%s: %d", var.toLocal8Bit().constData(), val );
00078     }
00079     else if ( function == NULL )
00080     {
00081       qDebug( "%s: %s: %d", file, var.toLocal8Bit().constData(), val );
00082     }
00083     else if ( line == -1 )
00084     {
00085       qDebug( "%s: (%s): %s: %d", file, function, var.toLocal8Bit().constData(), val );
00086     }
00087     else
00088     {
00089 #ifdef _MSC_VER
00090       qDebug( "%s(%d): (%s), %s: %d", file, line, function, var.toLocal8Bit().constData(), val );
00091 #else
00092       qDebug( "%s: %d: (%s), %s: %d", file, line, function, var.toLocal8Bit().constData(), val );
00093 #endif
00094     }
00095   }
00096 }
00097 
00098 void QgsLogger::debug( const QString& var, double val, int debuglevel, const char* file, const char* function, int line )
00099 {
00100   const char* dfile = debugFile();
00101   if ( dfile ) //exit if QGIS_DEBUG_FILE is set and the message comes from the wrong file
00102   {
00103     if ( !file || strcmp( dfile, file ) != 0 )
00104     {
00105       return;
00106     }
00107   }
00108 
00109   int dlevel = debugLevel();
00110   if ( dlevel >= debuglevel && debuglevel > 0 )
00111   {
00112     if ( file == NULL )
00113     {
00114       qDebug( "%s: %f", var.toLocal8Bit().constData(), val );
00115     }
00116     else if ( function == NULL )
00117     {
00118       qDebug( "%s: %s: %f", file, var.toLocal8Bit().constData(), val );
00119     }
00120     else if ( line == -1 )
00121     {
00122       qDebug( "%s: (%s): %s: %f", file, function, var.toLocal8Bit().constData(), val );
00123     }
00124     else
00125     {
00126 #ifdef _MSC_VER
00127       qDebug( "%s(%d): (%s), %s: %f", file, line, function, var.toLocal8Bit().constData(), val );
00128 #else
00129       qDebug( "%s: %d: (%s), %s: %f", file, line, function, var.toLocal8Bit().constData(), val );
00130 #endif
00131     }
00132   }
00133 }
00134 
00135 void QgsLogger::warning( const QString& msg )
00136 {
00137   qWarning( "%s", msg.toLocal8Bit().constData() );
00138 }
00139 
00140 void QgsLogger::critical( const QString& msg )
00141 {
00142   qCritical( "%s", msg.toLocal8Bit().constData() );
00143 }
00144 
00145 void QgsLogger::fatal( const QString& msg )
00146 {
00147   qFatal( "%s", msg.toLocal8Bit().constData() );
00148 }
00149 
00150 int QgsLogger::debugLevel()
00151 {
00152   if ( mDebugLevel == -999 )
00153   {
00154     // read the environment variable QGIS_DEBUG just once,
00155     // then reuse the value
00156 
00157     const char* dlevel = getenv( "QGIS_DEBUG" );
00158     if ( dlevel == NULL ) //environment variable not set
00159     {
00160 #ifdef QGISDEBUG
00161       mDebugLevel = 1; //1 is default value in debug mode
00162 #else
00163       mDebugLevel = 0;
00164 #endif
00165     }
00166     else
00167     {
00168       mDebugLevel = atoi( dlevel );
00169 #ifdef QGISDEBUG
00170       if ( mDebugLevel == 0 )
00171       {
00172         mDebugLevel = 1;
00173       }
00174 #endif
00175     }
00176   }
00177 
00178   return mDebugLevel;
00179 }
00180 
00181 const char* QgsLogger::debugFile()
00182 {
00183   const char* dfile = getenv( "QGIS_DEBUG_FILE" );
00184   return dfile;
00185 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines