gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
Debug.H
Go to the documentation of this file.
1 /* Copyright 2000-2018 Matt Flax <flatmax@flatmax.org>
2  This file is part of GTK+ IOStream class set
3 
4  GTK+ IOStream is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  GTK+ IOStream is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You have received a copy of the GNU General Public License
15  along with GTK+ IOStream
16  */
17 #ifndef DEBUG_H_
18 #define DEBUG_H_
19 
20 #include <stdio.h>
21 
22 #include <errno.h>
23 
24 #ifndef uint
25 typedef unsigned int uint;
26 #endif
27 
28 #ifdef NO_ERROR
29 #if (NO_ERROR!=0)
30 #error "NO_ERROR previously defined but not equal to zero"
31 #endif
32 #else
33 #define NO_ERROR 0
34 #endif
35 
36 #ifndef DEBUG_ERROR_OFFSET
37 #define DEBUG_ERROR_OFFSET -39901
38 #endif
39 
40 // Values between -39 900 and -1 are reserved for the user.
41 #ifndef WSOLA_ERROR_OFFSET
42 #define WSOLA_ERROR_OFFSET -40001
43 #endif
44 
45 #ifndef SOX_ERROR_OFFSET
46 #define SOX_ERROR_OFFSET -40020
47 #endif
48 
49 #ifndef JACK_ERROR_OFFSET
50 #define JACK_ERROR_OFFSET -40040
51 #endif
52 
53 #ifndef DECOMPOSITION_ERROR_OFFSET
54 #define DECOMPOSITION_ERROR_OFFSET -40080
55 #endif
56 
57 #ifndef AUDIOMASKER_ERROR_OFFSET
58 #define AUDIOMASKER_ERROR_OFFSET -40090
59 #endif
60 
61 #ifndef OVERLAPADD_ERROR_OFFSET
62 #define OVERLAPADD_ERROR_OFFSET -40110
63 #endif
64 
65 #ifndef IIO_ERROR_OFFSET
66 #define IIO_ERROR_OFFSET -40150
67 #endif
68 
69 #ifndef DIRSCAN_ERROR_OFFSET
70 #define DIRSCAN_ERROR_OFFSET -40200
71 #endif
72 
73 #ifndef THREAD_ERROR_OFFSET
74 #define THREAD_ERROR_OFFSET -40300
75 #endif
76 
77 #ifndef DRAGNDROP_ERROR_OFFSET
78 #define DRAGNDROP_ERROR_OFFSET -40400
79 #endif
80 
81 #ifndef ALSA_ERROR_OFFSET
82 #define ALSA_ERROR_OFFSET -40500
83 #endif
84 
85 #ifndef IIR_ERROR_OFFSET
86 #define IIR_ERROR_OFFSET -40600
87 #endif
88 
89 #ifndef FIR_ERROR_OFFSET
90 #define FIR_ERROR_OFFSET -40650
91 #endif
92 
93 // #ifndef DSF_ERROR_OFFSET
94 // #define DSF_ERROR_OFFSET -40650
95 // #endif
96 
97 #define MAX_ERROR_OFFSET DEBUG_ERROR_OFFSET
98 #define MIN_ERROR_OFFSET (FIR_ERROR_OFFSET-1000)
99 //#define MIN_ERROR_OFFSET (DSF_ERROR_OFFSET-1000)
100 
101 #include <map>
102 #include <string>
103 #include <string.h>
104 
112 class Debug {
113  #define MALLOC_ERROR DEBUG_ERROR_OFFSET-0
114 protected:
115  std::map<int, std::string> errors;
116 public:
117  Debug() {
118 #ifndef NDEBUG
119  errors[NO_ERROR]=std::string("No error. ");
120  errors[MALLOC_ERROR]=std::string("Couldn't allocate the memory error. ");
121 #endif
122  }
123  virtual ~Debug() {}
124 
125 #ifdef NDEBUG
126  inline
127 #endif
128 
132  virtual int evaluateError(int errorNum) {
133 #ifndef NDEBUG
134  if (errorNum!=NO_ERROR)
135  if (errors.find(errorNum)==errors.end()) // if we don't have this error then perhaps it is a system error ?
136  fprintf(stderr, "Error : %d strerror reports : %s ", errorNum, strerror(errno));
137  else
138  fprintf(stderr, "Error : %d %s ", errorNum, errors[errorNum].c_str());
139 #endif
140  return errorNum;
141  }
142 
143 #ifdef NDEBUG
144  inline
145 #endif
146 
151  virtual int evaluateError(int errorNum, std::string append) {
152 #ifndef NDEBUG
153  evaluateError(errorNum);
154  fprintf(stderr, "%s", append.c_str());
155 #endif
156  return errorNum;
157  }
158 
163  std::string getErrorString(int errorNum){
164  if (errors.find(errorNum)==errors.end()) // if the error doesn't exist, then return an unknown error std::string.
165  return std::string("Unknown Error");
166  return errors[errorNum];
167  }
168 };
169 
170 #if !defined(DEBUG_OUTPUT) || !defined(DEBUG_LOCAL_OUTPUT)
171 class Debugger {
172 public:
173  enum {NOTHING} endl;
174 
175  Debugger &operator<<(const char*str){
176  return *this;
177  }
178 
179  Debugger &operator<<(const int nothing){
180  return *this;
181  }
182 
183 };
184 #include <streambuf>
185 #include <ostream>
186 template <class cT, class traits = std::char_traits<cT> >
187 class basic_nullbuf: public std::basic_streambuf<cT, traits> {
188  typename traits::int_type overflow(typename traits::int_type c) {
189  return traits::not_eof(c); // indicate success
190  }
191 };
192 
193 template <class cT, class traits = std::char_traits<cT> >
194 class basic_onullstream: public std::basic_ostream<cT, traits> {
195 public:
196  basic_onullstream(): std::basic_ios<cT, traits>(&m_sbuf), std::basic_ostream<cT, traits>(&m_sbuf) {
197  //init(&m_sbuf);
198  }
199 
200 private:
202 };
203 
206 #endif
207 
208 #ifdef DEBUG_OUTPUT
209 #define Debugger std::cout
210 // #else
211 // #define Debugger nullstream
212 #endif
213 
214 #ifdef DEBUG_LOCAL_OUTPUT
215 #define DebuggerLocal std::cout
216 // #else
217 // #define DebuggerLocal nullstream
218 #endif
219 #endif // DEBUG_H_
virtual int evaluateError(int errorNum, std::string append)
Definition: Debug.H:151
basic_nullbuf< cT, traits > m_sbuf
Definition: Debug.H:201
Debugger & operator<<(const char *str)
Definition: Debug.H:175
virtual int evaluateError(int errorNum)
Definition: Debug.H:132
STL namespace.
virtual ~Debug()
Definition: Debug.H:123
Debug()
Definition: Debug.H:117
std::map< int, std::string > errors
This will contain a map between error numbers and descriptive std::strings for the errors...
Definition: Debug.H:115
unsigned int uint
Definition: Debug.H:25
#define NO_ERROR
There is no error.
Definition: Debug.H:33
#define MALLOC_ERROR
Error when trying to allocate memory.
Definition: Debug.H:113
static NullStream nullstream
Definition: Debug.H:205
traits::int_type overflow(typename traits::int_type c)
Definition: Debug.H:188
Debugger & operator<<(const int nothing)
Definition: Debug.H:179
basic_onullstream< char > NullStream
Definition: Debug.H:204
Definition: Debug.H:112
std::string getErrorString(int errorNum)
Definition: Debug.H:163
gtkIOStream: /tmp/gtkiostream/include/Debug.H Source File
GTK+ IOStream  Beta