OpenLibrary
debug.hpp
1 /*
2  Universal debug routines
3  Copyright (C) 2011 MichaƂ Walenciak <Kicer86@gmail.com>
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 
20 #ifndef DEBUG_HPP
21 #define DEBUG_HPP
22 
23 #include <iostream>
24 #include <sstream>
25 #include <vector>
26 
27 #ifdef __GNUC__
28  #define FUNCTION_NAME __PRETTY_FUNCTION__
29 #else
30  #define FUNCTION_NAME __FUNCTION__
31 #endif
32 
33 #define ol_debug(l) Debug(FUNCTION_NAME, l)
34 
35 //control macros:
36 //DEBUG_QSTRING_SUPPORT - support for QString
37 //DEBUG_VERBOSE - more verbose output
38 //DEBUG_DISABLE_OUTPUT - disable output
39 
40 #ifdef DEBUG_QSTRING_SUPPORT
41 #include <QString>
42 #endif
43 
44 
45 namespace ol
46 {
47 
48  enum class Level
49  {
50  Debug,
51  Info,
52  Warning,
53  Error
54  };
55 
56 
57  class Debug
58  {
59  std::stringstream data;
60  Level level;
61 
62  bool enableOutput() const
63  {
64  //debugging off?
65  #ifdef DEBUG_DISABLE_OUTPUT
66  return false;
67  #else
68  #ifdef NDEBUG
69  if (level == Level::Debug) //no output if NDEBUG was defined and output level==DEBUG
70  return false;
71  #endif
72  return true;
73  #endif
74  }
75 
76  public:
77  Debug(const char *f_name, DebugLevel::Level l = Level::Info): data(), level(l)
78  {
79  #ifdef DEBUG_VERBOSE
80  if (enableOutput())
81  data << f_name << ": ";
82  #else
83  if (enableOutput())
84  (void) f_name;
85  #endif
86  }
87 
88  ~Debug()
89  {
90  if (enableOutput())
91  std::clog << data.str() << std::endl;
92  }
93 
94  template <typename T> Debug& operator<<(const T &arg)
95  {
96  if (enableOutput())
97  data << arg;
98 
99  return *this;
100  }
101 
102  template <typename T> Debug& operator<<(const std::vector<T *> &v)
103  {
104  if (enableOutput())
105  for(T* item: v)
106  data << *item << "; ";
107 
108  return *this;
109  }
110 
111  template <typename T> Debug& operator<<(const std::vector<T> &v)
112  {
113  if (enableOutput())
114  for(T& item: v)
115  data << item << "; ";
116 
117  return *this;
118  }
119 
120  #ifdef DEBUG_QSTRING_SUPPORT
121  Debug& operator<<(const QString &arg)
122  {
123  return (*this) << arg.toLocal8Bit().data();
124  }
125  #endif
126  };
127 
128 }
129 
130 #endif // DEBUG_HPP
Definition: debug.hpp:57
Definition: debug.hpp:45