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-2013 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 <mia/core/defines.hh>
31 #include <mia/core/dictmap.hh>
32 
34 
35 #ifndef VSTREAM_DOMAIN
36 #define VSTREAM_DOMAIN "**"
37 #endif
38 
39 
40 template <typename T>
41 std::ostream& operator << (std::ostream& os, const std::vector<T>& v)
42 {
43  os << "[";
44  for(auto x: v)
45  os << x << ", ";
46  os << "]";
47  return os;
48 }
49 
63 public:
69  enum Level {
78  ml_undefined
79  };
80 
84  static vstream& instance();
85 
86 
90  void set_verbosity(Level l);
91 
93  vstream::Level get_level() const;
94 
100  std::ostream& set_stream(std::ostream& os);
101 
103  void flush();
104 
108  bool shows(Level l)const;
109 
111  bool show_debug() const;
112 
113 
117  vstream& operator << (Level const l);
118 
124  template <class T>
125  vstream& operator << (const T& text) {
126  if (m_message_level >= m_output_level)
127  *m_output << text;
128  return *this;
129  }
130 
135  vstream & operator<<(std::ostream& (*f)(std::ostream&));
136 
137 
138 
143  static void set_output_target(std::ostream* os);
144 
148  operator std::ostream& () {
149  return *m_output;
150  }
151 
152 private:
153  vstream(std::ostream& output, Level l);
154 
155  static std::ostream* m_output_target;
156  static __thread std::ostream* m_output;
157  Level m_output_level;
158  static __thread Level m_message_level;
159 
160 };
161 
167 
176 void set_verbose(bool verbose);
177 
178 
179 inline bool vstream::shows(Level l)const
180 {
181  return l >= m_output_level;
182 }
183 
184 
185 #ifdef NDEBUG
186 #define TRACE(DOMAIN)
187 #define TRACE_FUNCTION
188 #define FUNCTION_NOT_TESTED
189 class CDebugSink {
190 public:
191  template <class T>
192  CDebugSink& operator << (const T /*val*/) {
193  return *this;
194  }
195  CDebugSink & operator<<(std::ostream& (* /*f*/)(std::ostream&)) {
196  return *this;
197  }
198 };
199 
200 inline CDebugSink& cvdebug()
201 {
202  static CDebugSink sink;
204  return sink;
205 }
206 
207 #else
208 
213 inline vstream& cvdebug()
214 {
216  return vstream::instance();
217 }
218 
220 public:
221  CTrace(const char *domain):
222  m_domain(domain),
223  m_fill(m_depth, ' ') {
225  << m_fill << "enter " << m_domain << "\n";
226  ++m_depth;
227  };
230  << m_fill << "leave " << m_domain << "\n";
231  --m_depth;
232  }
233 private:
234  const char *m_domain;
235  std::string m_fill;
236  // should be thread local, or at least protected by a mutex
237  static __thread size_t m_depth;
238 };
239 
241 #define TRACE(DOMAIN) ::mia::CTrace _xxx_trace(DOMAIN)
242 
244 #define TRACE_FUNCTION ::mia::CTrace _xxx_trace(__PRETTY_FUNCTION__)
245 
247 #define FUNCTION_NOT_TESTED ::mia::cvwarn() << __PRETTY_FUNCTION__ << ":not tested\n"
248 
249 #endif
250 
256 inline vstream& cvinfo()
257 {
259  return vstream::instance();
260 }
261 
262 inline bool vstream::show_debug() const
263 {
264  return shows(ml_debug);
265 }
266 
267 
270 {
271  return m_output_level;
272 }
273 
274 inline void vstream::flush()
275 {
276  m_output->flush();
277 }
278 
279 // some inlines
280 
285 inline vstream& cvfatal()
286 {
288  return vstream::instance();
289 }
290 
295 inline vstream& cvfail()
296 {
298  return vstream::instance();
299 }
300 
305 inline vstream& cverr()
306 {
308  return vstream::instance();
309 }
310 
315 inline vstream& cvwarn()
316 {
318  return vstream::instance();
319 }
320 
325 inline vstream& cvmsg()
326 {
328  return vstream::instance();
329 }
330 
335 #define cverb ::mia::vstream::instance()
336 
341 template <typename T>
342 vstream& operator << (vstream& os, const std::vector<T>& v) {
343  os << "[";
344  for (auto i =v.begin(); i != v.end(); ++i)
345  os << *i << ", ";
346  os << "]";
347  return os;
348 }
349 
351 
352 #endif /* !CVERB_HH */