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