QGIS API Documentation  2.99.0-Master (b8fd1fd)
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 #include <QTime>
25 
26 #include "qgis_core.h"
27 
28 class QFile;
29 
30 #ifdef QGISDEBUG
31 #define QgsDebugMsg(str) QgsLogger::debug(QString(str), 1, __FILE__, __FUNCTION__, __LINE__)
32 #define QgsDebugMsgLevel(str, level) QgsLogger::debug(QString(str), (level), __FILE__, __FUNCTION__, __LINE__)
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 = nullptr, const char *function = nullptr, int line = -1 );
67 
69  static void debug( const QString &var, int val, int debuglevel = 1, const char *file = nullptr, const char *function = nullptr, 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 = nullptr, const char *function = nullptr, 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 = nullptr, const char *function = nullptr,
78  int line = -1, int debuglevel = 1 )
79  {
80  std::ostringstream os;
81  os << var.toLocal8Bit().data() << " = " << val;
82  debug( var, os.str().c_str(), file, function, line, debuglevel );
83  }
84 
86  static void warning( const QString &msg );
87 
89  static void critical( const QString &msg );
90 
92  static void fatal( const QString &msg );
93 
96  static int debugLevel() { init(); return sDebugLevel; }
97 
99  static void logMessageToFile( const QString &message );
100 
103  static const QString logFile() { init(); return sLogFile; }
104 
105  private:
106  static void init();
107 
109  static int sDebugLevel;
110  static int sPrefixLength;
111  static QString sLogFile;
112  static QString sFileFilter;
113  static QTime sTime;
114 };
115 
118 class QgsScopeLogger // clazy:exclude=rule-of-three
119 {
120  public:
121  QgsScopeLogger( const char *file, const char *func, int line )
122  : _file( file )
123  , _func( func )
124  , _line( line )
125  {
126  QgsLogger::debug( QStringLiteral( "Entering." ), 1, _file, _func, _line );
127  }
129  {
130  QgsLogger::debug( QStringLiteral( "Leaving." ), 1, _file, _func, _line );
131  }
132  private:
133  const char *_file = nullptr;
134  const char *_func = nullptr;
135  int _line;
136 };
137 
138 #endif
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:121
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:103
static int debugLevel()
Reads the environment variable QGIS_DEBUG and converts it to int.
Definition: qgslogger.h:96
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:77