QGIS API Documentation 4.1.0-Master (201b6d107df)
Loading...
Searching...
No Matches
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 "qgsconfig.h"
22
23#include <iostream>
24#include <sstream>
25
26#include "qgis_core.h"
27#include "qgis_sip.h"
28
29#include <QString>
30#include <QTime>
31
32using namespace Qt::StringLiterals;
33
34class QFile;
35
36#ifndef SIP_RUN
37
38#ifdef QGISDEBUG
39
40#define QgsDebugError( str ) QgsLogger::debug( QString( str ), 0, __FILE__, __FUNCTION__, __LINE__ )
41
42#define QgsDebugMsgLevel( str, level ) \
43 do \
44 { \
45 if ( ( level ) <= QgsLogger::debugLevel() ) \
46 { \
47 QgsLogger::debug( QString( str ), ( level ), __FILE__, __FUNCTION__, __LINE__ ); \
48 } \
49 } while ( false )
50
51#define QgsDebugErrorLoc( str, file, func, line ) QgsLogger::debug( QString( str ), 0, ( file ), ( func ), ( line ) )
52
53#define QgsDebugMsgLevelLoc( str, level, file, func, line ) \
54 do \
55 { \
56 if ( ( level ) <= QgsLogger::debugLevel() ) \
57 { \
58 QgsLogger::debug( QString( str ), ( level ), ( file ), ( func ), ( line ) ); \
59 } \
60 } while ( false )
61
62#define QgsDebugCall QgsScopeLogger _qgsScopeLogger( __FILE__, __FUNCTION__, __LINE__ )
63
64#else
65
66#define QgsDebugCall \
67 do \
68 { \
69 } while ( false )
70
71#define QgsDebugError( str ) \
72 do \
73 { \
74 if constexpr ( false ) \
75 { \
76 QgsLogger::debug( QString( str ), 0, "", "", 0 ); \
77 } \
78 } while ( false )
79
80#define QgsDebugMsgLevel( str, level ) \
81 do \
82 { \
83 if constexpr ( false ) \
84 { \
85 QgsLogger::debug( QString( str ), ( level ), "", "", 0 ); \
86 } \
87 } while ( false )
88
89#define QgsDebugErrorLoc( str, file, func, line ) \
90 do \
91 { \
92 if constexpr ( false ) \
93 { \
94 QgsLogger::debug( QString( str ), 0, ( file ), ( func ), ( line ) ); \
95 } \
96 } while ( false )
97
98#define QgsDebugMsgLevelLoc( str, level, file, func, line ) \
99 do \
100 { \
101 if constexpr ( false ) \
102 { \
103 QgsLogger::debug( QString( str ), ( level ), ( file ), ( func ), ( line ) ); \
104 } \
105 } while ( false )
106#endif
107
108#endif
109
110
128
129class CORE_EXPORT QgsLogger
130{
131 public:
140 static void debug( const QString &msg, int debuglevel = 1, const char *file = nullptr, const char *function = nullptr, int line = -1 );
141
143 static void debug( const QString &var, int val, int debuglevel = 1, const char *file = nullptr, const char *function = nullptr, int line = -1 );
144
149 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;
150
155 template<typename T> static void debug( const QString &var, T val, const char *file = nullptr, const char *function = nullptr, int line = -1, int debuglevel = 1 ) SIP_SKIP SIP_SKIP
156 {
157 std::ostringstream os;
158 os << var.toLocal8Bit().data() << " = " << val;
159 debug( var, os.str().c_str(), file, function, line, debuglevel );
160 }
161
163 static void warning( const QString &msg );
164
166 static void critical( const QString &msg );
167
169 static void fatal( const QString &msg );
170
175 static int debugLevel()
176 {
177 if ( sDebugLevel == -999 )
178 init();
179 return sDebugLevel;
180 }
181
183 static void logMessageToFile( const QString &message );
184
198 static QString logFile();
199
200 private:
201 static void init();
202
204 static int sDebugLevel;
205 static int sPrefixLength;
206};
207
212class CORE_EXPORT QgsScopeLogger // clazy:exclude=rule-of-three
213{
214 public:
215 QgsScopeLogger( const char *file, const char *func, int line )
216 : _file( file )
217 , _func( func )
218 , _line( line )
219 {
220 QgsLogger::debug( u"Entering."_s, 2, _file, _func, _line );
221 }
222 ~QgsScopeLogger() { QgsLogger::debug( u"Leaving."_s, 2, _file, _func, _line ); }
223
224 private:
225 const char *_file = nullptr;
226 const char *_func = nullptr;
227 int _line;
228};
229
230#endif
Responsible for printing debug/warning/error messages to the console.
Definition qgslogger.h:130
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:62
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:155
static int debugLevel()
Reads the environment variable QGIS_DEBUG and converts it to int.
Definition qgslogger.h:175
QgsScopeLogger(const char *file, const char *func, int line)
Definition qgslogger.h:215
#define SIP_SKIP
Definition qgis_sip.h:133