My Project
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 {
57 public:
63  enum Level {
72  ml_undefined
73  };
74 
78  static vstream& instance();
79 
80 
84  void set_verbosity(Level l);
85 
87  vstream::Level get_level() const;
88 
94  std::ostream& set_stream(std::ostream& os);
95 
97  void flush();
98 
102  bool shows(Level l)const;
103 
105  bool show_debug() const;
106 
107 
111  vstream& operator << (Level const l);
112 
118  template <class T>
119  vstream& operator << (const T& text)
120  {
121  if (m_message_level >= m_output_level)
122  *m_output << text;
123 
124  return *this;
125  }
126 
131  vstream& operator<<(std::ostream & (*f)(std::ostream&));
132 
133 
134 
139  static void set_output_target(std::ostream *os);
140 
144  operator std::ostream& ()
145  {
146  return *m_output;
147  }
148 
149 private:
150  vstream(std::ostream& output, Level l);
151 
152  static std::ostream *m_output_target;
153  static __thread std::ostream *m_output;
154  Level m_output_level;
155  static __thread Level m_message_level;
156 
157 };
158 
164 
173 void set_verbose(bool verbose);
174 
175 
176 inline bool vstream::shows(Level l)const
177 {
178  return l >= m_output_level;
179 }
180 
181 
183 {
184 public:
185  CTrace(const char *domain):
186  m_domain(domain),
187  m_fill(m_depth, ' ')
188  {
190  << m_fill << "enter " << m_domain << "\n";
191  ++m_depth;
192  };
194  {
196  << m_fill << "leave " << m_domain << "\n";
197  --m_depth;
198  }
199 private:
200  const char *m_domain;
201  std::string m_fill;
202  // should be thread local, or at least protected by a mutex
203  static __thread size_t m_depth;
204 };
205 
206 
207 #ifndef ENABLE_DEBUG_MESSAGES
208 #define TRACE(DOMAIN)
209 #define TRACE_FUNCTION
210 #define FUNCTION_NOT_TESTED
212 {
213 public:
214  template <class T>
215  CDebugSink& operator << (const T& MIA_PARAM_UNUSED(val))
216  {
217  return *this;
218  }
219 
220  CDebugSink& operator<<(std::ostream & ( * /*f*/)(std::ostream&))
221  {
222  return *this;
223  }
224 };
225 
227 {
228  static CDebugSink sink;
230  return sink;
231 }
232 
233 #else
234 
239 inline vstream& cvdebug()
240 {
242  return vstream::instance();
243 }
244 
245 
247 #define TRACE(DOMAIN) ::mia::CTrace _xxx_trace(DOMAIN)
248 
250 #define TRACE_FUNCTION ::mia::CTrace _xxx_trace(__PRETTY_FUNCTION__)
251 
253 #define FUNCTION_NOT_TESTED ::mia::cvwarn() << __PRETTY_FUNCTION__ << ":not tested\n"
254 
255 #endif
256 
262 inline vstream& cvinfo()
263 {
265  return vstream::instance();
266 }
267 
268 inline bool vstream::show_debug() const
269 {
270  return shows(ml_debug);
271 }
272 
273 
276 {
277  return m_output_level;
278 }
279 
280 inline void vstream::flush()
281 {
282  m_output->flush();
283 }
284 
285 // some inlines
286 
291 inline vstream& cvfatal()
292 {
294  return vstream::instance();
295 }
296 
301 inline vstream& cvfail()
302 {
304  return vstream::instance();
305 }
306 
311 inline vstream& cverr()
312 {
314  return vstream::instance();
315 }
316 
321 inline vstream& cvwarn()
322 {
324  return vstream::instance();
325 }
326 
331 inline vstream& cvmsg()
332 {
334  return vstream::instance();
335 }
336 
341 #define cverb ::mia::vstream::instance()
342 
344 
345 #endif /* !CVERB_HH */
operator<<
std::ostream & operator<<(std::ostream &os, const CAttribute &attr)
Definition: attributes.hh:89
CDebugSink::operator<<
CDebugSink & operator<<(std::ostream &(*)(std::ostream &))
Definition: msgstream.hh:220
cvinfo
vstream & cvinfo()
informal output that may be of interest to understand problems with a program and are of higher prior...
Definition: msgstream.hh:262
vstream::instance
static vstream & instance()
set_verbose
void set_verbose(bool verbose)
NS_MIA_BEGIN
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition: defines.hh:33
vstream
A output stream to enable certain levels of verbosity.
Definition: msgstream.hh:55
vstream::ml_fatal
Definition: msgstream.hh:71
dictmap.hh
vstream::ml_info
Definition: msgstream.hh:66
cvfatal
vstream & cvfatal()
direct output to this stream adapter to print out fatalities in the code
Definition: msgstream.hh:291
NS_MIA_END
#define NS_MIA_END
conveniance define to end the mia namespace
Definition: defines.hh:36
CDebugSink
Definition: msgstream.hh:211
CTrace
Definition: msgstream.hh:182
cvdebug
CDebugSink & cvdebug()
Definition: msgstream.hh:226
vstream::ml_message
Definition: msgstream.hh:67
cvwarn
vstream & cvwarn()
send warnings to this stream adapter
Definition: msgstream.hh:321
CDebugSink::operator<<
CDebugSink & operator<<(const T &val)
Definition: msgstream.hh:215
TDictMap
A mapper from emums to string values. - usefull for names flags.
Definition: dictmap.hh:45
g_verbose_dict
const EXPORT_CORE TDictMap< vstream::Level > g_verbose_dict
Dictonary for the verbosity of the logging as used by –verbose comand line option.
svector.hh
vstream::ml_debug
Definition: msgstream.hh:65
CTrace::~CTrace
~CTrace()
Definition: msgstream.hh:193
cvmsg
vstream & cvmsg()
send messages to this stream adapter
Definition: msgstream.hh:331
vstream::ml_warning
Definition: msgstream.hh:68
vstream::Level
Level
Definition: msgstream.hh:63
vstream::ml_fail
Definition: msgstream.hh:69
EXPORT_CORE
#define EXPORT_CORE
Macro to manage Visual C++ style dllimport/dllexport.
Definition: defines.hh:101
vstream::get_level
vstream::Level get_level() const
Definition: msgstream.hh:275
cvfail
vstream & cvfail()
direct output to this stream adapter to print out failtures in tests beyond BOOST_FAIL
Definition: msgstream.hh:301
vstream::ml_trace
Definition: msgstream.hh:64
vstream::shows
bool shows(Level l) const
Definition: msgstream.hh:176
vstream::ml_error
Definition: msgstream.hh:70
vstream::flush
void flush()
write pending output
Definition: msgstream.hh:280
CCmdOptionFlags::output
CTrace::CTrace
CTrace(const char *domain)
Definition: msgstream.hh:185
VSTREAM_DOMAIN
#define VSTREAM_DOMAIN
Definition: msgstream.hh:39
vstream::show_debug
bool show_debug() const
Definition: msgstream.hh:268
defines.hh
cverr
vstream & cverr()
send errors to this stream adapter
Definition: msgstream.hh:311