]> git.lyx.org Git - lyx.git/blob - src/support/debugstream.h
* src/encoding.C (latexChar,read):
[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 struct debug_trait {
34         enum type {
35                 NONE   = 0,
36                 EMERG  = 1,
37                 ALERT  = 2,
38                 CRIT   = 3,
39                 ERR    = 4,
40                 WARN   = 5,
41                 NOTICE = 6,
42                 INFO   = 7,
43                 DEBUG  = 8,
44                 ANY = 0xffffff
45         };
46
47         static bool match(type a, type b) {
48                 return (b <= a || (b == ANY && a > NONE));
49         }
50 };
51
52 #ifdef TEMPORARY_DEBUG_MACRO
53 # define DEBUG TEMPORARY_DEBUG_MACRO
54 # undef TEMPORARY_DEBUG_MACRO
55 #endif
56
57
58 template <class dtrait,
59           class charT = char,
60           class traits = std::char_traits<charT> >
61 class basic_debugstream : public std::basic_ostream<charT, traits> {
62 public:
63         typedef dtrait debug;
64         typedef typename debug::type Type;
65
66         basic_debugstream()
67                 : std::basic_ostream<charT, traits>(0), dt(debug::NONE),
68                   realbuf_(0), enabled_(true)
69         {}
70
71         /// Constructor, sets the debug level to t.
72         explicit basic_debugstream(std::basic_streambuf<charT, traits> * buf)
73                 : std::basic_ostream<charT, traits>(buf), dt(debug::NONE),
74                   realbuf_(0), enabled_(true)
75         {}
76
77         /// Sets the debug level to t.
78         void level(Type t) {
79                 dt = t;
80         }
81
82         /// Returns the current debug level.
83         Type level() const {
84                 return dt;
85         }
86
87         /// Returns true if t is part of the current debug level.
88         bool debugging(Type t = debug::ANY) const
89         {
90                 if (debug::match(dt, t)) return true;
91                 return false;
92         }
93
94         /** Returns the no-op stream if t is not part of the
95             current debug level otherwise the real debug stream
96             is used.
97             Use: dbgstream[Debug::INFO] << "Info!\n";
98         */
99         std::basic_ostream<charT, traits> & operator[](Type t) {
100                 if (debug::match(dt, t))
101                         return *this;
102                 return nullstream;
103         }
104         /// Disable the stream completely
105         void disable()
106         {
107                 if (enabled_) {
108                         realbuf_ = this->rdbuf();
109                         rdbuf(nullstream.rdbuf());
110                         enabled_ = false;
111                 }
112         }
113         /// Enable the stream after a possible call of disable()
114         void enable()
115         {
116                 if (!enabled_) {
117                         this->rdbuf(realbuf_);
118                         enabled_ = true;
119                 }
120         }
121 private:
122         /// The current debug level
123         Type dt;
124         /// The no-op stream.
125         boost::basic_onullstream<charT, traits> nullstream;
126         /// The buffer of the real stream
127         std::streambuf * realbuf_;
128         /// Is the stream enabled?
129         bool enabled_;
130 };
131
132 typedef basic_debugstream<debug_trait> debugstream;
133
134
135 //} // namespace lyx
136
137 #endif