QGIS API Documentation  2.99.0-Master (f1c3692)
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 "qgis_sip.h"
23 #include <sstream>
24 #include <QString>
25 #include <QTime>
26 
27 #include "qgis_core.h"
28 
29 class QFile;
30 
31 #ifdef QGISDEBUG
32 #define QgsDebugMsg(str) QgsLogger::debug(QString(str), 1, __FILE__, __FUNCTION__, __LINE__)
33 #define QgsDebugMsgLevel(str, level) QgsLogger::debug(QString(str), (level), __FILE__, __FUNCTION__, __LINE__)
34 #define QgsDebugCall QgsScopeLogger _qgsScopeLogger(__FILE__, __FUNCTION__, __LINE__)
35 #else
36 #define QgsDebugCall
37 #define QgsDebugMsg(str)
38 #define QgsDebugMsgLevel(str, level)
39 #endif
40 
58 class CORE_EXPORT QgsLogger
59 {
60  public:
61 
69  static void debug( const QString &msg, int debuglevel = 1, const char *file = nullptr, const char *function = nullptr, int line = -1 );
70 
72  static void debug( const QString &var, int val, int debuglevel = 1, const char *file = nullptr, const char *function = nullptr, int line = -1 );
73 
78  static void debug( const QString &var, double val, int debuglevel = 1, const char *file = nullptr, const char *function = nullptr, int line = -1 ) SIP_SKIP SIP_SKIP;
79 
84  template <typename T> static void debug( const QString &var, T val, const char *file = nullptr, const char *function = nullptr,
85  int line = -1, int debuglevel = 1 ) SIP_SKIP SIP_SKIP
86  {
87  std::ostringstream os;
88  os << var.toLocal8Bit().data() << " = " << val;
89  debug( var, os.str().c_str(), file, function, line, debuglevel );
90  }
91 
93  static void warning( const QString &msg );
94 
96  static void critical( const QString &msg );
97 
99  static void fatal( const QString &msg );
100 
104  static int debugLevel() { init(); return sDebugLevel; }
105 
107  static void logMessageToFile( const QString &message );
108 
112  static const QString logFile() { init(); return sLogFile; }
113 
114  private:
115  static void init();
116 
118  static int sDebugLevel;
119  static int sPrefixLength;
120  static QString sLogFile;
121  static QString sFileFilter;
122  static QTime sTime;
123 };
124 
128 class CORE_EXPORT QgsScopeLogger // clazy:exclude=rule-of-three
129 {
130  public:
131  QgsScopeLogger( const char *file, const char *func, int line )
132  : _file( file )
133  , _func( func )
134  , _line( line )
135  {
136  QgsLogger::debug( QStringLiteral( "Entering." ), 1, _file, _func, _line );
137  }
139  {
140  QgsLogger::debug( QStringLiteral( "Leaving." ), 1, _file, _func, _line );
141  }
142  private:
143  const char *_file = nullptr;
144  const char *_func = nullptr;
145  int _line;
146 };
147 
148 #endif
QgsLogger is a class to print debug/warning/error messages to the console.
Definition: qgslogger.h:58
QgsScopeLogger(const char *file, const char *func, int line)
Definition: qgslogger.h:131
#define SIP_SKIP
Definition: qgis_sip.h:119
static void debug(const QString &msg, int debuglevel=1, const char *file=nullptr, const char *function=nullptr, int line=-1)
Goes to qDebug.
Definition: qgslogger.cpp:60
static const QString logFile()
Reads the environment variable QGIS_LOG_FILE.
Definition: qgslogger.h:112
static int debugLevel()
Reads the environment variable QGIS_DEBUG and converts it to int.
Definition: qgslogger.h:104
static void debug(const QString &var, T val, const char *file=nullptr, const char *function=nullptr, int line=-1, int debuglevel=1)
Prints out a variable/value pair for types with overloaded operator<<.
Definition: qgslogger.h:84