]> git.lyx.org Git - lyx.git/blob - src/support/debugstream.h
Use the correct nullstream.hpp.
[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 #if BOOST_VERSION < 103301
20 #  include <boost/test/detail/nullstream.hpp>
21 #else
22 #  include <boost/test/utils/nullstream.hpp>
23 #endif
24
25 #ifdef DEBUG
26 # define TEMPORARY_DEBUG_MACRO DEBUG
27 # undef DEBUG
28 #endif
29
30 struct debug_trait {
31         enum type {
32                 NONE   = 0,
33                 EMERG  = 1,
34                 ALERT  = 2,
35                 CRIT   = 3,
36                 ERR    = 4,
37                 WARN   = 5,
38                 NOTICE = 6,
39                 INFO   = 7,
40                 DEBUG  = 8,
41                 ANY = 0xffffff
42         };
43
44         static bool match(type a, type b) {
45                 return (b <= a || (b == ANY && a > NONE));
46         }
47 };
48
49 #ifdef TEMPORARY_DEBUG_MACRO
50 # define DEBUG TEMPORARY_DEBUG_MACRO
51 # undef TEMPORARY_DEBUG_MACRO
52 #endif
53
54
55 template <class dtrait,
56           class charT = char,
57           class traits = std::char_traits<charT> >
58 class basic_debugstream : public std::basic_ostream<charT, traits> {
59 public:
60         typedef dtrait debug;
61         typedef typename debug::type Type;
62
63         basic_debugstream()
64                 : std::basic_ostream<charT, traits>(0), dt(debug::NONE)
65         {}
66
67         /// Constructor, sets the debug level to t.
68         explicit basic_debugstream(std::basic_streambuf<charT, traits> * buf)
69                 : std::basic_ostream<charT, traits>(buf), dt(debug::NONE)
70         {}
71
72         /// Sets the debug level to t.
73         void level(Type t) {
74                 dt = t;
75         }
76
77         /// Returns the current debug level.
78         Type level() const {
79                 return dt;
80         }
81
82         /// Returns true if t is part of the current debug level.
83         bool debugging(Type t = debug::ANY) const
84         {
85                 if (debug::match(dt, t)) return true;
86                 return false;
87         }
88
89         /** Returns the no-op stream if t is not part of the
90             current debug level otherwise the real debug stream
91             is used.
92             Use: dbgstream[Debug::INFO] << "Info!\n";
93         */
94         std::basic_ostream<charT, traits> & operator[](Type t) {
95                 if (debug::match(dt, t))
96                         return *this;
97                 return nullstream;
98         }
99 private:
100         /// The current debug level
101         Type dt;
102         /// The no-op stream.
103         boost::basic_onullstream<charT, traits> nullstream;
104 };
105
106 typedef basic_debugstream<debug_trait> debugstream;
107
108 #endif