Nuria Framework - Core
Core module of the NuriaProject Framework
 All Classes Functions Typedefs Enumerations Enumerator Groups
debug.hpp
1 /* Copyright (c) 2014, The Nuria Project
2  * This software is provided 'as-is', without any express or implied
3  * warranty. In no event will the authors be held liable for any damages
4  * arising from the use of this software.
5  * Permission is granted to anyone to use this software for any purpose,
6  * including commercial applications, and to alter it and redistribute it
7  * freely, subject to the following restrictions:
8  * 1. The origin of this software must not be misrepresented; you must not
9  * claim that you wrote the original software. If you use this software
10  * in a product, an acknowledgment in the product documentation would be
11  * appreciated but is not required.
12  * 2. Altered source versions must be plainly marked as such, and must not be
13  * misrepresented as being the original software.
14  * 3. This notice may not be removed or altered from any source
15  * distribution.
16  */
17 
18 #ifndef NURIA_DEBUG_HPP
19 #define NURIA_DEBUG_HPP
20 
21 #include "essentials.hpp"
22 #include "callback.hpp"
23 #include <QDebug>
24 
25 namespace Nuria {
26 
90 class NURIA_CORE_EXPORT Debug : public QDebug {
91 public:
92 
94  enum Type {
95  DebugMsg = 0,
96  LogMsg = 1,
97  WarnMsg = 2,
98  ErrorMsg = 3,
99  CriticalMsg = 4,
100 
101  AllLevels = CriticalMsg + 1,
102  DefaultLowestMsgLevel = DebugMsg
103 
104  };
105 
110  Debug (Type type, const char *module, const char *fileName, int line,
111  const char *className, const char *methodName);
112 
116  ~Debug ();
117 
129  static void setModuleLevel (const char *module, Type leastLevel);
130 
134  static bool isModuleDisabled (const char *module, Type level);
135 
137  static inline bool isModuleDisabled (uint32_t module, Type level) {
138  return (level < m_lowestLevel ||
139  level < m_disabledModules.value (module, DefaultLowestMsgLevel));
140  }
141 
151  static void qtMessageHandler (QtMsgType type, const QMessageLogContext &context, const QString &message);
152 
154  static void installMessageHandler ();
155 
163  static void setOutputDisabled (bool disabled);
164 
168  static bool isOutputDisabled ();
169 
175  static void setDestination (FILE *handle);
176 
182  static void setDestination (QIODevice *device);
183 
209  static void installOutputHandler (const Callback &callback);
210 
214  static void uninstallOutputHandler (const Callback &callback);
215 
238  static void setOutputFormat (const char *format);
239 
240 private:
241 
242  void setBuffer (const QString &buffer);
243 
244  // Exposing these for faster access
245  static Type m_lowestLevel;
246  static QMap< uint32_t, Type > m_disabledModules;
247 
248  QString m_buffer;
249  Type m_type;
250  int m_line;
251  QLatin1String m_module;
252  QLatin1String m_file;
253  QLatin1String m_class;
254  QLatin1String m_method;
255 
256 };
257 
261 class NURIA_CORE_EXPORT DebugIgnore {
262 public:
263  template< typename T >
264  inline DebugIgnore &operator<< (const T &)
265  { return *this; }
266 };
267 
268 }
269 
270 Q_DECLARE_METATYPE(Nuria::Debug::Type)
271 
272 // Macro magic
273 #ifndef NURIA_MODULE
274 # define NURIA_MODULE ""
275 #endif
276 
277 #define NURIA_DEBUG(type) \
278  if (Nuria::Debug::isModuleDisabled \
279  (Nuria::jenkinsHash (NURIA_MODULE, sizeof(NURIA_MODULE) - 1), type)) {} else \
280  Nuria::Debug(type, NURIA_MODULE, __FILE__, __LINE__, Q_FUNC_INFO, 0)
281 
282 #ifndef NURIA_DEBUG_NO_DEBUG
283 #define nDebug() NURIA_DEBUG(Nuria::Debug::DebugMsg)
284 #else
285 #define nDebug() Nuria::DebugIgnore()
286 #endif
287 
288 #ifndef NURIA_DEBUG_NO_LOG
289 #define nLog() NURIA_DEBUG(Nuria::Debug::LogMsg)
290 #else
291 #define nLog() Nuria::DebugIgnore()
292 #endif
293 
294 #ifndef NURIA_DEBUG_NO_WARN
295 #define nWarn() NURIA_DEBUG(Nuria::Debug::WarnMsg)
296 #else
297 #define nWarn() Nuria::DebugIgnore()
298 #endif
299 
300 #ifndef NURIA_DEBUG_NO_ERROR
301 #define nError() NURIA_DEBUG(Nuria::Debug::ErrorMsg)
302 #else
303 #define nError() Nuria::DebugIgnore()
304 #endif
305 
306 #ifndef NURIA_DEBUG_NO_CRITICAL
307 #define nCritical() NURIA_DEBUG(Nuria::Debug::CriticalMsg)
308 #else
309 #define nCritical() Nuria::DebugIgnore()
310 #endif
311 
312 #endif // NURIA_DEBUG_HPP
A modern style callback mechanism which can be bound to various method types including slots...
Definition: callback.hpp:141
Helper class for Nuria::Debug, which does nothing.
Definition: debug.hpp:261
static bool isModuleDisabled(uint32_t module, Type level)
Definition: debug.hpp:137
Type
Definition: debug.hpp:94
Debugging and logging class of the Nuria Framework.
Definition: debug.hpp:90