]> git.lyx.org Git - lyx.git/blob - src/support/debugstream.h
Some more cleanup of LyXView:
[lyx.git] / src / support / debugstream.h
1 // -*- C++ -*-
2 /**
3  * \file debugstream.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef DEBUG_STREAM_HPP
13 #define DEBUG_STREAM_HPP
14
15 #include <iostream>
16
17 #include <boost/version.hpp>
18
19
20 //namespace lyx {
21
22 #if BOOST_VERSION < 103300
23 #  include <boost/test/detail/nullstream.hpp>
24 #else
25 #  include <boost/test/utils/nullstream.hpp>
26 #endif
27
28 #ifdef DEBUG
29 # define TEMPORARY_DEBUG_MACRO DEBUG
30 # undef DEBUG
31 #endif
32
33 #ifdef TEMPORARY_DEBUG_MACRO
34 # define DEBUG TEMPORARY_DEBUG_MACRO
35 # undef TEMPORARY_DEBUG_MACRO
36 #endif
37
38
39 template <class dtrait,
40           class charT = char,
41           class traits = std::char_traits<charT> >
42 class basic_debugstream : public std::basic_ostream<charT, traits> {
43 public:
44         typedef dtrait debug;
45         typedef typename debug::type Type;
46
47         basic_debugstream()
48                 : std::basic_ostream<charT, traits>(0), dt(debug::NONE),
49                   realbuf_(0), enabled_(true)
50         {}
51
52         /// Constructor, sets the debug level to t.
53         explicit basic_debugstream(std::basic_streambuf<charT, traits> * buf)
54                 : std::basic_ostream<charT, traits>(buf), dt(debug::NONE),
55                   realbuf_(0), enabled_(true)
56         {}
57
58         /// Sets the debug level to t.
59         void level(Type t) {
60                 dt = t;
61         }
62
63         /// Returns the current debug level.
64         Type level() const {
65                 return dt;
66         }
67
68         /// Returns true if t is part of the current debug level.
69         bool debugging(Type t = debug::ANY) const
70         {
71                 if (debug::match(dt, t)) return true;
72                 return false;
73         }
74
75         /** Returns the no-op stream if t is not part of the
76             current debug level otherwise the real debug stream
77             is used.
78             Use: dbgstream[Debug::INFO] << "Info!\n";
79         */
80         std::basic_ostream<charT, traits> & operator[](Type t) {
81                 if (debug::match(dt, t))
82                         return *this;
83                 return nullstream;
84         }
85         /// Disable the stream completely
86         void disable()
87         {
88                 if (enabled_) {
89                         realbuf_ = this->rdbuf();
90                         rdbuf(nullstream.rdbuf());
91                         enabled_ = false;
92                 }
93         }
94         /// Enable the stream after a possible call of disable()
95         void enable()
96         {
97                 if (!enabled_) {
98                         this->rdbuf(realbuf_);
99                         enabled_ = true;
100                 }
101         }
102 private:
103         /// The current debug level
104         Type dt;
105         /// The no-op stream.
106         boost::basic_onullstream<charT, traits> nullstream;
107         /// The buffer of the real stream
108         std::streambuf * realbuf_;
109         /// Is the stream enabled?
110         bool enabled_;
111 };
112
113
114 //} // namespace lyx
115
116 #endif