QGIS API Documentation  2.0.1-Dufour
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgslogger.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgslogger.h - 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 #ifndef QGSLOGGER_H
19 #define QGSLOGGER_H
20 
21 #include <iostream>
22 #include <sstream>
23 #include <QString>
24 class QFile;
25 
26 #ifdef QGISDEBUG
27 #define QgsDebugMsg(str) QgsLogger::debug(QString(str), 1, __FILE__, __FUNCTION__, __LINE__)
28 #define QgsDebugMsgLevel(str, level) \
29  { \
30  if ( QgsLogger::debugLevel() >= (level) && (level) > 0 ) \
31  QgsLogger::debug(QString(str), (level), __FILE__, __FUNCTION__, __LINE__); \
32  }
33 #define QgsDebugCall QgsScopeLogger _qgsScopeLogger(__FILE__, __FUNCTION__, __LINE__)
34 #else
35 #define QgsDebugCall
36 #define QgsDebugMsg(str)
37 #define QgsDebugMsgLevel(str, level)
38 #endif
39 
56 class CORE_EXPORT QgsLogger
57 {
58  public:
59 
66  static void debug( const QString& msg, int debuglevel = 1, const char* file = NULL, const char* function = NULL, int line = -1 );
67 
69  static void debug( const QString& var, int val, int debuglevel = 1, const char* file = NULL, const char* function = NULL, int line = -1 );
70 
72  // @note not available in python bindings
73  static void debug( const QString& var, double val, int debuglevel = 1, const char* file = NULL, const char* function = NULL, int line = -1 );
74 
76  // @note not available in python bindings
77  template <typename T> static void debug( const QString& var, T val, const char* file = 0, const char* function = 0,
78  int line = -1, int debuglevel = 1 )
79  {
80  Q_UNUSED( debuglevel );
81  const char* dfile = debugFile();
82  if ( dfile ) //exit if QGIS_DEBUG_FILE is set and the message comes from the wrong file
83  {
84  if ( !file || strcmp( dfile, file ) != 0 )
85  {
86  return;
87  }
88  }
89  std::ostringstream os;
90  os << var.toLocal8Bit().data() << " = " << val;
91  if ( line == -1 )
92  {
93  qDebug( "%s: (%s) %s", file + sPrefixLength, function, os.str().c_str() );
94  }
95  else
96  {
97 #if defined(_MSC_VER)
98  qDebug( "%s(%d): (%s) %s", file + sPrefixLength, line, function, os.str().c_str() );
99 #else
100  qDebug( "%s: %d: (%s) %s", file + sPrefixLength, line, function, os.str().c_str() );
101 #endif
102  }
103  }
104 
106  static void warning( const QString& msg );
107 
109  static void critical( const QString& msg );
110 
112  static void fatal( const QString& msg );
113 
116  static int debugLevel();
117 
119  static void logMessageToFile( QString theMessage );
120 
121  private:
124  static const QString logFile();
125 
128  static const char* debugFile();
129 
131  static int sDebugLevel;
132  static int sPrefixLength;
133 };
134 
136 {
137  public:
138  QgsScopeLogger( const char* file, const char* func, int line )
139  : _file( file ), _func( func ), _line( line )
140  {
141  QgsLogger::debug( "Entering.", 1, _file, _func, _line );
142  }
144  {
145  QgsLogger::debug( "Leaving.", 1, _file, _func, _line );
146  }
147  private:
148  const char *_file;
149  const char *_func;
150  int _line;
151 };
152 
153 #endif