msgstream.hh
Go to the documentation of this file.
1 /* -*- mia-c++ -*-
2  *
3  * This file is part of MIA - a toolbox for medical image analysis
4  * Copyright (c) Leipzig, Madrid 1999-2017 Gert Wollny
5  *
6  * MIA 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 MIA; if not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 #ifndef CVERB_HH
23 #define CVERB_HH 1
24 
25 //#pragma interface
26 #include <vector>
27 #include <cassert>
28 #include <ostream>
29 #include <boost/call_traits.hpp>
30 #include <boost/ref.hpp>
31 #include <miaconfig.h>
32 #include <mia/core/defines.hh>
33 #include <mia/core/dictmap.hh>
34 #include <mia/core/svector.hh>
35 
37 
38 #ifndef VSTREAM_DOMAIN
39 #define VSTREAM_DOMAIN "**"
40 #endif
41 
42 
56 public:
62  enum Level {
71  ml_undefined
72  };
73 
77  static vstream& instance();
78 
79 
83  void set_verbosity(Level l);
84 
86  vstream::Level get_level() const;
87 
93  std::ostream& set_stream(std::ostream& os);
94 
96  void flush();
97 
101  bool shows(Level l)const;
102 
104  bool show_debug() const;
105 
106 
110  vstream& operator << (Level const l);
111 
117  template <class T>
118  vstream& operator << (const T& text) {
119  if (m_message_level >= m_output_level)
120  *m_output << text;
121  return *this;
122  }
123 
128  vstream & operator<<(std::ostream& (*f)(std::ostream&));
129 
130 
131 
136  static void set_output_target(std::ostream* os);
137 
141  operator std::ostream& () {
142  return *m_output;
143  }
144 
145 private:
146  vstream(std::ostream& output, Level l);
147 
148  static std::ostream* m_output_target;
149  static __thread std::ostream* m_output;
150  Level m_output_level;
151  static __thread Level m_message_level;
152 
153 };
154 
160 
169 void set_verbose(bool verbose);
170 
171 
172 inline bool vstream::shows(Level l)const
173 {
174  return l >= m_output_level;
175 }
176 
177 
179 public:
180  CTrace(const char *domain):
181  m_domain(domain),
182  m_fill(m_depth, ' ') {
184  << m_fill << "enter " << m_domain << "\n";
185  ++m_depth;
186  };
189  << m_fill << "leave " << m_domain << "\n";
190  --m_depth;
191  }
192 private:
193  const char *m_domain;
194  std::string m_fill;
195  // should be thread local, or at least protected by a mutex
196  static __thread size_t m_depth;
197 };
198 
199 
200 #ifndef ENABLE_DEBUG_MESSAGES
201 #define TRACE(DOMAIN)
202 #define TRACE_FUNCTION
203 #define FUNCTION_NOT_TESTED
204 class CDebugSink {
205 public:
206  template <class T>
207  CDebugSink& operator << (const T& MIA_PARAM_UNUSED(val)) {
208  return *this;
209  }
210 
211  CDebugSink & operator<<(std::ostream& (* /*f*/)(std::ostream&)) {
212  return *this;
213  }
214 };
215 
217 {
218  static CDebugSink sink;
220  return sink;
221 }
222 
223 #else
224 
229 inline vstream& cvdebug()
230 {
232  return vstream::instance();
233 }
234 
235 
237 #define TRACE(DOMAIN) ::mia::CTrace _xxx_trace(DOMAIN)
238 
240 #define TRACE_FUNCTION ::mia::CTrace _xxx_trace(__PRETTY_FUNCTION__)
241 
243 #define FUNCTION_NOT_TESTED ::mia::cvwarn() << __PRETTY_FUNCTION__ << ":not tested\n"
244 
245 #endif
246 
252 inline vstream& cvinfo()
253 {
255  return vstream::instance();
256 }
257 
258 inline bool vstream::show_debug() const
259 {
260  return shows(ml_debug);
261 }
262 
263 
266 {
267  return m_output_level;
268 }
269 
270 inline void vstream::flush()
271 {
272  m_output->flush();
273 }
274 
275 // some inlines
276 
281 inline vstream& cvfatal()
282 {
284  return vstream::instance();
285 }
286 
291 inline vstream& cvfail()
292 {
294  return vstream::instance();
295 }
296 
301 inline vstream& cverr()
302 {
304  return vstream::instance();
305 }
306 
311 inline vstream& cvwarn()
312 {
314  return vstream::instance();
315 }
316 
321 inline vstream& cvmsg()
322 {
324  return vstream::instance();
325 }
326 
331 #define cverb ::mia::vstream::instance()
332 
334 
335 #endif /* !CVERB_HH */
A output stream to enable certain levels of verbosity.
Definition: msgstream.hh:55
vstream & cvinfo()
informal output that may be of interest to understand problems with a program and are of higher prior...
Definition: msgstream.hh:252
vstream & cverr()
send errors to this stream adapter
Definition: msgstream.hh:301
CTrace(const char *domain)
Definition: msgstream.hh:180
CDebugSink & cvdebug()
Definition: msgstream.hh:216
bool shows(Level l) const
Definition: msgstream.hh:172
bool show_debug() const
Definition: msgstream.hh:258
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition: defines.hh:33
vstream::Level get_level() const
Definition: msgstream.hh:265
vstream & cvmsg()
send messages to this stream adapter
Definition: msgstream.hh:321
#define VSTREAM_DOMAIN
Definition: msgstream.hh:39
static vstream & instance()
~CTrace()
Definition: msgstream.hh:187
void set_verbose(bool verbose)
vstream & cvwarn()
send warnings to this stream adapter
Definition: msgstream.hh:311
#define EXPORT_CORE
Macro to manage Visual C++ style dllimport/dllexport.
Definition: defines.hh:101
CDebugSink & operator<<(std::ostream &(*)(std::ostream &))
Definition: msgstream.hh:211
void flush()
write pending output
Definition: msgstream.hh:270
vstream & cvfatal()
direct output to this stream adapter to print out fatalities in the code
Definition: msgstream.hh:281
vstream & cvfail()
direct output to this stream adapter to print out failtures in tests beyond BOOST_FAIL ...
Definition: msgstream.hh:291
EXPORT_CORE const TDictMap< vstream::Level > g_verbose_dict
Dictonary for the verbosity of the logging as used by –verbose comand line option.
std::ostream & operator<<(std::ostream &os, const CAttribute &attr)
Definition: attributes.hh:88
A mapper from emums to string values. - usefull for names flags.
Definition: dictmap.hh:45
#define NS_MIA_END
conveniance define to end the mia namespace
Definition: defines.hh:36