LibrePCB Developers Documentation
debug.h
Go to the documentation of this file.
1 /*
2  * LibrePCB - Professional EDA for everyone!
3  * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4  * https://librepcb.org/
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef LIBREPCB_CORE_DEBUG_H
21 #define LIBREPCB_CORE_DEBUG_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include "fileio/filepath.h"
27 
28 #include <QtCore>
29 
30 /*******************************************************************************
31  * Namespace / Forward Declarations
32  ******************************************************************************/
33 namespace librepcb {
34 
35 /*******************************************************************************
36  * Class Debug
37  ******************************************************************************/
38 
58 class Debug final {
59 public:
60  // Types
61 
66  enum class DebugLevel_t {
67  Nothing = 0,
68  Fatal =
69  10,
70  Critical = 20,
71  Exception = 30,
72  Warning = 40,
73  Info = 50,
74  DebugMsg =
75  60,
76  All = 100,
77  };
78 
79  // Constructors / Destructor
80  Debug(const Debug& other) = delete;
81 
82  // General methods
83 
92 
101 
108 
115 
121  const FilePath& getLogFilepath() const;
122 
138  void print(DebugLevel_t level, const QString& msg, const char* file,
139  int line);
140 
141  // Static methods
142 
153  static Debug* instance() {
154  static Debug dbg;
155  return &dbg;
156  }
157 
158  // Operator Overloadings
159  Debug& operator=(const Debug& rhs) = delete;
160 
161 private:
162  // Constructors / Destructor
163  Debug();
164  ~Debug();
165 
173  static void messageHandler(QtMsgType type, const QMessageLogContext& context,
174  const QString& msg);
175 
176  // General Attributes
181  QTextStream* mStderrStream;
183  QFile* mLogFile;
184  QMutex mMutex;
185 };
186 
187 /*******************************************************************************
188  * End of File
189  ******************************************************************************/
190 
191 } // namespace librepcb
192 
193 #endif
QFile * mLogFile
NULL if file logging is disabled.
Definition: debug.h:183
fatal errors [qFatal()] –> this will quit the application!
Definition: occmodel.cpp:77
The Exception class.
Definition: exceptions.h:84
DebugLevel_t getDebugLevelStderr() const
Get the current debug level for the stderr output.
Definition: debug.cpp:106
QTextStream * mStderrStream
the stream to stderr
Definition: debug.h:181
DebugLevel_t mDebugLevelLogFile
the current debug level for the log file
Definition: debug.h:180
The Debug class provides some methods for debugging/logging.
Definition: debug.h:58
Debug & operator=(const Debug &rhs)=delete
QMutex mMutex
for thread safety
Definition: debug.h:184
FilePath mLogFilepath
the filepath for the log file
Definition: debug.h:182
~Debug()
Definition: debug.cpp:57
irrelevant debug messages (a lot of messages!) [qDebug()]
info messages [qInfo()]
This class represents absolute, well-formatted paths to files or directories.
Definition: filepath.h:129
void setDebugLevelStderr(DebugLevel_t level)
Set the debug level for the stderr output.
Definition: debug.cpp:72
DebugLevel_t getDebugLevelLogFile() const
Get the current debug level for the log file.
Definition: debug.cpp:110
void setDebugLevelLogFile(DebugLevel_t level)
Set the debug level for the log file.
Definition: debug.cpp:76
DebugLevel_t mDebugLevelStderr
the current debug level for the stderr output
Definition: debug.h:178
static Debug * instance()
Get a pointer to the instance of the singleton Debug object.
Definition: debug.h:153
static void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
The message handler for qDebug(), qWarning(), qCritical() and qFatal()
Definition: debug.cpp:176
const FilePath & getLogFilepath() const
Get the filename of the log file (even if file logging is disabled)
Definition: debug.cpp:114
void print(DebugLevel_t level, const QString &msg, const char *file, int line)
Print a message to stderr/logfile (with respect to the current debug level)
Definition: debug.cpp:118
DebugLevel_t
Definition: debug.h:66
Debug()
Definition: debug.cpp:38