QGIS API Documentation  master-28efcda
src/core/qgslogger.h
Go to the documentation of this file.
00001 /***************************************************************************
00002                          qgslogger.h  -  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 #ifndef QGSLOGGER_H
00019 #define QGSLOGGER_H
00020 
00021 #include <iostream>
00022 #include <sstream>
00023 #include <QString>
00024 class QFile;
00025 
00026 #ifdef QGISDEBUG
00027 #define QgsDebugMsg(str) QgsLogger::debug(QString(str), 1, __FILE__, __FUNCTION__, __LINE__)
00028 #define QgsDebugMsgLevel(str, level) \
00029   { \
00030     if ( QgsLogger::debugLevel() >= (level) && (level) > 0 ) \
00031       QgsLogger::debug(QString(str), (level), __FILE__, __FUNCTION__, __LINE__); \
00032   }
00033 #define QgsDebugCall QgsScopeLogger _qgsScopeLogger(__FILE__, __FUNCTION__, __LINE__)
00034 #else
00035 #define QgsDebugCall
00036 #define QgsDebugMsg(str)
00037 #define QgsDebugMsgLevel(str, level)
00038 #endif
00039 
00056 class CORE_EXPORT QgsLogger
00057 {
00058   public:
00059 
00066     static void debug( const QString& msg, int debuglevel = 1, const char* file = NULL, const char* function = NULL, int line = -1 );
00067 
00069     static void debug( const QString& var, int val, int debuglevel = 1, const char* file = NULL, const char* function = NULL, int line = -1 );
00070 
00072     // @note not available in python bindings
00073     static void debug( const QString& var, double val, int debuglevel = 1, const char* file = NULL, const char* function = NULL, int line = -1 );
00074 
00076     // @note not available in python bindings
00077     template <typename T> static void debug( const QString& var, T val, const char* file = 0, const char* function = 0,
00078         int line = -1, int debuglevel = 1 )
00079     {
00080       Q_UNUSED( debuglevel );
00081       const char* dfile = debugFile();
00082       if ( dfile ) //exit if QGIS_DEBUG_FILE is set and the message comes from the wrong file
00083       {
00084         if ( !file || strcmp( dfile, file ) != 0 )
00085         {
00086           return;
00087         }
00088       }
00089       std::ostringstream os;
00090       os << var.toLocal8Bit().data() << " = " << val;
00091       if ( line == -1 )
00092       {
00093         qDebug( "%s: (%s) %s", file + sPrefixLength, function, os.str().c_str() );
00094       }
00095       else
00096       {
00097 #if defined(_MSC_VER)
00098         qDebug( "%s(%d): (%s) %s", file + sPrefixLength, line, function, os.str().c_str() );
00099 #else
00100         qDebug( "%s: %d: (%s) %s", file + sPrefixLength, line, function, os.str().c_str() );
00101 #endif
00102       }
00103     }
00104 
00106     static void warning( const QString& msg );
00107 
00109     static void critical( const QString& msg );
00110 
00112     static void fatal( const QString& msg );
00113 
00116     static int debugLevel();
00117 
00119     static void logMessageToFile( QString theMessage );
00120 
00121   private:
00124     static const QString logFile();
00125 
00128     static const char* debugFile();
00129 
00131     static int sDebugLevel;
00132     static int sPrefixLength;
00133 };
00134 
00135 class QgsScopeLogger
00136 {
00137   public:
00138     QgsScopeLogger( const char* file, const char* func, int line )
00139         : _file( file ), _func( func ), _line( line )
00140     {
00141       QgsLogger::debug( "Entering.", 1, _file, _func, _line );
00142     }
00143     ~QgsScopeLogger()
00144     {
00145       QgsLogger::debug( "Leaving.", 1, _file, _func, _line );
00146     }
00147   private:
00148     const char *_file;
00149     const char *_func;
00150     int _line;
00151 };
00152 
00153 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines