QGIS API Documentation  2.4.0-Chugiak
 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 
123  static const QString logFile();
124 
125  private:
126 
129  static const char* debugFile();
130 
132  static int sDebugLevel;
133  static int sPrefixLength;
134 };
135 
137 {
138  public:
139  QgsScopeLogger( const char* file, const char* func, int line )
140  : _file( file ), _func( func ), _line( line )
141  {
142  QgsLogger::debug( "Entering.", 1, _file, _func, _line );
143  }
145  {
146  QgsLogger::debug( "Leaving.", 1, _file, _func, _line );
147  }
148  private:
149  const char *_file;
150  const char *_func;
151  int _line;
152 };
153 
154 #endif
const char * _file
Definition: qgslogger.h:149
QgsLogger is a class to print debug/warning/error messages to the console.
Definition: qgslogger.h:56
QgsScopeLogger(const char *file, const char *func, int line)
Definition: qgslogger.h:139
static void debug(const QString &var, T val, const char *file=0, const char *function=0, int line=-1, int debuglevel=1)
Prints out a variable/value pair for types with overloaded operator<<.
Definition: qgslogger.h:77
static int sDebugLevel
current debug level
Definition: qgslogger.h:132
QString file
Definition: qgssvgcache.cpp:76
static void debug(const QString &msg, int debuglevel=1, const char *file=NULL, const char *function=NULL, int line=-1)
Goes to qDebug.
Definition: qgslogger.cpp:33
const char * _func
Definition: qgslogger.h:150
static int sPrefixLength
Definition: qgslogger.h:133