Gaia
debugging.h
1 /*
2  * Copyright (C) 2006-2013 Music Technology Group - Universitat Pompeu Fabra
3  *
4  * This file is part of Gaia
5  *
6  * Gaia is free software: you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License as published by the Free
8  * Software Foundation (FSF), either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the Affero GNU General Public License
17  * version 3 along with this program. If not, see http://www.gnu.org/licenses/
18  */
19 
20 #ifndef GAIA_DEBUGGING_H
21 #define GAIA_DEBUGGING_H
22 
23 #include <QDebug>
24 #include <QTextStream>
25 #include <QStringList>
26 #include <QVariant>
27 
28 namespace gaia2 {
29 
30 enum DebuggingModule {
31  // general modules
32  GMemory = 1 << 0,
33  GAlgorithms = 1 << 1,
34  GMath = 1 << 2,
35  GParser = 1 << 3,
36  GIO = 1 << 4,
37  GPerf = 1 << 5,
38  GTools = 1 << 6,
39 
40  // class specific modules
41  GDataSet = 1 << 10,
42  GDescriptorTree = 1 << 11,
43  GRegion = 1 << 12,
44  GPoint = 1 << 13,
45  GView = 1 << 14,
46  GSearchSpace = 1 << 15,
47 
48  GNone = 0,
49  GAll = (1 << 30) -1
50 };
51 
52 
53 const char* debugModuleDescription(DebuggingModule module);
54 
55 extern int activatedDebugLevels;
56 
57 void setDebugLevel(int levels);
58 void unsetDebugLevel(int levels);
59 
63 class Logger {
64  protected:
65  QStringList _msgQueue;
66  bool _addHeader;
67 
68  void flush();
69 
70  public:
71  Logger() : _addHeader(true) {}
72 
73  void debug(DebuggingModule module, const QString& msg, bool resetHeader = false);
74  void info(const QString& msg);
75  void warning(const QString& msg);
76  void error(const QString& msg);
77 
78 };
79 
80 extern Logger loggerInstance;
81 
82 class Stringifier {
83  protected:
84  QString _str;
85  QTextStream _stream;
86 
87  public:
88  Stringifier() : _str(), _stream(&_str) {}
89 
90  Stringifier& operator<<(const QVariant& var) {
91  _stream << '"' << var.toString() << '"';
92  return *this;
93  };
94 
95  Stringifier& operator<<(const QStringList& slist) {
96  if (slist.empty()) {
97  _stream << "[]";
98  return *this;
99  }
100 
101  _stream << "[ " << slist.join(", ") << " ]";
102  return *this;
103  }
104 
105  template <typename T>
106  Stringifier& operator<<(const QList<T>& list) {
107  if (list.isEmpty()) {
108  _stream << "[]";
109  return *this;
110  }
111 
112  typename QList<T>::const_iterator it = list.constBegin();
113  *this << "[ " << *it;
114  for (++it; it != list.constEnd(); ++it) {
115  *this << ", " << *it;
116  }
117  _stream << " ]";
118  return *this;
119  }
120 
121  template <typename T>
122  Stringifier& operator<<(const T& obj) {
123  _stream << obj;
124  return *this;
125  }
126 
127  const QString& str() const { return _str; }
128 };
129 
130 
131 } // namespace gaia2
132 
133 
134 #define G_ACTIVE(module) ((module) & activatedDebugLevels)
135 #define G_STRINGIFY(msg) (gaia2::Stringifier() << msg).str()
136 
137 #define G_HLINE "--------------------------------------------------------------------------------"
138 #define G_TITLE(str) G_STRINGIFY(QString((80-QString(str).size()-2)/2, '-') \
139  << ' ' << str << ' ' << \
140  QString((80-QString(str).size()-1)/2, '-'))
141 
142 #define G_DEBUG(module, msg) if (G_ACTIVE(module)) loggerInstance.debug(module, G_STRINGIFY(msg), true)
143 #define G_INFO(msg) if (::gaia2::verbose) loggerInstance.info(G_STRINGIFY(msg))
144 #define G_WARNING(msg) loggerInstance.warning(G_STRINGIFY(msg))
145 #define G_ERROR(msg) loggerInstance.error(G_STRINGIFY(msg))
146 
147 #endif // GAIA_DEBUGGING_H
Definition: debugging.h:82
Main Gaia namespace, which contains all the library functions.
Definition: addfield.cpp:22
Asynchronous thread-safe logger object.
Definition: debugging.h:63