QGIS API Documentation 3.41.0-Master (5b255a84cbc)
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 <iostream>
22#include "qgis_sip.h"
23#include <sstream>
24#include <QString>
25#include <QTime>
26
27#include "qgis_core.h"
28#include "qgsconfig.h"
29
30class QFile;
31
32#ifdef QGISDEBUG
33#define QgsDebugError(str) QgsLogger::debug(QString(str), 0, __FILE__, __FUNCTION__, __LINE__)
34#define QgsDebugMsgLevel(str, level) if ( level <= QgsLogger::debugLevel() ) { QgsLogger::debug(QString(str), (level), __FILE__, __FUNCTION__, __LINE__); }(void)(0)
35#define QgsDebugErrorLoc(str, file, func, line) QgsLogger::debug(QString(str), 0, file, func, line)
36#define QgsDebugMsgLevelLoc(str, level, file, func, line) if ( level <= QgsLogger::debugLevel() ) { QgsLogger::debug(QString(str), (level), file, func, line); }(void)(0)
37#define QgsDebugCall QgsScopeLogger _qgsScopeLogger(__FILE__, __FUNCTION__, __LINE__)
38#else
39#define QgsDebugCall do {} while(false)
40#define QgsDebugError(str) do {} while(false)
41#define QgsDebugMsgLevel(str, level) do {} while(false)
42#define QgsDebugErrorLoc(str, file, func, line) do {} while(false)
43#define QgsDebugMsgLevelLoc(str, level, file, func, line) do {} while(false)
44#endif
45
64class CORE_EXPORT QgsLogger
65{
66 public:
67
76 static void debug( const QString &msg, int debuglevel = 1, const char *file = nullptr, const char *function = nullptr, int line = -1 );
77
79 static void debug( const QString &var, int val, int debuglevel = 1, const char *file = nullptr, const char *function = nullptr, int line = -1 );
80
85 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;
86
91 template <typename T> static void debug( const QString &var, T val, const char *file = nullptr, const char *function = nullptr,
92 int line = -1, int debuglevel = 1 ) SIP_SKIP SIP_SKIP
93 {
94 std::ostringstream os;
95 os << var.toLocal8Bit().data() << " = " << val;
96 debug( var, os.str().c_str(), file, function, line, debuglevel );
97 }
98
100 static void warning( const QString &msg );
101
103 static void critical( const QString &msg );
104
106 static void fatal( const QString &msg );
107
112 static int debugLevel()
113 {
114 if ( sDebugLevel == -999 )
115 init();
116 return sDebugLevel;
117 }
118
120 static void logMessageToFile( const QString &message );
121
126 static QString logFile();
127
128 private:
129 static void init();
130
132 static int sDebugLevel;
133 static int sPrefixLength;
134};
135
139class CORE_EXPORT QgsScopeLogger // clazy:exclude=rule-of-three
140{
141 public:
142 QgsScopeLogger( const char *file, const char *func, int line )
143 : _file( file )
144 , _func( func )
145 , _line( line )
146 {
147 QgsLogger::debug( QStringLiteral( "Entering." ), 2, _file, _func, _line );
148 }
150 {
151 QgsLogger::debug( QStringLiteral( "Leaving." ), 2, _file, _func, _line );
152 }
153 private:
154 const char *_file = nullptr;
155 const char *_func = nullptr;
156 int _line;
157};
158
159#endif
QgsLogger is a class to print debug/warning/error messages to the console.
Definition qgslogger.h:65
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:59
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:91
static int debugLevel()
Reads the environment variable QGIS_DEBUG and converts it to int.
Definition qgslogger.h:112
QgsScopeLogger(const char *file, const char *func, int line)
Definition qgslogger.h:142
#define SIP_SKIP
Definition qgis_sip.h:126